-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
unity example added (thanks to johannes hucek)
- Loading branch information
Showing
1,131 changed files
with
3,837 additions
and
0 deletions.
There are no files selected for viewing
Binary file not shown.
Binary file not shown.
Empty file.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
using System; | ||
using System.Collections; | ||
using System.Collections.Generic; | ||
using UnityEngine; | ||
|
||
|
||
|
||
public class MicInput : MonoBehaviour { | ||
|
||
//output mic loudness | ||
public float MicLoudness; | ||
float MicLoudnessInDB; | ||
public float maxMicInputVolume; | ||
public float objectScaleMin; | ||
public float objectScaleMax; | ||
|
||
private Vector3 scale; | ||
|
||
//if you have more then one microphone attached | ||
private string _device; | ||
|
||
//AudioClip to temporary store our microphone input | ||
AudioClip _clipRecord; | ||
// samplerate of the audioclip | ||
private int samplerate = 44100; | ||
|
||
|
||
int _sampleWindow = 128; | ||
|
||
//is the microphone initialized | ||
bool _isInitialized; | ||
|
||
|
||
void Update() { | ||
// levelMax equals to the highest normalized value power 2, a small number because < 1 | ||
MicLoudnessInDB = LinearToDecibel(LevelMax()); | ||
MicLoudness = LevelMax(); | ||
|
||
//scale gameobject | ||
float scaleValue = map(MicLoudness, 0, maxMicInputVolume, objectScaleMin, objectScaleMax); | ||
scale.x = scaleValue; | ||
scale.y = scaleValue; | ||
scale.z = scaleValue; | ||
gameObject.transform.localScale = scale; | ||
|
||
} | ||
|
||
//mic initialization and start recording | ||
void InitMic() { | ||
//use the first microphone connected | ||
if (_device == null) | ||
_device = Microphone.devices[0]; | ||
|
||
//start recording 999 seconds | ||
_clipRecord = Microphone.Start(_device, true, 999, 44100); | ||
} | ||
|
||
void StopMicrophone() { | ||
Microphone.End(_device); | ||
} | ||
|
||
//get data from microphone into audioclip and returns the volume | ||
float LevelMax() { | ||
float levelMax = 0; | ||
float average = 0; | ||
float[] waveData = new float[_sampleWindow]; | ||
int micPosition = Microphone.GetPosition(null) - (_sampleWindow + 1); // null means the first microphone | ||
|
||
if (micPosition < 0) | ||
return 0; | ||
|
||
_clipRecord.GetData(waveData, micPosition); | ||
|
||
// Getting a peak on the last 128 samples | ||
// Getting average on the last 128 samples | ||
for (int i = 0; i < _sampleWindow; i++) { | ||
float wavePeak = waveData[i] * waveData[i]; | ||
if (levelMax < wavePeak) { | ||
levelMax = wavePeak; | ||
} | ||
average += waveData[i] * waveData[i]; | ||
} | ||
//return average / _sampleWindow; | ||
return levelMax; | ||
|
||
} | ||
|
||
//convert from linear input to decibel | ||
private float LinearToDecibel(float linear) { | ||
float dB; | ||
|
||
//linear != 0: epsilon comparison | ||
if (linear >= Double.Epsilon) | ||
dB = 20.0f * Mathf.Log10(linear); | ||
else | ||
dB = -144.0f; | ||
|
||
return dB; | ||
} | ||
|
||
float map(float value, float istart, float iend, float ostart, float oend) { | ||
return ostart + (oend - ostart) * ((value - istart) / (iend - istart)); | ||
} | ||
|
||
// start mic when scene starts | ||
void OnEnable() { | ||
InitMic(); | ||
_isInitialized = true; | ||
} | ||
|
||
//stop mic when loading a new level or quit application | ||
void OnDisable() { | ||
StopMicrophone(); | ||
} | ||
|
||
void OnDestroy() { | ||
StopMicrophone(); | ||
} | ||
|
||
|
||
// make sure the mic gets started & stopped when application gets focused | ||
void OnApplicationFocus(bool focus) { | ||
if (focus) { | ||
|
||
if (!_isInitialized) { | ||
InitMic(); | ||
_isInitialized = true; | ||
} | ||
} | ||
if (!focus) { | ||
StopMicrophone(); | ||
_isInitialized = false; | ||
|
||
} | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
%YAML 1.1 | ||
%TAG !u! tag:unity3d.com,2011: | ||
--- !u!1001 &100100000 | ||
Prefab: | ||
m_ObjectHideFlags: 1 | ||
serializedVersion: 2 | ||
m_Modification: | ||
m_TransformParent: {fileID: 0} | ||
m_Modifications: [] | ||
m_RemovedComponents: [] | ||
m_SourcePrefab: {fileID: 0} | ||
m_RootGameObject: {fileID: 1076460388697284} | ||
m_IsPrefabAsset: 1 | ||
--- !u!1 &1076460388697284 | ||
GameObject: | ||
m_ObjectHideFlags: 0 | ||
m_CorrespondingSourceObject: {fileID: 0} | ||
m_PrefabInternal: {fileID: 100100000} | ||
serializedVersion: 6 | ||
m_Component: | ||
- component: {fileID: 4995964064733632} | ||
- component: {fileID: 33558972647111860} | ||
- component: {fileID: 23684061862387830} | ||
- component: {fileID: 65019600772888056} | ||
- component: {fileID: 114021302544953256} | ||
m_Layer: 0 | ||
m_Name: MicInputCube | ||
m_TagString: Untagged | ||
m_Icon: {fileID: 0} | ||
m_NavMeshLayer: 0 | ||
m_StaticEditorFlags: 0 | ||
m_IsActive: 1 | ||
--- !u!4 &4995964064733632 | ||
Transform: | ||
m_ObjectHideFlags: 1 | ||
m_CorrespondingSourceObject: {fileID: 0} | ||
m_PrefabInternal: {fileID: 100100000} | ||
m_GameObject: {fileID: 1076460388697284} | ||
m_LocalRotation: {x: -0, y: -0, z: -0, w: 1} | ||
m_LocalPosition: {x: 0, y: 0, z: 0} | ||
m_LocalScale: {x: 1, y: 1, z: 1} | ||
m_Children: [] | ||
m_Father: {fileID: 0} | ||
m_RootOrder: 0 | ||
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} | ||
--- !u!23 &23684061862387830 | ||
MeshRenderer: | ||
m_ObjectHideFlags: 1 | ||
m_CorrespondingSourceObject: {fileID: 0} | ||
m_PrefabInternal: {fileID: 100100000} | ||
m_GameObject: {fileID: 1076460388697284} | ||
m_Enabled: 1 | ||
m_CastShadows: 1 | ||
m_ReceiveShadows: 1 | ||
m_DynamicOccludee: 1 | ||
m_MotionVectors: 1 | ||
m_LightProbeUsage: 1 | ||
m_ReflectionProbeUsage: 1 | ||
m_RenderingLayerMask: 4294967295 | ||
m_Materials: | ||
- {fileID: 10303, guid: 0000000000000000f000000000000000, type: 0} | ||
m_StaticBatchInfo: | ||
firstSubMesh: 0 | ||
subMeshCount: 0 | ||
m_StaticBatchRoot: {fileID: 0} | ||
m_ProbeAnchor: {fileID: 0} | ||
m_LightProbeVolumeOverride: {fileID: 0} | ||
m_ScaleInLightmap: 1 | ||
m_PreserveUVs: 0 | ||
m_IgnoreNormalsForChartDetection: 0 | ||
m_ImportantGI: 0 | ||
m_StitchLightmapSeams: 0 | ||
m_SelectedEditorRenderState: 3 | ||
m_MinimumChartSize: 4 | ||
m_AutoUVMaxDistance: 0.5 | ||
m_AutoUVMaxAngle: 89 | ||
m_LightmapParameters: {fileID: 0} | ||
m_SortingLayerID: 0 | ||
m_SortingLayer: 0 | ||
m_SortingOrder: 0 | ||
--- !u!33 &33558972647111860 | ||
MeshFilter: | ||
m_ObjectHideFlags: 1 | ||
m_CorrespondingSourceObject: {fileID: 0} | ||
m_PrefabInternal: {fileID: 100100000} | ||
m_GameObject: {fileID: 1076460388697284} | ||
m_Mesh: {fileID: 10202, guid: 0000000000000000e000000000000000, type: 0} | ||
--- !u!65 &65019600772888056 | ||
BoxCollider: | ||
m_ObjectHideFlags: 1 | ||
m_CorrespondingSourceObject: {fileID: 0} | ||
m_PrefabInternal: {fileID: 100100000} | ||
m_GameObject: {fileID: 1076460388697284} | ||
m_Material: {fileID: 0} | ||
m_IsTrigger: 0 | ||
m_Enabled: 1 | ||
serializedVersion: 2 | ||
m_Size: {x: 1, y: 1, z: 1} | ||
m_Center: {x: 0, y: 0, z: 0} | ||
--- !u!114 &114021302544953256 | ||
MonoBehaviour: | ||
m_ObjectHideFlags: 1 | ||
m_CorrespondingSourceObject: {fileID: 0} | ||
m_PrefabInternal: {fileID: 100100000} | ||
m_GameObject: {fileID: 1076460388697284} | ||
m_Enabled: 1 | ||
m_EditorHideFlags: 0 | ||
m_Script: {fileID: 11500000, guid: b874011ae29b94084bc65258ba6cedee, type: 3} | ||
m_Name: | ||
m_EditorClassIdentifier: | ||
MicLoudness: 0 | ||
maxMicInputVolume: 0.02 | ||
gameObjectToScale: {fileID: 1076460388697284} | ||
objectScaleMin: 1 | ||
objectScaleMax: 10 |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.