我目前正在使用Naudio录制用户的语音并将其转换为字节数组,以便我可以将FFT应用到其上并将其转换为双数组然后转换为频谱图。我目前停留在将WAV文件转换为字节数组。谢谢。
private void mic_Click(object sender, EventArgs e)
{
if (MicB.Text.Equals("Start"))
{
int deviceNumber = 0;
sourceStream = new NAudio.Wave.WaveIn();
sourceStream.DeviceNumber = deviceNumber;
sourceStream.WaveFormat = new NAudio.Wave.WaveFormat(44100, NAudio.Wave.WaveIn.GetCapabilities(deviceNumber).Channels);
sourceStream.DataAvailable += new EventHandler<NAudio.Wave.WaveInEventArgs>(sourceStream_DataAvailable);
waveWriter = new NAudio.Wave.WaveFileWriter(test.wav, sourceStream.WaveFormat);
sourceStream.StartRecording();
MicB.Text = "Stop";
}
else if (MicB.Text.Equals("Stop"))
{
if (waveOut != null)
{
waveOut.Stop();
waveOut.Dispose();
waveOut = null;
}
if (sourceStream != null)
{
sourceStream.StopRecording();
sourceStream.Dispose();
sourceStream = null;
}
if (waveWriter != null)
{
waveWriter.Dispose();
waveWriter = null;
}
MicB.Text = "Start";
}
}
private void sourceStream_DataAvailable(object sender, NAudio.Wave.WaveInEventArgs e)
{
if (waveWriter == null) return;
waveWriter.Write(e.Buffer, 0, e.BytesRecorded);
waveWriter.Flush();
}
编辑:我从Coding4Fun获得了这个代码并编辑了一小部分,但不确定这是我正在寻找的是转换为浮点数并将FFT应用到它上面。对不起,我对音频信号完全陌生。
private void spectro_Click(object sender, EventArgs e)
{
//convert to float
using (WaveFileReader reader = new WaveFileReader(test.wav))
{
IWaveProvider stream32 = new Wave16ToFloatProvider(reader);
using (WaveFileWriter converted = new WaveFileWriter(temp.wav, stream32.WaveFormat))
{
byte[] buffer = new byte[8192];
int bytesRead;
do
{
bytesRead = stream32.Read(buffer, 0, buffer.Length);
converted.Write(buffer, 0, bytesRead);
} while (bytesRead != 0 && converted.Length < reader.Length);
}
}
}
问题:如何在转换为浮点后应用FFT?
答案 0 :(得分:0)
这很难回答,但我可以猜测。您正在使用WaveFileWriter
。库中是否存在WaveStreamWriter
?在这种情况下,您可以提供MemoryStream
作为参数,并使用流上的ToArray
来获取字节。