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?