File: pcm_windows.c

package info (click to toggle)
brltty 6.8-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 35,776 kB
  • sloc: ansic: 150,447; java: 13,484; sh: 9,667; xml: 5,702; tcl: 2,634; makefile: 2,328; awk: 713; lisp: 366; python: 321; ml: 301
file content (293 lines) | stat: -rw-r--r-- 7,798 bytes parent folder | download | duplicates (2)
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
/*
 * BRLTTY - A background process providing access to the console screen (when in
 *          text mode) for a blind person using a refreshable braille display.
 *
 * Copyright (C) 1995-2025 by The BRLTTY Developers.
 *
 * BRLTTY comes with ABSOLUTELY NO WARRANTY.
 *
 * This is free software, placed under the terms of the
 * GNU Lesser General Public License, as published by the Free Software
 * Foundation; either version 2.1 of the License, or (at your option) any
 * later version. Please see the file LICENSE-LGPL for details.
 *
 * Web Page: http://brltty.app/
 *
 * This software is maintained by Dave Mielke <dave@mielke.cc>.
 */

#include "prologue.h"

#include "log.h"
#include "parse.h"
#include "pcm.h"

struct PcmDeviceStruct {
  HWAVEOUT handle;
  UINT deviceID;
  WAVEFORMATEX format;
  HANDLE done;
  WAVEHDR waveHdr;
  size_t bufSize;
};

static WAVEFORMATEX defaultFormat = { WAVE_FORMAT_PCM, 1, 11025, 11025, 1, 8, 0 };

static void
recomputeWaveOutFormat(WAVEFORMATEX *format) {
  format->nBlockAlign = format->nChannels * ((format->wBitsPerSample + 7) / 8);
  format->nAvgBytesPerSec = format->nBlockAlign * format->nSamplesPerSec;
}

static WAVEHDR initWaveHdr = { NULL, 0, 0, 0, 0, 1, NULL, 0 };

static void
LogWaveOutError(MMRESULT error, int errorLevel, const char *action) {
  char msg[MAXERRORLENGTH];
  waveOutGetErrorText(error, msg, sizeof(msg));
  logMessage(errorLevel, "%s error %d: %s.", action, error, msg);
}

PcmDevice *
openPcmDevice (int errorLevel, const char *device) {
  PcmDevice *pcm;
  MMRESULT mmres;
  WAVEOUTCAPS caps;
  int id = 0;

  if (*device) {
    if (!isInteger(&id, device) || (id < 0) || (id >= waveOutGetNumDevs())) {
      logMessage(errorLevel, "invalid PCM device number: %s", device);
      return NULL;
    }
  }

  if (!(pcm = malloc(sizeof(*pcm)))) {
    logSystemError("PCM device allocation");
    return NULL;
  }
  pcm->deviceID = id;

  if ((waveOutGetDevCaps(pcm->deviceID, &caps, sizeof(caps))) != MMSYSERR_NOERROR)
    pcm->format = defaultFormat;
  else {
    logMessage(errorLevel, "PCM device %d is %s", pcm->deviceID, caps.szPname);
    pcm->format.wFormatTag = WAVE_FORMAT_PCM;
    if (caps.dwFormats & 
	(WAVE_FORMAT_1S08
	|WAVE_FORMAT_1S16
	|WAVE_FORMAT_2S08
	|WAVE_FORMAT_2S16
	|WAVE_FORMAT_4S08
	|WAVE_FORMAT_4S16))
      pcm->format.nChannels = 2;
    else
      pcm->format.nChannels = 1;
    if (caps.dwFormats &
	(WAVE_FORMAT_4M08
	|WAVE_FORMAT_4M16
	|WAVE_FORMAT_4S08
	|WAVE_FORMAT_4S16))
      pcm->format.nSamplesPerSec = 44100;
    else if (caps.dwFormats &
	(WAVE_FORMAT_2M08
	|WAVE_FORMAT_2M16
	|WAVE_FORMAT_2S08
	|WAVE_FORMAT_2S16))
      pcm->format.nSamplesPerSec = 22050;
    else if (caps.dwFormats &
	(WAVE_FORMAT_1M08
	|WAVE_FORMAT_1M16
	|WAVE_FORMAT_1S08
	|WAVE_FORMAT_1S16))
      pcm->format.nSamplesPerSec = 11025;
    else {
      logMessage(errorLevel, "unknown PCM capability %#lx", caps.dwFormats);
      goto out;
    }
    if (caps.dwFormats &
	(WAVE_FORMAT_1M16
	|WAVE_FORMAT_1S16
	|WAVE_FORMAT_2M16
	|WAVE_FORMAT_2S16
	|WAVE_FORMAT_4M16
	|WAVE_FORMAT_4S16))
      pcm->format.wBitsPerSample = 16;
    else if (caps.dwFormats &
	(WAVE_FORMAT_1M08
	|WAVE_FORMAT_1S08
	|WAVE_FORMAT_2M08
	|WAVE_FORMAT_2S08
	|WAVE_FORMAT_4M08
	|WAVE_FORMAT_4S08))
      pcm->format.wBitsPerSample = 8;
    else {
      logMessage(LOG_ERR, "unknown PCM capability %#lx", caps.dwFormats);
      goto out;
    }
    recomputeWaveOutFormat(&pcm->format);
    pcm->format.cbSize = 0;
  }

  if (!(pcm->done = CreateEvent(NULL, FALSE, TRUE, NULL))) {
    logWindowsSystemError("creating PCM completion event");
    goto out;
  }

  pcm->waveHdr = initWaveHdr;
  pcm->bufSize = 0;

  if ((mmres = waveOutOpen(&pcm->handle, pcm->deviceID,
	  &pcm->format, (DWORD) pcm->done, 0, CALLBACK_EVENT)) != MMSYSERR_NOERROR) {
    LogWaveOutError(mmres, errorLevel, "opening PCM device");
    goto outEvent;
  }
  return pcm;

outEvent:
  CloseHandle(pcm->done);
out:
  free(pcm);
  return NULL;
}

