-1

在我的应用程序中,我需要录制一些 wav 文件并将它们设置为特定的音量和音高。现在我可以使用winmm.dll录制和播放 .wav 文件,但我不知道如何修改它们。

private void Record()
    {
        mciSendString("open new Type waveaudio Alias recsound", "", 0, 0);
        mciSendString("record recsound", "", 0, 0);            
    }

private void StopRecord(string file)
    {
        string path = "C:\\Users\\Workshop\\Dani\\audiofiles\\audiofiles\\" + this.Name + '\\' + file + ".wav";
        mciSendString("save recsound " + path, "", 0, 0);
        mciSendString("close recsound", "", 0, 0);

    }

private void Reproduce (string path)
    {
        SoundPlayer player = new SoundPlayer(path);
        player.Load();
        player.Play();
    }

通过搜索互联网,我看到使用 NAudio.dll 可以修改 .wav 文件,但我并不完全理解它是如何工作的。

4

1 回答 1

0

通过使用 Naudio 库,可以获取 .wav 文件的所有样本并根据需要进行修改。

wave = new NAudio.Wave.WaveFileReader(path);
float[] samples = new float[wave.SampleCount];
a = wave.ReadNextSampleFrame();
int i = 0;
while (a != null)
   {
     samples[i] = a[0]*ratio;
     i++;                
     a = wave.ReadNextSampleFrame();
   }
//create the new .wav with the samples modified
WaveFormat waveFormat = wave.WaveFormat;
wave.Dispose();
WaveFileWriter writer = new WaveFileWriter(path, waveFormat);
for (int u=0; u < i; u++)
  {
     writer.WriteSample(samples[u]);
  }
writer.Dispose();

重要的是要记住每个样本都是一个浮点数,它的值从 -1.0f 到 1.0f。同样重要的是要知道音频记录在多少个通道中,在我的情况下它只有一个通道,这就是我使用 a[0] 的原因,但如果有更多通道我应该使用 a[0],a[1 ],...,a[nChannels]

于 2017-09-22T10:36:59.197 回答