Beispiel #1
0
        public static Stream ConvertAiffToMp3(Stream aiffStream, string directory)
        {
            _uniqueTempFileCounter += 1;
            var tempFile = Path.GetTempFileName();

            using (var rdr = new AiffFileReader(aiffStream))
            {
                //can't go from 24 bits aif to mp3 directly, create temporary 16 bit wav
                if (rdr.WaveFormat.BitsPerSample == 24)
                {
                    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
                    return(ConvertWavFileToMp3MemoryStream(tempFile, true));                                      //file to mp3 bytes
                }
                else
                {
                    var retMs = FilesystemUtils.recyclableMemoryStreamManager.GetStream();
                    using (var wtr = new LameMP3FileWriter(retMs, rdr.WaveFormat, BitRate))
                    {
                        rdr.CopyTo(wtr);
                        return(retMs);
                    }
                }
            }
        }
Beispiel #2
0
        public static bool ConvertAiffToMp3(byte[] aiffFile, string directory, out byte[] mp3Bytes)
        {
            mp3Bytes = null;
            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();
                                }
                        }
                    }
                return(true);
            }
            catch (Exception)
            {
                // ignored
            }
            return(false);
        }