Audio plugin host https://kx.studio/carla
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1644 lines
54KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2020 - Raw Material Software Limited
  5. JUCE is an open source library subject to commercial or open-source
  6. licensing.
  7. The code included in this file is provided under the terms of the ISC license
  8. http://www.isc.org/downloads/software-support-policy/isc-license. Permission
  9. To use, copy, modify, and/or distribute this software for any purpose with or
  10. without fee is hereby granted provided that the above copyright notice and
  11. this permission notice appear in all copies.
  12. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  13. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  14. DISCLAIMED.
  15. ==============================================================================
  16. */
  17. namespace juce
  18. {
  19. #undef WINDOWS
  20. /* The ASIO SDK *should* declare its callback functions as being __cdecl, but different versions seem
  21. to be pretty random about whether or not they do this. If you hit an error using these functions
  22. it'll be because you're trying to build using __stdcall, in which case you'd need to either get hold of
  23. an ASIO SDK which correctly specifies __cdecl, or add the __cdecl keyword to its functions yourself.
  24. */
  25. #define JUCE_ASIOCALLBACK __cdecl
  26. //==============================================================================
  27. namespace ASIODebugging
  28. {
  29. #if JUCE_ASIO_DEBUGGING
  30. #define JUCE_ASIO_LOG(msg) ASIODebugging::logMessage (msg)
  31. #define JUCE_ASIO_LOG_ERROR(msg, errNum) ASIODebugging::logError ((msg), (errNum))
  32. static void logMessage (String message)
  33. {
  34. message = "ASIO: " + message;
  35. DBG (message);
  36. if (Logger::getCurrentLogger() != nullptr)
  37. Logger::writeToLog (message);
  38. }
  39. static void logError (const String& context, long error)
  40. {
  41. const char* err = "Unknown error";
  42. switch (error)
  43. {
  44. case ASE_OK: return;
  45. case ASE_NotPresent: err = "Not Present"; break;
  46. case ASE_HWMalfunction: err = "Hardware Malfunction"; break;
  47. case ASE_InvalidParameter: err = "Invalid Parameter"; break;
  48. case ASE_InvalidMode: err = "Invalid Mode"; break;
  49. case ASE_SPNotAdvancing: err = "Sample position not advancing"; break;
  50. case ASE_NoClock: err = "No Clock"; break;
  51. case ASE_NoMemory: err = "Out of memory"; break;
  52. default: break;
  53. }
  54. logMessage ("error: " + context + " - " + err);
  55. }
  56. #else
  57. static void dummyLog() {}
  58. #define JUCE_ASIO_LOG(msg) ASIODebugging::dummyLog()
  59. #define JUCE_ASIO_LOG_ERROR(msg, errNum) ignoreUnused (errNum); ASIODebugging::dummyLog()
  60. #endif
  61. }
  62. //==============================================================================
  63. struct ASIOSampleFormat
  64. {
  65. ASIOSampleFormat() noexcept {}
  66. ASIOSampleFormat (long type) noexcept
  67. {
  68. switch (type)
  69. {
  70. case ASIOSTInt16MSB: byteStride = 2; littleEndian = false; bitDepth = 16; break;
  71. case ASIOSTInt24MSB: byteStride = 3; littleEndian = false; break;
  72. case ASIOSTInt32MSB: bitDepth = 32; littleEndian = false; break;
  73. case ASIOSTFloat32MSB: bitDepth = 32; littleEndian = false; formatIsFloat = true; break;
  74. case ASIOSTFloat64MSB: bitDepth = 64; byteStride = 8; littleEndian = false; break;
  75. case ASIOSTInt32MSB16: bitDepth = 16; littleEndian = false; break;
  76. case ASIOSTInt32MSB18: littleEndian = false; break;
  77. case ASIOSTInt32MSB20: littleEndian = false; break;
  78. case ASIOSTInt32MSB24: littleEndian = false; break;
  79. case ASIOSTInt16LSB: byteStride = 2; bitDepth = 16; break;
  80. case ASIOSTInt24LSB: byteStride = 3; break;
  81. case ASIOSTInt32LSB: bitDepth = 32; break;
  82. case ASIOSTFloat32LSB: bitDepth = 32; formatIsFloat = true; break;
  83. case ASIOSTFloat64LSB: bitDepth = 64; byteStride = 8; break;
  84. case ASIOSTInt32LSB16: bitDepth = 16; break;
  85. case ASIOSTInt32LSB18: break; // (unhandled)
  86. case ASIOSTInt32LSB20: break; // (unhandled)
  87. case ASIOSTInt32LSB24: break;
  88. case ASIOSTDSDInt8LSB1: break; // (unhandled)
  89. case ASIOSTDSDInt8MSB1: break; // (unhandled)
  90. case ASIOSTDSDInt8NER8: break; // (unhandled)
  91. default:
  92. jassertfalse; // (not a valid format code..)
  93. break;
  94. }
  95. }
  96. void convertToFloat (const void* src, float* dst, int samps) const noexcept
  97. {
  98. if (formatIsFloat)
  99. {
  100. memcpy (dst, src, samps * sizeof (float));
  101. }
  102. else
  103. {
  104. switch (bitDepth)
  105. {
  106. case 16: convertInt16ToFloat (static_cast<const char*> (src), dst, byteStride, samps, littleEndian); break;
  107. case 24: convertInt24ToFloat (static_cast<const char*> (src), dst, byteStride, samps, littleEndian); break;
  108. case 32: convertInt32ToFloat (static_cast<const char*> (src), dst, byteStride, samps, littleEndian); break;
  109. default: jassertfalse; break;
  110. }
  111. }
  112. }
  113. void convertFromFloat (const float* src, void* dst, int samps) const noexcept
  114. {
  115. if (formatIsFloat)
  116. {
  117. memcpy (dst, src, samps * sizeof (float));
  118. }
  119. else
  120. {
  121. switch (bitDepth)
  122. {
  123. case 16: convertFloatToInt16 (src, static_cast<char*> (dst), byteStride, samps, littleEndian); break;
  124. case 24: convertFloatToInt24 (src, static_cast<char*> (dst), byteStride, samps, littleEndian); break;
  125. case 32: convertFloatToInt32 (src, static_cast<char*> (dst), byteStride, samps, littleEndian); break;
  126. default: jassertfalse; break;
  127. }
  128. }
  129. }
  130. void clear (void* dst, int numSamps) noexcept
  131. {
  132. if (dst != nullptr)
  133. zeromem (dst, numSamps * byteStride);
  134. }
  135. int bitDepth = 24, byteStride = 4;
  136. bool formatIsFloat = false, littleEndian = true;
  137. private:
  138. static void convertInt16ToFloat (const char* src, float* dest, int srcStrideBytes,
  139. int numSamples, bool littleEndian) noexcept
  140. {
  141. const double g = 1.0 / 32768.0;
  142. if (littleEndian)
  143. {
  144. while (--numSamples >= 0)
  145. {
  146. *dest++ = (float) (g * (short) ByteOrder::littleEndianShort (src));
  147. src += srcStrideBytes;
  148. }
  149. }
  150. else
  151. {
  152. while (--numSamples >= 0)
  153. {
  154. *dest++ = (float) (g * (short) ByteOrder::bigEndianShort (src));
  155. src += srcStrideBytes;
  156. }
  157. }
  158. }
  159. static void convertFloatToInt16 (const float* src, char* dest, int dstStrideBytes,
  160. int numSamples, bool littleEndian) noexcept
  161. {
  162. const double maxVal = (double) 0x7fff;
  163. if (littleEndian)
  164. {
  165. while (--numSamples >= 0)
  166. {
  167. *(uint16*) dest = ByteOrder::swapIfBigEndian ((uint16) (short) roundToInt (jlimit (-maxVal, maxVal, maxVal * *src++)));
  168. dest += dstStrideBytes;
  169. }
  170. }
  171. else
  172. {
  173. while (--numSamples >= 0)
  174. {
  175. *(uint16*) dest = ByteOrder::swapIfLittleEndian ((uint16) (short) roundToInt (jlimit (-maxVal, maxVal, maxVal * *src++)));
  176. dest += dstStrideBytes;
  177. }
  178. }
  179. }
  180. static void convertInt24ToFloat (const char* src, float* dest, int srcStrideBytes,
  181. int numSamples, bool littleEndian) noexcept
  182. {
  183. const double g = 1.0 / 0x7fffff;
  184. if (littleEndian)
  185. {
  186. while (--numSamples >= 0)
  187. {
  188. *dest++ = (float) (g * ByteOrder::littleEndian24Bit (src));
  189. src += srcStrideBytes;
  190. }
  191. }
  192. else
  193. {
  194. while (--numSamples >= 0)
  195. {
  196. *dest++ = (float) (g * ByteOrder::bigEndian24Bit (src));
  197. src += srcStrideBytes;
  198. }
  199. }
  200. }
  201. static void convertFloatToInt24 (const float* src, char* dest, int dstStrideBytes,
  202. int numSamples, bool littleEndian) noexcept
  203. {
  204. const double maxVal = (double) 0x7fffff;
  205. if (littleEndian)
  206. {
  207. while (--numSamples >= 0)
  208. {
  209. ByteOrder::littleEndian24BitToChars ((uint32) roundToInt (jlimit (-maxVal, maxVal, maxVal * *src++)), dest);
  210. dest += dstStrideBytes;
  211. }
  212. }
  213. else
  214. {
  215. while (--numSamples >= 0)
  216. {
  217. ByteOrder::bigEndian24BitToChars ((uint32) roundToInt (jlimit (-maxVal, maxVal, maxVal * *src++)), dest);
  218. dest += dstStrideBytes;
  219. }
  220. }
  221. }
  222. static void convertInt32ToFloat (const char* src, float* dest, int srcStrideBytes,
  223. int numSamples, bool littleEndian) noexcept
  224. {
  225. const double g = 1.0 / 0x7fffffff;
  226. if (littleEndian)
  227. {
  228. while (--numSamples >= 0)
  229. {
  230. *dest++ = (float) (g * (int) ByteOrder::littleEndianInt (src));
  231. src += srcStrideBytes;
  232. }
  233. }
  234. else
  235. {
  236. while (--numSamples >= 0)
  237. {
  238. *dest++ = (float) (g * (int) ByteOrder::bigEndianInt (src));
  239. src += srcStrideBytes;
  240. }
  241. }
  242. }
  243. static void convertFloatToInt32 (const float* src, char* dest, int dstStrideBytes,
  244. int numSamples, bool littleEndian) noexcept
  245. {
  246. const double maxVal = (double) 0x7fffffff;
  247. if (littleEndian)
  248. {
  249. while (--numSamples >= 0)
  250. {
  251. *(uint32*) dest = ByteOrder::swapIfBigEndian ((uint32) roundToInt (jlimit (-maxVal, maxVal, maxVal * *src++)));
  252. dest += dstStrideBytes;
  253. }
  254. }
  255. else
  256. {
  257. while (--numSamples >= 0)
  258. {
  259. *(uint32*) dest = ByteOrder::swapIfLittleEndian ((uint32) roundToInt (jlimit (-maxVal, maxVal, maxVal * *src++)));
  260. dest += dstStrideBytes;
  261. }
  262. }
  263. }
  264. };
  265. //==============================================================================
  266. constexpr int maxNumASIODevices = 16;
  267. class ASIOAudioIODevice;
  268. static ASIOAudioIODevice* currentASIODev[maxNumASIODevices] = {};
  269. extern HWND juce_messageWindowHandle;
  270. class ASIOAudioIODeviceType;
  271. static void sendASIODeviceChangeToListeners (ASIOAudioIODeviceType*);
  272. //==============================================================================
  273. class ASIOAudioIODevice : public AudioIODevice,
  274. private Timer
  275. {
  276. public:
  277. ASIOAudioIODevice (ASIOAudioIODeviceType* ownerType, const String& devName,
  278. CLSID clsID, int slotNumber)
  279. : AudioIODevice (devName, "ASIO"),
  280. owner (ownerType),
  281. classId (clsID)
  282. {
  283. ::CoInitialize (nullptr);
  284. name = devName;
  285. inBuffers.calloc (4);
  286. outBuffers.calloc (4);
  287. jassert (currentASIODev[slotNumber] == nullptr);
  288. currentASIODev[slotNumber] = this;
  289. openDevice();
  290. }
  291. ~ASIOAudioIODevice()
  292. {
  293. for (int i = 0; i < maxNumASIODevices; ++i)
  294. if (currentASIODev[i] == this)
  295. currentASIODev[i] = nullptr;
  296. close();
  297. JUCE_ASIO_LOG ("closed");
  298. if (! removeCurrentDriver())
  299. JUCE_ASIO_LOG ("** Driver crashed while being closed");
  300. }
  301. void updateSampleRates()
  302. {
  303. // find a list of sample rates..
  304. Array<double> newRates;
  305. if (asioObject != nullptr)
  306. {
  307. for (auto rate : { 8000, 11025, 16000, 22050, 32000,
  308. 44100, 48000, 88200, 96000, 176400,
  309. 192000, 352800, 384000, 705600, 768000 })
  310. if (asioObject->canSampleRate ((double) rate) == 0)
  311. newRates.add ((double) rate);
  312. }
  313. if (newRates.isEmpty())
  314. {
  315. auto cr = getSampleRate();
  316. JUCE_ASIO_LOG ("No sample rates supported - current rate: " + String ((int) cr));
  317. if (cr > 0)
  318. newRates.add ((int) cr);
  319. }
  320. if (sampleRates != newRates)
  321. {
  322. sampleRates.swapWith (newRates);
  323. #if JUCE_ASIO_DEBUGGING
  324. StringArray s;
  325. for (auto r : sampleRates)
  326. s.add (String (r));
  327. JUCE_ASIO_LOG ("Rates: " + s.joinIntoString (" "));
  328. #endif
  329. }
  330. }
  331. StringArray getOutputChannelNames() override { return outputChannelNames; }
  332. StringArray getInputChannelNames() override { return inputChannelNames; }
  333. Array<double> getAvailableSampleRates() override { return sampleRates; }
  334. Array<int> getAvailableBufferSizes() override { return bufferSizes; }
  335. int getDefaultBufferSize() override { return preferredBufferSize; }
  336. int getXRunCount() const noexcept override { return xruns; }
  337. String open (const BigInteger& inputChannels,
  338. const BigInteger& outputChannels,
  339. double sr, int bufferSizeSamples) override
  340. {
  341. if (isOpen())
  342. close();
  343. jassert (currentCallback == nullptr);
  344. if (bufferSizeSamples < 8 || bufferSizeSamples > 16384)
  345. shouldUsePreferredSize = true;
  346. if (asioObject == nullptr)
  347. {
  348. auto openingError = openDevice();
  349. if (asioObject == nullptr)
  350. return openingError;
  351. }
  352. isStarted = false;
  353. auto err = asioObject->getChannels (&totalNumInputChans, &totalNumOutputChans);
  354. jassert (err == ASE_OK);
  355. bufferSizeSamples = readBufferSizes (bufferSizeSamples);
  356. auto sampleRate = sr;
  357. currentSampleRate = sampleRate;
  358. currentBlockSizeSamples = bufferSizeSamples;
  359. currentChansOut.clear();
  360. currentChansIn.clear();
  361. updateSampleRates();
  362. if (sampleRate == 0 || (sampleRates.size() > 0 && ! sampleRates.contains (sampleRate)))
  363. sampleRate = sampleRates[0];
  364. if (sampleRate == 0)
  365. {
  366. jassertfalse;
  367. sampleRate = 44100.0;
  368. }
  369. updateClockSources();
  370. currentSampleRate = getSampleRate();
  371. error.clear();
  372. buffersCreated = false;
  373. setSampleRate (sampleRate);
  374. // (need to get this again in case a sample rate change affected the channel count)
  375. err = asioObject->getChannels (&totalNumInputChans, &totalNumOutputChans);
  376. jassert (err == ASE_OK);
  377. if (asioObject->future (kAsioCanReportOverload, nullptr) != ASE_OK)
  378. xruns = -1;
  379. inBuffers.calloc (totalNumInputChans + 8);
  380. outBuffers.calloc (totalNumOutputChans + 8);
  381. if (needToReset)
  382. {
  383. JUCE_ASIO_LOG (" Resetting");
  384. if (! removeCurrentDriver())
  385. JUCE_ASIO_LOG ("** Driver crashed while being closed");
  386. loadDriver();
  387. String initError = initDriver();
  388. if (initError.isNotEmpty())
  389. JUCE_ASIO_LOG ("ASIOInit: " + initError);
  390. setSampleRate (getSampleRate());
  391. needToReset = false;
  392. }
  393. auto totalBuffers = resetBuffers (inputChannels, outputChannels);
  394. setCallbackFunctions();
  395. JUCE_ASIO_LOG ("disposing buffers");
  396. err = asioObject->disposeBuffers();
  397. JUCE_ASIO_LOG ("creating buffers: " + String (totalBuffers) + ", " + String (currentBlockSizeSamples));
  398. err = asioObject->createBuffers (bufferInfos, totalBuffers, currentBlockSizeSamples, &callbacks);
  399. if (err != ASE_OK)
  400. {
  401. currentBlockSizeSamples = preferredBufferSize;
  402. JUCE_ASIO_LOG_ERROR ("create buffers 2", err);
  403. asioObject->disposeBuffers();
  404. err = asioObject->createBuffers (bufferInfos, totalBuffers, currentBlockSizeSamples, &callbacks);
  405. }
  406. if (err == ASE_OK)
  407. {
  408. buffersCreated = true;
  409. ioBufferSpace.calloc (totalBuffers * currentBlockSizeSamples + 32);
  410. int n = 0;
  411. Array<int> types;
  412. currentBitDepth = 16;
  413. for (int i = 0; i < (int) totalNumInputChans; ++i)
  414. {
  415. if (inputChannels[i])
  416. {
  417. inBuffers[n] = ioBufferSpace + (currentBlockSizeSamples * n);
  418. ASIOChannelInfo channelInfo = {};
  419. channelInfo.channel = i;
  420. channelInfo.isInput = 1;
  421. asioObject->getChannelInfo (&channelInfo);
  422. types.addIfNotAlreadyThere (channelInfo.type);
  423. inputFormat[n] = ASIOSampleFormat (channelInfo.type);
  424. currentBitDepth = jmax (currentBitDepth, inputFormat[n].bitDepth);
  425. ++n;
  426. }
  427. }
  428. jassert (numActiveInputChans == n);
  429. n = 0;
  430. for (int i = 0; i < (int) totalNumOutputChans; ++i)
  431. {
  432. if (outputChannels[i])
  433. {
  434. outBuffers[n] = ioBufferSpace + (currentBlockSizeSamples * (numActiveInputChans + n));
  435. ASIOChannelInfo channelInfo = {};
  436. channelInfo.channel = i;
  437. channelInfo.isInput = 0;
  438. asioObject->getChannelInfo (&channelInfo);
  439. types.addIfNotAlreadyThere (channelInfo.type);
  440. outputFormat[n] = ASIOSampleFormat (channelInfo.type);
  441. currentBitDepth = jmax (currentBitDepth, outputFormat[n].bitDepth);
  442. ++n;
  443. }
  444. }
  445. jassert (numActiveOutputChans == n);
  446. for (int i = types.size(); --i >= 0;)
  447. JUCE_ASIO_LOG ("channel format: " + String (types[i]));
  448. jassert (n <= totalBuffers);
  449. for (int i = 0; i < numActiveOutputChans; ++i)
  450. {
  451. outputFormat[i].clear (bufferInfos[numActiveInputChans + i].buffers[0], currentBlockSizeSamples);
  452. outputFormat[i].clear (bufferInfos[numActiveInputChans + i].buffers[1], currentBlockSizeSamples);
  453. }
  454. readLatencies();
  455. refreshBufferSizes();
  456. deviceIsOpen = true;
  457. JUCE_ASIO_LOG ("starting");
  458. calledback = false;
  459. err = asioObject->start();
  460. if (err != 0)
  461. {
  462. deviceIsOpen = false;
  463. JUCE_ASIO_LOG ("stop on failure");
  464. Thread::sleep (10);
  465. asioObject->stop();
  466. error = "Can't start device";
  467. Thread::sleep (10);
  468. }
  469. else
  470. {
  471. int count = 300;
  472. while (--count > 0 && ! calledback)
  473. Thread::sleep (10);
  474. isStarted = true;
  475. if (! calledback)
  476. {
  477. error = "Device didn't start correctly";
  478. JUCE_ASIO_LOG ("no callbacks - stopping..");
  479. asioObject->stop();
  480. }
  481. }
  482. }
  483. else
  484. {
  485. error = "Can't create i/o buffers";
  486. }
  487. if (error.isNotEmpty())
  488. {
  489. JUCE_ASIO_LOG_ERROR (error, err);
  490. disposeBuffers();
  491. Thread::sleep (20);
  492. isStarted = false;
  493. deviceIsOpen = false;
  494. auto errorCopy = error;
  495. close(); // (this resets the error string)
  496. error = errorCopy;
  497. }
  498. needToReset = false;
  499. return error;
  500. }
  501. void close() override
  502. {
  503. error.clear();
  504. stopTimer();
  505. stop();
  506. if (asioObject != nullptr && deviceIsOpen)
  507. {
  508. const ScopedLock sl (callbackLock);
  509. deviceIsOpen = false;
  510. isStarted = false;
  511. needToReset = false;
  512. JUCE_ASIO_LOG ("stopping");
  513. if (asioObject != nullptr)
  514. {
  515. Thread::sleep (20);
  516. asioObject->stop();
  517. Thread::sleep (10);
  518. disposeBuffers();
  519. }
  520. Thread::sleep (10);
  521. }
  522. }
  523. bool isOpen() override { return deviceIsOpen || insideControlPanelModalLoop; }
  524. bool isPlaying() override { return asioObject != nullptr && currentCallback != nullptr; }
  525. int getCurrentBufferSizeSamples() override { return currentBlockSizeSamples; }
  526. double getCurrentSampleRate() override { return currentSampleRate; }
  527. int getCurrentBitDepth() override { return currentBitDepth; }
  528. BigInteger getActiveOutputChannels() const override { return currentChansOut; }
  529. BigInteger getActiveInputChannels() const override { return currentChansIn; }
  530. int getOutputLatencyInSamples() override { return outputLatency; }
  531. int getInputLatencyInSamples() override { return inputLatency; }
  532. void start (AudioIODeviceCallback* callback) override
  533. {
  534. if (callback != nullptr)
  535. {
  536. callback->audioDeviceAboutToStart (this);
  537. const ScopedLock sl (callbackLock);
  538. currentCallback = callback;
  539. }
  540. }
  541. void stop() override
  542. {
  543. auto* lastCallback = currentCallback;
  544. {
  545. const ScopedLock sl (callbackLock);
  546. currentCallback = nullptr;
  547. }
  548. if (lastCallback != nullptr)
  549. lastCallback->audioDeviceStopped();
  550. }
  551. String getLastError() override { return error; }
  552. bool hasControlPanel() const override { return true; }
  553. bool showControlPanel() override
  554. {
  555. JUCE_ASIO_LOG ("showing control panel");
  556. bool done = false;
  557. insideControlPanelModalLoop = true;
  558. auto started = Time::getMillisecondCounter();
  559. if (asioObject != nullptr)
  560. {
  561. asioObject->controlPanel();
  562. auto spent = (int) (Time::getMillisecondCounter() - started);
  563. JUCE_ASIO_LOG ("spent: " + String (spent));
  564. if (spent > 300)
  565. {
  566. shouldUsePreferredSize = true;
  567. done = true;
  568. }
  569. }
  570. insideControlPanelModalLoop = false;
  571. return done;
  572. }
  573. void resetRequest() noexcept
  574. {
  575. startTimer (500);
  576. }
  577. void timerCallback() override
  578. {
  579. if (! insideControlPanelModalLoop)
  580. {
  581. stopTimer();
  582. JUCE_ASIO_LOG ("restart request!");
  583. auto* oldCallback = currentCallback;
  584. close();
  585. needToReset = true;
  586. open (BigInteger (currentChansIn), BigInteger (currentChansOut),
  587. currentSampleRate, currentBlockSizeSamples);
  588. reloadChannelNames();
  589. if (oldCallback != nullptr)
  590. start (oldCallback);
  591. sendASIODeviceChangeToListeners (owner);
  592. }
  593. else
  594. {
  595. startTimer (100);
  596. }
  597. }
  598. private:
  599. //==============================================================================
  600. WeakReference<ASIOAudioIODeviceType> owner;
  601. IASIO* asioObject = {};
  602. ASIOCallbacks callbacks;
  603. CLSID classId;
  604. String error;
  605. long totalNumInputChans = 0, totalNumOutputChans = 0;
  606. StringArray inputChannelNames, outputChannelNames;
  607. Array<double> sampleRates;
  608. Array<int> bufferSizes;
  609. long inputLatency = 0, outputLatency = 0;
  610. long minBufferSize = 0, maxBufferSize = 0, preferredBufferSize = 0, bufferGranularity = 0;
  611. ASIOClockSource clocks[32] = {};
  612. int numClockSources = 0;
  613. int currentBlockSizeSamples = 0;
  614. int currentBitDepth = 16;
  615. double currentSampleRate = 0;
  616. BigInteger currentChansOut, currentChansIn;
  617. AudioIODeviceCallback* currentCallback = {};
  618. CriticalSection callbackLock;
  619. HeapBlock<ASIOBufferInfo> bufferInfos;
  620. HeapBlock<float*> inBuffers, outBuffers;
  621. HeapBlock<float> ioBufferSpace;
  622. HeapBlock<ASIOSampleFormat> inputFormat, outputFormat;
  623. int numActiveInputChans = 0, numActiveOutputChans = 0;
  624. bool deviceIsOpen = false, isStarted = false, buffersCreated = false;
  625. std::atomic<bool> calledback { false };
  626. bool postOutput = true, needToReset = false;
  627. bool insideControlPanelModalLoop = false;
  628. bool shouldUsePreferredSize = false;
  629. int xruns = 0;
  630. //==============================================================================
  631. static String convertASIOString (char* text, int length)
  632. {
  633. if (CharPointer_UTF8::isValidString (text, length))
  634. return String::fromUTF8 (text, length);
  635. WCHAR wideVersion[512] = {};
  636. MultiByteToWideChar (CP_ACP, 0, text, length, wideVersion, numElementsInArray (wideVersion));
  637. return wideVersion;
  638. }
  639. String getChannelName (int index, bool isInput) const
  640. {
  641. ASIOChannelInfo channelInfo = {};
  642. channelInfo.channel = index;
  643. channelInfo.isInput = isInput ? 1 : 0;
  644. asioObject->getChannelInfo (&channelInfo);
  645. return convertASIOString (channelInfo.name, sizeof (channelInfo.name));
  646. }
  647. void reloadChannelNames()
  648. {
  649. long totalInChannels = 0, totalOutChannels = 0;
  650. if (asioObject != nullptr
  651. && asioObject->getChannels (&totalInChannels, &totalOutChannels) == ASE_OK)
  652. {
  653. totalNumInputChans = totalInChannels;
  654. totalNumOutputChans = totalOutChannels;
  655. inputChannelNames.clear();
  656. outputChannelNames.clear();
  657. for (int i = 0; i < totalNumInputChans; ++i)
  658. inputChannelNames.add (getChannelName (i, true));
  659. for (int i = 0; i < totalNumOutputChans; ++i)
  660. outputChannelNames.add (getChannelName (i, false));
  661. outputChannelNames.trim();
  662. inputChannelNames.trim();
  663. outputChannelNames.appendNumbersToDuplicates (false, true);
  664. inputChannelNames.appendNumbersToDuplicates (false, true);
  665. }
  666. }
  667. long refreshBufferSizes()
  668. {
  669. return asioObject->getBufferSize (&minBufferSize, &maxBufferSize, &preferredBufferSize, &bufferGranularity);
  670. }
  671. int readBufferSizes (int bufferSizeSamples)
  672. {
  673. minBufferSize = 0;
  674. maxBufferSize = 0;
  675. bufferGranularity = 0;
  676. long newPreferredSize = 0;
  677. if (asioObject->getBufferSize (&minBufferSize, &maxBufferSize, &newPreferredSize, &bufferGranularity) == ASE_OK)
  678. {
  679. if (preferredBufferSize != 0 && newPreferredSize != 0 && newPreferredSize != preferredBufferSize)
  680. shouldUsePreferredSize = true;
  681. if (bufferSizeSamples < minBufferSize || bufferSizeSamples > maxBufferSize)
  682. shouldUsePreferredSize = true;
  683. preferredBufferSize = newPreferredSize;
  684. }
  685. // unfortunate workaround for certain drivers which crash if you make
  686. // dynamic changes to the buffer size...
  687. shouldUsePreferredSize = shouldUsePreferredSize || getName().containsIgnoreCase ("Digidesign");
  688. if (shouldUsePreferredSize)
  689. {
  690. JUCE_ASIO_LOG ("Using preferred size for buffer..");
  691. auto err = refreshBufferSizes();
  692. if (err == ASE_OK)
  693. {
  694. bufferSizeSamples = (int) preferredBufferSize;
  695. }
  696. else
  697. {
  698. bufferSizeSamples = 1024;
  699. JUCE_ASIO_LOG_ERROR ("getBufferSize1", err);
  700. }
  701. shouldUsePreferredSize = false;
  702. }
  703. return bufferSizeSamples;
  704. }
  705. int resetBuffers (const BigInteger& inputChannels,
  706. const BigInteger& outputChannels)
  707. {
  708. numActiveInputChans = 0;
  709. numActiveOutputChans = 0;
  710. auto* info = bufferInfos.get();
  711. for (int i = 0; i < totalNumInputChans; ++i)
  712. {
  713. if (inputChannels[i])
  714. {
  715. currentChansIn.setBit (i);
  716. info->isInput = 1;
  717. info->channelNum = i;
  718. info->buffers[0] = info->buffers[1] = nullptr;
  719. ++info;
  720. ++numActiveInputChans;
  721. }
  722. }
  723. for (int i = 0; i < totalNumOutputChans; ++i)
  724. {
  725. if (outputChannels[i])
  726. {
  727. currentChansOut.setBit (i);
  728. info->isInput = 0;
  729. info->channelNum = i;
  730. info->buffers[0] = info->buffers[1] = nullptr;
  731. ++info;
  732. ++numActiveOutputChans;
  733. }
  734. }
  735. return numActiveInputChans + numActiveOutputChans;
  736. }
  737. void addBufferSizes (long minSize, long maxSize, long preferredSize, long granularity)
  738. {
  739. // find a list of buffer sizes..
  740. JUCE_ASIO_LOG (String ((int) minSize) + "->" + String ((int) maxSize) + ", "
  741. + String ((int) preferredSize) + ", " + String ((int) granularity));
  742. if (granularity >= 0)
  743. {
  744. granularity = jmax (16, (int) granularity);
  745. for (int i = jmax ((int) (minSize + 15) & ~15, (int) granularity); i < jmin (6400, (int) maxSize); i += granularity)
  746. bufferSizes.addIfNotAlreadyThere (granularity * (i / granularity));
  747. }
  748. else if (granularity < 0)
  749. {
  750. for (int i = 0; i < 18; ++i)
  751. {
  752. const int s = (1 << i);
  753. if (s >= minSize && s <= maxSize)
  754. bufferSizes.add (s);
  755. }
  756. }
  757. bufferSizes.addIfNotAlreadyThere (preferredSize);
  758. bufferSizes.sort();
  759. }
  760. double getSampleRate() const
  761. {
  762. double cr = 0;
  763. auto err = asioObject->getSampleRate (&cr);
  764. JUCE_ASIO_LOG_ERROR ("getSampleRate", err);
  765. return cr;
  766. }
  767. void setSampleRate (double newRate)
  768. {
  769. if (currentSampleRate != newRate)
  770. {
  771. JUCE_ASIO_LOG ("rate change: " + String (currentSampleRate) + " to " + String (newRate));
  772. auto err = asioObject->setSampleRate (newRate);
  773. if (err == ASE_NoClock && numClockSources > 0)
  774. {
  775. JUCE_ASIO_LOG ("trying to set a clock source..");
  776. Thread::sleep (10);
  777. err = asioObject->setClockSource (clocks[0].index);
  778. JUCE_ASIO_LOG_ERROR ("setClockSource2", err);
  779. Thread::sleep (10);
  780. err = asioObject->setSampleRate (newRate);
  781. }
  782. if (err == 0)
  783. currentSampleRate = newRate;
  784. // on fail, ignore the attempt to change rate, and run with the current one..
  785. }
  786. }
  787. void updateClockSources()
  788. {
  789. zeromem (clocks, sizeof (clocks));
  790. long numSources = numElementsInArray (clocks);
  791. asioObject->getClockSources (clocks, &numSources);
  792. numClockSources = (int) numSources;
  793. bool isSourceSet = false;
  794. // careful not to remove this loop because it does more than just logging!
  795. for (int i = 0; i < numClockSources; ++i)
  796. {
  797. String s ("clock: ");
  798. s += clocks[i].name;
  799. if (clocks[i].isCurrentSource)
  800. {
  801. isSourceSet = true;
  802. s << " (cur)";
  803. }
  804. JUCE_ASIO_LOG (s);
  805. }
  806. if (numClockSources > 1 && ! isSourceSet)
  807. {
  808. JUCE_ASIO_LOG ("setting clock source");
  809. auto err = asioObject->setClockSource (clocks[0].index);
  810. JUCE_ASIO_LOG_ERROR ("setClockSource1", err);
  811. Thread::sleep (20);
  812. }
  813. else
  814. {
  815. if (numClockSources == 0)
  816. JUCE_ASIO_LOG ("no clock sources!");
  817. }
  818. }
  819. void readLatencies()
  820. {
  821. inputLatency = outputLatency = 0;
  822. if (asioObject->getLatencies (&inputLatency, &outputLatency) != 0)
  823. JUCE_ASIO_LOG ("getLatencies() failed");
  824. else
  825. JUCE_ASIO_LOG ("Latencies: in = " + String ((int) inputLatency) + ", out = " + String ((int) outputLatency));
  826. }
  827. void createDummyBuffers (long preferredSize)
  828. {
  829. numActiveInputChans = 0;
  830. numActiveOutputChans = 0;
  831. auto* info = bufferInfos.get();
  832. int numChans = 0;
  833. for (int i = 0; i < jmin (2, (int) totalNumInputChans); ++i)
  834. {
  835. info->isInput = 1;
  836. info->channelNum = i;
  837. info->buffers[0] = info->buffers[1] = nullptr;
  838. ++info;
  839. ++numChans;
  840. }
  841. const int outputBufferIndex = numChans;
  842. for (int i = 0; i < jmin (2, (int) totalNumOutputChans); ++i)
  843. {
  844. info->isInput = 0;
  845. info->channelNum = i;
  846. info->buffers[0] = info->buffers[1] = nullptr;
  847. ++info;
  848. ++numChans;
  849. }
  850. setCallbackFunctions();
  851. JUCE_ASIO_LOG ("creating buffers (dummy): " + String (numChans) + ", " + String ((int) preferredSize));
  852. if (preferredSize > 0)
  853. {
  854. auto err = asioObject->createBuffers (bufferInfos, numChans, preferredSize, &callbacks);
  855. JUCE_ASIO_LOG_ERROR ("dummy buffers", err);
  856. }
  857. long newInps = 0, newOuts = 0;
  858. asioObject->getChannels (&newInps, &newOuts);
  859. if (totalNumInputChans != newInps || totalNumOutputChans != newOuts)
  860. {
  861. totalNumInputChans = newInps;
  862. totalNumOutputChans = newOuts;
  863. JUCE_ASIO_LOG (String ((int) totalNumInputChans) + " in; " + String ((int) totalNumOutputChans) + " out");
  864. }
  865. updateSampleRates();
  866. reloadChannelNames();
  867. for (int i = 0; i < totalNumOutputChans; ++i)
  868. {
  869. ASIOChannelInfo channelInfo = {};
  870. channelInfo.channel = i;
  871. channelInfo.isInput = 0;
  872. asioObject->getChannelInfo (&channelInfo);
  873. outputFormat[i] = ASIOSampleFormat (channelInfo.type);
  874. if (i < 2)
  875. {
  876. // clear the channels that are used with the dummy stuff
  877. outputFormat[i].clear (bufferInfos[outputBufferIndex + i].buffers[0], preferredBufferSize);
  878. outputFormat[i].clear (bufferInfos[outputBufferIndex + i].buffers[1], preferredBufferSize);
  879. }
  880. }
  881. }
  882. bool removeCurrentDriver()
  883. {
  884. bool releasedOK = true;
  885. if (asioObject != nullptr)
  886. {
  887. #if ! JUCE_MINGW
  888. __try
  889. #endif
  890. {
  891. asioObject->Release();
  892. }
  893. #if ! JUCE_MINGW
  894. __except (EXCEPTION_EXECUTE_HANDLER) { releasedOK = false; }
  895. #endif
  896. asioObject = nullptr;
  897. }
  898. return releasedOK;
  899. }
  900. bool loadDriver()
  901. {
  902. if (! removeCurrentDriver())
  903. JUCE_ASIO_LOG ("** Driver crashed while being closed");
  904. bool crashed = false;
  905. bool ok = tryCreatingDriver (crashed);
  906. if (crashed)
  907. JUCE_ASIO_LOG ("** Driver crashed while being opened");
  908. return ok;
  909. }
  910. bool tryCreatingDriver (bool& crashed)
  911. {
  912. #if ! JUCE_MINGW
  913. __try
  914. #endif
  915. {
  916. return CoCreateInstance (classId, 0, CLSCTX_INPROC_SERVER,
  917. classId, (void**) &asioObject) == S_OK;
  918. }
  919. #if ! JUCE_MINGW
  920. __except (EXCEPTION_EXECUTE_HANDLER) { crashed = true; }
  921. return false;
  922. #endif
  923. }
  924. String getLastDriverError() const
  925. {
  926. jassert (asioObject != nullptr);
  927. char buffer[512] = {};
  928. asioObject->getErrorMessage (buffer);
  929. return convertASIOString (buffer, sizeof (buffer));
  930. }
  931. String initDriver()
  932. {
  933. if (asioObject == nullptr)
  934. return "No Driver";
  935. auto initOk = (asioObject->init (juce_messageWindowHandle) > 0);
  936. String driverError;
  937. // Get error message if init() failed, or if it's a buggy Denon driver,
  938. // which returns true from init() even when it fails.
  939. if ((! initOk) || getName().containsIgnoreCase ("denon dj"))
  940. driverError = getLastDriverError();
  941. if ((! initOk) && driverError.isEmpty())
  942. driverError = "Driver failed to initialise";
  943. if (driverError.isEmpty())
  944. {
  945. char buffer[512] = {};
  946. asioObject->getDriverName (buffer); // just in case any flimsy drivers expect this to be called..
  947. }
  948. return driverError;
  949. }
  950. String openDevice()
  951. {
  952. // open the device and get its info..
  953. JUCE_ASIO_LOG ("opening device: " + getName());
  954. needToReset = false;
  955. outputChannelNames.clear();
  956. inputChannelNames.clear();
  957. bufferSizes.clear();
  958. sampleRates.clear();
  959. deviceIsOpen = false;
  960. totalNumInputChans = 0;
  961. totalNumOutputChans = 0;
  962. numActiveInputChans = 0;
  963. numActiveOutputChans = 0;
  964. xruns = 0;
  965. currentCallback = nullptr;
  966. error.clear();
  967. if (getName().isEmpty())
  968. return error;
  969. long err = 0;
  970. if (loadDriver())
  971. {
  972. if ((error = initDriver()).isEmpty())
  973. {
  974. numActiveInputChans = 0;
  975. numActiveOutputChans = 0;
  976. totalNumInputChans = 0;
  977. totalNumOutputChans = 0;
  978. if (asioObject != nullptr
  979. && (err = asioObject->getChannels (&totalNumInputChans, &totalNumOutputChans)) == 0)
  980. {
  981. JUCE_ASIO_LOG (String ((int) totalNumInputChans) + " in, " + String ((int) totalNumOutputChans) + " out");
  982. const int chansToAllocate = totalNumInputChans + totalNumOutputChans + 4;
  983. bufferInfos.calloc (chansToAllocate);
  984. inBuffers.calloc (chansToAllocate);
  985. outBuffers.calloc (chansToAllocate);
  986. inputFormat.calloc (chansToAllocate);
  987. outputFormat.calloc (chansToAllocate);
  988. if ((err = refreshBufferSizes()) == 0)
  989. {
  990. addBufferSizes (minBufferSize, maxBufferSize, preferredBufferSize, bufferGranularity);
  991. auto currentRate = getSampleRate();
  992. if (currentRate < 1.0 || currentRate > 192001.0)
  993. {
  994. JUCE_ASIO_LOG ("setting default sample rate");
  995. err = asioObject->setSampleRate (44100.0);
  996. JUCE_ASIO_LOG_ERROR ("setting sample rate", err);
  997. currentRate = getSampleRate();
  998. }
  999. currentSampleRate = currentRate;
  1000. postOutput = (asioObject->outputReady() == 0);
  1001. if (postOutput)
  1002. JUCE_ASIO_LOG ("outputReady true");
  1003. updateSampleRates();
  1004. readLatencies(); // ..doing these steps because cubase does so at this stage
  1005. createDummyBuffers (preferredBufferSize); // in initialisation, and some devices fail if we don't.
  1006. readLatencies();
  1007. // start and stop because cubase does it..
  1008. err = asioObject->start();
  1009. // ignore an error here, as it might start later after setting other stuff up
  1010. JUCE_ASIO_LOG_ERROR ("start", err);
  1011. Thread::sleep (80);
  1012. asioObject->stop();
  1013. }
  1014. else
  1015. {
  1016. error = "Can't detect buffer sizes";
  1017. }
  1018. }
  1019. else
  1020. {
  1021. error = "Can't detect asio channels";
  1022. }
  1023. }
  1024. }
  1025. else
  1026. {
  1027. error = "No such device";
  1028. }
  1029. if (error.isNotEmpty())
  1030. {
  1031. JUCE_ASIO_LOG_ERROR (error, err);
  1032. disposeBuffers();
  1033. if (! removeCurrentDriver())
  1034. JUCE_ASIO_LOG ("** Driver crashed while being closed");
  1035. }
  1036. else
  1037. {
  1038. JUCE_ASIO_LOG ("device open");
  1039. }
  1040. deviceIsOpen = false;
  1041. needToReset = false;
  1042. stopTimer();
  1043. return error;
  1044. }
  1045. void disposeBuffers()
  1046. {
  1047. if (asioObject != nullptr && buffersCreated)
  1048. {
  1049. buffersCreated = false;
  1050. asioObject->disposeBuffers();
  1051. }
  1052. }
  1053. //==============================================================================
  1054. void JUCE_ASIOCALLBACK callback (long index)
  1055. {
  1056. if (isStarted)
  1057. {
  1058. processBuffer (index);
  1059. }
  1060. else
  1061. {
  1062. if (postOutput && (asioObject != nullptr))
  1063. asioObject->outputReady();
  1064. }
  1065. calledback = true;
  1066. }
  1067. void processBuffer (long bufferIndex)
  1068. {
  1069. const ScopedLock sl (callbackLock);
  1070. if (bufferIndex >= 0)
  1071. {
  1072. auto* infos = bufferInfos.get();
  1073. auto samps = currentBlockSizeSamples;
  1074. if (currentCallback != nullptr)
  1075. {
  1076. for (int i = 0; i < numActiveInputChans; ++i)
  1077. {
  1078. jassert (inBuffers[i] != nullptr);
  1079. inputFormat[i].convertToFloat (infos[i].buffers[bufferIndex], inBuffers[i], samps);
  1080. }
  1081. currentCallback->audioDeviceIOCallback (const_cast<const float**> (inBuffers.getData()), numActiveInputChans,
  1082. outBuffers, numActiveOutputChans, samps);
  1083. for (int i = 0; i < numActiveOutputChans; ++i)
  1084. {
  1085. jassert (outBuffers[i] != nullptr);
  1086. outputFormat[i].convertFromFloat (outBuffers[i], infos[numActiveInputChans + i].buffers[bufferIndex], samps);
  1087. }
  1088. }
  1089. else
  1090. {
  1091. for (int i = 0; i < numActiveOutputChans; ++i)
  1092. outputFormat[i].clear (infos[numActiveInputChans + i].buffers[bufferIndex], samps);
  1093. }
  1094. }
  1095. if (postOutput)
  1096. asioObject->outputReady();
  1097. }
  1098. long asioMessagesCallback (long selector, long value)
  1099. {
  1100. switch (selector)
  1101. {
  1102. case kAsioSelectorSupported:
  1103. if (value == kAsioResetRequest || value == kAsioEngineVersion || value == kAsioResyncRequest
  1104. || value == kAsioLatenciesChanged || value == kAsioSupportsInputMonitor || value == kAsioOverload)
  1105. return 1;
  1106. break;
  1107. case kAsioBufferSizeChange: JUCE_ASIO_LOG ("kAsioBufferSizeChange"); resetRequest(); return 1;
  1108. case kAsioResetRequest: JUCE_ASIO_LOG ("kAsioResetRequest"); resetRequest(); return 1;
  1109. case kAsioResyncRequest: JUCE_ASIO_LOG ("kAsioResyncRequest"); resetRequest(); return 1;
  1110. case kAsioLatenciesChanged: JUCE_ASIO_LOG ("kAsioLatenciesChanged"); return 1;
  1111. case kAsioEngineVersion: return 2;
  1112. case kAsioSupportsTimeInfo:
  1113. case kAsioSupportsTimeCode: return 0;
  1114. case kAsioOverload: ++xruns; return 1;
  1115. }
  1116. return 0;
  1117. }
  1118. //==============================================================================
  1119. template <int deviceIndex>
  1120. struct ASIOCallbackFunctions
  1121. {
  1122. static ASIOTime* JUCE_ASIOCALLBACK bufferSwitchTimeInfoCallback (ASIOTime*, long index, long)
  1123. {
  1124. if (auto* d = currentASIODev[deviceIndex])
  1125. d->callback (index);
  1126. return {};
  1127. }
  1128. static void JUCE_ASIOCALLBACK bufferSwitchCallback (long index, long)
  1129. {
  1130. if (auto* d = currentASIODev[deviceIndex])
  1131. d->callback (index);
  1132. }
  1133. static long JUCE_ASIOCALLBACK asioMessagesCallback (long selector, long value, void*, double*)
  1134. {
  1135. if (auto* d = currentASIODev[deviceIndex])
  1136. return d->asioMessagesCallback (selector, value);
  1137. return {};
  1138. }
  1139. static void JUCE_ASIOCALLBACK sampleRateChangedCallback (ASIOSampleRate)
  1140. {
  1141. if (auto* d = currentASIODev[deviceIndex])
  1142. d->resetRequest();
  1143. }
  1144. static void setCallbacks (ASIOCallbacks& callbacks) noexcept
  1145. {
  1146. callbacks.bufferSwitch = &bufferSwitchCallback;
  1147. callbacks.asioMessage = &asioMessagesCallback;
  1148. callbacks.bufferSwitchTimeInfo = &bufferSwitchTimeInfoCallback;
  1149. callbacks.sampleRateDidChange = &sampleRateChangedCallback;
  1150. }
  1151. static void setCallbacksForDevice (ASIOCallbacks& callbacks, ASIOAudioIODevice* device) noexcept
  1152. {
  1153. if (currentASIODev[deviceIndex] == device)
  1154. setCallbacks (callbacks);
  1155. else
  1156. ASIOCallbackFunctions<deviceIndex + 1>::setCallbacksForDevice (callbacks, device);
  1157. }
  1158. };
  1159. void setCallbackFunctions() noexcept
  1160. {
  1161. ASIOCallbackFunctions<0>::setCallbacksForDevice (callbacks, this);
  1162. }
  1163. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ASIOAudioIODevice)
  1164. };
  1165. template <>
  1166. struct ASIOAudioIODevice::ASIOCallbackFunctions<maxNumASIODevices>
  1167. {
  1168. static void setCallbacksForDevice (ASIOCallbacks&, ASIOAudioIODevice*) noexcept {}
  1169. };
  1170. //==============================================================================
  1171. class ASIOAudioIODeviceType : public AudioIODeviceType
  1172. {
  1173. public:
  1174. ASIOAudioIODeviceType() : AudioIODeviceType ("ASIO") {}
  1175. //==============================================================================
  1176. void scanForDevices() override
  1177. {
  1178. hasScanned = true;
  1179. deviceNames.clear();
  1180. classIds.clear();
  1181. HKEY hk = 0;
  1182. int index = 0;
  1183. if (RegOpenKey (HKEY_LOCAL_MACHINE, _T("software\\asio"), &hk) == ERROR_SUCCESS)
  1184. {
  1185. TCHAR name[256] = {};
  1186. while (RegEnumKey (hk, index++, name, numElementsInArray (name)) == ERROR_SUCCESS)
  1187. {
  1188. if (isBlacklistedDriver (name))
  1189. continue;
  1190. addDriverInfo (name, hk);
  1191. }
  1192. RegCloseKey (hk);
  1193. }
  1194. }
  1195. StringArray getDeviceNames (bool /*wantInputNames*/) const override
  1196. {
  1197. jassert (hasScanned); // need to call scanForDevices() before doing this
  1198. return deviceNames;
  1199. }
  1200. int getDefaultDeviceIndex (bool) const override
  1201. {
  1202. jassert (hasScanned); // need to call scanForDevices() before doing this
  1203. for (int i = deviceNames.size(); --i >= 0;)
  1204. if (deviceNames[i].containsIgnoreCase ("asio4all"))
  1205. return i; // asio4all is a safe choice for a default..
  1206. #if JUCE_DEBUG
  1207. if (deviceNames.size() > 1 && deviceNames[0].containsIgnoreCase ("digidesign"))
  1208. return 1; // (the digi m-box driver crashes the app when you run
  1209. // it in the debugger, which can be a bit annoying)
  1210. #endif
  1211. return 0;
  1212. }
  1213. static int findFreeSlot()
  1214. {
  1215. for (int i = 0; i < maxNumASIODevices; ++i)
  1216. if (currentASIODev[i] == nullptr)
  1217. return i;
  1218. jassertfalse; // unfortunately you can only have a finite number
  1219. // of ASIO devices open at the same time..
  1220. return -1;
  1221. }
  1222. int getIndexOfDevice (AudioIODevice* d, bool /*asInput*/) const override
  1223. {
  1224. jassert (hasScanned); // need to call scanForDevices() before doing this
  1225. return d == nullptr ? -1 : deviceNames.indexOf (d->getName());
  1226. }
  1227. bool hasSeparateInputsAndOutputs() const override { return false; }
  1228. AudioIODevice* createDevice (const String& outputDeviceName,
  1229. const String& inputDeviceName) override
  1230. {
  1231. // ASIO can't open two different devices for input and output - they must be the same one.
  1232. jassert (inputDeviceName == outputDeviceName || outputDeviceName.isEmpty() || inputDeviceName.isEmpty());
  1233. jassert (hasScanned); // need to call scanForDevices() before doing this
  1234. auto deviceName = outputDeviceName.isNotEmpty() ? outputDeviceName
  1235. : inputDeviceName;
  1236. auto index = deviceNames.indexOf (deviceName);
  1237. if (index >= 0)
  1238. {
  1239. auto freeSlot = findFreeSlot();
  1240. if (freeSlot >= 0)
  1241. return new ASIOAudioIODevice (this, deviceName,
  1242. classIds.getReference (index), freeSlot);
  1243. }
  1244. return nullptr;
  1245. }
  1246. void sendDeviceChangeToListeners()
  1247. {
  1248. callDeviceChangeListeners();
  1249. }
  1250. JUCE_DECLARE_WEAK_REFERENCEABLE (ASIOAudioIODeviceType)
  1251. private:
  1252. StringArray deviceNames;
  1253. Array<CLSID> classIds;
  1254. bool hasScanned = false;
  1255. //==============================================================================
  1256. static bool checkClassIsOk (const String& classId)
  1257. {
  1258. HKEY hk = 0;
  1259. bool ok = false;
  1260. if (RegOpenKey (HKEY_CLASSES_ROOT, _T("clsid"), &hk) == ERROR_SUCCESS)
  1261. {
  1262. int index = 0;
  1263. TCHAR name[512] = {};
  1264. while (RegEnumKey (hk, index++, name, numElementsInArray (name)) == ERROR_SUCCESS)
  1265. {
  1266. if (classId.equalsIgnoreCase (name))
  1267. {
  1268. HKEY subKey, pathKey;
  1269. if (RegOpenKeyEx (hk, name, 0, KEY_READ, &subKey) == ERROR_SUCCESS)
  1270. {
  1271. if (RegOpenKeyEx (subKey, _T("InprocServer32"), 0, KEY_READ, &pathKey) == ERROR_SUCCESS)
  1272. {
  1273. TCHAR pathName[1024] = {};
  1274. DWORD dtype = REG_SZ;
  1275. DWORD dsize = sizeof (pathName);
  1276. if (RegQueryValueEx (pathKey, 0, 0, &dtype, (LPBYTE) pathName, &dsize) == ERROR_SUCCESS)
  1277. // In older code, this used to check for the existence of the file, but there are situations
  1278. // where our process doesn't have access to it, but where the driver still loads ok..
  1279. ok = (pathName[0] != 0);
  1280. RegCloseKey (pathKey);
  1281. }
  1282. RegCloseKey (subKey);
  1283. }
  1284. break;
  1285. }
  1286. }
  1287. RegCloseKey (hk);
  1288. }
  1289. return ok;
  1290. }
  1291. //==============================================================================
  1292. static bool isBlacklistedDriver (const String& driverName)
  1293. {
  1294. return driverName.startsWith ("ASIO DirectX Full Duplex") || driverName == "ASIO Multimedia Driver";
  1295. }
  1296. void addDriverInfo (const String& keyName, HKEY hk)
  1297. {
  1298. HKEY subKey;
  1299. if (RegOpenKeyEx (hk, keyName.toWideCharPointer(), 0, KEY_READ, &subKey) == ERROR_SUCCESS)
  1300. {
  1301. TCHAR buf[256] = {};
  1302. DWORD dtype = REG_SZ;
  1303. DWORD dsize = sizeof (buf);
  1304. if (RegQueryValueEx (subKey, _T("clsid"), 0, &dtype, (LPBYTE) buf, &dsize) == ERROR_SUCCESS)
  1305. {
  1306. if (dsize > 0 && checkClassIsOk (buf))
  1307. {
  1308. CLSID classId;
  1309. if (CLSIDFromString ((LPOLESTR) buf, &classId) == S_OK)
  1310. {
  1311. dtype = REG_SZ;
  1312. dsize = sizeof (buf);
  1313. String deviceName;
  1314. if (RegQueryValueEx (subKey, _T("description"), 0, &dtype, (LPBYTE) buf, &dsize) == ERROR_SUCCESS)
  1315. deviceName = buf;
  1316. else
  1317. deviceName = keyName;
  1318. JUCE_ASIO_LOG ("found " + deviceName);
  1319. deviceNames.add (deviceName);
  1320. classIds.add (classId);
  1321. }
  1322. }
  1323. RegCloseKey (subKey);
  1324. }
  1325. }
  1326. }
  1327. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ASIOAudioIODeviceType)
  1328. };
  1329. void sendASIODeviceChangeToListeners (ASIOAudioIODeviceType* type)
  1330. {
  1331. if (type != nullptr)
  1332. type->sendDeviceChangeToListeners();
  1333. }
  1334. } // namespace juce