Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert wav streamed over HTTP to mp3, in real-time

Background: I am consuming a service which returns data with a MIME type of audio/wav. I need to provide a playback mechanism for this audio (currently built as an MVC application). As an example, my endpoint looks something like https://audio.fooservice.com/GetAudio?audioId=123

The audio is 8kHz, 1-channel u-law.

Due to varying format support across browsers when using the HTML5 <audio> tag, I am unable to use the original u-law wav because Internet Explorer will not play it.

My proposed solution is to do a real-time conversion from the source format to mp3.

I've cobbled together a partially working solution from various other questions here and in the NAudio forums, but it throws an exception as noted in the comments below:

private void NAudioTest(string url)
{
    Stream outStream = new MemoryStream();
    var format = WaveFormat.CreateMuLawFormat(8000, 1);

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

        using (Stream stream = request.GetResponse().GetResponseStream())
        {
            using (var reader = new RawSourceWaveStream(stream, format))
            {
                // reader is not seekable; we need to convert to a byte array to seek
                var bytes = reader.ToByteArray();

                // create a new stream from the byte aray
                var seekableStream = new MemoryStream(bytes);

                // instantiating a WaveFileReader as follows will throw an exception:
                // "System.FormatException: Not a WAVE file - no RIFF header"
                using (var waveReader = new WaveFileReader(seekableStream))
                {
                    using (var pcmStream = WaveFormatConversionStream.CreatePcmStream(waveReader))
                    {
                        var pcmBytes = pcmStream.ToByteArray();
                        var mp3 = pcmBytes.ToMp3();
                    }
                }
            }
        }
    }
}

public static class StreamExtensions
{
    public static byte[] ToByteArray(this Stream stream)
    {
        var ms = new MemoryStream();
        var buffer = new byte[1024];
        int bytes = 0;

        while ((bytes = stream.Read(buffer, 0, buffer.Length)) > 0)
            ms.Write(buffer, 0, bytes);

        return ms.ToArray();
    }
}

public static class ByteExtensions
{
    public static byte[] ToMp3(this byte[] bytes)
    {
        using (var outStream = new MemoryStream())
        {
            using (var ms = new MemoryStream(bytes))
            {
                using (var reader = new WaveFileReader(ms))
                {
                    using (var writer = new LameMP3FileWriter(outStream, reader.WaveFormat, 64))
                    {
                        reader.CopyTo(writer);
                        return outStream.ToArray();
                    }
                }
            }
        }
    }
}

I've been poking around at this for most of the day and I feel like I'm introducing unnecessary complexity into something that seems like it should be fairly straightforward.

Any help would be much appreciated.

Note: I cannot change the source format and supporting IE is a requirement.

EDIT: I resolved the RIFF exception and am able to produce a stream of the MP3, but it's nothing but white noise. Hopefully I can resolve that as well. My new code is as follows:

[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");
}
like image 340
LiquidPony Avatar asked Nov 11 '22 06:11

LiquidPony


1 Answers

This works for me (and I needed to do exactly what you wanted to do). Hope this helps someone else as well. I used NAudio with LAME.

You have to make sure that you copy the libmp3lamexx.dll files to your webserver's BIN location or to some folder in the %PATH% variable, else it won't work.

        string sq = /* URL of WAV file (http://foo.com/blah.wav) */

        Response.ContentType = "audio/mpeg";

        using (WebClient wc = new WebClient())
        {
            if (!sq.ToLower().EndsWith(".wav"))
            {
                byte[] rawFile = wc.DownloadData(sq.Trim());
                Response.OutputStream.Write(rawFile, 0, rawFile.Length);
            }
            else
            {
                using (var wavReader = new WaveFileReader(new MemoryStream(wc.DownloadData(sq.Trim()))))
                {
                    try
                    {
                        using (var wavWriter = new LameMP3FileWriter(Response.OutputStream, wavReader.WaveFormat, LAMEPreset.ABR_128))
                        {
                            wavReader.CopyTo(wavWriter);
                        }
                    }
                    catch (ArgumentException)
                    {
                        var newFormat = new WaveFormat(wavReader.WaveFormat.SampleRate, 16, 2);

                        using (var pcmStream = new WaveFormatConversionStream(newFormat, wavReader))
                        {
                            using (var wavWriter = new LameMP3FileWriter(Response.OutputStream, pcmStream.WaveFormat, LAMEPreset.ABR_128))
                            {
                                pcmStream.CopyTo(wavWriter);
                            }
                        }
                    }
                }
            }

            Response.Flush();
            Response.End();
        }
like image 117
Shiroy Avatar answered Nov 14 '22 23:11

Shiroy