Пример #1
0
        private void Play(object stream_close)
        {
            object[] strmcls     = (object[])stream_close;
            Stream   stream      = (Stream)strmcls[0];
            bool     closeStream = (bool)strmcls[1];

            NAudio.Wave.WaveFileReader wfr = new NAudio.Wave.WaveFileReader(stream);
            NAudio.Wave.WaveOutEvent   wo  = new NAudio.Wave.WaveOutEvent();
            wo.DeviceNumber = dev;
            if (vol != -1)
            {
                wo.Volume = vol;
            }
            wo.Init(wfr);
            wo.Play();
            while (wo.PlaybackState == NAudio.Wave.PlaybackState.Playing)
            {
                System.Threading.Thread.Sleep(10);
            }
            if (closeStream)
            {
                wfr.Close();
                stream.Close();
                stream = null;
            }
            ;
        }
Пример #2
0
        public static async void PlayWavAsync(byte[] bytes)
        {
            try
            {
                using (MemoryStream stream = new MemoryStream(bytes))
                {
                    using (NAudio.Wave.WaveOutEvent waveOut = new NAudio.Wave.WaveOutEvent())
                    {
                        using (var reader = new NAudio.Wave.WaveFileReader(stream))
                        {
                            waveOut.Init(reader);
                            waveOut.Play();

                            while (waveOut.PlaybackState == NAudio.Wave.PlaybackState.Playing)
                            {
                                await Task.Delay(1);
                            }
                        }
                    }
                }
            }
            catch (Exception e)
            {
                Logger.MainLogger.Log(Logger.LogTypes.Error, e.ToString());
            }
        }
Пример #3
0
        public void PlayStream(Stream stream, bool wait, bool closeStream)
        {
            stream.Position = 0;

            if (wait)
            {
                NAudio.Wave.WaveFileReader wfr = new NAudio.Wave.WaveFileReader(stream);
                NAudio.Wave.WaveOutEvent   wo  = new NAudio.Wave.WaveOutEvent();
                wo.DeviceNumber = dev;
                if (vol != -1)
                {
                    wo.Volume = vol;
                }
                wo.Init(wfr);
                wo.Play();
                while (wo.PlaybackState == NAudio.Wave.PlaybackState.Playing)
                {
                    System.Threading.Thread.Sleep(10);
                }
                if (closeStream)
                {
                    wfr.Close();
                    stream.Close();
                    stream = null;
                }
                ;
            }
            else
            {
                System.Threading.Thread thr = new System.Threading.Thread(Play);
                thr.Start(new object[] { stream, closeStream });
            };
        }
Пример #4
0
        public void PlayFile(string filename, bool wait)
        {
            FileStream stream = new FileStream(filename, FileMode.Open, FileAccess.Read);

            if (wait)
            {
                NAudio.Wave.WaveFileReader wfr = new NAudio.Wave.WaveFileReader(stream);
                NAudio.Wave.WaveOutEvent   wo  = new NAudio.Wave.WaveOutEvent();
                wo.DeviceNumber = dev;
                if (vol != -1)
                {
                    wo.Volume = vol;
                }
                wo.Init(wfr);
                wo.Play();
                while (wo.PlaybackState == NAudio.Wave.PlaybackState.Playing)
                {
                    System.Threading.Thread.Sleep(10);
                }
                wfr.Close();
                stream.Close();
            }
            else
            {
                System.Threading.Thread thr = new System.Threading.Thread(Play);
                thr.Start(new object[] { stream, true });
            };
        }
Пример #5
0
 private void stopped(object sender, EventArgs e)
 {
     NAudio.Wave.AudioFileReader reader = new NAudio.Wave.AudioFileReader(File);
     NAudio.Wave.WaveOutEvent    output = (NAudio.Wave.WaveOutEvent)sender;
     output.Init(reader);
     output.Play();
 }
