forked from dzamkov/MD-old
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAudioFeed.cs
179 lines (160 loc) · 5.61 KB
/
AudioFeed.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
using System;
using System.Collections.Generic;
using System.Threading;
using OpenTK.Audio.OpenAL;
using OpenTK.Audio;
namespace MD
{
/// <summary>
/// A continous, infinitely large, collection of samples that may only be accessed at a certain location and in certain conditions.
/// </summary>
public abstract class AudioFeed
{
/// <summary>
/// Reads to the specified output array.
/// </summary>
/// <param name="Amount">The amount of samples to read.</param>
/// <param name="Output">The array to read to.</param>
/// <param name="Offset">The place in the array to begin reading to.</param>
/// <returns>The amount of samples actually read.</returns>
public abstract int Read(int Amount, byte[] Output, int Offset);
/// <summary>
/// Gets the amount of samples in a second.
/// </summary>
public abstract int SampleRate { get; }
/// <summary>
/// Gets the format of the audio in the audio source.
/// </summary>
public abstract ALFormat Format { get; }
/// <summary>
/// Gets the amount of channels this audio feed has.
/// </summary>
public int Channels
{
get
{
return AudioSource.GetChannels(this.Format);
}
}
/// <summary>
/// Gets the amount of bytes in a sample of this audio feed.
/// </summary>
public int BytesPerSample
{
get
{
return AudioSource.GetBytesPerSample(this.Format);
}
}
}
/// <summary>
/// Continously plays an audio feed.
/// </summary>
public class AudioOutput
{
public AudioOutput(AudioFeed Source, int BufferSize, int BufferAmount)
{
this._Feed = Source;
this._BufferSize = BufferSize;
this._BufferAmount = BufferAmount;
this._Init();
}
public AudioOutput(AudioFeed Source)
: this(Source, 4096, 2)
{
}
private void _Init()
{
this._Source = AL.GenSource();
int bps = this._Feed.BytesPerSample;
this._Data = new byte[this._BufferSize * bps];
this._BuffersPlaying = new LinkedList<int>();
this._BuffersAvailable = new Stack<int>();
for (int t = 0; t < this._BufferAmount; t++)
{
this._BuffersAvailable.Push(AL.GenBuffer());
}
// Approximate wait time needed in the loop
this._WaitTime = (int)((double)this._Feed.SampleRate / (double)this._BufferSize / (double)this._BufferAmount * 4.0);
}
/// <summary>
/// Causes the output to begin playing, if it hasn't already.
/// </summary>
public void Play()
{
if (!this._Playing)
{
if (this._BuffersPlaying.Count > 0 && AL.GetSourceState(this._Source) != ALSourceState.Playing)
{
AL.SourcePlay(this._Source);
}
this._Playing = true;
this._Thread = new Thread(this._PlayLoop);
this._Thread.IsBackground = true;
this._Thread.Start();
}
}
/// <summary>
/// Causes the output to immediately stop playing.
/// </summary>
public void Stop()
{
if (this._Playing)
{
this._Playing = false;
if (AL.GetSourceState(this._Source) != ALSourceState.Paused)
{
AL.SourcePause(this._Source);
}
this._Thread.Join();
this._Thread = null;
}
}
private void _PlayLoop()
{
int bps = this._Feed.BytesPerSample;
while (this._Playing)
{
// Buffer data
while (this._BuffersAvailable.Count > 0)
{
int amount = this._Feed.Read(this._BufferSize, this._Data, 0);
int buf = this._BuffersAvailable.Pop();
AL.BufferData<byte>(buf, this._Feed.Format, this._Data, amount * bps, this._Feed.SampleRate);
AL.SourceQueueBuffer(this._Source, buf);
this._BuffersPlaying.AddLast(buf);
if (this._Playing && AL.GetSourceState(this._Source) != ALSourceState.Playing)
{
AL.SourcePlay(this._Source);
}
}
// Wait a little
Thread.Sleep(this._WaitTime);
// Remove played buffers
int processed;
AL.GetSource(this._Source, ALGetSourcei.BuffersProcessed, out processed);
if (processed > 0)
{
AL.SourceUnqueueBuffers(this._Source, processed);
}
while (processed > 0)
{
processed--;
LinkedListNode<int> node = this._BuffersPlaying.First;
this._BuffersAvailable.Push(node.Value);
this._BuffersPlaying.Remove(node);
}
}
}
private bool _Playing;
private byte[] _Data;
private int _Source;
private int _WaitTime;
LinkedList<int> _BuffersPlaying;
Stack<int> _BuffersAvailable;
private int _BufferSize;
private int _BufferAmount;
private AudioFeed _Feed;
private Thread _Thread;
}
}