| 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 <time.h> |
| 17 | |
| 18 | #include "winamp.h" |
| 19 | #include "config.h" |
| 20 | |
| 21 | using namespace smooth::IO; |
| 22 | using namespace smooth::Threads; |
| 23 | |
| 24 | const String &BoCA::DecoderWinamp::GetComponentSpecs() |
| 25 | { |
| 26 | static String componentSpecs; |
| 27 | |
| 28 | if (winamp_in_plugins.Length() > 0) |
| 29 | { |
| 30 | componentSpecs = " \ |
| 31 | \ |
| 32 | <?xml version=\"1.0\" encoding=\"UTF-8\"?> \ |
| 33 | <component> \ |
| 34 | <name>Winamp Input Plug-In Adapter</name> \ |
| 35 | <version>1.0</version> \ |
| 36 | <id>winamp-dec</id> \ |
| 37 | <type threadSafe=\"false\">decoder</type> \ |
| 38 | \ |
| 39 | "; |
| 40 | } |
| 41 | |
| 42 | for (Int i = 0; i < winamp_in_plugins.Length(); i++) |
| 43 | { |
| 44 | Int n = 1; |
| 45 | Array<String> extensions; |
| 46 | |
| 47 | for (Int j = 0; true; j++) |
| 48 | { |
| 49 | String value = winamp_in_modules.GetNth(i)->FileExtensions + j; |
| 50 | |
| 51 | if (n & 1) |
| 52 | { |
| 53 | String extension; |
| 54 | Int o = 0; |
| 55 | |
| 56 | for (Int m = 0; m < value.Length(); m++) |
| 57 | { |
| 58 | extension[m - o] = value[m]; |
| 59 | |
| 60 | if (value[m + 1] == ';' || value[m + 1] == 0) |
| 61 | { |
| 62 | extensions.Add(extension); |
| 63 | |
| 64 | m++; |
| 65 | o = m; |
| 66 | extension = NIL; |
| 67 | } |
| 68 | } |
| 69 | } |
| 70 | else |
| 71 | { |
| 72 | componentSpecs.Append(" \ |
| 73 | \ |
| 74 | <format> \ |
| 75 | <name>").Append(value).Append("</name> \ |
| 76 | \ |
| 77 | "); |
| 78 | |
| 79 | for (Int l = 0; l < extensions.Length(); l++) |
| 80 | { |
| 81 | componentSpecs.Append(" \ |
| 82 | \ |
| 83 | <extension>").Append(extensions.GetNth(l).ToLower()).Append("</extension> \ |
| 84 | \ |
| 85 | "); |
| 86 | } |
| 87 | \ |
| 88 | componentSpecs.Append(" \ |
| 89 | \ |
| 90 | </format> \ |
| 91 | \ |
| 92 | "); |
| 93 | |
| 94 | extensions.RemoveAll(); |
| 95 | } |
| 96 | |
| 97 | j += value.Length(); |
| 98 | n++; |
| 99 | |
| 100 | if (winamp_in_modules.GetNth(i)->FileExtensions[j] == 0 && winamp_in_modules.GetNth(i)->FileExtensions[j + 1] == 0) break; |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | if (winamp_in_plugins.Length() > 0) |
| 105 | { |
| 106 | componentSpecs.Append(" \ |
| 107 | \ |
| 108 | </component> \ |
| 109 | \ |
| 110 | "); |
| 111 | } |
| 112 | |
| 113 | return componentSpecs; |
| 114 | } |
| 115 | |
| 116 | Void smooth::AttachDLL(Void *instance) |
| 117 | { |
| 118 | LoadWinampDLLs(); |
| 119 | } |
| 120 | |
| 121 | Void smooth::DetachDLL() |
| 122 | { |
| 123 | FreeWinampDLLs(); |
| 124 | } |
| 125 | |
| 126 | namespace BoCA |
| 127 | { |
| 128 | DecoderWinamp *filter = NIL; |
| 129 | |
| 130 | void SetInfo(int, int, int, int); |
| 131 | void VSASetInfo(int, int); |
| 132 | void VSAAddPCMData(void *, int, int, int); |
| 133 | int VSAGetMode(int *, int *); |
| 134 | int VSAAdd(void *, int); |
| 135 | void SAVSAInit(int, int); |
| 136 | void SAVSADeInit(); |
| 137 | void SAAddPCMData(void *, int, int, int); |
| 138 | int SAGetMode(); |
| 139 | int SAAdd(void *, int, int); |
| 140 | int dsp_isactive(); |
| 141 | int dsp_dosamples(short int *, int, int, int, int); |
| 142 | |
| 143 | int Out_Open(int, int, int, int, int); |
| 144 | void Out_Close(); |
| 145 | void Out_Flush(int); |
| 146 | int Out_Write(char *, int); |
| 147 | int Out_CanWrite(); |
| 148 | int Out_IsPlaying(); |
| 149 | int Out_Pause(int); |
| 150 | void Out_SetVolume(int); |
| 151 | void Out_SetPan(int); |
| 152 | int Out_GetOutputTime(); |
| 153 | int Out_GetWrittenTime(); |
| 154 | }; |
| 155 | |
| 156 | Bool BoCA::DecoderWinamp::CanOpenStream(const String &streamURI) |
| 157 | { |
| 158 | return (GetModuleForFile(streamURI) != NIL); |
| 159 | } |
| 160 | |
| 161 | Error BoCA::DecoderWinamp::GetStreamInfo(const String &streamURI, Track &track) |
| 162 | { |
| 163 | InStream *f_in = new InStream(STREAM_FILE, streamURI, IS_READ); |
| 164 | |
| 165 | track.fileSize = f_in->Size(); |
| 166 | track.length = -1; |
| 167 | |
| 168 | delete f_in; |
| 169 | |
| 170 | /* Get the correct module and set callback functions. |
| 171 | */ |
| 172 | In_Module *module = GetModuleForFile(streamURI); |
| 173 | |
| 174 | module->SetInfo = SetInfo; |
| 175 | module->VSASetInfo = VSASetInfo; |
| 176 | module->VSAAddPCMData = VSAAddPCMData; |
| 177 | module->VSAGetMode = VSAGetMode; |
| 178 | module->VSAAdd = VSAAdd; |
| 179 | module->SAVSAInit = SAVSAInit; |
| 180 | module->SAVSADeInit = SAVSADeInit; |
| 181 | module->SAAddPCMData = SAAddPCMData; |
| 182 | module->SAGetMode = SAGetMode; |
| 183 | module->SAAdd = SAAdd; |
| 184 | module->dsp_isactive = dsp_isactive; |
| 185 | module->dsp_dosamples = dsp_dosamples; |
| 186 | |
| 187 | module->outMod = new Out_Module(); |
| 188 | |
| 189 | module->outMod->Open = Out_Open; |
| 190 | module->outMod->Close = Out_Close; |
| 191 | module->outMod->Flush = Out_Flush; |
| 192 | module->outMod->Write = Out_Write; |
| 193 | module->outMod->CanWrite = Out_CanWrite; |
| 194 | module->outMod->IsPlaying = Out_IsPlaying; |
| 195 | module->outMod->Pause = Out_Pause; |
| 196 | module->outMod->SetVolume = Out_SetVolume; |
| 197 | module->outMod->SetPan = Out_SetPan; |
| 198 | module->outMod->GetOutputTime = Out_GetOutputTime; |
| 199 | module->outMod->GetWrittenTime = Out_GetWrittenTime; |
| 200 | |
| 201 | /* Create and setup mutex. |
| 202 | */ |
| 203 | samplesBufferMutex = new Mutex(); |
| 204 | |
| 205 | filter = this; |
| 206 | infoTrack = &track; |
| 207 | |
| 208 | String trackTitle; |
| 209 | |
| 210 | /* Copy the file and play the temporary copy |
| 211 | * if the file name contains Unicode characters. |
| 212 | */ |
| 213 | if (String::IsUnicode(streamURI)) |
| 214 | { |
| 215 | File(streamURI).Copy(Utilities::GetNonUnicodeTempFileName(streamURI).Append(".in")); |
| 216 | |
| 217 | errorState = module->Play(Utilities::GetNonUnicodeTempFileName(streamURI).Append(".in")); |
| 218 | } |
| 219 | else |
| 220 | { |
| 221 | errorState = module->Play(streamURI); |
| 222 | } |
| 223 | |
| 224 | if (!errorState) |
| 225 | { |
| 226 | /* Give the module one second to start sending samples. |
| 227 | */ |
| 228 | UnsignedInt64 start = S::System::System::Clock(); |
| 229 | |
| 230 | while (S::System::System::Clock() - start < 1000 && samplesBuffer.Size() <= 0) S::System::System::Sleep(1); |
| 231 | |
| 232 | /* Get track info and stop module. |
| 233 | */ |
| 234 | int length_ms = -1; |
| 235 | char *title = new char [1024]; |
| 236 | |
| 237 | module->GetFileInfo(NIL, title, &length_ms); |
| 238 | |
| 239 | track.approxLength = (Int) (Float(length_ms) * Float(track.GetFormat().rate) / 1000.0); |
| 240 | |
| 241 | trackTitle = title; |
| 242 | |
| 243 | delete [] title; |
| 244 | |
| 245 | module->Stop(); |
| 246 | } |
| 247 | |
| 248 | delete module->outMod; |
| 249 | delete samplesBufferMutex; |
| 250 | |
| 251 | /* Remove temporary copy if necessary. |
| 252 | */ |
| 253 | if (String::IsUnicode(streamURI)) |
| 254 | { |
| 255 | File(Utilities::GetNonUnicodeTempFileName(streamURI).Append(".in")).Delete(); |
| 256 | } |
| 257 | |
| 258 | if (!trackTitle.Contains(File( streamURI ).GetFileName()) && |
| 259 | !trackTitle.Contains(File(Utilities::GetNonUnicodeTempFileName(streamURI)).GetFileName())) |
| 260 | { |
| 261 | const Array<String> &parts = trackTitle.Explode(" - "); |
| 262 | |
| 263 | if (parts.Length() > 1) |
| 264 | { |
| 265 | Info info = track.GetInfo(); |
| 266 | |
| 267 | info.artist = parts.GetFirst(); |
| 268 | info.title = parts.GetLast(); |
| 269 | |
| 270 | track.SetInfo(info); |
| 271 | } |
| 272 | } |
| 273 | |
| 274 | /* Return an error if we didn't get useful format data. |
| 275 | */ |
| 276 | if (track.GetFormat().rate == 0 || |
| 277 | track.GetFormat().channels == 0 || |
| 278 | track.GetFormat().bits == 0) |
| 279 | { |
| 280 | errorState = True; |
| 281 | } |
| 282 | |
| 283 | if (errorState) return Error(); |
| 284 | else return Success(); |
| 285 | } |
| 286 | |
| 287 | BoCA::DecoderWinamp::DecoderWinamp() |
| 288 | { |
| 289 | configLayer = NIL; |
| 290 | |
| 291 | infoTrack = NIL; |
| 292 | module = NIL; |
| 293 | |
| 294 | samplesBufferMutex = NIL; |
| 295 | |
| 296 | samplesDone = 0; |
| 297 | } |
| 298 | |
| 299 | BoCA::DecoderWinamp::~DecoderWinamp() |
| 300 | { |
| 301 | if (configLayer != NIL) Object::DeleteObject(configLayer); |
| 302 | } |
| 303 | |
| 304 | Bool BoCA::DecoderWinamp::Activate() |
| 305 | { |
| 306 | /* Get the correct module and set callback functions. |
| 307 | */ |
| 308 | module = GetModuleForFile(track.fileName); |
| 309 | |
| 310 | module->SetInfo = SetInfo; |
| 311 | module->VSASetInfo = VSASetInfo; |
| 312 | module->VSAAddPCMData = VSAAddPCMData; |
| 313 | module->VSAGetMode = VSAGetMode; |
| 314 | module->VSAAdd = VSAAdd; |
| 315 | module->SAVSAInit = SAVSAInit; |
| 316 | module->SAVSADeInit = SAVSADeInit; |
| 317 | module->SAAddPCMData = SAAddPCMData; |
| 318 | module->SAGetMode = SAGetMode; |
| 319 | module->SAAdd = SAAdd; |
| 320 | module->dsp_isactive = dsp_isactive; |
| 321 | module->dsp_dosamples = dsp_dosamples; |
| 322 | |
| 323 | module->outMod = new Out_Module(); |
| 324 | |
| 325 | module->outMod->Open = Out_Open; |
| 326 | module->outMod->Close = Out_Close; |
| 327 | module->outMod->Flush = Out_Flush; |
| 328 | module->outMod->Write = Out_Write; |
| 329 | module->outMod->CanWrite = Out_CanWrite; |
| 330 | module->outMod->IsPlaying = Out_IsPlaying; |
| 331 | module->outMod->Pause = Out_Pause; |
| 332 | module->outMod->SetVolume = Out_SetVolume; |
| 333 | module->outMod->SetPan = Out_SetPan; |
| 334 | module->outMod->GetOutputTime = Out_GetOutputTime; |
| 335 | module->outMod->GetWrittenTime = Out_GetWrittenTime; |
| 336 | |
| 337 | /* Create and setup mutex. |
| 338 | */ |
| 339 | samplesBufferMutex = new Mutex(); |
| 340 | samplesBufferMutex->Lock(); |
| 341 | |
| 342 | filter = this; |
| 343 | |
| 344 | /* Copy the file and play the temporary copy |
| 345 | * if the file name contains Unicode characters. |
| 346 | */ |
| 347 | if (String::IsUnicode(track.fileName)) |
| 348 | { |
| 349 | File(track.fileName).Copy(Utilities::GetNonUnicodeTempFileName(track.fileName).Append(".in")); |
| 350 | |
| 351 | module->Play(Utilities::GetNonUnicodeTempFileName(track.fileName).Append(".in")); |
| 352 | } |
| 353 | else |
| 354 | { |
| 355 | module->Play(track.fileName); |
| 356 | } |
| 357 | |
| 358 | return True; |
| 359 | } |
| 360 | |
| 361 | Bool BoCA::DecoderWinamp::Deactivate() |
| 362 | { |
| 363 | samplesBufferMutex->Release(); |
| 364 | |
| 365 | module->Stop(); |
| 366 | |
| 367 | delete module->outMod; |
| 368 | delete samplesBufferMutex; |
| 369 | |
| 370 | /* Remove temporary copy if necessary. |
| 371 | */ |
| 372 | if (String::IsUnicode(track.fileName)) |
| 373 | { |
| 374 | File(Utilities::GetNonUnicodeTempFileName(track.fileName).Append(".in")).Delete(); |
| 375 | } |
| 376 | |
| 377 | return True; |
| 378 | } |
| 379 | |
| 380 | Int BoCA::DecoderWinamp::ReadData(Buffer<UnsignedByte> &data) |
| 381 | { |
| 382 | /* Give the module one second to send more samples. |
| 383 | */ |
| 384 | samplesBufferMutex->Release(); |
| 385 | |
| 386 | UnsignedInt64 start = S::System::System::Clock(); |
| 387 | |
| 388 | while (S::System::System::Clock() - start < 1000 && samplesBuffer.Size() <= 0) S::System::System::Sleep(1); |
| 389 | |
| 390 | samplesBufferMutex->Lock(); |
| 391 | |
| 392 | /* Copy samples to output buffer. |
| 393 | */ |
| 394 | Int size = samplesBuffer.Size(); |
| 395 | |
| 396 | data.Resize(size); |
| 397 | |
| 398 | memcpy(data, samplesBuffer, size); |
| 399 | |
| 400 | samplesBuffer.Resize(0); |
| 401 | |
| 402 | /* Set inBytes to a value that reflects |
| 403 | * our approximate position in the file. |
| 404 | */ |
| 405 | samplesDone += size / track.GetFormat().channels / (track.GetFormat().bits / 8); |
| 406 | inBytes = track.fileSize * samplesDone / track.approxLength; |
| 407 | |
| 408 | if (size == 0) return -1; |
| 409 | else return size; |
| 410 | } |
| 411 | |
| 412 | ConfigLayer *BoCA::DecoderWinamp::GetConfigurationLayer() |
| 413 | { |
| 414 | if (configLayer == NIL) configLayer = new ConfigureWinamp(); |
| 415 | |
| 416 | return configLayer; |
| 417 | } |
| 418 | |
| 419 | In_Module *BoCA::DecoderWinamp::GetModuleForFile(const String &file) const |
| 420 | { |
| 421 | In_Module *module = NIL; |
| 422 | |
| 423 | for (Int i = 0; i < winamp_in_plugins.Length(); i++) |
| 424 | { |
| 425 | Int n = 1; |
| 426 | |
| 427 | for (Int j = 0; True; j++) |
| 428 | { |
| 429 | String value = winamp_in_modules.GetNth(i)->FileExtensions + j; |
| 430 | |
| 431 | if (n & 1) |
| 432 | { |
| 433 | String extension; |
| 434 | Int o = 0; |
| 435 | |
| 436 | for (Int m = 0; m < value.Length(); m++) |
| 437 | { |
| 438 | extension[m - o] = value[m]; |
| 439 | |
| 440 | if (value[m + 1] == ';' || value[m + 1] == 0) |
| 441 | { |
| 442 | if (extension.ToLower() == file.Tail(extension.Length()).ToLower()) |
| 443 | { |
| 444 | module = winamp_in_modules.GetNth(i); |
| 445 | |
| 446 | break; |
| 447 | } |
| 448 | |
| 449 | m++; |
| 450 | o = m + 1; |
| 451 | extension = NIL; |
| 452 | } |
| 453 | } |
| 454 | |
| 455 | if (module != NIL) break; |
| 456 | } |
| 457 | |
| 458 | j += value.Length(); |
| 459 | n++; |
| 460 | |
| 461 | if (winamp_in_modules.GetNth(i)->FileExtensions[j] == 0 && winamp_in_modules.GetNth(i)->FileExtensions[j + 1] == 0) break; |
| 462 | } |
| 463 | } |
| 464 | |
| 465 | return module; |
| 466 | } |
| 467 | |
| 468 | void BoCA::SetInfo(int bitrate, int srate, int stereo, int synched) |
| 469 | { |
| 470 | } |
| 471 | |
| 472 | void BoCA::VSASetInfo(int srate, int nch) |
| 473 | { |
| 474 | } |
| 475 | |
| 476 | void BoCA::VSAAddPCMData(void *PCMData, int nch, int bps, int timestamp) |
| 477 | { |
| 478 | } |
| 479 | |
| 480 | int BoCA::VSAGetMode(int *specNch, int *waveNch) |
| 481 | { |
| 482 | return 0; |
| 483 | } |
| 484 | |
| 485 | int BoCA::VSAAdd(void *data, int timestamp) |
| 486 | { |
| 487 | return 0; |
| 488 | } |
| 489 | |
| 490 | void BoCA::SAVSAInit(int latency, int srate) |
| 491 | { |
| 492 | } |
| 493 | |
| 494 | void BoCA::SAVSADeInit() |
| 495 | { |
| 496 | } |
| 497 | |
| 498 | void BoCA::SAAddPCMData(void *PCMData, int nch, int bps, int timestamp) |
| 499 | { |
| 500 | } |
| 501 | |
| 502 | int BoCA::SAGetMode() |
| 503 | { |
| 504 | return 0; |
| 505 | } |
| 506 | |
| 507 | int BoCA::SAAdd(void *data, int timestamp, int csa) |
| 508 | { |
| 509 | return 0; |
| 510 | } |
| 511 | |
| 512 | int BoCA::dsp_isactive() |
| 513 | { |
| 514 | return false; |
| 515 | } |
| 516 | |
| 517 | int BoCA::dsp_dosamples(short int *samples, int numsamples, int bps, int nch, int srate) |
| 518 | { |
| 519 | return numsamples; |
| 520 | } |
| 521 | |
| 522 | int BoCA::Out_Open(int samplerate, int numchannels, int bitspersamp, int bufferlenms, int prebufferms) |
| 523 | { |
| 524 | if (filter->infoTrack == NIL) return 0; |
| 525 | |
| 526 | Format format = filter->infoTrack->GetFormat(); |
| 527 | |
| 528 | format.channels = numchannels; |
| 529 | format.rate = samplerate; |
| 530 | format.bits = bitspersamp; |
| 531 | |
| 532 | filter->infoTrack->SetFormat(format); |
| 533 | |
| 534 | return 0; |
| 535 | } |
| 536 | |
| 537 | void BoCA::Out_Close() |
| 538 | { |
| 539 | } |
| 540 | |
| 541 | void BoCA::Out_Flush(int t) |
| 542 | { |
| 543 | } |
| 544 | |
| 545 | int BoCA::Out_Write(char *buf, int len) |
| 546 | { |
| 547 | filter->samplesBufferMutex->Lock(); |
| 548 | |
| 549 | Int oSize = filter->samplesBuffer.Size(); |
| 550 | |
| 551 | filter->samplesBuffer.Resize(oSize + len); |
| 552 | |
| 553 | memcpy(filter->samplesBuffer + oSize, buf, len); |
| 554 | |
| 555 | filter->samplesBufferMutex->Release(); |
| 556 | |
| 557 | return 0; |
| 558 | } |
| 559 | |
| 560 | int BoCA::Out_CanWrite() |
| 561 | { |
| 562 | return Math::Max(0, 32768 - filter->samplesBuffer.Size()); |
| 563 | } |
| 564 | |
| 565 | int BoCA::Out_IsPlaying() |
| 566 | { |
| 567 | return true; |
| 568 | } |
| 569 | |
| 570 | int BoCA::Out_Pause(int pause) |
| 571 | { |
| 572 | return 0; |
| 573 | } |
| 574 | |
| 575 | void BoCA::Out_SetVolume(int vol) |
| 576 | { |
| 577 | } |
| 578 | |
| 579 | void BoCA::Out_SetPan(int pan) |
| 580 | { |
| 581 | } |
| 582 | |
| 583 | int BoCA::Out_GetOutputTime() |
| 584 | { |
| 585 | return 0; |
| 586 | } |
| 587 | |
| 588 | int BoCA::Out_GetWrittenTime() |
| 589 | { |
| 590 | return 0; |
| 591 | } |