/
SoundX.cs
235 lines (195 loc) · 6.82 KB
/
SoundX.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
/// Sharp 80 (c) Matthew Hamilton
/// Licensed Under GPL v3. See license.txt for details.
using System;
using System.Threading;
using System.Threading.Tasks;
using SharpDX;
using SharpDX.Multimedia;
using SharpDX.XAudio2;
using Sharp80.TRS80;
namespace Sharp80.DirectX
{
public partial class SoundX : ISound
{
public int SampleRate { get; private set; }
private static bool AnyInitFail { get; set; } = false;
public bool Stopped { get; private set; }
private XAudio2 xaudio;
private MasteringVoice masteringVoice;
private SourceVoice sourceVoice;
private Task playingTask;
private AutoResetEvent bufferEndEvent;
private const int FRAMES_PER_SECOND = 20;
private const int BITS_PER_SAMPLE = 16;
private const int BYTES_PER_SAMPLE = BITS_PER_SAMPLE / 8;
private const int CHANNELS = 1; // glorious TRS-80 mono
private const int RING_SIZE = 2;
private const short MAX_SOUND_AMPLITUDE = 0x40 * 0x100;
private const short NULL_SOUND_LEVEL = 0x0;
private const short MIN_SOUND_LEVEL = NULL_SOUND_LEVEL - MAX_SOUND_AMPLITUDE;
private const short MAX_SOUND_LEVEL = NULL_SOUND_LEVEL + MAX_SOUND_AMPLITUDE;
private readonly short[] outTable = new short[] { NULL_SOUND_LEVEL, MAX_SOUND_LEVEL, MIN_SOUND_LEVEL, NULL_SOUND_LEVEL };
private Noise noise;
private bool on = false;
private bool mute = false;
private bool enabled = true;
private bool isDisposed = false;
public bool On
{
get { return on; }
set
{
if (on != value)
{
on = value;
UpdateEnabled();
}
}
}
public bool Mute
{
get => mute;
set
{
if (mute != value)
{
mute = value;
UpdateEnabled();
}
}
}
public bool UseDriveNoise { get; set; } = false;
public bool DriveMotorRunning { get; set; } = false;
public void TrackStep() => noise.TrackStep();
private int ringCursor = 0;
private AudioBuffer[] audioBuffersRing = new AudioBuffer[RING_SIZE];
private DataPointer[] memBuffers = new DataPointer[RING_SIZE];
private FrameBuffer<short> frameBuffer;
public SampleCallback SampleCallback { private get; set; }
public SoundX(int SampleRate)
{
this.SampleRate = SampleRate;
Stopped = true;
if (AnyInitFail)
return;
try
{
frameBuffer = new FrameBuffer<short>(SampleRate / FRAMES_PER_SECOND * CHANNELS, SampleRate / 20);
noise = new Noise(SampleRate, MAX_SOUND_AMPLITUDE);
xaudio = new XAudio2();
masteringVoice = new MasteringVoice(xaudio, CHANNELS, SampleRate);
bufferEndEvent = new AutoResetEvent(false);
var frameSizeBytes = SampleRate / FRAMES_PER_SECOND * CHANNELS * 2;
for (int i = 0; i < RING_SIZE; i++)
{
audioBuffersRing[i] = new AudioBuffer()
{
AudioBytes = frameSizeBytes,
LoopCount = 0,
Flags = BufferFlags.None,
};
memBuffers[i].Size = frameSizeBytes;
memBuffers[i].Pointer = Utilities.AllocateMemory(memBuffers[i].Size);
}
sourceVoice = new SourceVoice(xaudio, new WaveFormat(SampleRate, BITS_PER_SAMPLE, CHANNELS), true);
xaudio.StartEngine();
sourceVoice.BufferEnd += (o) => bufferEndEvent?.Set();
sourceVoice.Start();
playingTask = Task.Factory.StartNew(Loop, TaskCreationOptions.LongRunning);
enabled = false;
on = false;
mute = false;
Stopped = false;
}
catch (Exception)
{
AnyInitFail = true;
enabled = false;
Stopped = true;
}
}
public void Sample()
{
if (enabled)
{
// Get the z80 sound output port current value
short outputLevel;
outputLevel = outTable[SampleCallback() & 0x03];
if (UseDriveNoise && DriveMotorRunning)
outputLevel += noise.GetNoiseSample();
frameBuffer.Sample(outputLevel);
}
}
private void UpdateEnabled()
{
if (enabled != (On && !Mute && !Stopped))
{
enabled = On && !Mute && !Stopped;
if (enabled)
{
frameBuffer.Reset();
noise.Reset();
}
}
}
private void Loop()
{
try
{
while (!Stopped)
{
if (sourceVoice.State.BuffersQueued >= RING_SIZE)
{
bufferEndEvent.WaitOne(1);
}
else
{
var buf = memBuffers[ringCursor];
if (enabled)
frameBuffer.ReadFrame(buf);
else
{
frameBuffer.ReadSilentFrame(buf);
}
audioBuffersRing[ringCursor].AudioDataPointer = buf.Pointer;
sourceVoice.SubmitSourceBuffer(audioBuffersRing[ringCursor], null);
ringCursor = ++ringCursor % RING_SIZE;
}
}
}
catch (Exception)
{
Mute = true;
Task.Run(Shutdown);
}
}
public async Task Shutdown()
{
Stopped = true;
if (playingTask != null)
{
await playingTask;
playingTask = null;
}
Dispose();
}
public void Dispose()
{
if (!isDisposed)
{
isDisposed = true;
enabled = false;
Stopped = true;
sourceVoice?.DestroyVoice();
sourceVoice?.Dispose();
bufferEndEvent?.Dispose();
masteringVoice?.Dispose();
xaudio?.Dispose();
sourceVoice = null;
bufferEndEvent = null;
masteringVoice = null;
xaudio = null;
}
}
}
}