Exemplo n.º 1
0
        public void Stop()
        {
            if (waveOut != null)
            {
                if (audioFileReader != null)
                {
                    try
                    {
                        audioFileReader.Dispose();
                    }
                    catch (Exception)
                    {
                        Console.WriteLine("AudioFileReader already disposed.");
                    }
                    audioFileReader = null;
                }

                if (memoryFile != null)
                {
                    memoryFile.Dispose();
                    memoryFile = null;
                }

                CloseWaveOut();
            }
        }
Exemplo n.º 2
0
        //public void DecodeMemoryAhead(string filePath)
        //{
        //    if ((memoryPlay && !scheduleMemoryPlaySettingsChange) || (!memoryPlay && scheduleMemoryPlaySettingsChange))
        //    {
        //        try
        //        {
        //            nextMemoryFile = new MemoryReaderNew(filePath);
        //            nextMemoryFile.MemoryFileNewFinishedEvent += HandleFileReaderFinishedEvent;
        //        }
        //        catch (Exception e)
        //        {
        //            nextMemoryFile = null;
        //            GC.Collect();
        //            Console.WriteLine("next File too large to play in memory: " + e.Message);
        //        }
        //    }
        //}
        private void PlayFileFromMemory(string filePath, AudioOutput output)
        {
            memoryFileTooBig = false;
            // first check to see if we are playing the same file or are paused.
            if (waveOut != null)
            {
                if (waveOut.PlaybackState == PlaybackState.Playing && filePath.Equals(currentFile))
                {
                    //isDriverStopped = false;
                    return;
                }
                else if (waveOut.PlaybackState == PlaybackState.Paused)
                {
                    waveOut.Play();
                    //playbackTimer.Enabled = true;
                    return;
                }
            }

            if (IsFlacFile(filePath))
            {
                string tempWavName = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\wamp\\temp.wav";
                string aheadFileName = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\wamp\\" + filePath.Split('\\').Last().ToLower().Replace(".flac", ".wav");
                if (File.Exists(aheadFileName))
                {

                    if (File.Exists(tempWavName))
                    {
                        mixingSampleProvider.RemoveAllMixerInputs();
                        File.Delete(tempWavName);
                    }

                    File.Move(aheadFileName, tempWavName);
                    filePath = tempWavName;
                }
                else
                {
                    FlacDecoder.DecodeFlacToWav(filePath, tempWavName);
                    filePath = tempWavName;
                }
            }

            if (memoryFile != null)
            {
                memoryFile.Dispose();
                memoryFile = null;
            }

            GC.Collect();

            try
            {
                memoryFile = new MemoryReaderNew(filePath);
                memoryFile.MemoryFileNewFinishedEvent += HandleFileReaderFinishedEvent;
            }
            catch (Exception e)
            {
                memoryFileTooBig = true;
                memoryFile = null;
                GC.Collect();
                Console.WriteLine("File to large to play in memory: " + e.Message);
                Play(filePath, output);
            }

            try
            {
                bitDepth = memoryFile.WaveFormat.BitsPerSample;
                sampleRate = memoryFile.WaveFormat.SampleRate;
            }
            catch (Exception createException)
            {
                Console.WriteLine(String.Format("{0}", createException.Message), "Error Loading File");
                return;
            }

            //MemoryFileSampleProvider memorySampleProvider = new MemoryFileSampleProvider(memoryFile);
            if (isVolumeEnabled)
            {
                //volumeSampleProvider = new VolumeSampleProvider(memorySampleProvider);
                volumeSampleProvider = new VolumeSampleProvider(memoryFile);
                volumeSampleProvider.Volume = currentVolume;
            }

            // if we already have an existing output of the same bit depth and sample rate, use it
            if (waveOut != null && bitDepth == previousBitDepth && sampleRate == previousSampleRate)
            {
                if (isVolumeEnabled)
                    mixingSampleProvider.AddMixerInput(volumeSampleProvider);
                else
                    mixingSampleProvider.AddMixerInput((ISampleProvider)memoryFile);
            }
            else //create a new output
            {
                mixingSampleProvider = null;
                mixingSampleProvider = new MixingSampleProvider(memoryFile.WaveFormat);
                mixingSampleProvider.ReadFully = true;
                //mixingSampleProvider.ReadFully = false;

                if (isVolumeEnabled)
                    mixingSampleProvider.AddMixerInput(volumeSampleProvider);
                else
                    //mixingSampleProvider.AddMixerInput(memoryFile.MemorySampleProvider);
                    mixingSampleProvider.AddMixerInput((ISampleProvider)memoryFile);

                try
                {
                    CreateWaveOut(output);
                    waveOut.Init(mixingSampleProvider);
                }
                catch (Exception initException)
                {
                    Console.WriteLine(String.Format("{0}", initException.Message), "Error Initializing Output");
                    return;
                }
                waveOut.Play();
            }

            currentFile = filePath;
            previousBitDepth = bitDepth;
            previousSampleRate = sampleRate;
        }
