Some documentation for settings view
[cmus.git] / waveout.c
blob3f31ea0d694d48101f2862d3d279909957c0dd3c
1 /*
2 * Copyright 2007 dnk <dnk@bjum.net>
3 * Based on oss.c
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation; either version 2 of the
8 * License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
18 * 02111-1307, USA.
21 #include "op.h"
22 #include "sf.h"
23 #include "utils.h"
24 #include "xmalloc.h"
25 #include "debug.h"
27 #define WIN32_LEAN_AND_MEAN
28 #include <windows.h>
29 #include <mmsystem.h>
31 static HWAVEOUT wave_out;
32 static sample_format_t waveout_sf;
33 static int buffer_size = 4096;
34 static int buffer_count = 12;
35 static WAVEHDR *buffers;
36 static int buffer_idx;
37 static int buffers_free;
39 #define FRAME_SIZE_ALIGN(x) \
40 (((x) / sf_get_frame_size(waveout_sf)) * sf_get_frame_size(waveout_sf))
42 static void waveout_error(const char *name, int rc)
44 const char *errstr = "UNKNOWN";
46 switch (rc) {
47 case MMSYSERR_ALLOCATED: errstr = "MMSYSERR_ALLOCATED"; break;
48 case MMSYSERR_INVALHANDLE: errstr = "MMSYSERR_INVALHANDLE"; break;
49 case MMSYSERR_NODRIVER: errstr = "MMSYSERR_NODRIVER"; break;
50 case MMSYSERR_BADDEVICEID: errstr = "MMSYSERR_BADDEVICEID"; break;
51 case MMSYSERR_NOMEM: errstr = "MMSYSERR_NOMEM"; break;
52 case MMSYSERR_NOTSUPPORTED: errstr = "MMSYSERR_NOTSUPPORTED"; break;
53 case WAVERR_STILLPLAYING: errstr = "WAVERR_STILLPLAYING"; break;
54 case WAVERR_UNPREPARED: errstr = "WAVERR_UNPREPARED"; break;
55 case WAVERR_BADFORMAT: errstr = "WAVERR_BADFORMAT"; break;
56 case WAVERR_SYNC: errstr = "WAVERR_SYNC"; break;
59 d_print("%s returned error %s (%d)\n", name, errstr, rc);
62 static void clean_buffers(void)
64 int i;
66 /* mark used buffers clean */
67 for (i = 0; i < buffer_count; i++) {
68 WAVEHDR *hdr = &buffers[(buffer_idx + i) % buffer_count];
70 if (!(hdr->dwFlags & WHDR_DONE))
71 break;
72 buffers_free++;
73 waveOutUnprepareHeader(wave_out, hdr, sizeof(WAVEHDR));
74 hdr->dwFlags = 0;
78 static int waveout_open(sample_format_t sf)
80 WAVEFORMATEX format;
81 int rc, i;
83 /* WAVEFORMATEX does not support channels > 2, waveOutWrite() wants little endian signed PCM */
84 if (sf_get_bigendian(sf) || !sf_get_signed(sf) || sf_get_channels(sf) > 2) {
85 return -OP_ERROR_SAMPLE_FORMAT;
88 memset(&format, 0, sizeof(format));
89 format.cbSize = sizeof(format);
90 format.wFormatTag = WAVE_FORMAT_PCM;
91 format.nChannels = sf_get_channels(sf);
92 format.nSamplesPerSec = sf_get_rate(sf);
93 format.wBitsPerSample = sf_get_bits(sf);
94 format.nAvgBytesPerSec = sf_get_second_size(sf);
95 format.nBlockAlign = sf_get_frame_size(sf);
97 if ((rc = waveOutOpen(&wave_out, WAVE_MAPPER, &format, 0, 0, CALLBACK_NULL)) != MMSYSERR_NOERROR) {
98 switch (rc) {
99 case MMSYSERR_ALLOCATED:
100 errno = EBUSY;
101 return -OP_ERROR_ERRNO;
102 case MMSYSERR_BADDEVICEID:
103 case MMSYSERR_NODRIVER:
104 errno = ENODEV;
105 return -OP_ERROR_ERRNO;
106 case MMSYSERR_NOMEM:
107 errno = ENOMEM;
108 return -OP_ERROR_ERRNO;
109 case WAVERR_BADFORMAT:
110 return -OP_ERROR_SAMPLE_FORMAT;
112 return -OP_ERROR_INTERNAL;
115 /* reset buffers */
116 for (i = 0; i < buffer_count; i++) {
117 buffers[i].dwFlags = 0;
119 buffer_idx = 0;
120 buffers_free = buffer_count;
122 waveout_sf = sf;
124 return 0;
127 static int waveout_close(void)
129 int rc;
131 waveOutReset(wave_out);
133 clean_buffers();
135 if ((rc = waveOutClose(wave_out)) != MMSYSERR_NOERROR) {
136 waveout_error("waveOutClose", rc);
137 return -1;
139 wave_out = NULL;
141 return 0;
144 static int waveout_init(void)
146 WAVEHDR *hdr;
147 int i;
149 /* create buffers */
150 buffers = xnew(WAVEHDR, buffer_count);
151 for (i = 0; i < buffer_count; i++) {
152 hdr = &buffers[i];
154 memset(hdr, 0, sizeof(WAVEHDR));
155 hdr->lpData = xmalloc(buffer_size);
157 return 0;
160 static int waveout_exit(void)
162 int i;
164 for (i = 0; i < buffer_count; i++) {
165 free(buffers[i].lpData);
167 free(buffers);
168 buffers = NULL;
170 return 0;
173 static int waveout_write(const char *buffer, int count)
175 int written = 0;
176 int len, rc;
178 count = FRAME_SIZE_ALIGN(count);
180 clean_buffers();
182 while (count > 0) {
183 WAVEHDR *hdr = &buffers[buffer_idx];
185 if (hdr->dwFlags != 0) {
186 /* no free buffers */
187 break;
190 len = FRAME_SIZE_ALIGN( min(count, buffer_size) );
191 hdr->dwBufferLength = len;
192 memcpy(hdr->lpData, buffer + written, len);
194 if ((rc = waveOutPrepareHeader(wave_out, hdr, sizeof(WAVEHDR))) != MMSYSERR_NOERROR) {
195 waveout_error("waveOutPrepareHeader", rc);
196 break;
199 if ((rc = waveOutWrite(wave_out, hdr, sizeof(WAVEHDR))) != MMSYSERR_NOERROR) {
200 waveOutUnprepareHeader(wave_out, hdr, sizeof(WAVEHDR));
201 hdr->dwFlags = 0;
202 waveout_error("waveOutWrite", rc);
203 break;
206 written += len;
207 count -= len;
208 buffer_idx = (buffer_idx + 1) % buffer_count;
209 buffers_free--;
212 return written;
215 static int waveout_pause(void)
217 if (waveOutPause(wave_out) != MMSYSERR_NOERROR)
218 return -1;
219 return 0;
222 static int waveout_unpause(void)
224 if (waveOutRestart(wave_out) != MMSYSERR_NOERROR)
225 return -1;
226 return 0;
229 static int waveout_buffer_space(void)
231 clean_buffers();
232 if (buffers_free == 0)
233 return -1;
234 return buffers_free * FRAME_SIZE_ALIGN(buffer_size);
237 static int waveout_set_option(int key, const char *val)
239 long int ival;
240 int reinit = 0;
242 switch (key) {
243 case 0:
244 if (str_to_int(val, &ival) || ival < 4096 || ival > 65536) {
245 errno = EINVAL;
246 return -OP_ERROR_ERRNO;
248 if (buffers) {
249 waveout_exit();
250 reinit = 1;
252 buffer_size = ival;
253 break;
254 case 1:
255 if (str_to_int(val, &ival) || ival < 2 || ival > 64) {
256 errno = EINVAL;
257 return -OP_ERROR_ERRNO;
259 if (buffers) {
260 waveout_exit();
261 reinit = 1;
263 buffer_count = ival;
264 break;
265 default:
266 return -OP_ERROR_NOT_OPTION;
269 if (reinit) {
270 waveout_init();
273 return 0;
276 static int waveout_get_option(int key, char **val)
278 switch (key) {
279 case 0:
280 *val = xnew(char, 22);
281 snprintf(*val, 22, "%d", buffer_size);
282 break;
283 case 1:
284 *val = xnew(char, 22);
285 snprintf(*val, 22, "%d", buffer_count);
286 break;
287 default:
288 return -OP_ERROR_NOT_OPTION;
290 return 0;
293 const struct output_plugin_ops op_pcm_ops = {
294 .init = waveout_init,
295 .exit = waveout_exit,
296 .open = waveout_open,
297 .close = waveout_close,
298 .write = waveout_write,
299 .pause = waveout_pause,
300 .unpause = waveout_unpause,
301 .buffer_space = waveout_buffer_space,
302 .set_option = waveout_set_option,
303 .get_option = waveout_get_option
306 const char * const op_pcm_options[] = {
307 "buffer_size",
308 "buffer_count",
309 NULL
312 const int op_priority = 0;