我目前正在使用 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?