I need to be able to play a short[] array filled with 16-bit PCM values (after decode) in real time. The array keeps getting longer and the player should be able to play it while the array is "growing".
I an writing in C#.
thanks ahead, Ofek
EDIT: I used the BufferedWaveProvider and the code looks like this:
Class Player
{
Private WaveFormat waveForamt=new WaveFormat(8000,16,1);
private BufferedWaveProvider bufferedWaveProvider;
public Player()
{
bufferedWaveProvider=new BufferedWaveProvider(waveFormat);
}
public void AddSamples(byte[] array)
{
bufferedWaveProvider.AddSamples(array,0,array.length);
WaveOut waveout=new WaveOut();
waveout.Init(bufferedWaveProvider);
waveout.Play();
}
}
the array that the AddSamples func gets is 32000 long I can see the parameters bufferedWaveProvider.BufferedBytes = 32000 bufferedWaveProvider.BufferedBytes = 80000 bufferedWaveProvider.BufferedDuration = 00:00:02 bufferedWaveProvider.BufferDuration = 00:00:05
when I record the array I speak for 2 seconds and say: "one, two, three..." The porblem is that when I play it, I just hear a quick sound, and nothing like what I said..
any ideas about what went wrong?
Thnaks, Ofek
EDIT 2:
Now my code looks like this, I call the AddSamples function from the Capture class.
after each time the waveout.Play() function is called - I clear the buffer and wait for it to fill up again. once it is full again I play it, and so on. the problem is that in the second time I play the buffer the sound starts fast and then it slows down... I heard something about working with two buffers.. do you know anything about this?
Thnaks a lot!
Here is the class that calls the player.AddSamples
You basically need to be able to set up a soundcard driver to read from an audio buffer and then write to that same buffer. The BufferedWaveProvider class in naudio could help. Take a look at http://naudio.codeplex.com/.
Edit : Your class works fine for me in an empty 'forms' project, playing 2 seconds of audio:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
var player = new Player();
var rand = new Random();
var bytes = new byte[32000];
rand.NextBytes(bytes);
player.AddSamples(bytes);
}
}
Are you calling it in a background thread, or maybe a console app? It May be a threading or callback issue - see the 1) WaveOut section in http://naudio.codeplex.com/wikipage?title=Audio%20Output%20Driver%20Selection. You may need to pass different callback options into WaveOut()?