AR Design
UBC EML collab with UBC SALA - visualizing IoT data in AR
MicStream.cs
Go to the documentation of this file.
1 // Copyright (c) Microsoft Corporation. All rights reserved.
2 // Licensed under the MIT License. See LICENSE in the project root for license information.
3 
4 using System.Runtime.InteropServices;
5 using System.Text;
6 using UnityEngine;
7 
8 namespace HoloToolkit.Unity.InputModule
9 {
10  public class MicStream
11  {
12  // This class replaces Unity's Microphone object.
13  // This class is made for HoloLens mic stream selection but should work well on all Windows 10 devices.
14  // Choose from one of three possible microphone modes on HoloLens.
15  // There is an example of how to use this script in HoloToolkit-Tests\Input\Scripts\MicStreamDemo.cs.
16 
17  // Streams: LOW_QUALITY_VOICE is optimized for speech analysis.
18  // COMMUNICATIONS is higher quality voice and is probably preferred.
19  // ROOM_CAPTURE tries to get the sounds of the room more than the voice of the user.
20  // This can only be set on initialization.
21  public enum StreamCategory { LOW_QUALITY_VOICE, HIGH_QUALITY_VOICE, ROOM_CAPTURE }
22 
23  public enum ErrorCodes { ALREADY_RUNNING = -10, NO_AUDIO_DEVICE, NO_INPUT_DEVICE, ALREADY_RECORDING, GRAPH_NOT_EXIST, CHANNEL_COUNT_MISMATCH, FILE_CREATION_PERMISSION_ERROR, NOT_ENOUGH_DATA, NEED_ENABLED_MIC_CAPABILITY };
24 
25  const int MAX_PATH = 260; // 260 is maximum path length in windows, to be returned when we MicStopRecording
26 
27  [UnmanagedFunctionPointer(CallingConvention.StdCall)] // If included in MicStartStream, this callback will be triggered when audio data is ready. This is not the preferred method for Game Engines and can probably be ignored.
28  public delegate void LiveMicCallback();
29 
35  [DllImport("MicStreamSelector", ExactSpelling = true)]
36  public static extern int MicInitializeDefault(int category);
37 
44  [DllImport("MicStreamSelector", ExactSpelling = true)]
45  public static extern int MicInitializeCustomRate(int category, int samplerate);
46 
54  [DllImport("MicStreamSelector", ExactSpelling = true)]
55  public static extern int MicStartStream(bool keepData, bool previewOnDevice, LiveMicCallback micsignal);
56 
63  public static int MicStartStream(bool keepData, bool previewOnDevice)
64  {
65  return MicStartStream(keepData, previewOnDevice, null);
66  }
67 
72  [DllImport("MicStreamSelector", ExactSpelling = true)]
73  public static extern int MicStopStream();
74 
81  [DllImport("MicStreamSelector", ExactSpelling = true)]
82  public static extern int MicStartRecording(string filename, bool previewOnDevice);
83 
88  [DllImport("MicStreamSelector", ExactSpelling = true)]
89  public static extern void MicStopRecording(StringBuilder sb);
90 
95  public static string MicStopRecording()
96  {
97  StringBuilder builder = new StringBuilder(MAX_PATH);
98  MicStopRecording(builder);
99  return builder.ToString();
100  }
101 
106  [DllImport("MicStreamSelector", ExactSpelling = true)]
107  public static extern int MicDestroy();
108 
113  [DllImport("MicStreamSelector", ExactSpelling = true)]
114 
115  public static extern int MicPause();
116 
121  [DllImport("MicStreamSelector", ExactSpelling = true)]
122  public static extern int MicResume();
123 
129  [DllImport("MicStreamSelector", ExactSpelling = true)]
130  public static extern int MicSetGain(float g);
131 
136  [DllImport("MicStreamSelector", ExactSpelling = true)]
137  private static extern int MicGetDefaultBufferSize();
138 
143  [DllImport("MicStreamSelector", ExactSpelling = true)]
144  private static extern int MicGetDefaultNumChannels();
145 
153  [DllImport("MicStreamSelector", ExactSpelling = true)]
154  public static extern int MicGetFrame(float[] buffer, int length, int numchannels);
155 
161  public static bool CheckForErrorOnCall(int returnCode)
162  {
163  switch (returnCode)
164  {
165  case (int)ErrorCodes.ALREADY_RECORDING:
166  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.");
167  return false;
168  case (int)ErrorCodes.ALREADY_RUNNING:
169  Debug.LogWarning("WARNING: Tried to initialize microphone more than once");
170  return false;
171  case (int)ErrorCodes.GRAPH_NOT_EXIST:
172  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 ??");
173  return false;
174  case (int)ErrorCodes.NO_AUDIO_DEVICE:
175  Debug.LogError("ERROR: Tried to start microphone, but you don't appear to have a functional audio device. check your OS audio settings.");
176  return false;
177  case (int)ErrorCodes.NO_INPUT_DEVICE:
178  Debug.LogError("ERROR: Tried to start microphone, but you don't have one plugged in, do you?");
179  return false;
180  case (int)ErrorCodes.CHANNEL_COUNT_MISMATCH:
181  Debug.LogError("ERROR: Microphone had a channel count mismatch internally on device. Try setting different mono/stereo options in OS mic settings.");
182  return false;
183  case (int)ErrorCodes.FILE_CREATION_PERMISSION_ERROR:
184  Debug.LogError("ERROR: Didn't have access to create file in Music library. Make sure permissions to write to Music library are set granted.");
185  return false;
186  case (int)ErrorCodes.NOT_ENOUGH_DATA:
187  // usually not an error, means the device hasn't produced enough data yet because it just started running
188  return false;
189  case (int)ErrorCodes.NEED_ENABLED_MIC_CAPABILITY:
190  Debug.LogError("ERROR: Seems like you forgot to enable the microphone capabilities in your Unity permissions");
191  return false;
192  }
193  return true;
194  }
195  }
196 }
static string MicStopRecording()
Finishes writing the file recording started with MicStartRecording.
Definition: MicStream.cs:95
static int MicStartStream(bool keepData, bool previewOnDevice)
Call this to start receiving data from a microphone. Then, each frame, call MicGetFrame.
Definition: MicStream.cs:63
static bool CheckForErrorOnCall(int returnCode)
Prints useful error/warning messages based on error codes returned from the functions in this class ...
Definition: MicStream.cs:161