Sumit Rana
Sumit Rana

Reputation: 111

C# Naudio BufferedWaveProvider Buffer Full Exception

I am getting a buffer full exception on BufferedWaveProvider when I am sending a real time audio stream from Android using websocket connection. Audio format Sent from Android: 44100, Sterio, 16bit I have also tried increasing the buffer length of BufferedWaveProvider but still no good, the only thing changed is that the program runs a little longer, Also I tried recording the audio to a file using WaveFileWriter, the file was around 80mb for 10 seconds audio stream and has no audio in it.

class Program
{


    static WaveOut waveOut;
    static BufferedWaveProvider bufferedWaveProvider = null;
    private static SpeechSynthesizer ss;
    private static SpeechRecognitionEngine sre;
    private static int temperature = 75;
    private static WebSocketServer appServer = new WebSocketServer();
    private static int total = 0;
    private static WaveFileWriter writer = new WaveFileWriter("recording.wav", new WaveFormat(44100, 16, 2));
    private static MemoryStream memStream = new MemoryStream();

    static void Main(string[] args)
    {

        Console.WriteLine("Press any key to start the WebSocketServer!");
        Console.ReadKey();
        Console.WriteLine();
        waveOut = new WaveOut();            
        bufferedWaveProvider = new BufferedWaveProvider(new WaveFormat());           
        waveOut.Init(bufferedWaveProvider);
        waveOut.Play();    
        appServer.NewDataReceived += new SessionHandler<WebSocketSession, byte[]>(appServer_NewDataReceived);
        appServer.NewSessionConnected += AppServer_NewSessionConnected;           
        appServer.SessionClosed += new SessionHandler<WebSocketSession, CloseReason>(appServer_SessionClosed);    

        //Setup the appServer
        if (!appServer.Setup(80)) //Setup with listening port
        {
            Console.WriteLine("Failed to setup!");
            Console.ReadKey();
            return;
        }

        //Try to start the appServer
        if (!appServer.Start())
        {
            Console.WriteLine("Failed to start!");
            Console.ReadKey();
            return;
        }

        Console.WriteLine("The server started successfully, press key 'q' to stop it!");

        while (Console.ReadKey().KeyChar != 'q')
        {
            Console.WriteLine();
            continue;
        }

        //Stop the appServer
        appServer.Stop();
        Console.WriteLine();
        Console.WriteLine("The server was stopped!");
        Console.ReadKey();
    }

    static void appServer_NewDataReceived(WebSocketSession session, byte[] value)
    {
        total = total + value.Length;            
        Console.WriteLine("Receiving!! " + total);
        bufferedWaveProvider.AddSamples(value, 0, value.Length);
    }

Upvotes: 0

Views: 1606

Answers (1)

Mark Heath
Mark Heath

Reputation: 49492

well you need to start by understanding what the format of the incoming data is. If the wav file you recorded has no audio in it, then something is wrong, although you must call Dispose on the wave file writer before attempting to access it.

Upvotes: 0

Related Questions