Using Microphones
Feature Rich User Audio
NatDevice provides access to hardware microphones on all platforms it supports. These microphones will stream audio directly to the client on a dedicated audio thread. The audio data can then be processed by the user.
When building for iOS, you must enable the
Prepare iOS for Recording
setting in Player Settings.We begin by finding a microphone with a device query:
void Start () {
// Create a device query to find microphones
var filter = MediaDeviceCriteria.AudioDevice;
var query = new MediaDeviceQuery(filter);
// Get the microphone
var device = query.current as AudioDevice;
}
Audio devices work by streaming audio buffers to a user-provided delegate function:
void Start () {
...
// Stream audio to our callback
device.StartRunning(OnAudioBuffer);
}
The provided delegate is invoked with
AudioBuffer
instances as they are streamed from the audio device:void OnAudioBuffer (AudioBuffer audioBuffer) {
// Do stuff
...
}
The audio buffers always contain linear PCM samples interleaved by channel.
The provided delegate is not guaranteed to be invoked on the Unity main thread, so refrain from using Unity APIs within this method.
Audio devices allow for specifying the format of the audio from the microphone. The audio format is defined by the
sampleRate
and channelCount
:void Start () {
...
// Set the sample rate
device.sampleRate = 44100;
// Set the channel count to stereo
device.channelCount = 2;
// Stream audio to our callback
device.StartRunning(OnAudioBuffer);
}
When you are done with the microphone, you must stop it:
// Stop streaming audio
device.StopRunning();
Last modified 10mo ago