Opentk/Source/Examples/OpenAL/StreamingPlayback.cs

98 lines
3.3 KiB
C#
Raw Normal View History

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;
2008-04-06 14:28:29 +00:00
using System.ComponentModel;
2008-04-04 21:05:03 +00:00
using OpenTK.Audio;
2008-04-04 20:19:53 +00:00
namespace Examples.OpenAL
{
2008-04-06 14:28:29 +00:00
// Not working correctly (sound pops).
//[Example("Streaming Playback", ExampleCategory.OpenAL)]
2008-04-04 21:05:03 +00:00
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-06 14:28:29 +00:00
const int buffer_size = (int)(0.5*44100);
const int buffer_count = 4;
2008-04-04 21:05:03 +00:00
2008-04-06 14:28:29 +00:00
static object openal_lock = new object(); // Should be global in your app.
2008-04-04 21:05:03 +00:00
public static void Main()
{
2008-04-06 14:28:29 +00:00
using (AudioContext context = new AudioContext())
2008-04-06 14:48:47 +00:00
using (AudioReader sound = new AudioReader(filename))
2008-04-04 21:05:03 +00:00
{
int source = AL.GenSource();
2008-04-06 14:28:29 +00:00
int[] buffers = AL.GenBuffers(buffer_count);
2008-04-04 21:05:03 +00:00
int state;
Console.WriteLine("Testing WaveReader({0}).ReadSamples()", filename);
Console.Write("Playing");
2008-04-06 14:28:29 +00:00
foreach (int buffer in buffers)
AL.BufferData(buffer, sound.ReadSamples(buffer_size));
AL.SourceQueueBuffers(source, buffers.Length, buffers);
AL.SourcePlay(source);
int processed_count, queued_count;
2008-04-04 21:05:03 +00:00
do
{
//Console.Write(".");
2008-04-06 14:28:29 +00:00
do
2008-04-04 21:05:03 +00:00
{
2008-04-06 14:28:29 +00:00
AL.GetSource(source, ALGetSourcei.BuffersProcessed, out processed_count);
//Thread.Sleep(1);
2008-04-04 21:05:03 +00:00
}
2008-04-06 14:28:29 +00:00
while (processed_count == 0);
2008-04-06 14:28:29 +00:00
Console.WriteLine(processed_count);
while (processed_count > 0)
2008-04-04 21:05:03 +00:00
{
2008-04-06 14:28:29 +00:00
int buffer = AL.SourceUnqueueBuffer(source);
if (buffer != 0 && !sound.EndOfFile)
2008-04-04 21:05:03 +00:00
{
2008-04-06 14:28:29 +00:00
Console.WriteLine(" " + buffer.ToString());
AL.BufferData(buffer, sound.ReadSamples(buffer_size));
2008-04-04 21:05:03 +00:00
AL.SourceQueueBuffer(source, buffer);
}
2008-04-06 14:28:29 +00:00
--processed_count;
2008-04-04 21:05:03 +00:00
}
2008-04-06 14:28:29 +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);
Console.WriteLine("r");
}
}
else
break;
2008-04-04 21:05:03 +00:00
}
2008-04-06 14:28:29 +00:00
while (true);
AL.SourceStop(source);
AL.DeleteSource(source);
AL.DeleteBuffers(buffers);
2008-04-04 21:05:03 +00:00
}
2008-04-06 14:28:29 +00:00
Console.WriteLine();
2008-04-04 21:05:03 +00:00
}
2008-04-04 20:19:53 +00:00
}
}