Пример #6
0
                    public void Acquire(int input_device, Signal_Type ST, int output_device)
                    {
                        Running         = true;
                        Channels_in     = NAudio.Wave.WaveIn.GetCapabilities(input_device).Channels;
                        Response        = new List <short> [Channels_in];
                        block           = 2 * Channels_in;
                        WI              = new NAudio.Wave.WaveInEvent();
                        WI.WaveFormat   = new NAudio.Wave.WaveFormat(SampleFreq, 16, Channels_in);
                        WI.DeviceNumber = input_device;

                        WI.BufferMilliseconds = 100;
                        WI.NumberOfBuffers    = 3;
                        WI.RecordingStopped  += WI_RecordingStopped;
                        WI.DataAvailable     += WI_DataAvailable;
                        WO.DeviceNumber       = output_device;
                        for (int c = 0; c < Channels_in; c++)
                        {
                            Response[c] = new List <short>();
                        }

                        SignalProvider Signal;

                        switch (ST)
                        {
                        case Signal_Type.Pink_Noise:
                            Signal = new Noise_Provider(1, (int)CT_Averages, SampleFreq);
                            break;

                        case Signal_Type.MLS:
                            throw new NotImplementedException();

                        case Signal_Type.Swept_Sine:
                            Signal = new Sweep_Provider((float)Signal_Length, CT_Averages, 63, 20000, SampleFreq);
                            break;

                        case Signal_Type.Logarithmic_Swept_Sine:
                            throw new NotImplementedException();

                        default:
                            System.Windows.Forms.MessageBox.Show("Select a Signal...");
                            return;
                        }

                        TD_Signal = Signal.Signal;

                        WO.NumberOfBuffers = 1;
                        WO.DesiredLatency  = 3000 * CT_Averages;
                        WO.Volume          = 1.0f;
                        WO.Init(Signal);
                        WI.StartRecording();
                        WO.Play();
                        System.Threading.Thread.Sleep((int)(Signal_Time_s * (3 + CT_Averages) * 1000));
                        WO.Stop();
                        WI.StopRecording();
                        System.Threading.Thread.Sleep(100);
                        WI_RecordingStopped(this, null);
                    }
Пример #7
0
 /// <summary>
 /// Playing an .ogg audio stream
 /// </summary>
 /// <param name="audioStream"></param>
 private static void PlayAudio(Stream audioStream)
 {
     using (var vorbisStream = new VorbisWaveReader(audioStream))
         using (var waveOut = new NAudio.Wave.WaveOutEvent())
         {
             waveOut.Init(vorbisStream);
             waveOut.Volume = 0.5f;
             waveOut.Play();
             Thread.Sleep(vorbisStream.TotalTime);
         }
 }
Пример #8
0
 public void Alert()
 {
     using (var audioFile = new NAudio.Wave.AudioFileReader(@"./Bensound-moose.wav"))
         using (var outputDevice = new NAudio.Wave.WaveOutEvent())
         {
             outputDevice.Init(audioFile);
             outputDevice.Play();
             while (outputDevice.PlaybackState == NAudio.Wave.PlaybackState.Playing)
             {
                 System.Threading.Thread.Sleep(1000);
             }
         }
 }
Пример #9
0
 public void Play()
 {
     NAudio.Wave.AudioFileReader reader = new NAudio.Wave.AudioFileReader(File);
     NAudio.Wave.WaveOutEvent    output = new NAudio.Wave.WaveOutEvent
     {
         Volume = Volume
     };
     output.Init(reader);
     if (Looping)
     {
         output.PlaybackStopped += stopped;
     }
     output.Play();
 }
Пример #10
0
 private void Play(Stream stream)
 {
     mStop = false;
     Task.Factory.StartNew(() =>
     {
         using (var vorbisStream = new NAudio.Vorbis.VorbisWaveReader(stream))
         using (var waveOut = new NAudio.Wave.WaveOutEvent())
         {
             try
             {
                 waveOut.Init(vorbisStream);
                 waveOut.Play();
                 SpinWait.SpinUntil(() => vorbisStream.Position >= vorbisStream.Length || mStop);
                 Thread.Sleep(200);
             }
             catch (NAudio.MmException) { }
         }
     }).ContinueWith((t) => { if (OnPlayFinished != null) OnPlayFinished(); });
 }
