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