-
Notifications
You must be signed in to change notification settings - Fork 1
/
vdp_audio.c
236 lines (206 loc) · 4.62 KB
/
vdp_audio.c
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
/*
* Title: AGON VDPAUDIO - VDP audio C interface implementation
* Author: James Higgs
* Created: 2023-05-06
* Last Updated: 2023-12-26
*
* Modinfo:
* 2023-05-06: Initial version
* 2023-12-26: Updated for MOS 1.04
*/
#include <defines.h>
#include "mos-interface.h"
// MOS/VDP 1.04 audio
// https://github.com/breakintoprogram/agon-docs/wiki/VDP-%E2%80%90-Enhanced-Audio-API
// ("wave" parameter now seems to be used as a "command id")
// Audio "command" IDs
enum {
CMD_PLAYNOTE = 0, // 0 to 7 - Player and player bullet bitmap id's
CMD_STATUS,
CMD_SETVOLUME,
CMD_SETFREQ,
CMD_SETWAVEFORM,
CMD_SAMPLE,
CMD_VOLUMEENVELOPE,
CMD_FREQENVELOPE,
CMD_ENABLECHANNEL,
CMD_DISABLECHANNEL,
CMD_RESETCHANNEL
};
// Command 0 - Play note
// volume - 0 to 127
// freqency - frequency in Hz
// duration - duration in milliseconds
UINT8 audio_playNote(UINT8 channel, UINT8 volume, UINT16 frequency, UINT16 duration)
{
// Notes:
// VDU 23, 0, 0x85, channel8, 0, vol8, freq16, duration16
unsigned int delay = 1000;
putch(23);
putch(0);
putch(0x85);
putch(channel);
putch(CMD_PLAYNOTE); // Command ID 0 - "Play Note"
putch(volume);
putch(frequency & 0xFF);
putch(frequency >> 8);
putch(duration & 0xFF);
putch(duration >> 8);
// wait for VDP to process and return status
while (delay--);
return getsysvar_audioSuccess();
}
// Command 1 - Status
UINT8 audio_status(UINT8 channel)
{
unsigned int delay = 1000;
putch(23);
putch(0);
putch(0x85);
putch(channel);
putch(CMD_STATUS);
// wait for VDP to process and return status
while (delay--);
return getsysvar_audioChannel(); // TODO - Check
}
// Command 2 - Set Volume
// volume - 0 to 127
void audio_setVolume(UINT8 channel, UINT8 volume)
{
putch(23);
putch(0);
putch(0x85);
putch(channel);
putch(CMD_SETVOLUME);
putch(volume);
}
// Command 3 - Set frequency
// freqency - frequency in Hz
void audio_setFreq(UINT8 channel, UINT16 frequency)
{
putch(23);
putch(0);
putch(0x85);
putch(channel);
putch(CMD_SETFREQ);
putch(frequency & 0xFF);
putch(frequency >> 8);
}
// Command 4 - Set waveform
// waveform - waveform type as per ID's in vdp_audio.h
void audio_setWaveform(UINT8 channel, INT8 waveform)
{
putch(23);
putch(0);
putch(0x85);
putch(channel);
putch(CMD_SETWAVEFORM);
putch(waveform);
}
// Command 5 - Sample management
// VDU 23, 0, &85, sample, 5, 0, length; lengthHighByte, <sampleData>
UINT8 audio_loadSample(INT8 sampleId, UINT24 length, UINT8 *data)
{
unsigned int delay = 1000;
UINT24 count = 0;
UINT8 *p = data;
putch(23);
putch(0);
putch(0x85);
putch(sampleId);
putch(CMD_SAMPLE);
putch(0); // sample command "load sample"
putch(length & 0xFF);
putch(length >> 8);
putch(length >> 16);
for (count = 0; count < length; count++)
{
putch(*p);
p++;
}
// wait for VDP to process and return status
while (delay--);
return getsysvar_audioSuccess(); // TODO - Check
}
UINT8 audio_clearSample(INT8 sampleId)
{
unsigned int delay = 1000;
putch(23);
putch(0);
putch(0x85);
putch(sampleId);
putch(CMD_SAMPLE);
putch(1); // sample command "clear sample"
// wait for VDP to process and return status
while (delay--);
return getsysvar_audioSuccess(); // TODO - Check
}
// Command 6 - Volume envelope
// attack - attack time in ms
// decay - decay time in ms
// sustain - sustain "level" (0 to 127)
// release - release time in ms
void audio_setVolumeEnvelope(UINT8 channel, UINT16 attack, UINT16 decay, UINT8 sustain, UINT16 release)
{
putch(23);
putch(0);
putch(0x85);
putch(channel);
putch(CMD_VOLUMEENVELOPE);
putch(1); // Env type 1 = ADSR (type 0 = None)
putch(attack & 0xFF);
putch(attack >> 8);
putch(decay & 0xFF);
putch(decay >> 8);
putch(sustain);
putch(release & 0xFF);
putch(release >> 8);
}
void audio_disableVolumeEnvelope(UINT8 channel)
{
putch(23);
putch(0);
putch(0x85);
putch(channel);
putch(CMD_VOLUMEENVELOPE);
putch(0); // disable vol env
}
// Command 7 - Frequency envelope
// TODO !
//void audio_setFreqEnvelope(UINT8 channel, ...);
void audio_disableFreqEnvelope(UINT8 channel)
{
putch(23);
putch(0);
putch(0x85);
putch(channel);
putch(CMD_FREQENVELOPE);
putch(0); // disable freq env
}
// Command 8 - Enable channel
void audio_enableChannel(UINT8 channel)
{
putch(23);
putch(0);
putch(0x85);
putch(channel);
putch(CMD_ENABLECHANNEL);
}
// Command 9 - Disable channel
void audio_disableChannel(UINT8 channel)
{
putch(23);
putch(0);
putch(0x85);
putch(channel);
putch(CMD_DISABLECHANNEL);
}
// Command 10 - Reset channel
void audio_resetChannel(UINT8 channel)
{
putch(23);
putch(0);
putch(0x85);
putch(channel);
putch(CMD_RESETCHANNEL);
}