Ejemplo n.º 1
0
        public void Convert()
        {
            var tempWavFile = GetTemporaryWavFileName();
            var bitRate = new TagLibEditor().GetBitRate(aifFile);

            using (var reader = new AiffFileReader(aifFile))
            {
                WaveFileWriter.CreateWaveFile(tempWavFile, reader);
            }

            var mp3 = new LameConverter(tempWavFile, mp3File, bitRate);
            mp3.Convert();
        }
Ejemplo n.º 2
0
 private static void ConvertAiffToWav(string aiffFile, string wavFile)
 {
     using (var reader = new AiffFileReader(aiffFile))
     {
         using (var writer = new WaveFileWriter(wavFile, reader.WaveFormat))
         {
             byte[] buffer = new byte[4096];
             int bytesRead = 0;
             do
             {
                 bytesRead = reader.Read(buffer, 0, buffer.Length);
                 writer.Write(buffer, 0, bytesRead);
             } while (bytesRead > 0);
         }
     }
 }
Ejemplo n.º 3
0
 public IWaveProvider Load(string fileName)
 {
     WaveStream readerStream = null;
     if (fileName.EndsWith(".wav", StringComparison.OrdinalIgnoreCase)){
         readerStream = new WaveFileReader(fileName);
         if (readerStream.WaveFormat.Encoding != WaveFormatEncoding.Pcm && readerStream.WaveFormat.Encoding != WaveFormatEncoding.IeeeFloat)
         {
             readerStream = WaveFormatConversionStream.CreatePcmStream(readerStream);
             readerStream = new BlockAlignReductionStream(readerStream);
         }
     }else if (fileName.EndsWith(".mp3", StringComparison.OrdinalIgnoreCase)){
         readerStream = new Mp3FileReader(fileName);
     }else if (fileName.EndsWith(".aiff")){
         readerStream = new AiffFileReader(fileName);
     }else{
         readerStream = new MediaFoundationReader(fileName);
     }
     return readerStream;
 }
        public static byte[] ConvertAiffToMp3(byte[] aiffFile, string directory)
        {
            byte[] mp3bytes = null;
            var newFormat = new WaveFormat(bitRate, bitDepth, channels);
            try
            {
                uniqueTempFileCounter += 1;
                var tempFile = Path.Combine(directory, "tempdata"+ uniqueTempFileCounter +".wav");

                using (var ms = new MemoryStream(aiffFile))
                using (var rdr = new AiffFileReader(ms))
                {
                    if (rdr.WaveFormat.BitsPerSample == 24) //can't go from 24 bits aif to mp3 directly, create temporary 16 bit wav
                    {
                            ISampleProvider sampleprovider = new Pcm24BitToSampleProvider(rdr); //24 bit to sample
                            var resampler = new WdlResamplingSampleProvider(sampleprovider, sampleRate); //sample to new sample rate
                            WaveFileWriter.CreateWaveFile16(tempFile, resampler); //sample to actual wave file
                            mp3bytes = ConvertWavToMp3(tempFile, true); //file to mp3 bytes
                    }
                    else
                    {
                        using (var retMs = new MemoryStream())
                        using (var wtr = new LameMP3FileWriter(retMs, rdr.WaveFormat, bitRate))
                        {
                            rdr.CopyTo(wtr);
                            mp3bytes = retMs.ToArray();
                        }
                    }
                }
            }
            catch (Exception e)
            {
                Debug.WriteLine(e);
            }
            return mp3bytes;
        }
Ejemplo n.º 5
0
        private static WaveStream CreateReaderStream(string fileName)
        {
            WaveStream readerStream = null;
            if (fileName.EndsWith(".wav", StringComparison.OrdinalIgnoreCase))
            {
                readerStream = new WaveFileReader(fileName);
                if (readerStream.WaveFormat.Encoding == WaveFormatEncoding.Pcm ||
                    readerStream.WaveFormat.Encoding == WaveFormatEncoding.IeeeFloat) return readerStream;
                readerStream = WaveFormatConversionStream.CreatePcmStream(readerStream);
                readerStream = new BlockAlignReductionStream(readerStream);
            }
            else if (fileName.EndsWith(".mp3", StringComparison.OrdinalIgnoreCase))
                readerStream = new Mp3FileReader(fileName);

            else if (fileName.EndsWith(".aiff"))
                readerStream = new AiffFileReader(fileName);

            return readerStream;
        }
