This project has moved. For the latest updates, please go here.

two problems with wave playack

Mar 19, 2014 at 12:19 PM
Edited Mar 19, 2014 at 12:24 PM
Hi,

I'm writing WPF application with the newest NAudio. I have two problems with playing wave file.
First problem is wit not executing PlaybackStopped event. This is my code:
        private void InitializeAudio()
        {
            System.OperatingSystem osInfo = System.Environment.OSVersion;

            if (osInfo.Version.Major >= 6)
            {
                waveOut = new WasapiOut(AudioClientShareMode.Exclusive, false, 300);
            }
            else
            {
                waveOut = new DirectSoundOut(300);
            }
        }


        private void CloseWaveOut()
        {
            if (waveOut != null)
            {
                if (waveOut.PlaybackState == PlaybackState.Playing)
                {
                    waveOut.Stop();
                }
                waveOut.Dispose();
                waveOut = null;
            }

            if (waveStream != null)
            {
                waveStream.Close();
                waveStream.Dispose();
                waveStream = null;
            }

            if (waveReader != null)
            {
                waveReader.Close();
                waveReader.Dispose();
                waveReader = null;
            }
        }


        private void OnWaveOutPlaybackStopped(object sender, StoppedEventArgs stoppedEventArgs)
        {
            log.Debug("Odtwarzanie pliku wav zakończone");

            if (stoppedEventArgs.Exception != null)
            {
                log.Error(stoppedEventArgs.Exception.Message);
            }
            CloseWaveOut();
            //InitializeAudio();
        }


        private void PlayVoiceMessage()
        {
            if (File.Exists(m_waveFile))
            {
                try
                {
                    CloseWaveOut();
                    InitializeAudio();

                    waveReader = new WaveFileReader(m_waveFile);
                    waveStream = new WaveChannel32(waveReader);
#if DEBUG
                    string strFormat = waveStream.WaveFormat.ToString();
                    string strSampleRate = waveStream.WaveFormat.SampleRate.ToString();
                    string strChannels = waveStream.WaveFormat.Channels.ToString();
                    string sMsg = String.Format("Format: {0}, samplerate: {1}, channels: {2}.", strFormat, strSampleRate, strChannels);
                    log.Debug(sMsg);
#endif                                  
                    waveOut.Init(waveStream);
                    waveOut.PlaybackStopped += new EventHandler<NAudio.Wave.StoppedEventArgs>(OnWaveOutPlaybackStopped);
                    log.Info("Rozpoczynam odtwarzanie pliku wav");
                    waveOut.Play();

                    //CloseWaveOut();
                }
                catch (Exception e)
                {
                    string sMsg = String.Format("Błąd odtwarzania! w {0}. Przyczyna: {1}.\nŚcieżka {2}", e.Source, e.Message, e.StackTrace);
                    log.Error(sMsg);
                    AddMessageToLog(sMsg);
                }
            }
            else
            {
                log.Error("Plik Wav nie istnieje");
            }
        }
If I uncomment CloseWaveOut(); just after waveOut.Play(); then sound is immediately cut. If I don't uncomment it playing finishes but event doesn't occur. Can anybody suggest something?

Second problem is knocking in speekers at the beginning and at the end of playing. Is it possible to avoid this knocking?

tegards
Tomek
Mar 25, 2014 at 9:18 AM
I solved first problem by adding
waveStream.PadWithZeroes = false;
in the PlayVoiceMessage() method. Now the event OnWaveOutPlaybackStopped is fired correctly.

Second problem remains. Any suggestions appreciated.

regards
Tomek
Coordinator
Mar 25, 2014 at 9:30 AM
I'm not sure what you mean by knocking? Does the Wave file you are playing start and end with silence?
Mar 25, 2014 at 9:39 AM
Edited Mar 25, 2014 at 9:45 AM
Wave file played in WinAmp starts and ends with silence.

I hear some kind of sound in the speakers at the beginning of the playback and at the end. It is the same sound I hear when I turn on my speakers. It is not sound of the switch.
Coordinator
Apr 4, 2014 at 12:58 PM
It may just be a discontinuity, which will sound as a click. You'd need to apply a small fade to eliminate it. Were the audio files created by trimming existing files?
Dec 11, 2016 at 6:27 PM
Edited Dec 11, 2016 at 6:31 PM
I record speech synthesis to wave file.
                            reader = new SpeechSynthesizer();
                            reader.SelectVoice(AppSettings.VoiceName);
                            reader.SpeakCompleted += new EventHandler<SpeakCompletedEventArgs>(OnReaderSpeakCompleted);
                            reader.SpeakStarted += new EventHandler<SpeakStartedEventArgs>(OnReaderSpeekStarted);
                            reader.StateChanged += new EventHandler<StateChangedEventArgs>(OnReaderStateChanged);

                            reader.SetOutputToWaveFile(m_waveFile,
                                new System.Speech.AudioFormat.SpeechAudioFormatInfo(44100, AudioBitsPerSample.Eight, AudioChannel.Mono)
                            );

                            reader.Speak(strMessage);

                            reader.SpeakAsync(WeatherMessage);

                            reader.Dispose();
                            reader = null;
How can I add fade before and after playback?
Dec 18, 2016 at 9:01 AM
I changed file playback to memory stream playback and now knock at the beginning and at the end of playback do not occur.