1
votes

I am working on an application that applies filters on sound files, the filters are applied in frequency domain so I get the samples from the .wav file using NAudio library with this code :

audio = new AudioFileReader(wav_file);
samples = new float[wave.Length];
audio.Read(samples, 0, samples.Length);

after applying the previous code, now I have the samples as an array of float, then I apply the short-time Fourier transform on the samples getting the frequency domain data, and then the filters are applied on the frequency domain data.
and then the inverse short-time Fourier transform is applied on the frequency domain data to convert it back to time domain which should be similar to the initial samples but with the filters applied.
the steps again:

  1. Get samples (time domain data) array from the wav file.
  2. Apply short-time Fourier transform on the samples to get frequency domain data.
  3. Apply filters on frequency domain data.
  4. Apply inverse short-time Fourier transform on the frequency domain data to get samples (time domain data).
  5. Convert samples into wav form back again to save and play it.

now the problem is in the last step, I have the float array of samples (time domain data), how do I convert it into a .wav file and play it?

2
You should be able to find what you need either here: stackoverflow.com/questions/19715553/double-array-to-wav - or here: stackoverflow.com/questions/9805407/… - it has some extra steps, but I believe by the end, it has all that is needed to generate your wav file.gmiley

2 Answers

3
votes

To save samples as a .wav file, the following code is used:

WaveFormat waveFormat = new WaveFormat(sampleRate, bitDepth, channels);
using (WaveFileWriter writer = new WaveFileWriter("C:\\track1.wav", waveFormat))
{
    writer.WriteSamples(floatOutput, 0, floatOutput.Length);
}

the sampleRate, bitDepth, and channels are extracted from the input file like this:

sampleRate = wave.WaveFormat.SampleRate;
bitDepth = wave.WaveFormat.BitsPerSample;
channels = wave.WaveFormat.Channels;
0
votes

1- create a class that implement ISampleProvider 2- use this code to play (sourse is ISampleProvider)

         var xs = new NAudio.Wave.SampleProviders.SampleToWaveProvider16(source);
                var l = new NAudio.Wave.WaveOut();
                l.Init(xs);
                l.Play();