本文介绍了c#中的自动语音记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
i希望在调用某个if语句后自动将麦克风的语音记录15秒,并将记录的.wav文件存储在c中:
到现在为止我已经阅读并使用按钮实现了MSDN记录功能。
但是我不想要按钮我希望它是自动的。
[]
从上面链接中你可以看到他们如何记录但是使用按钮。
任何人都可以分享关于如何自动制作的提示或想法吗?记录没有按钮?
谢谢各位朋友
Hi,
i want to record users speech from mic automatically for 15 seconds after a certain if statement has been called and store that recorded .wav file in c:
till now i have read and implemented MSDN record function using buttons.
but i dont want button i want it to be automatic.
http://msdn.microsoft.com/en-us/library/ff827802.aspx[^]
from above link you can see how they record but using buttons .
can anyone share a tip or an idea on how to make it automatically? record without buttons?
Thank you friends
推荐答案
class MicrophoneRec {
private Timer timer = null;
private Microphone mic = Microphone.Default;
List<byte[]> byteList = new List<byte[]>();
private string outPutFile = @"D:\TempDir\test.wav";
public void micStart(int durationToRecordSec) {
if (mic != null) {
timer = new Timer();
timer.Interval = (durationToRecordSec * 1000);
timer.Elapsed += new ElapsedEventHandler(t_Elapsed);
mic.BufferDuration = TimeSpan.FromMilliseconds(100);
mic.BufferReady += new EventHandler<EventArgs>(mic_BufferReady);
timer.Start();
mic.Start();
}
}
void mic_BufferReady(object sender, EventArgs e) {
byte[] data = new byte[mic.GetSampleSizeInBytes(mic.BufferDuration)];
mic.GetData(data);
byteList.Add(data);
}
void t_Elapsed(object sender, ElapsedEventArgs e) {
mic.Stop();
timer.Stop();
byte[] buffer = new byte[byteList.Select(x => x.Length).Sum()];
int offset = 0;
foreach (var buff in byteList) {
System.Buffer.BlockCopy(buff, 0, buffer, offset, buff.Length);
offset += buff.Length;
}
using (FileStream fs = new FileStream(outPutFile, FileMode.Create)) {
WaveHeader.WriteHeader(fs, buffer.Length, 1, mic.SampleRate);
fs.Write(buffer, 0, buffer.Length);
}
}
}
class WaveHeader {
static byte[] RIFF_HEADER = new byte[] { 0x52, 0x49, 0x46, 0x46 };
static byte[] FORMAT_WAVE = new byte[] { 0x57, 0x41, 0x56, 0x45 };
static byte[] FORMAT_TAG = new byte[] { 0x66, 0x6d, 0x74, 0x20 };
static byte[] AUDIO_FORMAT = new byte[] { 0x01, 0x00 };
static byte[] SUBCHUNK_ID = new byte[] { 0x64, 0x61, 0x74, 0x61 };
private const int BYTES_PER_SAMPLE = 2;
public static void WriteHeader(System.IO.Stream targetStream,
int byteStreamSize, int channelCount, int sampleRate) {
int byteRate = sampleRate * channelCount * BYTES_PER_SAMPLE;
int blockAlign = channelCount * BYTES_PER_SAMPLE;
targetStream.Write(RIFF_HEADER, 0, RIFF_HEADER.Length);
targetStream.Write(PackageInt(byteStreamSize + 42, 4), 0, 4);
targetStream.Write(FORMAT_WAVE, 0, FORMAT_WAVE.Length);
targetStream.Write(FORMAT_TAG, 0, FORMAT_TAG.Length);
targetStream.Write(PackageInt(16, 4), 0, 4);//Subchunk1Size
targetStream.Write(AUDIO_FORMAT, 0, AUDIO_FORMAT.Length);//AudioFormat
targetStream.Write(PackageInt(channelCount, 2), 0, 2);
targetStream.Write(PackageInt(sampleRate, 4), 0, 4);
targetStream.Write(PackageInt(byteRate, 4), 0, 4);
targetStream.Write(PackageInt(blockAlign, 2), 0, 2);
targetStream.Write(PackageInt(BYTES_PER_SAMPLE * 8), 0, 2);
targetStream.Write(SUBCHUNK_ID, 0, SUBCHUNK_ID.Length);
targetStream.Write(PackageInt(byteStreamSize, 4), 0, 4);
}
static byte[] PackageInt(int source, int length = 2) {
if ((length != 2) && (length != 4))
throw new ArgumentException("length must be either 2 or 4", "length");
var retVal = new byte[length];
retVal[0] = (byte)(source & 0xFF);
retVal[1] = (byte)((source >> 8) & 0xFF);
if (length == 4) {
retVal[2] = (byte)((source >> 0x10) & 0xFF);
retVal[3] = (byte)((source >> 0x18) & 0xFF);
}
return retVal;
}
}
这篇关于c#中的自动语音记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!