C# 使用Kinect录制音频
我尝试调整此Wav录制示例: 对于新SDK(1.6版),由于某种原因,生成的.wav文件无效 在Init中:C# 使用Kinect录制音频,c#,audio,kinect,wav,kinect-sdk,C#,Audio,Kinect,Wav,Kinect Sdk,我尝试调整此Wav录制示例: 对于新SDK(1.6版),由于某种原因,生成的.wav文件无效 在Init中: this.audioStream = this.sensor.AudioSource.Start(); // Use a separate thread for capturing audio because audio stream read operations // will block, and we don't want to
this.audioStream = this.sensor.AudioSource.Start();
// Use a separate thread for capturing audio because audio stream read operations
// will block, and we don't want to block main UI thread.
this.readingThread = new Thread(AudioReadingThread);
this.readingThread.Start();
fileStream = new FileStream(@"d:\temp\temp.wav", FileMode.Create);
int rec_time = (int) 20 * 2 * 16000;//20 sec
WriteWavHeader(fileStream, rec_time);
线程:
private void AudioReadingThread()
{
while (this.reading)
{
int readCount = audioStream.Read(audioBuffer, 0, audioBuffer.Length);
fileStream.Write(audioBuffer, 0, readCount);
}
}
Wav标题:
static void WriteWavHeader(Stream stream, int dataLength)
{
//We need to use a memory stream because the BinaryWriter will close the underlying stream when it is closed
using (var memStream = new MemoryStream(64))
{
int cbFormat = 18; //sizeof(WAVEFORMATEX)
WAVEFORMATEX format = new WAVEFORMATEX()
{
wFormatTag = 1,
nChannels = 1,
nSamplesPerSec = 16000,
nAvgBytesPerSec = 32000,
nBlockAlign = 2,
wBitsPerSample = 16,
cbSize = 0
};
using (var bw = new BinaryWriter(memStream))
{
bw.Write(dataLength + cbFormat + 4); //File size - 8
bw.Write(cbFormat);
//WAVEFORMATEX
bw.Write(format.wFormatTag);
bw.Write(format.nChannels);
bw.Write(format.nSamplesPerSec);
bw.Write(format.nAvgBytesPerSec);
bw.Write(format.nBlockAlign);
bw.Write(format.wBitsPerSample);
bw.Write(format.cbSize);
//data header
bw.Write(dataLength);
memStream.WriteTo(stream);
}
}
}
您忘了添加代码以将“RIFF头”添加到文件中。代码非常简单,如下所示:
//RIFF header
WriteString(memStream, "RIFF");
bw.Write(dataLength + cbFormat + 4); //File size - 8
WriteString(memStream, "WAVE");
WriteString(memStream, "fmt ");
bw.Write(cbFormat);
您还忘了在“数据头”处修改memStream
,您需要行:
WriteString(memStream, "data");
此.audioStream是否有效?如何设置传感器?代码运行正常,传感器已设置,wav文件不为空。只是格式可能有问题,你能把结果文件发给我吗?在试图达到相同的目标时,我在设置此代码以运行时遇到了一些问题。WAVEFORMATEX和WriteString类来自哪些名称空间?在代码中,我应该将riff头放在哪里,并在数据头处修改memstream?