| 1 | /* BoCA - BonkEnc Component Architecture |
| 2 | * Copyright (C) 2007-2019 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 <smooth.h> |
| 14 | #include <smooth/dll.h> |
| 15 | |
| 16 | #include <mmreg.h> |
| 17 | |
| 18 | #include "directsound.h" |
| 19 | |
| 20 | const String &BoCA::OutputDirectSound::GetComponentSpecs() |
| 21 | { |
| 22 | static String componentSpecs; |
| 23 | |
| 24 | Initialize(); |
| 25 | |
| 26 | IDirectSound8 *directSound = NIL; |
| 27 | |
| 28 | if (DirectSoundCreate8(&DSDEVID_DefaultPlayback, &directSound, NIL) == DS_OK) |
| 29 | { |
| 30 | /* Query device capabilities. |
| 31 | */ |
| 32 | DSCAPS caps; |
| 33 | |
| 34 | caps.dwSize = sizeof(DSCAPS); |
| 35 | |
| 36 | directSound->GetCaps(&caps); |
| 37 | directSound->Release(); |
| 38 | |
| 39 | /* Set component specs. |
| 40 | */ |
| 41 | String rateString = String::FromInt(caps.dwMinSecondarySampleRate).Append("-") |
| 42 | .Append(String::FromInt(caps.dwMaxSecondarySampleRate)); |
| 43 | |
| 44 | componentSpecs = String(" \ |
| 45 | \ |
| 46 | <?xml version=\"1.0\" encoding=\"UTF-8\"?> \ |
| 47 | <component> \ |
| 48 | <name>DirectSound Output Plugin</name> \ |
| 49 | <version>1.0</version> \ |
| 50 | <id>directsound-out</id> \ |
| 51 | <type>output</type> \ |
| 52 | <precede>waveout-out</precede> \ |
| 53 | <input bits=\"8\" signed=\"false\" rate=\"").Append(rateString).Append("\"/> \ |
| 54 | <input bits=\"16-32\" rate=\"").Append(rateString).Append("\"/> \ |
| 55 | <input float=\"true\" rate=\"").Append(rateString).Append("\"/> \ |
| 56 | </component> \ |
| 57 | \ |
| 58 | "); |
| 59 | } |
| 60 | |
| 61 | Cleanup(); |
| 62 | |
| 63 | return componentSpecs; |
| 64 | } |
| 65 | |
| 66 | Void smooth::AttachDLL(Void *instance) |
| 67 | { |
| 68 | /* Register initialization and cleanup handlers. |
| 69 | */ |
| 70 | BoCA::Engine *engine = BoCA::Engine::Get(); |
| 71 | |
| 72 | engine->onInitialize.Connect(&BoCA::OutputDirectSound::Initialize); |
| 73 | engine->onCleanup.Connect(&BoCA::OutputDirectSound::Cleanup); |
| 74 | } |
| 75 | |
| 76 | Void smooth::DetachDLL() |
| 77 | { |
| 78 | /* Unregister initialization and cleanup handlers. |
| 79 | */ |
| 80 | BoCA::Engine *engine = BoCA::Engine::Get(); |
| 81 | |
| 82 | engine->onInitialize.Disconnect(&BoCA::OutputDirectSound::Initialize); |
| 83 | engine->onCleanup.Disconnect(&BoCA::OutputDirectSound::Cleanup); |
| 84 | } |
| 85 | |
| 86 | Void BoCA::OutputDirectSound::Initialize() |
| 87 | { |
| 88 | /* Init the Microsoft COM library. |
| 89 | */ |
| 90 | CoInitialize(NIL); |
| 91 | } |
| 92 | |
| 93 | Void BoCA::OutputDirectSound::Cleanup() |
| 94 | { |
| 95 | /* Uninit the Microsoft COM library. |
| 96 | */ |
| 97 | CoUninitialize(); |
| 98 | } |
| 99 | |
| 100 | BoCA::OutputDirectSound::OutputDirectSound() |
| 101 | { |
| 102 | directSound = NIL; |
| 103 | |
| 104 | primaryBuffer = NIL; |
| 105 | workBuffer = NIL; |
| 106 | |
| 107 | bufferSize = 0; |
| 108 | bufferPos = 0; |
| 109 | |
| 110 | firstBlock = True; |
| 111 | } |
| 112 | |
| 113 | BoCA::OutputDirectSound::~OutputDirectSound() |
| 114 | { |
| 115 | } |
| 116 | |
| 117 | Bool BoCA::OutputDirectSound::Activate() |
| 118 | { |
| 119 | const Format &format = track.GetFormat(); |
| 120 | |
| 121 | /* Set helper variables. |
| 122 | */ |
| 123 | firstBlock = True; |
| 124 | bufferSize = format.rate * format.channels * (format.bits / 8) / 2; |
| 125 | |
| 126 | /* Init wave format descriptor. |
| 127 | */ |
| 128 | WAVEFORMATEXTENSIBLE wfx; |
| 129 | |
| 130 | ZeroMemory(&wfx, sizeof(WAVEFORMATEXTENSIBLE)); |
| 131 | |
| 132 | wfx.Format.nSamplesPerSec = format.rate; |
| 133 | wfx.Format.wBitsPerSample = format.bits; |
| 134 | wfx.Format.nChannels = format.channels; |
| 135 | |
| 136 | wfx.Format.cbSize = sizeof(WAVEFORMATEXTENSIBLE) - sizeof(WAVEFORMATEX); |
| 137 | wfx.Format.wFormatTag = WAVE_FORMAT_EXTENSIBLE; |
| 138 | wfx.Format.nBlockAlign = (wfx.Format.wBitsPerSample >> 3) * wfx.Format.nChannels; |
| 139 | wfx.Format.nAvgBytesPerSec = wfx.Format.nBlockAlign * wfx.Format.nSamplesPerSec; |
| 140 | |
| 141 | wfx.SubFormat = format.fp ? KSDATAFORMAT_SUBTYPE_IEEE_FLOAT : KSDATAFORMAT_SUBTYPE_PCM; |
| 142 | |
| 143 | wfx.Samples.wValidBitsPerSample = wfx.Format.wBitsPerSample; |
| 144 | |
| 145 | if (format.channels == 1) wfx.dwChannelMask = SPEAKER_FRONT_CENTER; |
| 146 | else if (format.channels == 2) wfx.dwChannelMask = SPEAKER_FRONT_LEFT | SPEAKER_FRONT_RIGHT; |
| 147 | else if (format.channels == 3) wfx.dwChannelMask = SPEAKER_FRONT_LEFT | SPEAKER_FRONT_RIGHT | SPEAKER_FRONT_CENTER; |
| 148 | else if (format.channels == 4) wfx.dwChannelMask = SPEAKER_FRONT_LEFT | SPEAKER_FRONT_RIGHT | SPEAKER_BACK_LEFT | SPEAKER_BACK_RIGHT; |
| 149 | else if (format.channels == 5) wfx.dwChannelMask = SPEAKER_FRONT_LEFT | SPEAKER_FRONT_RIGHT | SPEAKER_FRONT_CENTER | SPEAKER_BACK_LEFT | SPEAKER_BACK_RIGHT; |
| 150 | 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; |
| 151 | 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; |
| 152 | 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; |
| 153 | |
| 154 | /* Open audio device. |
| 155 | */ |
| 156 | DirectSoundCreate8(&DSDEVID_DefaultPlayback, &directSound, NIL); |
| 157 | |
| 158 | directSound->SetCooperativeLevel((HWND) Window::GetNthWindow(0)->GetSystemWindow(), DSSCL_PRIORITY); |
| 159 | |
| 160 | /* Create primary sound buffer. |
| 161 | */ |
| 162 | DSBUFFERDESC soundBuffDesc; |
| 163 | |
| 164 | ZeroMemory(&soundBuffDesc, sizeof(DSBUFFERDESC)); |
| 165 | |
| 166 | soundBuffDesc.dwSize = sizeof(DSBUFFERDESC); |
| 167 | soundBuffDesc.dwFlags = DSBCAPS_PRIMARYBUFFER; |
| 168 | |
| 169 | if (directSound->CreateSoundBuffer(&soundBuffDesc, &primaryBuffer, NIL) != DS_OK) |
| 170 | { |
| 171 | directSound->Release(); |
| 172 | |
| 173 | return False; |
| 174 | } |
| 175 | |
| 176 | if (primaryBuffer->SetFormat((WAVEFORMATEX *) &wfx) != DS_OK) |
| 177 | { |
| 178 | primaryBuffer->Release(); |
| 179 | |
| 180 | directSound->Release(); |
| 181 | |
| 182 | return False; |
| 183 | } |
| 184 | |
| 185 | /* Create secondary sound buffer. |
| 186 | */ |
| 187 | soundBuffDesc.dwFlags = DSBCAPS_GLOBALFOCUS; |
| 188 | soundBuffDesc.dwBufferBytes = bufferSize; |
| 189 | soundBuffDesc.lpwfxFormat = (WAVEFORMATEX *) &wfx; |
| 190 | |
| 191 | if (directSound->CreateSoundBuffer(&soundBuffDesc, &workBuffer, NIL) != DS_OK) |
| 192 | { |
| 193 | primaryBuffer->Release(); |
| 194 | |
| 195 | directSound->Release(); |
| 196 | |
| 197 | return False; |
| 198 | } |
| 199 | |
| 200 | return True; |
| 201 | } |
| 202 | |
| 203 | Bool BoCA::OutputDirectSound::Deactivate() |
| 204 | { |
| 205 | workBuffer->Stop(); |
| 206 | |
| 207 | workBuffer->Release(); |
| 208 | primaryBuffer->Release(); |
| 209 | |
| 210 | directSound->Release(); |
| 211 | |
| 212 | return True; |
| 213 | } |
| 214 | |
| 215 | Int BoCA::OutputDirectSound::WriteData(Buffer<UnsignedByte> &data) |
| 216 | { |
| 217 | Int size = data.Size(); |
| 218 | Int bytesWritten = 0; |
| 219 | |
| 220 | /* Loop until all bytes are fed to the buffer. |
| 221 | */ |
| 222 | while (size > 0) |
| 223 | { |
| 224 | /* Get current playback position. |
| 225 | */ |
| 226 | DWORD playCursor = bufferSize; |
| 227 | |
| 228 | if (!firstBlock) workBuffer->GetCurrentPosition(&playCursor, NIL); |
| 229 | |
| 230 | if (playCursor == bufferPos) S::System::System::Sleep(25); |
| 231 | |
| 232 | /* Lock buffer and feed samples. |
| 233 | */ |
| 234 | HRESULT hr = DS_OK; |
| 235 | |
| 236 | Void *block1 = NIL; |
| 237 | Void *block2 = NIL; |
| 238 | |
| 239 | DWORD block1Size = 0; |
| 240 | DWORD block2Size = 0; |
| 241 | |
| 242 | if (playCursor >= bufferPos) hr = workBuffer->Lock(bufferPos, Math::Min(size, playCursor - bufferPos), &block1, &block1Size, NIL, NIL, 0); |
| 243 | else hr = workBuffer->Lock(bufferPos, Math::Min(size, playCursor - bufferPos + bufferSize), &block1, &block1Size, &block2, &block2Size, 0); |
| 244 | |
| 245 | if (hr == DS_OK) |
| 246 | { |
| 247 | if (block1 != NIL) memcpy(block1, data + bytesWritten, block1Size); |
| 248 | if (block2 != NIL) memcpy(block2, data + bytesWritten + block1Size, block2Size); |
| 249 | |
| 250 | workBuffer->Unlock(block1, block1Size, block2, block2Size); |
| 251 | |
| 252 | size -= block1Size + block2Size; |
| 253 | |
| 254 | bufferPos += block1Size + block2Size; |
| 255 | bytesWritten += block1Size + block2Size; |
| 256 | |
| 257 | if (bufferPos >= bufferSize) bufferPos -= bufferSize; |
| 258 | } |
| 259 | |
| 260 | /* Start playback. |
| 261 | */ |
| 262 | if (firstBlock) workBuffer->Play(0, 0, DSBPLAY_LOOPING); |
| 263 | |
| 264 | firstBlock = False; |
| 265 | } |
| 266 | |
| 267 | return bytesWritten; |
| 268 | } |
| 269 | |
| 270 | Bool BoCA::OutputDirectSound::Finish() |
| 271 | { |
| 272 | /* Fill buffer with null samples. |
| 273 | */ |
| 274 | Buffer<UnsignedByte> buffer(bufferSize); |
| 275 | |
| 276 | memset(buffer, 0, bufferSize); |
| 277 | |
| 278 | WriteData(buffer); |
| 279 | |
| 280 | /* Stop playing the buffer as we must have |
| 281 | * reached the null samples at this point. |
| 282 | */ |
| 283 | workBuffer->Stop(); |
| 284 | |
| 285 | return True; |
| 286 | } |
| 287 | |
| 288 | Int BoCA::OutputDirectSound::CanWrite() |
| 289 | { |
| 290 | if (workBuffer == NIL) return 0; |
| 291 | |
| 292 | if (firstBlock) return bufferSize; |
| 293 | |
| 294 | DWORD playCursor = 0; |
| 295 | |
| 296 | workBuffer->GetCurrentPosition(&playCursor, NIL); |
| 297 | |
| 298 | if (playCursor >= bufferPos) return playCursor - bufferPos; |
| 299 | else return playCursor - bufferPos + bufferSize; |
| 300 | } |
| 301 | |
| 302 | Int BoCA::OutputDirectSound::SetPause(Bool pause) |
| 303 | { |
| 304 | if (workBuffer == NIL) return Error(); |
| 305 | |
| 306 | if (pause) workBuffer->Stop(); |
| 307 | else workBuffer->Play(0, 0, DSBPLAY_LOOPING); |
| 308 | |
| 309 | return Success(); |
| 310 | } |
| 311 | |
| 312 | Bool BoCA::OutputDirectSound::IsPlaying() |
| 313 | { |
| 314 | if (workBuffer == NIL) return False; |
| 315 | |
| 316 | /* Get and check buffer status. |
| 317 | */ |
| 318 | DWORD status = 0; |
| 319 | |
| 320 | workBuffer->GetStatus(&status); |
| 321 | |
| 322 | if (status & DSBSTATUS_PLAYING) return True; |
| 323 | else return False; |
| 324 | } |