Ejemplo n.º 1
0
 //We will play the .wav file when we click the play response button
 private void playButton_Click(object sender, EventArgs e)
 {
     //We we haven't started playing the response.
     if (waveReader == null)
     {
         waveReader  = new NAudio.Wave.WaveFileReader(recordFileName);
         outputSound = new NAudio.Wave.DirectSoundOut();
         outputSound.Init(new NAudio.Wave.WaveChannel32(waveReader));
         outputSound.Play();
         playResponseButton.Text        = "Pause Response";
         stopResponseButton.Enabled     = true;
         questionCheckedListBox.Enabled = false;
     }
     else
     {
         if (outputSound.PlaybackState == NAudio.Wave.PlaybackState.Playing)
         {
             outputSound.Pause();
             playResponseButton.Text = "Play Response";
         }
         else if (outputSound.PlaybackState == NAudio.Wave.PlaybackState.Paused)
         {
             outputSound.Play();
             playResponseButton.Text = "Pause Response";
         }
     }
 }
Ejemplo n.º 2
0
 private void DisposeWave()
 {
     if (outputSound != null)
     {
         if (outputSound.PlaybackState == NAudio.Wave.PlaybackState.Playing)
         {
             outputSound.Stop();
         }
         outputSound.Dispose();
         outputSound = null;
     }
     if (waveReader != null)
     {
         waveReader.Dispose();
         waveReader = null;
     }
     if (waveSource != null)
     {
         waveSource.Dispose();
         waveSource = null;
     }
     if (waveFile != null)
     {
         waveFile.Dispose();
         waveFile = null;
     }
 }
Ejemplo n.º 3
0
 public DirectSoundPlayer(INetworkChatCodec c)
     : base(c)
 {
     waveProvider = new BufferedWaveProvider(codec.RecordFormat);
     wavePlayer   = new DirectSoundOut();
     wavePlayer.Init(waveProvider);
 }
Ejemplo n.º 4
0
        private void Listening()
        {
            //Прослушиваем по адресу
            IPEndPoint           localIP = myEpAudio;
            BufferedWaveProvider bufferedWaveProvider = new BufferedWaveProvider(new WaveFormat(8000, 16, 1));
            // WaveOut player = new WaveOut();
            DirectSoundOut player = new DirectSoundOut();

            do
            {
                try
                {
                    byte[] receiveBytes = client2.Receive(ref localIP);
                    //добавляем данные в буфер, откуда output будет воспроизводить звук

                    bufferedWaveProvider.AddSamples(receiveBytes, 0, receiveBytes.Length);

                    player.Init(bufferedWaveProvider);

                    player.Play();
                    //player.Dispose();
                }
                catch (Exception ex)
                {
                    //   MessageBox.Show(ex.Message);
                }
            } while (true);
        }
Ejemplo n.º 5
0
        public bool SetAudioEndpoint(string dev, bool usedefault = false)
        {
            System.Collections.ObjectModel.ReadOnlyCollection <DirectSoundDevice> list = DirectSoundDeviceEnumerator.EnumerateDevices();

            DirectSoundDevice dsd = null;

            if (dev != null)                                               // active selection
            {
                dsd = list.FirstOrDefault(x => x.Description.Equals(dev)); // find

                if (dsd == null && !usedefault)                            // if not found, and don't use the default (used by constructor)
                {
                    return(false);
                }
            }

            DirectSoundOut dso = new DirectSoundOut(100, System.Threading.ThreadPriority.Highest); // seems good quality at 200 ms latency

            if (dso == null)                                                                       // if no DSO, fail..
            {
                return(false);
            }

            if (dsd != null)
            {
                dso.Device = dsd.Guid;
            }
            else
            {
                DirectSoundDevice def = DirectSoundDevice.DefaultDevice;
                dso.Device = def.Guid;  // use default GUID
            }

            NullWaveSource nullw = new NullWaveSource(10);

            try
            {
                dso.Initialize(nullw);  // check it takes it.. may not if no sound devices there..
                dso.Stop();
                nullw.Dispose();
            }
            catch
            {
                nullw.Dispose();
                dso.Dispose();
                return(false);
            }

            if (aout != null)                 // clean up last
            {
                aout.Stopped -= Output_Stopped;
                aout.Stop();
                aout.Dispose();
            }

            aout          = dso;
            aout.Stopped += Output_Stopped;

            return(true);
        }
        public void CanPlayMonoToStereoSourceTest()
        {
            var source = new StereoToMonoSource(GlobalTestConfig.TestMp3().ToStereo().ToSampleSource());

            Assert.AreEqual(1, source.WaveFormat.Channels);

            var monoSource = new MonoToStereoSource(source);

            Assert.AreEqual(2, monoSource.WaveFormat.Channels);

            ISoundOut soundOut;

            if (WasapiOut.IsSupportedOnCurrentPlatform)
            {
                soundOut = new WasapiOut();
            }
            else
            {
                soundOut = new DirectSoundOut();
            }

            soundOut.Initialize(monoSource.ToWaveSource(16));
            soundOut.Play();

            Thread.Sleep((int)Math.Min(source.GetMilliseconds(source.Length), 60000));

            soundOut.Dispose();
        }