Пример #11
0
        private void btnOpen_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                OpenFileDialog open = new OpenFileDialog();
                open.Filter = "Sound Files (*.mp3;*.wav)|*.mp3;*.wav;";
                open.ShowDialog();

                DisposeWave();
                //wave = new NAudio.Wave.WaveFileReader(open.FileName);
                if (open.FileName.EndsWith(".mp3"))
                {
                    NAudio.Wave.WaveStream pcm = new NAudio.Wave.Mp3FileReader(open.FileName);
                    stream = new NAudio.Wave.BlockAlignReductionStream(pcm);
                }
                else if (open.FileName.EndsWith(".wav"))
                {
                    NAudio.Wave.WaveStream pcm = new NAudio.Wave.WaveChannel32(new NAudio.Wave.WaveFileReader(open.FileName));
                    stream = new NAudio.Wave.BlockAlignReductionStream(pcm);
                }

                output = new NAudio.Wave.WaveOutEvent();
                output.Init(new NAudio.Wave.WaveChannel32(stream));
                output.Play();
                StopWatch();
                if (stopWatch.IsRunning)
                {
                    stopWatch.Stop();
                }
                else
                {
                    stopWatch.Start();
                }

                btnPausePlay.IsEnabled = true;
            }catch (ArgumentException) {
                System.Windows.MessageBox.Show("Choose a real file");
            }
        }
Пример #12
0
        /// <summary>
        /// Converts .wem files to .ogg and plays them.
        /// </summary>
        /// <param name="file">The file to play.</param>
        public static void PlayAudio(string file)
        {
            file = ConvertToOgg(file);

            try
            {
                Task.Run(() => {
                    using (var vorbisStream = new NAudio.Vorbis.VorbisWaveReader(file))
                        using (var waveOut = new NAudio.Wave.WaveOutEvent())
                        {
                            waveOut.Init(vorbisStream);
                            waveOut.Play(); // is async
                            while (waveOut.PlaybackState != NAudio.Wave.PlaybackState.Stopped)
                            {
                                ;
                            }
                        }
                });
            }
            catch
            {
                GeneralHelper.WriteToConsole($"Problem playing audio file!!\n");
            }
        }
Пример #13
0
        public void DoPlay()
        {
            try
            {
                Console.WriteLine();
                Console.WriteLine("playing track. press space to play/pause; ESC to stop");
                System.Threading.Thread.Sleep(1000);

                NAudio.Wave.WaveOutEvent   wavout    = new NAudio.Wave.WaveOutEvent();
                NAudio.Wave.WaveFileReader wavreader = new NAudio.Wave.WaveFileReader(_beatFile.GetWavFilename());

                wavout.Init(wavreader);
                wavout.Play();

                int index = 0;
                List <BeatInstance> bl = _beatFile.GetAllBeats();

                while (wavout.PlaybackState != NAudio.Wave.PlaybackState.Stopped)
                {
                    if (System.Console.KeyAvailable)
                    {
                        switch (System.Console.ReadKey(true).Key)
                        {
                        case ConsoleKey.Spacebar:
                            if (wavout.PlaybackState == NAudio.Wave.PlaybackState.Paused)
                            {
                                wavout.Play();
                            }
                            else if (wavout.PlaybackState == NAudio.Wave.PlaybackState.Playing)
                            {
                                wavout.Pause();
                            }
                            break;

                        case ConsoleKey.Escape:
                            Console.WriteLine("stopped playback");
                            wavout.Stop();
                            break;

                        case ConsoleKey.OemPeriod:
                            Console.WriteLine("{0}", wavreader.CurrentTime.TotalSeconds);
                            break;
                        }
                    }

                    while (index < bl.Count && bl[index].TriggerTime <= wavreader.CurrentTime.TotalSeconds)
                    {
                        if (!_playQuiet)
                        {
                            ViewOneBeat(bl[index]);
                        }

                        index++;
                    }

                    System.Threading.Thread.Sleep(10);
                }

                wavout.Dispose();
                wavreader.Dispose();
            }
            catch (Exception ex)
            {
                throw new Exception("error playing track", ex);
            }
        }