Exemplo n.º 3
0
        public void Play(string filePath, AudioOutput output)
        {
            if (scheduleMemoryPlaySettingsChange)
            {
                if (memoryPlay == true)
                {
                    memoryPlay = false;
                    if (memoryFile != null)
                    {
                        memoryFile.Dispose();
                        memoryFile = null;
                    }
                    GC.Collect();
                }
                else
                {
                    memoryPlay = true;
                }

                scheduleMemoryPlaySettingsChange = false;
            }

            if (memoryPlay)
            {
                if (!memoryFileTooBig)
                {
                    PlayFileFromMemory(filePath, output);
                    return;
                }
                else
                {
                    memoryFileTooBig = false;
                }
            }

            // first check to see if we are playing the same file or are paused.
            if (waveOut != null)
            {
                if (waveOut.PlaybackState == PlaybackState.Playing && filePath.Equals(currentFile))
                {
                    //isDriverStopped = false;
                    return;
                }
                else if (waveOut.PlaybackState == PlaybackState.Paused)
                {
                    waveOut.Play();
                    //playbackTimer.Enabled = true;
                    return;
                }
            }

            if (IsFlacFile(filePath))
            {
                string tempWavName   = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\wamp\\temp.wav";
                string aheadFileName = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\wamp\\" + filePath.Split('\\').Last().ToLower().Replace(".flac", ".wav");
                if (File.Exists(aheadFileName))
                {
                    if (File.Exists(tempWavName))
                    {
                        mixingSampleProvider.RemoveAllMixerInputs();
                        File.Delete(tempWavName);
                    }

                    File.Move(aheadFileName, tempWavName);
                    filePath = tempWavName;
                }
                else
                {
                    FlacDecoder.DecodeFlacToWav(filePath, tempWavName);
                    filePath = tempWavName;
                }
            }

            if (memoryFile != null)
            {
                memoryFile.Dispose();
                memoryFile = null;
            }

            audioFileReader = new AudioFileReader(filePath);

            try
            {
                bitDepth   = audioFileReader.WaveFormat.BitsPerSample;
                sampleRate = audioFileReader.WaveFormat.SampleRate;
            }
            catch (Exception createException)
            {
                Console.WriteLine(String.Format("{0}", createException.Message), "Error Loading File");
                return;
            }

            AutoDisposeFileReader autoDisposeFileReader = new AutoDisposeFileReader(audioFileReader);

            autoDisposeFileReader.FileReaderFinishedEvent += HandleFileReaderFinishedEvent;

            if (isVolumeEnabled)
            {
                volumeSampleProvider        = new VolumeSampleProvider(autoDisposeFileReader);
                volumeSampleProvider.Volume = currentVolume;
            }

            // if we already have an existing output of the same bit depth and sample rate, use it
            if (waveOut != null && bitDepth == previousBitDepth && sampleRate == previousSampleRate)
            {
                if (isVolumeEnabled)
                {
                    mixingSampleProvider.AddMixerInput(volumeSampleProvider);
                }
                else
                {
                    mixingSampleProvider.AddMixerInput(autoDisposeFileReader);
                }
            }
            else //create a new output
            {
                try
                {
                    CreateWaveOut(output);
                }
                catch (InvalidOutputDeviceException e)
                {
                    audioFileReader.Dispose();
                    audioFileReader = null;
                    throw e;
                }

                mixingSampleProvider           = null;
                mixingSampleProvider           = new MixingSampleProvider(audioFileReader.WaveFormat);
                mixingSampleProvider.ReadFully = true;

                if (isVolumeEnabled)
                {
                    mixingSampleProvider.AddMixerInput(volumeSampleProvider);
                }
                else
                {
                    mixingSampleProvider.AddMixerInput(autoDisposeFileReader);
                }

                try
                {
                    waveOut.Init(mixingSampleProvider);
                }
                catch (Exception initException)
                {
                    Console.WriteLine(String.Format("{0}", initException.Message), "Error Initializing Output");
                    return;
                }
                waveOut.Play();
            }

            currentFile        = filePath;
            previousBitDepth   = bitDepth;
            previousSampleRate = sampleRate;
            //playbackTimer.Enabled = true;
        }
Exemplo n.º 4
0
        public void Stop()
        {
            if (waveOut != null)
            {
                if (audioFileReader != null)
                {
                    try
                    {
                        audioFileReader.Dispose();
                    }
                    catch (Exception)
                    {
                        Console.WriteLine("AudioFileReader already disposed.");
                    }
                    audioFileReader = null;
                }

                if (memoryFile != null)
                {
                    memoryFile.Dispose();
                    memoryFile = null;
                }

                CloseWaveOut();
            }
        }