Initialization of a microphone, and recorded sounds processing:
private void Initialize()
{
Microphone microphone = Microphone.Default;
// 100 ms is a minimum buffer duration
microphone.BufferDuration = TimeSpan.FromMilliseconds(100);
DispatcherTimer updateTimer = new DispatcherTimer()
{
Interval = TimeSpan.FromMilliseconds(0.1)
};
updateTimer.Tick += (s, e) =>
{
FrameworkDispatcher.Update();
};
updateTimer.Start();
byte[] microphoneSignal = new byte[microphone.GetSampleSizeInBytes(microphone.BufferDuration)];
microphone.BufferReady += (s, e) =>
{
int microphoneDataSize = microphone.GetData(microphoneSignal);
double amplitude = GetSignalAmplitude(microphoneSignal);
// do your stuff with amplitude here
};
microphone.Start();
}
Amplitude of the overall signal. You can find averages not in all byte array, but in smaller windows to get amplitude curve:
private double GetSignalAmplitude(byte[] signal)
{
int BytesInSample = 2;
int signalSize = signal.Length / BytesInSample;
double Sum = 0.0;
for (int i = 0; i < signalSize; i++)
{
int sample = Math.Abs(BitConverter.ToInt16(signal, i * BytesInSample));
Sum += sample;
}
double amplitude = Sum / signalSize;
return amplitude;
}
Other stuff for generating sounds on-the-fly that possible help you in future:
DynamicSoundEffectInstance generatedSound = new DynamicSoundEffectInstance(SampleRate, AudioChannels.Mono);
generatedSound.SubmitBuffer(buffer);
private void Int16ToTwoBytes(byte[] output, Int16 value, int offset)
{
output[offset + 1] = (byte)(value >> 8);
output[offset] = (byte)(value & 0x00FF);
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…