Ejemplo n.º 7
0
            public void Dispose()
            {
                if (this.WaveFileReader != null)
                {
                    this.WaveFileReader.Dispose();
                }

                this.WaveFileReader = null;

                if (this.WaveChannel != null)
                {
                    this.WaveChannel.Dispose();
                }

                this.WaveChannel = null;

                if (this.MemoryStream != null)
                {
                    this.MemoryStream.Dispose();
                }

                this.MemoryStream = null;

                if (this.DirectSoundOut != null)
                {
                    this.DirectSoundOut.Dispose();
                }

                this.DirectSoundOut = null;
            }
Ejemplo n.º 8
0
        public void PlayFromBeginning()
        {
            Console.WriteLine("Starting playback...");
            using (var waveFileReader = new WaveFileReader(_waveFileWriter.Filename))
                using (var waveChannel32 = new WaveChannel32(waveFileReader)
                {
                    PadWithZeroes = false
                })
                    using (var directSoundOut = new DirectSoundOut())
                    {
                        var totalBytes = waveFileReader.Length;
                        var totalTime  = waveFileReader.TotalTime;
                        Console.WriteLine($"Playing {totalBytes}bytes ({totalTime})");

                        directSoundOut.Init(waveChannel32);
                        directSoundOut.Play();

                        while (directSoundOut.PlaybackState != PlaybackState.Stopped)
                        {
                            Thread.Sleep(20);
                        }
                        directSoundOut.Stop();
                        Console.WriteLine("Recording ended.");
                    }
        }
Ejemplo n.º 9
0
 private void AudioLoader()
 {
     waveProvider = new BufferedWaveProvider(new WaveFormat(8000, 16, 1));
     waveProvider.DiscardOnBufferOverflow = true;
     waveOut = new DirectSoundOut();
     waveOut.Init(waveProvider);
     waveOut.Volume = VideoConf.vol;
 }
Ejemplo n.º 10
0
        public DeviceManager()
        {
            AudioMixer = new GameBoyAudioMixer();
            var player = new DirectSoundOut();

            player.Init(AudioMixer);
            player.Play();
        }
Ejemplo n.º 11
0
 public void PlayRecording(string path)
 {
     DisposeAll();
     reader = new WaveFileReader(path);
     player = new DirectSoundOut();
     player.Init(new WaveChannel32(reader));
     player.Play();
 }
Ejemplo n.º 12
0
 /// <summary>
 /// インスタンスを破棄する
 /// </summary>
 protected override void Dispose()
 {
     if (this._audioRender is not null)
     {
         this._audioRender?.Dispose();
         this._audioRender = null;
     }
 }
Ejemplo n.º 13
0
 public SynthThread()
 {
     synth          = new Synthesizer(Properties.Settings.Default.SampleRate, 2, Properties.Settings.Default.BufferSize, Properties.Settings.Default.BufferCount, Properties.Settings.Default.poly);
     mseq           = new MidiFileSequencer(synth);
     synth_provider = new SynthWaveProvider(synth, mseq);
     direct_out     = new DirectSoundOut(Properties.Settings.Default.Latency);
     direct_out.Init(synth_provider);
 }
Ejemplo n.º 14
0
 public Audio()
 {
     _output      = new DirectSoundOut();
     _soundbuffer = new BufferedWaveProvider(new WaveFormat(SAMPLE_RATE, 1));
     _soundbuffer.BufferLength            = ((SAMPLE_RATE / 60) * 20); //Aproxx 20 frames of sound in buffer
     _soundbuffer.DiscardOnBufferOverflow = true;
     _output.Init(_soundbuffer);
 }
Ejemplo n.º 15
0
 private void timerFFT_Tick(object sender, EventArgs e)
 {
     if (Output == null)
     {
         Output = new DirectSoundOut();
     }
     app.RunAnalysis();
 }
Ejemplo n.º 16
0
 private void PlayMusic_Load(object sender, EventArgs e)
 {
     lblTenBaiHat.Text            = _mtenbaihat;
     directSoundOutReceive        = new DirectSoundOut();
     directSoundOutReceive.Volume = 1.0f;
     directSoundOutReceive.Init(waveChannel32Receive);
     directSoundOutReceive.Play();
 }
Ejemplo n.º 17
0
        /// <summary>
        /// フォームのクロージング処理
        /// Wave関連オブジェクトのDispose処理を担当
        /// </summary>
        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            output?.Dispose();
            output = null;

            stream?.Dispose();
            stream = null;
        }
Ejemplo n.º 18
0
 void wavePlayer_PlaybackStopped(object sender, StoppedEventArgs e)
 {
     // dispose of wave output
     if (_waveOut != null)
     {
         _waveOut.Dispose();
         _waveOut = null;
     }
 }
