Skip to content

Commit 7aa56cf

Browse files
Updated Mic Stream Demo.
Fixes null reference when starting & NaN errors from MicStreamSelector.dll
1 parent 08e190c commit 7aa56cf

File tree

3 files changed

+98
-60
lines changed

3 files changed

+98
-60
lines changed

Assets/HoloToolkit/Input/Scripts/Microphone/MicStream.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -162,10 +162,10 @@ public static bool CheckForErrorOnCall(int returnCode)
162162
switch (returnCode)
163163
{
164164
case (int)ErrorCodes.ALREADY_RECORDING:
165-
Debug.LogError("WARNING: Tried to start recording when you were already doing so. You need to stop your previous recording before you can start again.");
165+
Debug.LogWarning("WARNING: Tried to start recording when you were already doing so. You need to stop your previous recording before you can start again.");
166166
return false;
167167
case (int)ErrorCodes.ALREADY_RUNNING:
168-
Debug.LogError("WARNING: Tried to initialize microphone more than once");
168+
Debug.LogWarning("WARNING: Tried to initialize microphone more than once");
169169
return false;
170170
case (int)ErrorCodes.GRAPH_NOT_EXIST:
171171
Debug.LogError("ERROR: Tried to do microphone things without a properly initialized microphone. \n Do you have a mic plugged into a functional audio system and did you call MicInitialize() before anything else ??");

Assets/HoloToolkit/Input/Tests/Scenes/MicrophoneStream.unity

Lines changed: 45 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
%YAML 1.1
22
%TAG !u! tag:unity3d.com,2011:
33
--- !u!29 &1
4-
SceneSettings:
4+
OcclusionCullingSettings:
55
m_ObjectHideFlags: 0
6-
m_PVSData:
7-
m_PVSObjectsArray: []
8-
m_PVSPortalsArray: []
6+
serializedVersion: 2
97
m_OcclusionBakeSettings:
108
smallestOccluder: 5
119
smallestHole: 0.25
1210
backfaceThreshold: 100
11+
m_SceneGUID: 00000000000000000000000000000000
12+
m_OcclusionCullingData: {fileID: 0}
1313
--- !u!104 &2
1414
RenderSettings:
1515
m_ObjectHideFlags: 0
@@ -37,7 +37,7 @@ RenderSettings:
3737
m_ReflectionIntensity: 1
3838
m_CustomReflection: {fileID: 0}
3939
m_Sun: {fileID: 0}
40-
m_IndirectSpecularColor: {r: 0.44692558, g: 0.49678743, b: 0.57508624, a: 1}
40+
m_IndirectSpecularColor: {r: 0.44657898, g: 0.4964133, b: 0.5748178, a: 1}
4141
--- !u!157 &3
4242
LightmapSettings:
4343
m_ObjectHideFlags: 0
@@ -79,29 +79,30 @@ NavMeshSettings:
7979
m_ObjectHideFlags: 0
8080
m_BuildSettings:
8181
serializedVersion: 2
82+
agentTypeID: 0
8283
agentRadius: 0.5
8384
agentHeight: 2
8485
agentSlope: 45
8586
agentClimb: 0.4
8687
ledgeDropHeight: 0
8788
maxJumpAcrossDistance: 0
88-
accuratePlacement: 0
8989
minRegionArea: 2
90-
cellSize: 0.16666667
9190
manualCellSize: 0
91+
cellSize: 0.16666667
92+
accuratePlacement: 0
9293
m_NavMeshData: {fileID: 0}
9394
--- !u!1 &156915665
9495
GameObject:
9596
m_ObjectHideFlags: 0
9697
m_PrefabParentObject: {fileID: 0}
9798
m_PrefabInternal: {fileID: 0}
98-
serializedVersion: 4
99+
serializedVersion: 5
99100
m_Component:
100-
- 4: {fileID: 156915670}
101-
- 20: {fileID: 156915669}
102-
- 92: {fileID: 156915668}
103-
- 124: {fileID: 156915667}
104-
- 81: {fileID: 156915666}
101+
- component: {fileID: 156915670}
102+
- component: {fileID: 156915669}
103+
- component: {fileID: 156915668}
104+
- component: {fileID: 156915667}
105+
- component: {fileID: 156915666}
105106
m_Layer: 0
106107
m_Name: Main Camera
107108
m_TagString: MainCamera
@@ -173,19 +174,19 @@ Transform:
173174
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
174175
m_LocalPosition: {x: 0, y: 0, z: 0}
175176
m_LocalScale: {x: 1, y: 1, z: 1}
176-
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
177177
m_Children: []
178178
m_Father: {fileID: 0}
179179
m_RootOrder: 0
180+
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
180181
--- !u!1 &345633917
181182
GameObject:
182183
m_ObjectHideFlags: 0
183184
m_PrefabParentObject: {fileID: 0}
184185
m_PrefabInternal: {fileID: 0}
185-
serializedVersion: 4
186+
serializedVersion: 5
186187
m_Component:
187-
- 4: {fileID: 345633919}
188-
- 108: {fileID: 345633918}
188+
- component: {fileID: 345633919}
189+
- component: {fileID: 345633918}
189190
m_Layer: 0
190191
m_Name: Directional Light
191192
m_TagString: Untagged
@@ -236,23 +237,23 @@ Transform:
236237
m_LocalRotation: {x: 0.40821788, y: -0.23456968, z: 0.10938163, w: 0.8754261}
237238
m_LocalPosition: {x: 0, y: 3, z: 0}
238239
m_LocalScale: {x: 1, y: 1, z: 1}
239-
m_LocalEulerAnglesHint: {x: 50, y: -30, z: 0}
240240
m_Children: []
241241
m_Father: {fileID: 0}
242242
m_RootOrder: 1
243+
m_LocalEulerAnglesHint: {x: 50, y: -30, z: 0}
243244
--- !u!1 &1702215501
244245
GameObject:
245246
m_ObjectHideFlags: 0
246247
m_PrefabParentObject: {fileID: 0}
247248
m_PrefabInternal: {fileID: 0}
248-
serializedVersion: 4
249+
serializedVersion: 5
249250
m_Component:
250-
- 4: {fileID: 1702215505}
251-
- 33: {fileID: 1702215504}
252-
- 65: {fileID: 1702215503}
253-
- 23: {fileID: 1702215502}
254-
- 82: {fileID: 1702215507}
255-
- 114: {fileID: 1702215506}
251+
- component: {fileID: 1702215505}
252+
- component: {fileID: 1702215504}
253+
- component: {fileID: 1702215503}
254+
- component: {fileID: 1702215502}
255+
- component: {fileID: 1702215507}
256+
- component: {fileID: 1702215506}
256257
m_Layer: 0
257258
m_Name: Cube
258259
m_TagString: Untagged
@@ -274,15 +275,17 @@ MeshRenderer:
274275
m_ReflectionProbeUsage: 1
275276
m_Materials:
276277
- {fileID: 2100000, guid: 474f0e0481d4aa14e92a5ada98042f3a, type: 2}
277-
m_SubsetIndices:
278+
m_StaticBatchInfo:
279+
firstSubMesh: 0
280+
subMeshCount: 0
278281
m_StaticBatchRoot: {fileID: 0}
279282
m_ProbeAnchor: {fileID: 0}
280283
m_LightProbeVolumeOverride: {fileID: 0}
281284
m_ScaleInLightmap: 1
282285
m_PreserveUVs: 1
283286
m_IgnoreNormalsForChartDetection: 0
284287
m_ImportantGI: 0
285-
m_SelectedWireframeHidden: 0
288+
m_SelectedEditorRenderState: 3
286289
m_MinimumChartSize: 4
287290
m_AutoUVMaxDistance: 0.5
288291
m_AutoUVMaxAngle: 89
@@ -317,10 +320,10 @@ Transform:
317320
m_LocalRotation: {x: -0, y: -0, z: -0, w: 1}
318321
m_LocalPosition: {x: 0, y: 0, z: 5}
319322
m_LocalScale: {x: 0.5, y: 0.5, z: 0.5}
320-
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
321323
m_Children: []
322324
m_Father: {fileID: 0}
323325
m_RootOrder: 2
326+
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
324327
--- !u!114 &1702215506
325328
MonoBehaviour:
326329
m_ObjectHideFlags: 0
@@ -332,12 +335,13 @@ MonoBehaviour:
332335
m_Script: {fileID: 11500000, guid: 19fe6525d557689439c0225dda308086, type: 3}
333336
m_Name:
334337
m_EditorClassIdentifier:
335-
StreamType: 1
336-
InputGain: 1
338+
StreamType: 0
339+
InputGain: 0.5
337340
KeepAllData: 0
338341
AutomaticallyStartStream: 1
339342
ListenToAudioSource: 1
340343
SaveFileName: MicrophoneTest.wav
344+
minObjectScale: 0.3
341345
--- !u!82 &1702215507
342346
AudioSource:
343347
m_ObjectHideFlags: 0
@@ -354,6 +358,7 @@ AudioSource:
354358
Loop: 0
355359
Mute: 0
356360
Spatialize: 0
361+
SpatializePostEffects: 0
357362
Priority: 128
358363
DopplerLevel: 1
359364
MinDistance: 1
@@ -366,12 +371,14 @@ AudioSource:
366371
rolloffCustomCurve:
367372
serializedVersion: 2
368373
m_Curve:
369-
- time: 0
374+
- serializedVersion: 2
375+
time: 0
370376
value: 1
371377
inSlope: 0
372378
outSlope: 0
373379
tangentMode: 0
374-
- time: 1
380+
- serializedVersion: 2
381+
time: 1
375382
value: 0
376383
inSlope: 0
377384
outSlope: 0
@@ -382,7 +389,8 @@ AudioSource:
382389
panLevelCustomCurve:
383390
serializedVersion: 2
384391
m_Curve:
385-
- time: 0
392+
- serializedVersion: 2
393+
time: 0
386394
value: 0
387395
inSlope: 0
388396
outSlope: 0
@@ -393,7 +401,8 @@ AudioSource:
393401
spreadCustomCurve:
394402
serializedVersion: 2
395403
m_Curve:
396-
- time: 0
404+
- serializedVersion: 2
405+
time: 0
397406
value: 0
398407
inSlope: 0
399408
outSlope: 0
@@ -404,7 +413,8 @@ AudioSource:
404413
reverbZoneMixCustomCurve:
405414
serializedVersion: 2
406415
m_Curve:
407-
- time: 0
416+
- serializedVersion: 2
417+
time: 0
408418
value: 1
409419
inSlope: 0
410420
outSlope: 0

Assets/HoloToolkit/Input/Tests/Scripts/MicStreamDemo.cs

Lines changed: 51 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -9,45 +9,50 @@ namespace HoloToolkit.Unity.InputModule.Tests
99
public class MicStreamDemo : MonoBehaviour
1010
{
1111
/// <summary>
12-
/// Which type of microphone/quality to access
12+
/// Which type of microphone/quality to access.
1313
/// </summary>
1414
public MicStream.StreamCategory StreamType = MicStream.StreamCategory.HIGH_QUALITY_VOICE;
1515

1616
/// <summary>
17-
/// can boost volume here as desired. 1 is default but probably too quiet. can change during operation.
17+
/// Can boost volume here as desired. 1 is default.
18+
/// <remarks>Can be updated at runtime.</remarks>
1819
/// </summary>
1920
public float InputGain = 1;
2021

2122
/// <summary>
22-
/// if keepAllData==false, you'll always get the newest data no matter how long the program hangs for any reason, but will lose some data if the program does hang
23-
/// can only be set on initialization
23+
/// if keepAllData==false, you'll always get the newest data no matter how long the program hangs for any reason,
24+
/// but will lose some data if the program does hang.
25+
/// <remarks>Can only be set on initialization.</remarks>
2426
/// </summary>
2527
public bool KeepAllData;
2628

2729
/// <summary>
28-
/// Should the mic stream start automatically when this component is enabled?
30+
/// If true, Starts the mic stream automatically when this component is enabled.
2931
/// </summary>
3032
public bool AutomaticallyStartStream = true;
3133

3234
/// <summary>
33-
/// Do you want to hear what the microphone sounds like by listening to the AudioSource in Unity?
35+
/// Plays back the microphone audio source though default audio device.
3436
/// </summary>
35-
public bool ListenToAudioSource = true;
37+
public bool PlaybackMicrophoneAudioSource = true;
3638

3739
/// <summary>
38-
/// The name of the file to which to save audio (for commands that save to a file)
40+
/// The name of the file to which to save audio (for commands that save to a file).
3941
/// </summary>
4042
public string SaveFileName = "MicrophoneTest.wav";
4143

4244
/// <summary>
43-
/// Records estimation of volume from the microphone to affect other elements of the game object
45+
/// Records estimation of volume from the microphone to affect other elements of the game object.
4446
/// </summary>
4547
private float averageAmplitude;
4648

4749
/// <summary>
48-
/// how small can our object be in this demo?
50+
/// Minimum size the demo cube can be during runtime.
4951
/// </summary>
50-
private float minSize = .3f;
52+
[SerializeField]
53+
private float minObjectScale = .3f;
54+
55+
private bool isRunning;
5156

5257
private void OnAudioFilterRead(float[] buffer, int numChannels)
5358
{
@@ -59,17 +64,23 @@ private void OnAudioFilterRead(float[] buffer, int numChannels)
5964
// figure out the average amplitude from this new data
6065
for (int i = 0; i < buffer.Length; i++)
6166
{
62-
sumOfValues += Mathf.Abs(buffer[i]);
67+
if (float.IsNaN(buffer[i]))
68+
{
69+
buffer[i] = 0;
70+
}
71+
72+
buffer[i] = Mathf.Clamp(buffer[i], -1f, 1f);
73+
sumOfValues += Mathf.Clamp01(Mathf.Abs(buffer[i]));
6374
}
6475
averageAmplitude = sumOfValues / buffer.Length;
6576
}
6677

67-
private void Awake()
78+
private void Start()
6879
{
6980
CheckForErrorOnCall(MicStream.MicInitializeCustomRate((int)StreamType, AudioSettings.outputSampleRate));
7081
CheckForErrorOnCall(MicStream.MicSetGain(InputGain));
7182

72-
if (!ListenToAudioSource)
83+
if (!PlaybackMicrophoneAudioSource)
7384
{
7485
gameObject.GetComponent<AudioSource>().volume = 0; // can set to zero to mute mic monitoring
7586
}
@@ -84,6 +95,7 @@ private void Awake()
8495
print("It will start a recording and save it to a wav file. S will stop that recording.");
8596
print("Since this all goes through the AudioSource, you can mute the mic while using it there, or do anything else you would do with an AudioSource");
8697
print("In this demo, we start the stream automatically, and then change the size of the gameobject based on microphone signal amplitude");
98+
isRunning = true;
8799
}
88100

89101
private void OnDestroy()
@@ -114,35 +126,51 @@ private void Update()
114126
CheckForErrorOnCall(MicStream.MicStopStream());
115127
}
116128

117-
gameObject.transform.localScale = new Vector3(minSize + averageAmplitude, minSize + averageAmplitude, minSize + averageAmplitude);
129+
gameObject.transform.localScale = new Vector3(minObjectScale + averageAmplitude, minObjectScale + averageAmplitude, minObjectScale + averageAmplitude);
118130
}
119131

