Set specific volume and pitch to a .wav file

844 views Asked by At

In my application I need to record some wav files and set them to a specific volume and pitch. Right now I can record and play the .wav files using winmm.dll but I have no idea how to modify them.

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();
    }

I've seen, by searching the internet, that using the NAudio.dll it is possible to modify the .wav files, but I'm not fully understanding how it works.

1

There are 1 answers

0
Dani On

By using Naudio library is possible to get all the samples of a .wav file and modify them as you want.

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();

It's important to keep in mind that each sample is a float an its value goes from -1.0f to 1.0f. Also it's important to know in how many channels the audio has been recorded, in my case it was only one channel, that's why I used a[0], but if there where more channels i should use a[0],a[1],...,a[nChannels]