Ejemplo n.º 19
0
        static void Main(string[] args)
        {
            IWavePlayer waveOutDevice;
            WaveStream  mainOutputStream;
            string      fileName = @"C:\Users\Yuval\Sources\PracticeSharpProject\Audio\Ringtone 01.wma";

            Console.WriteLine("Initiailizing NAudio");
            try
            {
                waveOutDevice = new DirectSoundOut(50);
            }
            catch (Exception driverCreateException)
            {
                Console.WriteLine(String.Format("{0}", driverCreateException.Message));
                return;
            }

            mainOutputStream = CreateInputStream(fileName);
            try
            {
                waveOutDevice.Init(mainOutputStream);
            }
            catch (Exception initException)
            {
                Console.WriteLine(String.Format("{0}", initException.Message), "Error Initializing Output");
                return;
            }

            Console.WriteLine("NAudio Total Time: " + (mainOutputStream as WaveChannel32).TotalTime);

            Console.WriteLine("Playing WMA..");

            waveOutDevice.Volume = 1.0f;
            waveOutDevice.Play();


            Console.ReadKey();

            Console.WriteLine("Seeking to new time: 00:00:20..");

            (mainOutputStream as WaveChannel32).CurrentTime = new TimeSpan(0, 0, 20);

            Console.ReadKey();

            Console.WriteLine("Hit key to stop..");

            waveOutDevice.Stop();

            Console.WriteLine("Finished..");

            mainOutputStream.Dispose();

            waveOutDevice.Dispose();

            Console.WriteLine("Press key to exit...");
            Console.ReadKey();
        }
Ejemplo n.º 20
0
        static MusicFile()
        {
            soundOut     = new DirectSoundOut();
            codecFactory = CodecFactory.Instance;

            soundOut.Stopped += (sender, args) => {
                FileFinishedPlaying?.Invoke(LastFilePlayed);
            };
        }
Ejemplo n.º 21
0
        private void InitWAV(string filePath)
        {
            //source
            ActiveStream = new WaveFileReader(filePath);

            directSoundOut = new DirectSoundOut();

            directSoundOut.Init(new WaveChannel32(ActiveStream));
        }
Ejemplo n.º 22
0
        public static void PlayWAV(string path)
        {
            WaveFileReader wave = new WaveFileReader(path);

            DirectSoundOut output = new DirectSoundOut();

            output.Init(wave);
            output.Play();
        }
Ejemplo n.º 23
0
        /// <summary>
        /// Intialize the WaveOut Device and set Volume
        /// </summary>
        public void Initialize(Guid deviceGuid)
        {
            directOut         = new DirectSoundOut(deviceGuid);
            CurrentDeviceGuid = deviceGuid;
            directOut.Init(volumeChannel);

            Settings.Set(Setting.OutputDeviceGuid, deviceGuid.ToString());
            Settings.WriteSettings();
        }
Ejemplo n.º 24
0
 public void BeginLoopback()
 {
     if (waveIn != null && waveOut == null)
     {
         waveOut = new DirectSoundOut(DirectSoundOut.DSDEVID_DefaultPlayback, 300);
         waveOut.Init(new WaveInProvider(waveIn));
         waveOut.Play();
     }
 }
Ejemplo n.º 25
0
        /// <inheritdoc />
        public void Open()
        {
            _circularBuffer = new CircularSampleBuffer(BufferSize * BufferCount);

            _context = new DirectSoundOut(100);
            _context.Init(this);

            ((EventEmitter)Ready).Trigger();
        }
Ejemplo n.º 26
0
 public void EndLoopback()
 {
     if (waveOut != null)
     {
         waveOut.Stop();
         waveOut.Dispose();
         waveOut = null;
     }
 }
Ejemplo n.º 27
0
        private void button2_Click(object sender, EventArgs e)
        {
            Stream play = new MemoryStream(File.ReadAllBytes(path2));

            mp3    = new Mp3FileReader(play);
            output = new DirectSoundOut();
            output.Init(mp3);
            output.Play();
        }
Ejemplo n.º 28
0
        private void play_Click(object sender, EventArgs e)
        {
            Stream play = new MemoryStream(audiobyte);

            mp3    = new Mp3FileReader(play);
            output = new DirectSoundOut();
            output.Init(mp3);
            output.Play();
        }
Ejemplo n.º 29
0
        public void Play()
        {
            var reader      = new WaveFileReader(SongToPlay);
            var MediaPlayer = new DirectSoundOut();
            var loop        = new LoopStream(reader);

            MediaPlayer.Init(new WaveChannel32(loop));
            MediaPlayer.Play();
        }
Ejemplo n.º 30
0
        public NoiseWindow()
        {
            InitializeComponent();

            waveTone = new WaveTone(this.sldFreq.Value, this.sldAmpl.Value);
            stream   = new BlockAlignReductionStream(waveTone);

            output = new DirectSoundOut();
            output.Init(stream);
        }