2008-04-04 21:05:03 +00:00
|
|
|
|
#region --- License ---
|
|
|
|
|
/* Licensed under the MIT/X11 license.
|
|
|
|
|
* Copyright (c) 2006-2008 the OpenTK Team.
|
|
|
|
|
* This notice may not be removed from any source distribution.
|
|
|
|
|
* See license.txt for licensing details.
|
|
|
|
|
*/
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
using System;
|
2008-04-04 20:19:53 +00:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Text;
|
2008-04-04 21:05:03 +00:00
|
|
|
|
using System.Threading;
|
|
|
|
|
|
|
|
|
|
using OpenTK.Audio;
|
2008-04-04 20:19:53 +00:00
|
|
|
|
|
|
|
|
|
namespace Examples.OpenAL
|
|
|
|
|
{
|
2008-04-04 21:05:03 +00:00
|
|
|
|
[Example("Streaming Playback", ExampleCategory.OpenAL)]
|
|
|
|
|
public class StreamingPlayback
|
2008-04-04 20:19:53 +00:00
|
|
|
|
{
|
2008-04-04 21:05:03 +00:00
|
|
|
|
const string filename = "Data\\Audio\\the_ring_that_fell.wav";
|
2008-04-04 22:14:42 +00:00
|
|
|
|
const int buffer_size = 18096;
|
2008-04-04 21:05:03 +00:00
|
|
|
|
|
|
|
|
|
static object openal_lock = new object();
|
|
|
|
|
|
|
|
|
|
public static void Main()
|
|
|
|
|
{
|
|
|
|
|
AudioContext context = new AudioContext();
|
|
|
|
|
|
|
|
|
|
using (SoundReader sound = new SoundReader(filename))
|
|
|
|
|
{
|
|
|
|
|
int[] buffers = AL.GenBuffers(2);
|
|
|
|
|
int source = AL.GenSource();
|
|
|
|
|
int state;
|
|
|
|
|
|
|
|
|
|
Console.WriteLine("Testing WaveReader({0}).ReadSamples()", filename);
|
|
|
|
|
|
|
|
|
|
Console.Write("Playing");
|
|
|
|
|
|
2008-04-04 21:33:34 +00:00
|
|
|
|
SoundStreamer streamer = new SoundStreamer(sound, source, buffers);
|
|
|
|
|
streamer.Start();
|
2008-04-04 22:14:42 +00:00
|
|
|
|
lock (openal_lock)
|
|
|
|
|
{
|
|
|
|
|
AL.SourcePlay(source);
|
|
|
|
|
}
|
2008-04-04 21:05:03 +00:00
|
|
|
|
|
|
|
|
|
// Query the source to find out when it stops playing.
|
|
|
|
|
do
|
|
|
|
|
{
|
2008-04-04 22:14:42 +00:00
|
|
|
|
//Thread.Sleep(100);
|
|
|
|
|
//Console.Write(".");
|
2008-04-04 21:05:03 +00:00
|
|
|
|
lock (openal_lock)
|
|
|
|
|
{
|
|
|
|
|
AL.GetSource(source, ALGetSourcei.SourceState, out state);
|
|
|
|
|
}
|
2008-04-04 21:33:34 +00:00
|
|
|
|
}
|
|
|
|
|
while ((ALSourceState)state == ALSourceState.Playing);
|
2008-04-04 21:05:03 +00:00
|
|
|
|
|
2008-04-04 22:14:42 +00:00
|
|
|
|
Console.WriteLine("asd");
|
2008-04-04 21:05:03 +00:00
|
|
|
|
|
2008-04-04 22:14:42 +00:00
|
|
|
|
lock (openal_lock)
|
|
|
|
|
{
|
|
|
|
|
AL.SourceStop(source);
|
|
|
|
|
AL.DeleteSource(source);
|
|
|
|
|
AL.DeleteBuffers(buffers);
|
|
|
|
|
}
|
2008-04-04 21:05:03 +00:00
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class SoundStreamer
|
|
|
|
|
{
|
2008-04-04 21:33:34 +00:00
|
|
|
|
SoundReader reader;
|
|
|
|
|
int source;
|
|
|
|
|
int[] buffers;
|
2008-04-04 22:14:42 +00:00
|
|
|
|
Thread thread;
|
2008-04-04 21:33:34 +00:00
|
|
|
|
|
2008-04-04 21:05:03 +00:00
|
|
|
|
public SoundStreamer(SoundReader sound, int source, int[] buffers)
|
|
|
|
|
{
|
2008-04-04 21:33:34 +00:00
|
|
|
|
reader = sound;
|
|
|
|
|
this.source = source;
|
|
|
|
|
this.buffers = buffers;
|
|
|
|
|
|
|
|
|
|
lock (openal_lock)
|
|
|
|
|
{
|
|
|
|
|
foreach (int buffer in buffers)
|
|
|
|
|
AL.BufferData(buffer, sound.ReadSamples(buffer_size));
|
|
|
|
|
|
|
|
|
|
AL.SourceQueueBuffers(source, buffers.Length, buffers);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Start()
|
|
|
|
|
{
|
2008-04-04 22:14:42 +00:00
|
|
|
|
thread = new Thread(new ThreadStart(StartStreaming));
|
|
|
|
|
thread.Start();
|
2008-04-04 21:33:34 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void StartStreaming()
|
|
|
|
|
{
|
|
|
|
|
while (!reader.EndOfFile)
|
2008-04-04 21:05:03 +00:00
|
|
|
|
{
|
|
|
|
|
lock (openal_lock)
|
|
|
|
|
{
|
|
|
|
|
int processed_count;
|
|
|
|
|
AL.GetSource(source, ALGetSourcei.BuffersProcessed, out processed_count);
|
|
|
|
|
while (processed_count-- > 0)
|
|
|
|
|
{
|
|
|
|
|
int buffer = AL.SourceUnqueueBuffer(source);
|
2008-04-04 21:33:34 +00:00
|
|
|
|
AL.BufferData(buffer, reader.ReadSamples(buffer_size));
|
2008-04-04 21:05:03 +00:00
|
|
|
|
AL.SourceQueueBuffer(source, buffer);
|
|
|
|
|
}
|
|
|
|
|
}
|
2008-04-04 21:33:34 +00:00
|
|
|
|
|
|
|
|
|
Thread.Sleep(5);
|
2008-04-04 21:05:03 +00:00
|
|
|
|
}
|
2008-04-04 22:14:42 +00:00
|
|
|
|
Console.WriteLine("booh");
|
2008-04-04 21:05:03 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2008-04-04 20:19:53 +00:00
|
|
|
|
}
|
|
|
|
|
}
|