Ejemplo n.º 6
0
 private static AiffFileReader.AiffChunk ReadChunkHeader(BinaryReader br)
 {
     AiffFileReader.AiffChunk result = new AiffFileReader.AiffChunk((uint)br.BaseStream.Position, AiffFileReader.ReadChunkName(br), AiffFileReader.ConvertInt(br.ReadBytes(4)));
     return(result);
 }
Ejemplo n.º 7
0
        public static void ReadAiffHeader(Stream stream, out WaveFormat format, out long dataChunkPosition, out int dataChunkLength, List <AiffFileReader.AiffChunk> chunks)
        {
            dataChunkPosition = -1L;
            format            = null;
            BinaryReader binaryReader = new BinaryReader(stream);

            if (AiffFileReader.ReadChunkName(binaryReader) != "FORM")
            {
                throw new FormatException("Not an AIFF file - no FORM header.");
            }
            AiffFileReader.ConvertInt(binaryReader.ReadBytes(4));
            string a = AiffFileReader.ReadChunkName(binaryReader);

            if (a != "AIFC" && a != "AIFF")
            {
                throw new FormatException("Not an AIFF file - no AIFF/AIFC header.");
            }
            dataChunkLength = 0;
            while (binaryReader.BaseStream.Position < binaryReader.BaseStream.Length)
            {
                AiffFileReader.AiffChunk item = AiffFileReader.ReadChunkHeader(binaryReader);
                if (item.ChunkName == "COMM")
                {
                    short channels = AiffFileReader.ConvertShort(binaryReader.ReadBytes(2));
                    AiffFileReader.ConvertInt(binaryReader.ReadBytes(4));
                    short  bits = AiffFileReader.ConvertShort(binaryReader.ReadBytes(2));
                    double num  = IEEE.ConvertFromIeeeExtended(binaryReader.ReadBytes(10));
                    format = new WaveFormat((int)num, (int)bits, (int)channels);
                    if (item.ChunkLength > 18u && a == "AIFC")
                    {
                        string a2 = new string(binaryReader.ReadChars(4)).ToLower();
                        if (a2 != "none")
                        {
                            throw new FormatException("Compressed AIFC is not supported.");
                        }
                        binaryReader.ReadBytes((int)(item.ChunkLength - 22u));
                    }
                    else
                    {
                        binaryReader.ReadBytes((int)(item.ChunkLength - 18u));
                    }
                }
                else if (item.ChunkName == "SSND")
                {
                    uint num2 = AiffFileReader.ConvertInt(binaryReader.ReadBytes(4));
                    AiffFileReader.ConvertInt(binaryReader.ReadBytes(4));
                    dataChunkPosition = (long)((ulong)(item.ChunkStart + 16u + num2));
                    dataChunkLength   = (int)(item.ChunkLength - 8u);
                    binaryReader.ReadBytes((int)(item.ChunkLength - 8u));
                }
                else
                {
                    if (chunks != null)
                    {
                        chunks.Add(item);
                    }
                    binaryReader.ReadBytes((int)item.ChunkLength);
                }
                if (item.ChunkName == "\0\0\0\0")
                {
                    break;
                }
            }
            if (format == null)
            {
                throw new FormatException("Invalid AIFF file - No COMM chunk found.");
            }
            if (dataChunkPosition == -1L)
            {
                throw new FormatException("Invalid AIFF file - No SSND chunk found.");
            }
        }
Ejemplo n.º 8
0
 public AiffFileReader(Stream inputStream)
 {
     this.waveStream = inputStream;
     AiffFileReader.ReadAiffHeader(this.waveStream, out this.waveFormat, out this.dataPosition, out this.dataChunkLength, this.chunks);
     this.Position = 0L;
 }
        /// <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;
        }