64 lines
1.3 KiB
C#
64 lines
1.3 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using OpenTK.Audio.OpenAL;
|
|
using OpenTK.Audio;
|
|
|
|
namespace AudioStreamer
|
|
{
|
|
public class OpenALPlayback : AudioInput
|
|
{
|
|
AudioContext context = new AudioContext();
|
|
|
|
int alsourceid;
|
|
Stack<int> buffers;
|
|
|
|
Random rand = new Random();
|
|
|
|
public OpenALPlayback()
|
|
{
|
|
|
|
alsourceid = AL.GenSource();
|
|
this.buffers = new Stack<int>();
|
|
}
|
|
|
|
public void Connect(AudioOutput output)
|
|
{
|
|
output.NewAudioAvailable += newAudioAvailable;
|
|
}
|
|
|
|
|
|
void newAudioAvailable(AudioOutput sender, Int16[] samples)
|
|
{
|
|
int processed;
|
|
AL.GetSource(alsourceid, ALGetSourcei.BuffersProcessed, out processed);
|
|
|
|
if (processed > 0){
|
|
int[] pb = AL.SourceUnqueueBuffers(alsourceid, processed);
|
|
foreach (int buf in pb){
|
|
buffers.Push(buf);
|
|
}
|
|
}
|
|
|
|
int buffer;
|
|
|
|
if (buffers.Count > 0){
|
|
buffer = buffers.Pop();
|
|
} else {
|
|
buffer = AL.GenBuffer();
|
|
}
|
|
|
|
AL.BufferData<Int16>(buffer, ALFormat.Mono16, samples, sizeof(Int16) * samples.Length, 44100);
|
|
AL.SourceQueueBuffer(this.alsourceid, buffer);
|
|
|
|
int state;
|
|
|
|
AL.GetSource(alsourceid,ALGetSourcei.SourceState, out state);
|
|
if ((ALSourceState)state != ALSourceState.Playing){
|
|
AL.SourcePlay(alsourceid);
|
|
Console.WriteLine("source restart");
|
|
}
|
|
}
|
|
|
|
}
|
|
}
|