120132
private static void CheckForErrorOnCall(int returnCode)
121133
{
122134
MicStream.CheckForErrorOnCall(returnCode);
123135
}
124136

125-
#if DOTNET_FX
126-
// On device, deal with all the ways that we could suspend our program in as few lines as possible.
127-
private void OnApplicationPause(bool pause)
137+
private void ToggleMicStream(bool pause)
128138
{
129139
CheckForErrorOnCall(pause ? MicStream.MicPause() : MicStream.MicResume());
140+
isRunning = pause;
130141
}
131142

132-
private void OnApplicationFocus(bool focused)
143+
private void OnApplicationPause(bool pause)
133144
{
134-
OnApplicationPause(!focused);
145+
if (isRunning)
146+
{
147+
ToggleMicStream(pause);
148+
}
135149
}
136150

151+
#if !UNITY_EDITOR
152+
private void OnApplicationFocus(bool focused)
153+
{
154+
if (isInitialized)
155+
{
156+
ToggleMicStream(!focused);
157+
}
158+
}
159+
#endif
137160
private void OnDisable()
138161
{
139-
OnApplicationPause(true);
162+
if (isRunning)
163+
{
164+
ToggleMicStream(true);
165+
}
140166
}
141167

142168
private void OnEnable()
143169
{
144-
OnApplicationPause(false);
170+
if (isRunning)
171+
{
172+
ToggleMicStream(false);
173+
}
145174
}
146-
#endif
147175
}
148176
}

0 commit comments

Comments
 (0)