Ejemplo n.º 1
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;
        }
Ejemplo n.º 2
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;
        }