| 1 | /* BoCA - BonkEnc Component Architecture |
| 2 | * Copyright (C) 2007-2017 Robert Kausch <[email protected]> |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU General Public License as |
| 6 | * published by the Free Software Foundation, either version 2 of |
| 7 | * the License, or (at your option) any later version. |
| 8 | * |
| 9 | * THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR |
| 10 | * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED |
| 11 | * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. */ |
| 12 | |
| 13 | #include "waveout.h" |
| 14 | |
| 15 | #include <mmreg.h> |
| 16 | |
| 17 | const String &BoCA::OutputWaveOut::GetComponentSpecs() |
| 18 | { |
| 19 | static String componentSpecs = " \ |
| 20 | \ |
| 21 | <?xml version=\"1.0\" encoding=\"UTF-8\"?> \ |
| 22 | <component> \ |
| 23 | <name>WaveOut Output Plugin</name> \ |
| 24 | <version>1.0</version> \ |
| 25 | <id>waveout-out</id> \ |
| 26 | <type>output</type> \ |
| 27 | <input bits=\"8\" signed=\"false\"/> \ |
| 28 | <input bits=\"16-32\"/> \ |
| 29 | <input float=\"true\"/> \ |
| 30 | </component> \ |
| 31 | \ |
| 32 | "; |
| 33 | |
| 34 | return componentSpecs; |
| 35 | } |
| 36 | |
| 37 | BoCA::OutputWaveOut::OutputWaveOut() |
| 38 | { |
| 39 | hWaveOut = NIL; |
| 40 | hEvent = NIL; |
| 41 | |
| 42 | write_ptr = 0; |
| 43 | p_time = 0; |
| 44 | |
| 45 | paused = False; |
| 46 | needplay = False; |
| 47 | stop = False; |
| 48 | newpause = False; |
| 49 | |
| 50 | buffer = NIL; |
| 51 | |
| 52 | buf_size = 0; |
| 53 | buf_size_used = 0; |
| 54 | |
| 55 | maxblock = 0; |
| 56 | minblock = 0; |
| 57 | avgblock = 0; |
| 58 | |
| 59 | n_playing = 0; |
| 60 | data_written = 0; |
| 61 | last_time = 0; |
| 62 | |
| 63 | thread = NIL; |
| 64 | |
| 65 | memset(&sync, 0, sizeof(sync)); |
| 66 | } |
| 67 | |
| 68 | BoCA::OutputWaveOut::~OutputWaveOut() |
| 69 | { |
| 70 | } |
| 71 | |
| 72 | Bool BoCA::OutputWaveOut::Activate() |
| 73 | { |
| 74 | const Format &format = track.GetFormat(); |
| 75 | |
| 76 | hWaveOut = NIL; |
| 77 | |
| 78 | write_ptr = 0; |
| 79 | p_time = 0; |
| 80 | |
| 81 | paused = False; |
| 82 | needplay = False; |
| 83 | stop = False; |
| 84 | newpause = False; |
| 85 | |
| 86 | InitializeCriticalSection(&sync); |
| 87 | |
| 88 | /* Init wave format descriptor. |
| 89 | */ |
| 90 | WAVEFORMATEXTENSIBLE wfx; |
| 91 | |
| 92 | ZeroMemory(&wfx, sizeof(WAVEFORMATEXTENSIBLE)); |
| 93 | |
| 94 | wfx.Format.nSamplesPerSec = format.rate; |
| 95 | wfx.Format.wBitsPerSample = format.bits; |
| 96 | wfx.Format.nChannels = format.channels; |
| 97 | |
| 98 | wfx.Format.cbSize = sizeof(WAVEFORMATEXTENSIBLE) - sizeof(WAVEFORMATEX); |
| 99 | wfx.Format.wFormatTag = WAVE_FORMAT_EXTENSIBLE; |
| 100 | wfx.Format.nBlockAlign = (wfx.Format.wBitsPerSample >> 3) * wfx.Format.nChannels; |
| 101 | wfx.Format.nAvgBytesPerSec = wfx.Format.nBlockAlign * wfx.Format.nSamplesPerSec; |
| 102 | |
| 103 | wfx.SubFormat = format.fp ? KSDATAFORMAT_SUBTYPE_IEEE_FLOAT : KSDATAFORMAT_SUBTYPE_PCM; |
| 104 | |
| 105 | wfx.Samples.wValidBitsPerSample = wfx.Format.wBitsPerSample; |
| 106 | |
| 107 | if (format.channels == 1) wfx.dwChannelMask = SPEAKER_FRONT_CENTER; |
| 108 | else if (format.channels == 2) wfx.dwChannelMask = SPEAKER_FRONT_LEFT | SPEAKER_FRONT_RIGHT; |
| 109 | else if (format.channels == 3) wfx.dwChannelMask = SPEAKER_FRONT_LEFT | SPEAKER_FRONT_RIGHT | SPEAKER_FRONT_CENTER; |
| 110 | else if (format.channels == 4) wfx.dwChannelMask = SPEAKER_FRONT_LEFT | SPEAKER_FRONT_RIGHT | SPEAKER_BACK_LEFT | SPEAKER_BACK_RIGHT; |
| 111 | else if (format.channels == 5) wfx.dwChannelMask = SPEAKER_FRONT_LEFT | SPEAKER_FRONT_RIGHT | SPEAKER_FRONT_CENTER | SPEAKER_BACK_LEFT | SPEAKER_BACK_RIGHT; |
| 112 | else if (format.channels == 6) wfx.dwChannelMask = SPEAKER_FRONT_LEFT | SPEAKER_FRONT_RIGHT | SPEAKER_FRONT_CENTER | SPEAKER_LOW_FREQUENCY | SPEAKER_BACK_LEFT | SPEAKER_BACK_RIGHT; |
| 113 | else if (format.channels == 7) wfx.dwChannelMask = SPEAKER_FRONT_LEFT | SPEAKER_FRONT_RIGHT | SPEAKER_FRONT_CENTER | SPEAKER_LOW_FREQUENCY | SPEAKER_BACK_CENTER | SPEAKER_SIDE_LEFT | SPEAKER_SIDE_RIGHT; |
| 114 | else if (format.channels == 8) wfx.dwChannelMask = SPEAKER_FRONT_LEFT | SPEAKER_FRONT_RIGHT | SPEAKER_FRONT_CENTER | SPEAKER_LOW_FREQUENCY | SPEAKER_BACK_LEFT | SPEAKER_BACK_RIGHT | SPEAKER_SIDE_LEFT | SPEAKER_SIDE_RIGHT; |
| 115 | |
| 116 | /* Open audio device. |
| 117 | */ |
| 118 | hEvent = CreateEvent(0, 0, 0, 0); |
| 119 | |
| 120 | MMRESULT mr = waveOutOpen(&hWaveOut, WAVE_MAPPER, (WAVEFORMATEX *) &wfx, (DWORD) (Int64) hEvent, 0, CALLBACK_EVENT); |
| 121 | |
| 122 | if (mr) |
| 123 | { |
| 124 | String e = String("Unknown MMSYSTEM error."); |
| 125 | String e2 = String("Unknown error."); |
| 126 | |
| 127 | wchar_t waveOutError[MAXERRORLENGTH]; |
| 128 | |
| 129 | if (waveOutGetErrorText(mr, waveOutError, MAXERRORLENGTH) == MMSYSERR_NOERROR) e = waveOutError; |
| 130 | |
| 131 | switch (mr) |
| 132 | { |
| 133 | case 32: |
| 134 | /* fixme: some broken drivers blow mmsystem032 for no reason, with "standard" 44khz/16bps/stereo, need better error message when pcm format isn't weird |
| 135 | */ |
| 136 | e2 = String("Unsupported PCM format (").Append(String::FromInt(format.rate)).Append(" Hz, ").Append(String::FromInt(format.bits)).Append(" bits per sample, ").Append(String::FromInt(format.channels)).Append(" channels). Please change format settings in input plug-in configuration or change output device in waveOut plug-in configuration."); |
| 137 | |
| 138 | break; |
| 139 | case 4: |
| 140 | e2 = String("Another program is using your soundcard. Please change output device (in waveOut plug-in configuration), switch to DirectSound output (in Winamp preferences / plug-ins / output) or get a soundcard which can play multiple streams."); |
| 141 | |
| 142 | break; |
| 143 | case 2: |
| 144 | e2 = String("No sound devices found. Please buy a soundcard first (or install proper drivers if you think that you have one).\nMore info can be found at http://www.geocities.com/mmsystem002/"); |
| 145 | |
| 146 | break; |
| 147 | case 20: |
| 148 | e2 = String("Internal driver error; please reboot your computer."); |
| 149 | |
| 150 | break; |
| 151 | case 7: |
| 152 | e2 = String("Please reinstall soundcard drivers."); |
| 153 | |
| 154 | break; |
| 155 | case 8: |
| 156 | // fixme |
| 157 | |
| 158 | break; |
| 159 | } |
| 160 | |
| 161 | errorString = String(e2).Append("\n\nError code: ").Append(String::FromInt(mr)).Append("\nWindows error message: \n\"").Append(e).Append("\""); |
| 162 | errorState = True; |
| 163 | |
| 164 | return False; |
| 165 | } |
| 166 | |
| 167 | buf_size = MulDiv(2000, wfx.Format.nAvgBytesPerSec, 1000); |
| 168 | |
| 169 | maxblock = 0x10000; |
| 170 | minblock = 0x100; |
| 171 | avgblock = buf_size >> 4; |
| 172 | |
| 173 | if (maxblock > buf_size >> 2) maxblock = buf_size >> 2; |
| 174 | if (avgblock > maxblock) avgblock = maxblock; |
| 175 | if (maxblock < minblock) maxblock = minblock; |
| 176 | if (avgblock < minblock) avgblock = minblock; |
| 177 | |
| 178 | buffer = (char *) LocalAlloc(LMEM_FIXED | LMEM_ZEROINIT, buf_size + maxblock); // extra space at the end of the buffer |
| 179 | |
| 180 | n_playing = 0; |
| 181 | data_written = 0; |
| 182 | buf_size_used = 0; |
| 183 | last_time = 0; |
| 184 | |
| 185 | waveOutRestart(hWaveOut); |
| 186 | |
| 187 | /* Start worker thread. |
| 188 | */ |
| 189 | thread = NonBlocking0<>(&OutputWaveOut::WorkerThread, this).Call(); |
| 190 | |
| 191 | return True; |
| 192 | } |
| 193 | |
| 194 | Bool BoCA::OutputWaveOut::Deactivate() |
| 195 | { |
| 196 | EnterCriticalSection(&sync); |
| 197 | |
| 198 | stop = True; |
| 199 | |
| 200 | SetEvent(hEvent); |
| 201 | |
| 202 | LeaveCriticalSection(&sync); |
| 203 | |
| 204 | /* Wait for worker thread to exit. |
| 205 | */ |
| 206 | thread->Wait(); |
| 207 | |
| 208 | if (hEvent) CloseHandle(hEvent); |
| 209 | |
| 210 | if (hWaveOut) |
| 211 | { |
| 212 | waveOutReset(hWaveOut); |
| 213 | |
| 214 | for (Int i = 0; i < headers.Length(); i++) |
| 215 | { |
| 216 | WAVEHDR *hdr = headers.GetNth(i); |
| 217 | |
| 218 | if (hdr->dwFlags & WHDR_PREPARED) |
| 219 | { |
| 220 | waveOutUnprepareHeader(hWaveOut, hdr, sizeof(WAVEHDR)); |
| 221 | } |
| 222 | |
| 223 | delete hdr; |
| 224 | |
| 225 | headers.RemoveNth(i--); |
| 226 | } |
| 227 | |
| 228 | waveOutClose(hWaveOut); |
| 229 | } |
| 230 | |
| 231 | if (buffer) LocalFree(buffer); |
| 232 | |
| 233 | DeleteCriticalSection(&sync); |
| 234 | |
| 235 | return True; |
| 236 | } |
| 237 | |
| 238 | Int BoCA::OutputWaveOut::WriteData(Buffer<UnsignedByte> &data) |
| 239 | { |
| 240 | UnsignedByte *_data = (UnsignedByte *) data; |
| 241 | |
| 242 | Int size = data.Size(); |
| 243 | Int bytesWritten = 0; |
| 244 | |
| 245 | EnterCriticalSection(&sync); |
| 246 | |
| 247 | while (size > 0) |
| 248 | { |
| 249 | UINT ptr = (data_written + write_ptr) % buf_size; |
| 250 | UINT delta = size; |
| 251 | |
| 252 | if (ptr + delta > buf_size) delta = buf_size - ptr; |
| 253 | |
| 254 | memcpy(buffer + ptr, _data, delta); |
| 255 | |
| 256 | _data += delta; |
| 257 | size -= delta; |
| 258 | bytesWritten += delta; |
| 259 | data_written += delta; |
| 260 | } |
| 261 | |
| 262 | if (bytesWritten) SetEvent(hEvent); // new shit, time to update |
| 263 | |
| 264 | LeaveCriticalSection(&sync); |
| 265 | |
| 266 | return bytesWritten; |
| 267 | } |
| 268 | |
| 269 | Int BoCA::OutputWaveOut::CanWrite() |
| 270 | { |
| 271 | EnterCriticalSection(&sync); |
| 272 | |
| 273 | Int bytes = paused ? 0 : buf_size - buf_size_used - data_written; |
| 274 | |
| 275 | LeaveCriticalSection(&sync); |
| 276 | |
| 277 | return bytes; |
| 278 | } |
| 279 | |
| 280 | Int BoCA::OutputWaveOut::SetPause(Bool pause) |
| 281 | { |
| 282 | EnterCriticalSection(&sync); |
| 283 | |
| 284 | newpause = pause ? True : False; // needs to be done in our thread to keep stupid win2k/xp happy |
| 285 | |
| 286 | SetEvent(hEvent); |
| 287 | |
| 288 | LeaveCriticalSection(&sync); |
| 289 | |
| 290 | while (paused != newpause) S::System::System::Sleep(0); |
| 291 | |
| 292 | return Success(); |
| 293 | } |
| 294 | |
| 295 | Bool BoCA::OutputWaveOut::IsPlaying() |
| 296 | { |
| 297 | if (!hWaveOut) return False; |
| 298 | |
| 299 | EnterCriticalSection(&sync); // needs to be done in our thread |
| 300 | |
| 301 | if (!paused) { needplay = True; SetEvent(hEvent); } |
| 302 | |
| 303 | const Format &format = track.GetFormat(); |
| 304 | Int r = MulDiv(buf_size_used + data_written, 1000, (format.bits >> 3) * format.channels * format.rate); |
| 305 | |
| 306 | if (paused) r -= p_time; |
| 307 | else if (n_playing) r -= timeGetTime() - last_time; |
| 308 | |
| 309 | LeaveCriticalSection(&sync); |
| 310 | |
| 311 | return (r > 0); |
| 312 | } |
| 313 | |
| 314 | Void BoCA::OutputWaveOut::WorkerThread() |
| 315 | { |
| 316 | SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_TIME_CRITICAL); |
| 317 | |
| 318 | const Format &format = track.GetFormat(); |
| 319 | |
| 320 | while (hWaveOut) |
| 321 | { |
| 322 | WaitForSingleObject(hEvent, INFINITE); |
| 323 | |
| 324 | EnterCriticalSection(&sync); |
| 325 | |
| 326 | if (stop) { LeaveCriticalSection(&sync); break; } |
| 327 | |
| 328 | for (Int i = 0; i < headers.Length(); i++) |
| 329 | { |
| 330 | WAVEHDR *hdr = headers.GetNth(i); |
| 331 | |
| 332 | if (hdr->dwFlags & WHDR_DONE) |
| 333 | { |
| 334 | n_playing--; |
| 335 | |
| 336 | buf_size_used -= hdr->dwBufferLength; |
| 337 | |
| 338 | last_time = timeGetTime(); |
| 339 | |
| 340 | waveOutUnprepareHeader(hWaveOut, hdr, sizeof(WAVEHDR)); |
| 341 | |
| 342 | delete hdr; |
| 343 | |
| 344 | headers.RemoveNth(i--); |
| 345 | } |
| 346 | } |
| 347 | |
| 348 | if (!paused && newpause) |
| 349 | { |
| 350 | paused = True; |
| 351 | |
| 352 | if (hWaveOut) waveOutPause(hWaveOut); |
| 353 | |
| 354 | p_time = timeGetTime() - last_time; |
| 355 | } |
| 356 | |
| 357 | if (paused && !newpause) |
| 358 | { |
| 359 | paused = False; |
| 360 | |
| 361 | if (hWaveOut) waveOutRestart(hWaveOut); |
| 362 | |
| 363 | last_time = timeGetTime() - p_time; |
| 364 | } |
| 365 | |
| 366 | |
| 367 | UINT limit = 0; |
| 368 | |
| 369 | if (!needplay && !n_playing && limit < avgblock) limit = avgblock; |
| 370 | else if (buf_size_used < (buf_size >> 1) || n_playing < 3) limit = minblock; // skipping warning, blow whatever we have |
| 371 | else limit = avgblock; // just a block |
| 372 | |
| 373 | while (data_written > limit) |
| 374 | { |
| 375 | UINT d = (data_written > maxblock) ? maxblock : data_written; |
| 376 | |
| 377 | d -= d % ((format.bits >> 3) * format.channels); |
| 378 | |
| 379 | if (!d) break; |
| 380 | |
| 381 | data_written -= d; |
| 382 | buf_size_used += d; |
| 383 | |
| 384 | WAVEHDR *hdr = new WAVEHDR; |
| 385 | |
| 386 | ZeroMemory(hdr, sizeof(WAVEHDR)); |
| 387 | |
| 388 | hdr->dwBufferLength = d; |
| 389 | hdr->dwBytesRecorded = d; |
| 390 | hdr->lpData = buffer + write_ptr; |
| 391 | |
| 392 | headers.Add(hdr); |
| 393 | |
| 394 | write_ptr += d; |
| 395 | |
| 396 | if (write_ptr > buf_size) |
| 397 | { |
| 398 | write_ptr -= buf_size; |
| 399 | memcpy(buffer + buf_size, buffer, write_ptr); |
| 400 | } |
| 401 | |
| 402 | n_playing++; |
| 403 | |
| 404 | waveOutPrepareHeader(hWaveOut, hdr, sizeof(WAVEHDR)); |
| 405 | waveOutWrite(hWaveOut, hdr, sizeof(WAVEHDR)); // important: make all waveOutWrite calls from *our* thread to keep win2k/xp happy |
| 406 | |
| 407 | if (n_playing == 1) last_time = timeGetTime(); |
| 408 | } |
| 409 | |
| 410 | needplay = False; |
| 411 | |
| 412 | LeaveCriticalSection(&sync); |
| 413 | } |
| 414 | } |