Ejemplo n.º 1
0
        /// <summary>
        /// Converts a WMA file to a WAV stream
        /// </summary>
        /// <param name="outputStream">Stream to store the converted wav.</param>
        /// <param name="filePath">Path to a .wma file to convert</param>
        /// <returns>The WaveFormat object of the converted wav</returns>
        private static WaveFormat wmaToWav(string pathToWma, Stream outputStream, int sampleRate, int bitDepth, int numChannels)
        {
            if (!Path.GetExtension(pathToWma).ToLowerInvariant().Contains("wma"))
                throw new ArgumentException("Must be a .wma file!");

            using (var reader = new WMAFileReader(pathToWma))
            {
                var targetFormat = new NAudio.Wave.WaveFormat(sampleRate, bitDepth, numChannels);
                var pcmStream = new WaveFormatConversionStream(targetFormat, reader);
                var buffer = new byte[pcmStream.Length];
                pcmStream.Read(buffer, 0, (int)pcmStream.Length);
                outputStream.Write(buffer, 0, buffer.Length);
                outputStream.Position = 0;

                pcmStream.Close();

                return targetFormat;
            }
        }
        /// <summary>
        /// Creates an input WaveChannel
        /// (Audio file reader for MP3/WAV/OGG/FLAC/WMA/AIFF/Other formats in the future)
        /// </summary>
        /// <param name="filename"></param>
        public static WaveStream CreateInputWaveChannel(string filename)
        {
            WaveStream m_blockAlignedStream = null;
            WaveStream m_waveReader = null;
            WaveChannel32 m_waveChannel = null;

            string fileExt = Path.GetExtension( filename.ToLower() );
            if ( fileExt == MP3Extension ) {
                m_waveReader = new Mp3FileReader(filename);
                m_blockAlignedStream = new BlockAlignReductionStream(m_waveReader);
                // Wave channel - reads from file and returns raw wave blocks
                m_waveChannel = new WaveChannel32(m_blockAlignedStream);
            } else if ( fileExt == WAVExtension ) {
                m_waveReader = new WaveFileReader(filename);
                if (m_waveReader.WaveFormat.Encoding != WaveFormatEncoding.Pcm) {
                    m_waveReader = WaveFormatConversionStream.CreatePcmStream(m_waveReader);
                    m_waveReader = new BlockAlignReductionStream(m_waveReader);
                } if (m_waveReader.WaveFormat.BitsPerSample != 16) {
                    var format = new WaveFormat(m_waveReader.WaveFormat.SampleRate,
                                                16, m_waveReader.WaveFormat.Channels);
                    m_waveReader = new WaveFormatConversionStream(format, m_waveReader);
                }

                m_waveChannel = new WaveChannel32(m_waveReader);
            } else if (fileExt == OGGVExtension) {
                m_waveReader = new OggFileReader(filename);
                if (m_waveReader.WaveFormat.Encoding != WaveFormatEncoding.Pcm) {
                    m_waveReader = WaveFormatConversionStream.CreatePcmStream(m_waveReader);
                    m_waveReader = new BlockAlignReductionStream(m_waveReader);
                } if (m_waveReader.WaveFormat.BitsPerSample != 16) {
                    var format = new WaveFormat(m_waveReader.WaveFormat.SampleRate,
                                                16, m_waveReader.WaveFormat.Channels);
                    m_waveReader = new WaveFormatConversionStream(format, m_waveReader);
                }

                m_waveChannel = new WaveChannel32(m_waveReader);
            } else if (fileExt == FLACExtension) {
                m_waveReader = new FLACFileReader(filename);
                if (m_waveReader.WaveFormat.Encoding != WaveFormatEncoding.Pcm) {
                    m_waveReader = WaveFormatConversionStream.CreatePcmStream(m_waveReader);
                    m_waveReader = new BlockAlignReductionStream(m_waveReader);
                } if (m_waveReader.WaveFormat.BitsPerSample != 16) {
                    var format = new WaveFormat(m_waveReader.WaveFormat.SampleRate,
                                                16, m_waveReader.WaveFormat.Channels);
                    m_waveReader = new WaveFormatConversionStream(format, m_waveReader);
                }

                m_waveChannel = new WaveChannel32(m_waveReader);
            }
            else if (fileExt == WMAExtension) {
                m_waveReader = new WMAFileReader(filename);
                if (m_waveReader.WaveFormat.Encoding != WaveFormatEncoding.Pcm) {
                    m_waveReader = WaveFormatConversionStream.CreatePcmStream(m_waveReader);
                    m_waveReader = new BlockAlignReductionStream(m_waveReader);
                } if (m_waveReader.WaveFormat.BitsPerSample != 16) {
                    var format = new WaveFormat(m_waveReader.WaveFormat.SampleRate,
                                                16, m_waveReader.WaveFormat.Channels);
                    m_waveReader = new WaveFormatConversionStream(format, m_waveReader);
                }

                m_waveChannel = new WaveChannel32(m_waveReader);
            } else if (fileExt == AIFFExtension) {
                m_waveReader = new AiffFileReader(filename);
                m_waveChannel = new WaveChannel32(m_waveReader);
            } else {
                throw new ApplicationException("Cannot create Input WaveChannel - Unknown file type: " + fileExt);
            }
            //return m_waveReader;
            return m_waveChannel;
        }