0

I want to send read bytes from NAudio BufferedWaveProvider to Icecast server. I have already done, but I´m facing problems with audio chunks and silence. The sound is not being send continuously.

I´ve tried the following:

void init_broadcast(){

    //Connect to the icecast server
    Libshout icecast = new Libshout();
    icecast.connect();

    //Audio format
    WaveFormat format = new WaveFormat(44100, 2);

    //Audio input and buffer
    WaveIn wavein = new WaveIn(format);
    BufferedWaveProvider bwp = new BufferedWaveProvider(format);

    //Save audio to buffer
    wavein.DataAvailable += delegate(object sender, WaveInEventArgs args){
        bwp.AddSamples(args.Buffer, 0, args.BytesRecorded);
    }

    //Start recording
    wavein.StartRecording();

    //Thread to send continuously audio to icecast server
    new Thread(() => {
        while(true){
            if(!icecast.isConnected)
                break;

            var buffer = new byte[bwp.BufferLength];
            bwp.Read(buffer, 0, buffer.Length);

            byte[] mp3 = to_mp3_encoder(buffer);

            icecast.send(mp3);
        }
    }).Start();

}

This is my to_mp3_encoder method:

public byte[] to_mp3_encoder(byte[] buffer) {

    byte[] buffer_mp3;

    using(MemoryStream ms = new MemoryStream()) {

        LameMP3FileWriter writer = new LameMP3FileWriter(ms, new WaveFormat(44100, 2), LAMEPreset.ABR_128);

        writer.Write(buffer, 0, buffer.Length);
        writer.Flush();

        buffer_mp3 = ms.GetBuffer();

    }

    return buffer_mp3;

}

It works, server connect and send audio correctly, but when I play audio on icecast client, it has a silent chunk in the middle of the audio. The audio is not continuous. It looks like it sends a half audio a half of silence.

Can somebody help me with this? Am I using buffer wrong with threads?

Brad
  • 159,648
  • 54
  • 349
  • 530
Wilson Faustino
  • 157
  • 1
  • 7
  • Where does `to_mp3_encoder()` go? Seems strange that you're expecting a synchronous and proportional response for a chunk of PCM data. MP3 frames can reference prior frames, so it's not like you can just give it a fixed number of samples and encode an MP3 frame standalone (generally). – Brad Jun 05 '19 at 23:23
  • Brad, I have updated my question with `to_mp3_encoder()` method. If i dont encode mp3 chunks, how can I do it for continous encoding? – Wilson Faustino Jun 06 '19 at 00:40
  • 1
    I haven't used this stack in years, but I think if you just keep your one instance of `LameMP3FileWriter` going, continually write buffers to it, and then read from it only when it's ready (rather than flushing) and then take the data you read from it and write it to the socket, you'll be in good shape. – Brad Jun 06 '19 at 00:53
  • 1
    Also, consider using WebM and Opus. :-) Better sound quality, good compatibility, no licensing fees/patents to worry about... – Brad Jun 06 '19 at 00:57

0 Answers0