-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwaveout.c
315 lines (266 loc) · 6.69 KB
/
waveout.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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
/*
* Copyright 2008-2013 Various Authors
* Copyright 2007 dnk <dnk@bjum.net>
*
* Based on oss.c
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, see <http://www.gnu.org/licenses/>.
*/
#include "op.h"
#include "sf.h"
#include "utils.h"
#include "xmalloc.h"
#include "debug.h"
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <mmsystem.h>
#include <stdio.h>
static HWAVEOUT wave_out;
static sample_format_t waveout_sf;
static int buffer_size = 4096;
static int buffer_count = 12;
static WAVEHDR *buffers;
static int buffer_idx;
static int buffers_free;
#define FRAME_SIZE_ALIGN(x) \
(((x) / sf_get_frame_size(waveout_sf)) * sf_get_frame_size(waveout_sf))
static void waveout_error(const char *name, int rc)
{
const char *errstr = "UNKNOWN";
switch (rc) {
case MMSYSERR_ALLOCATED: errstr = "MMSYSERR_ALLOCATED"; break;
case MMSYSERR_INVALHANDLE: errstr = "MMSYSERR_INVALHANDLE"; break;
case MMSYSERR_NODRIVER: errstr = "MMSYSERR_NODRIVER"; break;
case MMSYSERR_BADDEVICEID: errstr = "MMSYSERR_BADDEVICEID"; break;
case MMSYSERR_NOMEM: errstr = "MMSYSERR_NOMEM"; break;
case MMSYSERR_NOTSUPPORTED: errstr = "MMSYSERR_NOTSUPPORTED"; break;
case WAVERR_STILLPLAYING: errstr = "WAVERR_STILLPLAYING"; break;
case WAVERR_UNPREPARED: errstr = "WAVERR_UNPREPARED"; break;
case WAVERR_BADFORMAT: errstr = "WAVERR_BADFORMAT"; break;
case WAVERR_SYNC: errstr = "WAVERR_SYNC"; break;
}
d_print("%s returned error %s (%d)\n", name, errstr, rc);
}
static void clean_buffers(void)
{
int i;
/* mark used buffers clean */
for (i = 0; i < buffer_count; i++) {
WAVEHDR *hdr = &buffers[(buffer_idx + i) % buffer_count];
if (!(hdr->dwFlags & WHDR_DONE))
break;
buffers_free++;
waveOutUnprepareHeader(wave_out, hdr, sizeof(WAVEHDR));
hdr->dwFlags = 0;
}
}
static int waveout_open(sample_format_t sf, const channel_position_t *channel_map)
{
WAVEFORMATEX format = {
.cbSize = sizeof(format),
.wFormatTag = WAVE_FORMAT_PCM,
.nChannels = sf_get_channels(sf),
.nSamplesPerSec = sf_get_rate(sf),
.wBitsPerSample = sf_get_bits(sf),
.nAvgBytesPerSec = sf_get_second_size(sf),
.nBlockAlign = sf_get_frame_size(sf)
};
int rc, i;
/* WAVEFORMATEX does not support channels > 2, waveOutWrite() wants little endian signed PCM */
if (sf_get_bigendian(sf) || !sf_get_signed(sf) || sf_get_channels(sf) > 2) {
return -OP_ERROR_SAMPLE_FORMAT;
}
rc = waveOutOpen(&wave_out, WAVE_MAPPER, &format, 0, 0, CALLBACK_NULL);
if (rc != MMSYSERR_NOERROR) {
switch (rc) {
case MMSYSERR_ALLOCATED:
errno = EBUSY;
return -OP_ERROR_ERRNO;
case MMSYSERR_BADDEVICEID:
case MMSYSERR_NODRIVER:
errno = ENODEV;
return -OP_ERROR_ERRNO;
case MMSYSERR_NOMEM:
errno = ENOMEM;
return -OP_ERROR_ERRNO;
case WAVERR_BADFORMAT:
return -OP_ERROR_SAMPLE_FORMAT;
}
return -OP_ERROR_INTERNAL;
}
/* reset buffers */
for (i = 0; i < buffer_count; i++) {
buffers[i].dwFlags = 0;
}
buffer_idx = 0;
buffers_free = buffer_count;
waveout_sf = sf;
return 0;
}
static int waveout_close(void)
{
int rc;
waveOutReset(wave_out);
clean_buffers();
rc = waveOutClose(wave_out);
if (rc != MMSYSERR_NOERROR) {
waveout_error("waveOutClose", rc);
return -1;
}
wave_out = NULL;
return 0;
}
static int waveout_init(void)
{
WAVEHDR *hdr;
int i;
/* create buffers */
buffers = xnew(WAVEHDR, buffer_count);
for (i = 0; i < buffer_count; i++) {
hdr = &buffers[i];
memset(hdr, 0, sizeof(WAVEHDR));
hdr->lpData = xmalloc(buffer_size);
}
return 0;
}
static int waveout_exit(void)
{
int i;
for (i = 0; i < buffer_count; i++) {
free(buffers[i].lpData);
}
free(buffers);
buffers = NULL;
return 0;
}
static int waveout_write(const char *buffer, int count)
{
int written = 0;
int len, rc;
count = FRAME_SIZE_ALIGN(count);
clean_buffers();
while (count > 0) {
WAVEHDR *hdr = &buffers[buffer_idx];
if (hdr->dwFlags != 0) {
/* no free buffers */
break;
}
len = FRAME_SIZE_ALIGN(min(count, buffer_size));
hdr->dwBufferLength = len;
memcpy(hdr->lpData, buffer + written, len);
rc = waveOutPrepareHeader(wave_out, hdr, sizeof(WAVEHDR));
if (rc != MMSYSERR_NOERROR) {
waveout_error("waveOutPrepareHeader", rc);
break;
}
rc = waveOutWrite(wave_out, hdr, sizeof(WAVEHDR));
if (rc != MMSYSERR_NOERROR) {
waveOutUnprepareHeader(wave_out, hdr, sizeof(WAVEHDR));
hdr->dwFlags = 0;
waveout_error("waveOutWrite", rc);
break;
}
written += len;
count -= len;
buffer_idx = (buffer_idx + 1) % buffer_count;
buffers_free--;
}
return written;
}
static int waveout_pause(void)
{
if (waveOutPause(wave_out) != MMSYSERR_NOERROR)
return -1;
return 0;
}
static int waveout_unpause(void)
{
if (waveOutRestart(wave_out) != MMSYSERR_NOERROR)
return -1;
return 0;
}
static int waveout_buffer_space(void)
{
clean_buffers();
return buffers_free * FRAME_SIZE_ALIGN(buffer_size);
}
static int waveout_set_option(int key, const char *val)
{
long int ival;
int reinit = 0;
switch (key) {
case 0:
if (str_to_int(val, &ival) || ival < 4096 || ival > 65536) {
errno = EINVAL;
return -OP_ERROR_ERRNO;
}
if (buffers) {
waveout_exit();
reinit = 1;
}
buffer_size = ival;
break;
case 1:
if (str_to_int(val, &ival) || ival < 2 || ival > 64) {
errno = EINVAL;
return -OP_ERROR_ERRNO;
}
if (buffers) {
waveout_exit();
reinit = 1;
}
buffer_count = ival;
break;
default:
return -OP_ERROR_NOT_OPTION;
}
if (reinit) {
waveout_init();
}
return 0;
}
static int waveout_get_option(int key, char **val)
{
switch (key) {
case 0:
*val = xnew(char, 22);
snprintf(*val, 22, "%d", buffer_size);
break;
case 1:
*val = xnew(char, 22);
snprintf(*val, 22, "%d", buffer_count);
break;
default:
return -OP_ERROR_NOT_OPTION;
}
return 0;
}
const struct output_plugin_ops op_pcm_ops = {
.init = waveout_init,
.exit = waveout_exit,
.open = waveout_open,
.close = waveout_close,
.write = waveout_write,
.pause = waveout_pause,
.unpause = waveout_unpause,
.buffer_space = waveout_buffer_space,
.set_option = waveout_set_option,
.get_option = waveout_get_option
};
const char * const op_pcm_options[] = {
"buffer_size",
"buffer_count",
NULL
};
const int op_priority = 0;