0

I'm hearing a brief click at the end of each wave file. My program is an OpenGL game engine running from within a console window and the answer to this unrelated question recommends using function callbacks when using NAudio inside a console app so perhaps the problem is routed there I don't know. Any ideas?

My usage is:

Audio.play("Music.wav");

Audio class:

public class Audio
{
    private NAudio.Wave.BlockAlignReductionStream stream = null;
    private NAudio.Wave.DirectSoundOut output = null;

    public static void Play(string file)
    {
        Audio audio = new Audio();
        audio.DisposeWave();

        string fullPath = @"Audio/" + file;

        if (fullPath.EndsWith(".mp3"))
        {
            NAudio.Wave.WaveStream pcm = NAudio.Wave.WaveFormatConversionStream.CreatePcmStream(new NAudio.Wave.Mp3FileReader(fullPath));
            audio.stream = new NAudio.Wave.BlockAlignReductionStream(pcm);
        }
        else if (fullPath.EndsWith(".wav"))
        {
            NAudio.Wave.WaveStream pcm = new NAudio.Wave.WaveChannel32(new NAudio.Wave.WaveFileReader(fullPath));
            audio.stream = new NAudio.Wave.BlockAlignReductionStream(pcm);
        }
        else throw new InvalidOperationException("Not a correct audio file type.");

        audio.output = new NAudio.Wave.DirectSoundOut();
        audio.output.Init(audio.stream);
        audio.output.Play();
    }

    private void DisposeWave()
    {
        if (output != null)
        {
            if (output.PlaybackState == NAudio.Wave.PlaybackState.Playing) output.Stop();
            output.Dispose();
            output = null;
        }
        if (stream != null)
        {
            stream.Dispose();
            stream = null;
        }
    }
}

}

livin_amuk
  • 1,285
  • 12
  • 26
  • 1
    Sounds like you're stopping playback at a point where the waveform is not zero. This introduces audible clicks when the waveform is truncated, caused by a vertical edge in the played waveform (i.e. a click). When you say "the end", does this actually mean "when stopping playback mid way"? Are you sure that your audio files converge to zero at the end of playback? If you play a silent audio file, still clicking? – spender Jun 23 '17 at 15:43
  • The code above is the entirety of my audio "engine". I simply call `Audio.play("file.wav")` and it plays the file and clicks at the end. As a dirty fix, I pasted a short amount of silence to the end of the files, faded them out just to be sure, re-exported them, and problem solved. It's somewhat of a hassle though and I'm sure will come back to haunt me. – livin_amuk Jun 23 '17 at 16:01
  • Is it the same with other output methods (i.e. not direct sound)? – spender Jun 23 '17 at 16:58
  • What do you suggest I try? – livin_amuk Jun 23 '17 at 17:27
  • Hah! That old question. It depends on who your target audience is. MMAudio is oldschool and should work on any machine right back to XP and beyond... the other choices are dependent on Vista etc. I don't remember the specifics. All I'm really trying to do is eliminate your choice of output. When I used nAudio in anger about 5 years ago, none of the choices were perfect, and it ended up being better to constantly feed (possibly silent) data to the soundcard and to mix the media onto this silent output stream. Stopping and starting was pretty toxic, so I avoided it almost completely. – spender Jun 23 '17 at 17:37
  • Well I guess my target audience is regular people owning regular computers expecting not to hear clicking artifacts. What's MMAudio? I couldn't find anything on it. – livin_amuk Jun 24 '17 at 07:27
  • I suppose that would be `NAudio.Wave.WaveOut` as opposed to `NAudio.Wave.DirectSoundOut`. You might also try `NAudio.Wave.WasapiOut`, which uses a newer Windows audio API. – spender Jun 25 '17 at 16:57
  • Sorry, it's actually [Windows MME](https://en.wikipedia.org/wiki/Windows_legacy_audio_components#Multimedia_Extensions_.28MME.29) audio (as in MultiMedia Extensions). – spender Jun 25 '17 at 17:03
  • Wow. Switching to `WaveOut` or`WasapiOut` actually brings the clicking problem back. Which is strange because after editing the end of the sound files and using `DirectSoundOut`, it was gone completely. I'll look into Windows MME. – livin_amuk Jun 26 '17 at 02:24
  • `NAudio.Wave.WaveOut` **is** the Windows MME "adapter" for naudio. If you want this problem to go away for sure, I'd consider never stopping the audio, as described above. – spender Jun 26 '17 at 10:41

0 Answers0