Quantcast
Channel: NAudio
Viewing all articles
Browse latest Browse all 5831

New Post: Convert streaming mu-law wav to mp3 in real-time

$
0
0
Thanks Mark.

I did resolve my RIFF exception (aka, I actually thought about what I was trying to do for a few minutes), but the mp3 stream I'm producing is nothing but white noise.

Do you think that's due to the low sample rate of the source stream?
    [HttpGet]
    public ActionResult GetMp3(string url)
    {
        if (String.IsNullOrWhiteSpace(url))
            return null;

        var muLawFormat = WaveFormat.CreateMuLawFormat(8000, 1);
        var compressedStream = new MemoryStream();

        using (var ms = new MemoryStream())
        {
            var request = (HttpWebRequest)WebRequest.Create(url);
            request.KeepAlive = false;
            request.ProtocolVersion = HttpVersion.Version10;

            using (Stream webStream = request.GetResponse().GetResponseStream())
            {
                var buffer = new byte[4096];
                int read;
                while (webStream != null && (read = webStream.Read(buffer, 0, buffer.Length)) > 0)
                    ms.Write(buffer, 0, read);
            }

            ms.Position = 0;

            using (WaveStream wav = WaveFormatConversionStream.CreatePcmStream(new RawSourceWaveStream(ms, muLawFormat)))
            using (var mp3 = new LameMP3FileWriter(compressedStream, new WaveFormat(), LAMEPreset.MEDIUM_FAST))
                wav.CopyTo(mp3);
        }

        compressedStream.Seek(0, 0);
        return new FileStreamResult(compressedStream, "audio/mpeg");
    }

Viewing all articles
Browse latest Browse all 5831

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>