2011-01-18 11:40:49 +00:00
|
|
|
|
// This code was written for the OpenTK library and has been released
|
|
|
|
|
// to the Public Domain.
|
|
|
|
|
// It is provided "as is" without express or implied warranty of any kind.
|
2009-02-22 10:43:35 +00:00
|
|
|
|
|
|
|
|
|
using System;
|
2009-10-24 10:07:43 +00:00
|
|
|
|
using System.Diagnostics;
|
2009-02-22 10:43:35 +00:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.ComponentModel;
|
|
|
|
|
|
|
|
|
|
using OpenTK.Audio;
|
2009-08-17 10:32:20 +00:00
|
|
|
|
using OpenTK.Audio.OpenAL;
|
2009-02-22 10:43:35 +00:00
|
|
|
|
|
|
|
|
|
namespace Examples.OpenAL
|
|
|
|
|
{
|
|
|
|
|
// Not working correctly (sound pops).
|
|
|
|
|
|
|
|
|
|
//[Example("Streaming Playback", ExampleCategory.OpenAL)]
|
|
|
|
|
public class StreamingPlayback
|
|
|
|
|
{
|
|
|
|
|
const string filename = "Data\\Audio\\the_ring_that_fell.wav";
|
|
|
|
|
const int buffer_size = (int)(0.5*44100);
|
|
|
|
|
const int buffer_count = 4;
|
|
|
|
|
|
|
|
|
|
//static object openal_lock = new object(); // Should be global in your app.
|
|
|
|
|
|
|
|
|
|
public static void Main()
|
|
|
|
|
{
|
|
|
|
|
using (AudioContext context = new AudioContext())
|
|
|
|
|
{
|
|
|
|
|
int source = AL.GenSource();
|
|
|
|
|
int[] buffers = AL.GenBuffers(buffer_count);
|
|
|
|
|
int state;
|
|
|
|
|
|
2009-10-24 10:07:43 +00:00
|
|
|
|
Trace.WriteLine("Testing WaveReader({0}).ReadSamples()", filename);
|
2009-02-22 10:43:35 +00:00
|
|
|
|
|
2009-10-24 10:07:43 +00:00
|
|
|
|
Trace.Write("Playing");
|
2009-02-22 10:43:35 +00:00
|
|
|
|
|
2009-08-17 10:32:20 +00:00
|
|
|
|
//foreach (int buffer in buffers)
|
|
|
|
|
// AL.BufferData(buffer, sound.ReadSamples(buffer_size));
|
2009-02-22 10:43:35 +00:00
|
|
|
|
AL.SourceQueueBuffers(source, buffers.Length, buffers);
|
|
|
|
|
AL.SourcePlay(source);
|
|
|
|
|
|
|
|
|
|
int processed_count, queued_count;
|
|
|
|
|
|
|
|
|
|
do
|
|
|
|
|
{
|
2009-10-24 10:07:43 +00:00
|
|
|
|
//Trace.Write(".");
|
2009-02-22 10:43:35 +00:00
|
|
|
|
|
|
|
|
|
do
|
|
|
|
|
{
|
|
|
|
|
AL.GetSource(source, ALGetSourcei.BuffersProcessed, out processed_count);
|
|
|
|
|
//Thread.Sleep(1);
|
|
|
|
|
}
|
|
|
|
|
while (processed_count == 0);
|
|
|
|
|
|
2009-10-24 10:07:43 +00:00
|
|
|
|
Trace.WriteLine(processed_count);
|
2009-08-17 10:32:20 +00:00
|
|
|
|
//while (processed_count > 0)
|
|
|
|
|
//{
|
|
|
|
|
// int buffer = AL.SourceUnqueueBuffer(source);
|
|
|
|
|
// if (buffer != 0 && !sound.EndOfFile)
|
|
|
|
|
// {
|
2009-10-24 10:07:43 +00:00
|
|
|
|
// Trace.WriteLine(" " + buffer.ToString());
|
2009-08-17 10:32:20 +00:00
|
|
|
|
// AL.BufferData(buffer, sound.ReadSamples(buffer_size));
|
|
|
|
|
// AL.SourceQueueBuffer(source, buffer);
|
|
|
|
|
// }
|
|
|
|
|
// --processed_count;
|
|
|
|
|
//}
|
2009-02-22 10:43:35 +00:00
|
|
|
|
|
|
|
|
|
AL.GetSource(source, ALGetSourcei.BuffersQueued, out queued_count);
|
|
|
|
|
if (queued_count > 0)
|
|
|
|
|
{
|
|
|
|
|
AL.GetSource(source, ALGetSourcei.SourceState, out state);
|
|
|
|
|
if ((ALSourceState)state != ALSourceState.Playing)
|
|
|
|
|
{
|
|
|
|
|
AL.SourcePlay(source);
|
2009-10-24 10:07:43 +00:00
|
|
|
|
Trace.WriteLine("r");
|
2009-02-22 10:43:35 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
while (true);
|
|
|
|
|
|
|
|
|
|
AL.SourceStop(source);
|
|
|
|
|
AL.DeleteSource(source);
|
|
|
|
|
AL.DeleteBuffers(buffers);
|
|
|
|
|
}
|
|
|
|
|
|
2009-10-24 10:07:43 +00:00
|
|
|
|
Trace.WriteLine("");
|
2009-02-22 10:43:35 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|