Example #1
0
        protected override void BeforePlay()
        {
            if (m_Playing)
            {
                Stop();
            }

            try
            {
                if (m_Stream != null)
                {
                    m_Stream.Close();
                    m_Stream = null;
                }

                m_Stream  = new MP3Stream(Path, NUMBER_OF_PCM_BYTES_TO_READ_PER_CHUNK);
                Frequency = m_Stream.Frequency;

                m_Playing = true;
            }
            catch
            {
                // file in use or access denied.
                m_Playing = false;
            }
        }
 public override void Close()
 {
     if (_stream != null)
     {
         _stream.Close();
         _stream = null;
     }
 }
Example #3
0
 protected override void AfterStop()
 {
     if (m_Playing)
     {
         m_Playing = false;
         m_Stream.Close();
         m_Stream = null;
     }
 }
Example #4
0
 protected override void AfterStop()
 {
     if (_playing)
     {
         _playing = false;
         _stream.Close();
         _stream = null;
     }
 }
Example #5
0
 public void Dispose()
 {
     if (_Playing)
     {
         Stop();
     }
     _Instance.Dispose();
     _Instance = null;
     _Stream.Close();
     _Stream = null;
 }
Example #6
0
        public void Dispose()
        {
            if (m_Playing)
            {
                Stop();
            }

            m_Instance.Dispose();
            m_Instance = null;

            m_Stream.Close();
            m_Stream = null;
        }
Example #7
0
        /// <summary>
        /// Disposes of the resources used by this FAR3Archive instance.
        /// <param name="Disposed">Was this resource disposed explicitly?</param>
        /// </summary>
        protected virtual void Dispose(bool Disposed)
        {
            if (Disposed)
            {
                if (m_Stream != null)
                {
                    m_Stream.Close();
                }
                if (RFullyStream != null)
                {
                    RFullyStream.Close();
                }

                // Prevent the finalizer from calling ~MP3File, since the object is already disposed at this point.
                GC.SuppressFinalize(this);
            }
            else
            {
                m_Logger.Error("MP3File not explicitly disposed!");
            }
        }
Example #8
0
        private static double[] LoadMP3AsArray(String fileName = "Music\\Sample.mp3")
        {
            MP3Stream mp3 = new MP3Stream(fileName: fileName);

            byte[]    buffer         = new byte[4096];
            int       bytesReturned  = 1;
            int       totalBytesRead = 0;
            ArrayList wav            = new ArrayList();
            int       Sum;

            #region :obsoleted full-sized extraction
            //byte[] wav = new byte[0];
            //byte[] wav_new;
            #endregion
            while (bytesReturned > 0)
            {
                Sum             = 0x00;
                bytesReturned   = mp3.Read(buffer, 0, buffer.Length);
                totalBytesRead += bytesReturned;
                foreach (byte b in buffer)
                {
                    Sum += (int)b;
                }
                wav.Add(Sum / buffer.Length);
                #region :obsoleted full-sized extraction
                //wav_new = new byte[wav.Length + buffer.Length];
                //wav.CopyTo(wav_new, 0);
                //buffer.CopyTo(wav_new, wav.Length);
                //wav = wav_new;
                #endregion
            }
            mp3.Close();
            int[]    wav_array_int = (int[])wav.ToArray(typeof(int));
            double[] wav_array     = new double[wav_array_int.Length];
            for (int i = 0; i < wav_array_int.Length; i++)
            {
                wav_array[i] = (double)wav_array_int[i];
            }
            return(wav_array);
        }
Example #9
0
        static void ExampleReadEntireMP3File()
        {
            MP3Stream stream = new MP3Stream("@sample.mp3");

            // Create the buffer
            const int NUMBER_OF_PCM_BYTES_TO_READ_PER_CHUNK = 4096;

            byte[] buffer = new byte[NUMBER_OF_PCM_BYTES_TO_READ_PER_CHUNK];

            int bytesReturned = -1;
            int totalBytes    = 0;

            while (bytesReturned != 0)
            {
                bytesReturned = stream.Read(buffer, 0, buffer.Length);
                totalBytes   += bytesReturned;
            }
            Console.WriteLine("Read a total of " + totalBytes + " bytes.");

            stream.Close();
            stream = null;
        }
Example #10
0
        /// <summary>
        /// Play a sound, supported only WAV and MP3
        /// </summary>
        /// <param name="fileName"></param>
        public void Play(string fileName)
        {
            IsPlaying = true;
            new Thread(() =>
            {
                SoundConnectionSettings settings = new SoundConnectionSettings();
                try
                {
                    using (SoundDevice device = SoundDevice.Create(settings))
                    {
                        // Check the file type
                        if (fileName.Substring(fileName.Length - 3).ToLower() == "mp3")
                        {
                            // open the mp3 file.
                            MP3Stream stream = new MP3Stream(fileName);

                            // Create the buffer.
                            byte[] buffer = new byte[4096];
                            // read the entire mp3 file.
                            int bytesReturned  = 1;
                            int totalBytesRead = 0;
                            Stream fsWav       = new MemoryStream();

                            while (bytesReturned > 0)
                            {
                                bytesReturned = stream.Read(buffer, 0, buffer.Length);
                                fsWav.Write(buffer, 0, bytesReturned);
                                totalBytesRead += bytesReturned;
                            }
                            // close the stream after we're done with it.
                            stream.Close();
                            stream.Dispose();
                            fsWav.Position = 0;

                            WavHeaderChunk chunk = new WavHeaderChunk
                            {
                                ChunkId   = new[] { 'R', 'I', 'F', 'F' },
                                ChunkSize = (uint)totalBytesRead + 36
                            };
                            WavHeaderChunk subChunk1 = new WavHeaderChunk
                            {
                                ChunkId   = new[] { 'f', 'm', 't', ' ' },
                                ChunkSize = 16
                            };
                            WavHeaderChunk subChunk2 = new WavHeaderChunk
                            {
                                ChunkId   = new[] { 'd', 'a', 't', 'a' },
                                ChunkSize = (uint)totalBytesRead
                            };

                            WavHeader header = new WavHeader
                            {
                                Chunk         = chunk,
                                Format        = new[] { 'W', 'A', 'V', 'E' },
                                SubChunk1     = subChunk1,
                                AudioFormat   = 1,
                                NumChannels   = (ushort)(stream.Format == SoundFormat.Pcm16BitMono ? 1 : 2),
                                SampleRate    = (uint)stream.Frequency,
                                ByteRate      = (uint)(16 * stream.Frequency * (stream.Format == SoundFormat.Pcm16BitMono ? 1 : 2) / 8),
                                BlockAlign    = (ushort)((stream.Format == SoundFormat.Pcm16BitMono ? 1 : 2) * 2),
                                BitsPerSample = 16,
                                SubChunk2     = subChunk2
                            };

                            device.WriteWavHeader(fsWav, header);
                            fsWav.Position = 0;
                            device.Play(fsWav);
                            fsWav.Close();
                            fsWav.Dispose();
                        }
                        else
                        {
                            device.Play(fileName);
                        }
                        IsPlaying = false;
                    }
                }
                catch
                {
                    IsPlaying = false;
                }
            }).Start();
        }
Example #11
0
 public void Close()
 {
     Debug.Assert(isOpen);
     audioStream.Close();
     audioStream = null;
 }