0

The following code will successfully, load, play, edit audio samples and (almost) write to audio file. I say almost because when I comment out the "Play" code it works, but leaving it in causes the buffer read:

audioFile.Read(buffer, 0, numSamples);

to result in zeros.

Do I need to reset the audioFile somehow? All the examples I've found don't mention any need for this.

using System;
using NAudio.Wave;

namespace NAudioTest
{
class TestPlayer
{
    static void Main(string[] args)
    {
        string infileName = "c:\\temp\\pink.wav";
        string outfileName = "c:\\temp\\pink_out.wav";

        // load the file
        var audioFile = new AudioFileReader(infileName);

        // play the file
        var outputDevice = new WaveOutEvent();
        outputDevice.Init(audioFile);
        outputDevice.Play();
        //Since Play only means "start playing" and isn't blocking, we can wait in a loop until playback finishes....
        while (outputDevice.PlaybackState == PlaybackState.Playing) { System.Threading.Thread.Sleep(1000); }

        // edit the samples in file
        int fs = audioFile.WaveFormat.SampleRate;
        int numSamples = (int)audioFile.Length / sizeof(float); // length is the number of bytes - 4 bytes in a float

        float[] buffer = new float[numSamples];
        audioFile.Read(buffer, 0, numSamples);

        float volume = 0.5f;
        for (int n = 0; n < numSamples; n++) { buffer[n] *= volume; }

        // write edited samples to new file
        var writer = new WaveFileWriter(outfileName,audioFile.WaveFormat);
        writer.WriteSamples(buffer,0,numSamples);
    }
}

}

AlexS
  • 510
  • 2
  • 7
  • 23
  • get the audacity source and debug why it wont play - this is the ultimate way to find out – pm100 Mar 21 '18 at 15:17
  • That would be an enormous amount of effort, if I knew my way around audicity’s source code enough to do that I probably wouldn’t be trying to learn how to use NAudio to implement these things. I’m quite certain the the issue is with my code, rather than audacity’s read wav file ability. – AlexS Mar 21 '18 at 15:45
  • just saying that if you dont get any other answer then thats the solution – pm100 Mar 21 '18 at 15:56

1 Answers1

1

You must call Dispose on your writer before it is a valid WAV file. I recommend that you put it in a using block.

using(var writer = new WaveFileWriter(outfileName,audioFile.WaveFormat))
{
    writer.WriteSamples(buffer,0,numSamples);
}
Mark Heath
  • 48,273
  • 29
  • 137
  • 194
  • Hi, thanks but unfortunately I think that is only part of the problem solved. I edited the question a couple of days ago based on developments. It appears that commenting the “play” section determines what is read into the buffer.?? – AlexS Mar 24 '18 at 08:31
  • it's better to ask a new question, rather than editing an existing one, as the answers now don't make sense. The answer to your question is that with Play in there, now two things (the sound card and the wave file writer) are reading from one source. – Mark Heath Mar 24 '18 at 09:39
  • Thanks Mark, I don’t understand the logic behind why this happens. It suggests that while two separate and independent actions rely on the same underlying data; they affect each other, despite being unrelated. How can I separate them? Or would you prefer I start a new post? Kind regards – AlexS Mar 24 '18 at 20:33
  • There is only one AudioFileReader but two different things are reading from it. – Mark Heath Mar 25 '18 at 21:49