This project is read-only.
Encoding Example
SpeexEncoder encoder = new SpeexEncoder(BandMode.Wide);

// Audio capture callback
private void waveIn_DataAvailable(object sender, WaveInEventArgs e)
{
	// convert to short
	short[] data = new short[e.BytesRecorded / 2];
	Buffer.BlockCopy(e.Buffer, 0, data, 0, e.BytesRecorded);
	var encodedData = new byte[e.BytesRecorded];
	// note: the number of samples per frame must be a multiple of encoder.FrameSize
	var encodedBytes = encoder.Encode(data, 0, data.Length, encodedData, 0, encodedData.Length);
	if (encodedBytes != 0)
	{
	// todo: do something with the encoded data
	}
}


Decoding Example
SpeexDecoder decoder = new SpeexDecoder(BandMode.Wide);

private void Decode(byte[] encodedData)
{
	short[] decodedFrame = new short[1024]; // should be the same number of samples as on the capturing side
	decoder.Decode(encodedData, 0, encodedData.Length, decodedFrame, 0, false);
	
	// todo: do something with the decoded data
}


Decoding with JitterBuffer Example
SpeexDecoder decoder = new SpeexDecoder(BandMode.Wide);
SpeexJitterBuffer jitterBuffer = new SpeexJitterBuffer(decoder);

private void OnReadIntoAudioPlayback(byte[] buffer, int offset, int count)
{
	short[] decodedBuffer = new short[decoder.FrameSize * 2];
	// the jitterbuffer automatically fill missed frames with silence
	jitterBuffer.Get(decodedBuffer);
	for (int i = 0; i < decodedBuffer.Length; ++i)
	{
		if (bytesRead < count)
		{
			short currentSample = decodedBuffer[i];
			BitConverter.GetBytes(currentSample).CopyTo(buffer, offset + bytesRead);
			bytesRead += 2;
		}
		else
		{
			// todo: handle the overhead; we decoded more bytes as the audio playback has requested
		}
	}
}

Last edited Feb 8, 2012 at 7:08 PM by balistof, version 4

Comments

tirambad Sep 26, 2011 at 11:00 AM 
private void OnReadIntoAudioPlayback(byte[] buffer, int offset, int count)
In this line... the parameters what is the parameter to be passed as "byte[] buffer" is it the the encodedData from the encoder??