static int
unprepareHeader(PcmDevice *pcm) {
  MMRESULT mmres;
  awaitPcmOutput(pcm);
  if ((mmres = waveOutUnprepareHeader(pcm->handle, &pcm->waveHdr, sizeof(pcm->waveHdr))) != MMSYSERR_NOERROR) {
    LogWaveOutError(mmres, LOG_ERR, "unpreparing PCM data header");
    return 0;
  }
  return 1;
}

static int
updateWaveOutFormat(PcmDevice *pcm, WAVEFORMATEX *format, const char *errmsg) {
  MMRESULT mmres;
  recomputeWaveOutFormat(format);
  if (!(unprepareHeader(pcm))) return 0;
  if (waveOutOpen(NULL, pcm->deviceID, format, 0, 0, WAVE_FORMAT_QUERY) == MMSYSERR_NOERROR) {
    waveOutClose(pcm->handle);
    pcm->handle = INVALID_HANDLE_VALUE;
    if ((mmres = waveOutOpen(&pcm->handle, pcm->deviceID, format,
	    (DWORD) pcm->done, 0, CALLBACK_EVENT)) == MMSYSERR_NOERROR) {
      pcm->format = *format;
      return 1;
    }
    LogWaveOutError(mmres, LOG_ERR, errmsg);
  }
  return 0;
}

void
closePcmDevice (PcmDevice *pcm) {
  CloseHandle(pcm->done);
  unprepareHeader(pcm);
  waveOutClose(pcm->handle);
  free(pcm->waveHdr.lpData);
  free(pcm);
}

int
writePcmData (PcmDevice *pcm, const unsigned char *buffer, int count) {
  MMRESULT mmres;
  void *newBuf;
  if (!count) return 1;
  if (count > pcm->bufSize) {
    if (!(unprepareHeader(pcm))) return 0;
    if (!(newBuf = realloc(pcm->waveHdr.lpData, 2 * count))) {
      logSystemError("allocating PCM data buffer");
      return 0;
    }
    pcm->waveHdr.lpData = newBuf;
    pcm->waveHdr.dwFlags = 0;
    pcm->waveHdr.dwBufferLength = pcm->bufSize = 2 * count;
  }
  awaitPcmOutput(pcm);
  if (!(pcm->waveHdr.dwFlags & WHDR_PREPARED))
    if ((mmres = waveOutPrepareHeader(pcm->handle, &pcm->waveHdr, sizeof(pcm->waveHdr))) != MMSYSERR_NOERROR) {
      LogWaveOutError(mmres, LOG_ERR, "preparing PCM data header");
      return 0;
    }
  pcm->waveHdr.dwBufferLength = count;
  memcpy(pcm->waveHdr.lpData, buffer, count);
  ResetEvent(pcm->done);
  if ((mmres = waveOutWrite(pcm->handle, &pcm->waveHdr, sizeof(pcm->waveHdr))) != MMSYSERR_NOERROR) {
    SetEvent(pcm->done);
    LogWaveOutError(mmres, LOG_ERR, "writing PCM data");
    return 0;
  }
  return 1;
}

int
getPcmBlockSize (PcmDevice *pcm) {
  return 0X10000;
}

int
getPcmSampleRate (PcmDevice *pcm) {
  return pcm->format.nSamplesPerSec;
}

int
setPcmSampleRate (PcmDevice *pcm, int rate) {
  WAVEFORMATEX format = pcm->format;
  format.nSamplesPerSec = rate;
  if (!updateWaveOutFormat(pcm, &format, "setting PCM sample rate"))
    return getPcmSampleRate(pcm);
  else
    return rate;
}

int
getPcmChannelCount (PcmDevice *pcm) {
  return pcm->format.nChannels;
}

int
setPcmChannelCount (PcmDevice *pcm, int channels) {
  WAVEFORMATEX format = pcm->format;
  format.nChannels = channels;
  if (!updateWaveOutFormat(pcm, &format, "setting PCM channel count"))
    return getPcmChannelCount(pcm);
  else
    return channels;
}

PcmAmplitudeFormat
getPcmAmplitudeFormat (PcmDevice *pcm) {
  if (pcm->format.wBitsPerSample == 8) return PCM_FMT_U8;
  if (pcm->format.wBitsPerSample == 16) return PCM_FMT_S16L;
  return PCM_FMT_UNKNOWN;
}

PcmAmplitudeFormat
setPcmAmplitudeFormat (PcmDevice *pcm, PcmAmplitudeFormat format) {
  WAVEFORMATEX newFormat = pcm->format;
  if (format == PCM_FMT_U8) newFormat.wBitsPerSample = 8;
  else if (format == PCM_FMT_S16L) newFormat.wBitsPerSample = 16;
  else return getPcmAmplitudeFormat(pcm);
  if (!updateWaveOutFormat(pcm, &newFormat, "setting PCM amplitude format"))
    return getPcmAmplitudeFormat(pcm);
  else
    return format;
}

void
pushPcmOutput (PcmDevice *pcm) {
}

void
awaitPcmOutput (PcmDevice *pcm) {
  while ((pcm->waveHdr.dwFlags & WHDR_PREPARED)
		  && !(pcm->waveHdr.dwFlags & WHDR_DONE))
    WaitForSingleObject(pcm->done, INFINITE);
  SetEvent(pcm->done);
}

void
cancelPcmOutput (PcmDevice *pcm) {
  waveOutReset(pcm->handle);
}