1 /*
2  * Copyright 2008-2013 Various Authors
3  * Copyright 2007 dnk <dnk@bjum.net>
4  *
5  * Based on oss.c
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License as
9  * published by the Free Software Foundation; either version 2 of the
10  * License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, see <http://www.gnu.org/licenses/>.
19  */
20 
21 #include "../op.h"
22 #include "../sf.h"
23 #include "../utils.h"
24 #include "../xmalloc.h"
25 #include "../debug.h"
26 
27 #define WIN32_LEAN_AND_MEAN
28 #include <windows.h>
29 #include <mmsystem.h>
30 #include <stdio.h>
31 
32 static HWAVEOUT wave_out;
33 static sample_format_t waveout_sf;
34 static int buffer_size = 4096;
35 static int buffer_count = 12;
36 static WAVEHDR *buffers;
37 static int buffer_idx;
38 static int buffers_free;
39 
40 #define FRAME_SIZE_ALIGN(x) \
41 	(((x) / sf_get_frame_size(waveout_sf)) * sf_get_frame_size(waveout_sf))
42 
waveout_error(const char * name,int rc)43 static void waveout_error(const char *name, int rc)
44 {
45 	const char *errstr = "UNKNOWN";
46 
47 	switch (rc) {
48 	case MMSYSERR_ALLOCATED:	errstr = "MMSYSERR_ALLOCATED"; break;
49 	case MMSYSERR_INVALHANDLE:	errstr = "MMSYSERR_INVALHANDLE"; break;
50 	case MMSYSERR_NODRIVER:		errstr = "MMSYSERR_NODRIVER"; break;
51 	case MMSYSERR_BADDEVICEID:	errstr = "MMSYSERR_BADDEVICEID"; break;
52 	case MMSYSERR_NOMEM:		errstr = "MMSYSERR_NOMEM"; break;
53 	case MMSYSERR_NOTSUPPORTED:	errstr = "MMSYSERR_NOTSUPPORTED"; break;
54 	case WAVERR_STILLPLAYING:	errstr = "WAVERR_STILLPLAYING"; break;
55 	case WAVERR_UNPREPARED:		errstr = "WAVERR_UNPREPARED"; break;
56 	case WAVERR_BADFORMAT:		errstr = "WAVERR_BADFORMAT"; break;
57 	case WAVERR_SYNC:		errstr = "WAVERR_SYNC"; break;
58 	}
59 
60 	d_print("%s returned error %s (%d)\n", name, errstr, rc);
61 }
62 
clean_buffers(void)63 static void clean_buffers(void)
64 {
65 	int i;
66 
67 	/* mark used buffers clean */
68 	for (i = 0; i < buffer_count; i++) {
69 		WAVEHDR *hdr = &buffers[(buffer_idx + i) % buffer_count];
70 
71 		if (!(hdr->dwFlags & WHDR_DONE))
72 			break;
73 		buffers_free++;
74 		waveOutUnprepareHeader(wave_out, hdr, sizeof(WAVEHDR));
75 		hdr->dwFlags = 0;
76 	}
77 }
78 
waveout_open(sample_format_t sf,const channel_position_t * channel_map)79 static int waveout_open(sample_format_t sf, const channel_position_t *channel_map)
80 {
81 	WAVEFORMATEX format = {
82 		.cbSize          = sizeof(format),
83 		.wFormatTag      = WAVE_FORMAT_PCM,
84 		.nChannels       = sf_get_channels(sf),
85 		.nSamplesPerSec  = sf_get_rate(sf),
86 		.wBitsPerSample  = sf_get_bits(sf),
87 		.nAvgBytesPerSec = sf_get_second_size(sf),
88 		.nBlockAlign     = sf_get_frame_size(sf)
89 	};
90 	int rc, i;
91 
92 	/* WAVEFORMATEX does not support channels > 2, waveOutWrite() wants little endian signed PCM */
93 	if (sf_get_bigendian(sf) || !sf_get_signed(sf) || sf_get_channels(sf) > 2) {
94 		return -OP_ERROR_SAMPLE_FORMAT;
95 	}
96 
97 	rc = waveOutOpen(&wave_out, WAVE_MAPPER, &format, 0, 0, CALLBACK_NULL);
98 	if (rc != MMSYSERR_NOERROR) {
99 		switch (rc) {
100 		case MMSYSERR_ALLOCATED:
101 			errno = EBUSY;
102 			return -OP_ERROR_ERRNO;
103 		case MMSYSERR_BADDEVICEID:
104 		case MMSYSERR_NODRIVER:
105 			errno = ENODEV;
106 			return -OP_ERROR_ERRNO;
107 		case MMSYSERR_NOMEM:
108 			errno = ENOMEM;
109 			return -OP_ERROR_ERRNO;
110 		case WAVERR_BADFORMAT:
111 			return -OP_ERROR_SAMPLE_FORMAT;
112 		}
113 		return -OP_ERROR_INTERNAL;
114 	}
115 
116 	/* reset buffers */
117 	for (i = 0; i < buffer_count; i++) {
118 		buffers[i].dwFlags = 0;
119 	}
120 	buffer_idx = 0;
121 	buffers_free = buffer_count;
122 
123 	waveout_sf = sf;
124 
125 	return 0;
126 }
127 
waveout_close(void)128 static int waveout_close(void)
129 {
130 	int rc;
131 
132 	waveOutReset(wave_out);
133 
134 	clean_buffers();
135 
136 	rc = waveOutClose(wave_out);
137 	if (rc != MMSYSERR_NOERROR) {
138 		waveout_error("waveOutClose", rc);
139 		return -1;
140 	}
141 	wave_out = NULL;
142 
143 	return 0;
144 }
145 
waveout_init(void)146 static int waveout_init(void)
147 {
148 	WAVEHDR *hdr;
149 	int i;
150 
151 	/* create buffers */
152 	buffers = xnew(WAVEHDR, buffer_count);
153 	for (i = 0; i < buffer_count; i++) {
154 		hdr = &buffers[i];
155 
156 		memset(hdr, 0, sizeof(WAVEHDR));
157 		hdr->lpData = xmalloc(buffer_size);
158 	}
159 	return 0;
160 }
161 
waveout_exit(void)162 static int waveout_exit(void)
163 {
164 	int i;
165 
166 	for (i = 0; i < buffer_count; i++) {
167 		free(buffers[i].lpData);
168 	}
169 	free(buffers);
170 	buffers = NULL;
171 
172 	return 0;
173 }
174 
waveout_write(const char * buffer,int count)175 static int waveout_write(const char *buffer, int count)
176 {
177 	int written = 0;
178 	int len, rc;
179 
180 	count = FRAME_SIZE_ALIGN(count);
181 
182 	clean_buffers();
183 
184 	while (count > 0) {
185 		WAVEHDR *hdr = &buffers[buffer_idx];
186 
187 		if (hdr->dwFlags != 0) {
188 			/* no free buffers */
189 			break;
190 		}
191 
192 		len = FRAME_SIZE_ALIGN(min_i(count, buffer_size));
193 		hdr->dwBufferLength = len;
194 		memcpy(hdr->lpData, buffer + written, len);
195 
196 		rc = waveOutPrepareHeader(wave_out, hdr, sizeof(WAVEHDR));
197 		if (rc != MMSYSERR_NOERROR) {
198 			waveout_error("waveOutPrepareHeader", rc);
199 			break;
200 		}
201 
202 		rc = waveOutWrite(wave_out, hdr, sizeof(WAVEHDR));
203 		if (rc != MMSYSERR_NOERROR) {
204 			waveOutUnprepareHeader(wave_out, hdr, sizeof(WAVEHDR));
205 			hdr->dwFlags = 0;
206 			waveout_error("waveOutWrite", rc);
207 			break;
208 		}
209 
210 		written += len;
211 		count -= len;
212 		buffer_idx = (buffer_idx + 1) % buffer_count;
213 		buffers_free--;
214 	}
215 
216 	return written;
217 }
218 
waveout_pause(void)219 static int waveout_pause(void)
220 {
221 	if (waveOutPause(wave_out) != MMSYSERR_NOERROR)
222 		return -1;
223 	return 0;
224 }
225 
waveout_unpause(void)226 static int waveout_unpause(void)
227 {
228 	if (waveOutRestart(wave_out) != MMSYSERR_NOERROR)
229 		return -1;
230 	return 0;
231 }
232 
waveout_buffer_space(void)233 static int waveout_buffer_space(void)
234 {
235 	clean_buffers();
236 
237 	return buffers_free * FRAME_SIZE_ALIGN(buffer_size);
238 }
239 
waveout_set_buffer_common(long int val,long int * dst)240 static int waveout_set_buffer_common(long int val, long int *dst)
241 {
242 	int reinit = 0;
243 
244 	if (buffers) {
245 		waveout_exit();
246 		reinit = 1;
247 	}
248 	*dst = val;
249 
250 	if (reinit) {
251 		waveout_init();
252 	}
253 
254 	return 0;
255 }
256 
waveout_set_buffer_size(const char * val)257 static int waveout_set_buffer_size(const char *val)
258 {
259 	long int ival;
260 	if (str_to_int(val, &ival) || ival < 4096 || ival > 65536) {
261 		errno = EINVAL;
262 		return -OP_ERROR_ERRNO;
263 	}
264 	return waveout_set_buffer_common(ival, &buffer_size);
265 }
266 
waveout_set_buffer_count(const char * val)267 static int waveout_set_buffer_count(const char *val)
268 {
269 	long int ival;
270 	if (str_to_int(val, &ival) || ival < 2 || ival > 64) {
271 		errno = EINVAL;
272 		return -OP_ERROR_ERRNO;
273 	}
274 	return waveout_set_buffer_common(ival, &buffer_count);
275 }
276 
waveout_get_buffer_size(char ** val)277 static int waveout_get_buffer_size(char **val)
278 {
279 	*val = xnew(char, 22);
280 	snprintf(*val, 22, "%d", buffer_size);
281 	return 0;
282 }
283 
waveout_get_buffer_count(char ** val)284 static int waveout_get_buffer_count(char **val)
285 {
286 	*val = xnew(char, 22);
287 	snprintf(*val, 22, "%d", buffer_count);
288 	return 0;
289 }
290 
291 const struct output_plugin_ops op_pcm_ops = {
292 	.init = waveout_init,
293 	.exit = waveout_exit,
294 	.open = waveout_open,
295 	.close = waveout_close,
296 	.write = waveout_write,
297 	.pause = waveout_pause,
298 	.unpause = waveout_unpause,
299 	.buffer_space = waveout_buffer_space,
300 };
301 
302 const struct output_plugin_opt op_pcm_options[] = {
303 	OPT(waveout, buffer_size),
304 	OPT(waveout, buffer_count),
305 	{ NULL },
306 };
307 
308 const int op_priority = 0;
309 const unsigned op_abi_version = OP_ABI_VERSION;
310