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.

juce_linux_ALSA.cpp 46KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314
  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2017 - ROLI Ltd.
  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. namespace
  20. {
  21. #ifndef JUCE_ALSA_LOGGING
  22. #define JUCE_ALSA_LOGGING 0
  23. #endif
  24. #if JUCE_ALSA_LOGGING
  25. #define JUCE_ALSA_LOG(dbgtext) { juce::String tempDbgBuf ("ALSA: "); tempDbgBuf << dbgtext; Logger::writeToLog (tempDbgBuf); DBG (tempDbgBuf); }
  26. #define JUCE_CHECKED_RESULT(x) (logErrorMessage (x, __LINE__))
  27. static int logErrorMessage (int err, int lineNum)
  28. {
  29. if (err < 0)
  30. JUCE_ALSA_LOG ("Error: line " << lineNum << ": code " << err << " (" << snd_strerror (err) << ")");
  31. return err;
  32. }
  33. #else
  34. #define JUCE_ALSA_LOG(x) {}
  35. #define JUCE_CHECKED_RESULT(x) (x)
  36. #endif
  37. #define JUCE_ALSA_FAILED(x) failed (x)
  38. static void getDeviceSampleRates (snd_pcm_t* handle, Array<double>& rates)
  39. {
  40. const int ratesToTry[] = { 22050, 32000, 44100, 48000, 88200, 96000, 176400, 192000, 0 };
  41. snd_pcm_hw_params_t* hwParams;
  42. snd_pcm_hw_params_alloca (&hwParams);
  43. for (int i = 0; ratesToTry[i] != 0; ++i)
  44. {
  45. if (snd_pcm_hw_params_any (handle, hwParams) >= 0
  46. && snd_pcm_hw_params_test_rate (handle, hwParams, (unsigned int) ratesToTry[i], 0) == 0)
  47. {
  48. rates.addIfNotAlreadyThere ((double) ratesToTry[i]);
  49. }
  50. }
  51. }
  52. static void getDeviceNumChannels (snd_pcm_t* handle, unsigned int* minChans, unsigned int* maxChans)
  53. {
  54. snd_pcm_hw_params_t *params;
  55. snd_pcm_hw_params_alloca (&params);
  56. if (snd_pcm_hw_params_any (handle, params) >= 0)
  57. {
  58. snd_pcm_hw_params_get_channels_min (params, minChans);
  59. snd_pcm_hw_params_get_channels_max (params, maxChans);
  60. JUCE_ALSA_LOG ("getDeviceNumChannels: " << (int) *minChans << " " << (int) *maxChans);
  61. // some virtual devices (dmix for example) report 10000 channels , we have to clamp these values
  62. *maxChans = jmin (*maxChans, 256u);
  63. *minChans = jmin (*minChans, *maxChans);
  64. }
  65. else
  66. {
  67. JUCE_ALSA_LOG ("getDeviceNumChannels failed");
  68. }
  69. }
  70. static void getDeviceProperties (const String& deviceID,
  71. unsigned int& minChansOut,
  72. unsigned int& maxChansOut,
  73. unsigned int& minChansIn,
  74. unsigned int& maxChansIn,
  75. Array<double>& rates,
  76. bool testOutput,
  77. bool testInput)
  78. {
  79. minChansOut = maxChansOut = minChansIn = maxChansIn = 0;
  80. if (deviceID.isEmpty())
  81. return;
  82. JUCE_ALSA_LOG ("getDeviceProperties(" << deviceID.toUTF8().getAddress() << ")");
  83. snd_pcm_info_t* info;
  84. snd_pcm_info_alloca (&info);
  85. if (testOutput)
  86. {
  87. snd_pcm_t* pcmHandle;
  88. if (JUCE_CHECKED_RESULT (snd_pcm_open (&pcmHandle, deviceID.toUTF8().getAddress(), SND_PCM_STREAM_PLAYBACK, SND_PCM_NONBLOCK)) >= 0)
  89. {
  90. getDeviceNumChannels (pcmHandle, &minChansOut, &maxChansOut);
  91. getDeviceSampleRates (pcmHandle, rates);
  92. snd_pcm_close (pcmHandle);
  93. }
  94. }
  95. if (testInput)
  96. {
  97. snd_pcm_t* pcmHandle;
  98. if (JUCE_CHECKED_RESULT (snd_pcm_open (&pcmHandle, deviceID.toUTF8(), SND_PCM_STREAM_CAPTURE, SND_PCM_NONBLOCK) >= 0))
  99. {
  100. getDeviceNumChannels (pcmHandle, &minChansIn, &maxChansIn);
  101. if (rates.size() == 0)
  102. getDeviceSampleRates (pcmHandle, rates);
  103. snd_pcm_close (pcmHandle);
  104. }
  105. }
  106. }
  107. static void ensureMinimumNumBitsSet (BigInteger& chans, int minNumChans)
  108. {
  109. int i = 0;
  110. while (chans.countNumberOfSetBits() < minNumChans)
  111. chans.setBit (i++);
  112. }
  113. static void silentErrorHandler (const char*, int, const char*, int, const char*,...) {}
  114. //==============================================================================
  115. class ALSADevice
  116. {
  117. public:
  118. ALSADevice (const String& devID, bool forInput)
  119. : handle (nullptr),
  120. bitDepth (16),
  121. numChannelsRunning (0),
  122. latency (0),
  123. deviceID (devID),
  124. isInput (forInput),
  125. isInterleaved (true)
  126. {
  127. JUCE_ALSA_LOG ("snd_pcm_open (" << deviceID.toUTF8().getAddress() << ", forInput=" << (int) forInput << ")");
  128. int err = snd_pcm_open (&handle, deviceID.toUTF8(),
  129. forInput ? SND_PCM_STREAM_CAPTURE : SND_PCM_STREAM_PLAYBACK,
  130. SND_PCM_ASYNC);
  131. if (err < 0)
  132. {
  133. if (-err == EBUSY)
  134. error << "The device \"" << deviceID << "\" is busy (another application is using it).";
  135. else if (-err == ENOENT)
  136. error << "The device \"" << deviceID << "\" is not available.";
  137. else
  138. error << "Could not open " << (forInput ? "input" : "output") << " device \"" << deviceID
  139. << "\": " << snd_strerror(err) << " (" << err << ")";
  140. JUCE_ALSA_LOG ("snd_pcm_open failed; " << error);
  141. }
  142. }
  143. ~ALSADevice()
  144. {
  145. closeNow();
  146. }
  147. void closeNow()
  148. {
  149. if (handle != nullptr)
  150. {
  151. snd_pcm_close (handle);
  152. handle = nullptr;
  153. }
  154. }
  155. bool setParameters (unsigned int sampleRate, int numChannels, int bufferSize)
  156. {
  157. if (handle == nullptr)
  158. return false;
  159. JUCE_ALSA_LOG ("ALSADevice::setParameters(" << deviceID << ", "
  160. << (int) sampleRate << ", " << numChannels << ", " << bufferSize << ")");
  161. snd_pcm_hw_params_t* hwParams;
  162. snd_pcm_hw_params_alloca (&hwParams);
  163. if (snd_pcm_hw_params_any (handle, hwParams) < 0)
  164. {
  165. // this is the error message that aplay returns when an error happens here,
  166. // it is a bit more explicit that "Invalid parameter"
  167. error = "Broken configuration for this PCM: no configurations available";
  168. return false;
  169. }
  170. if (snd_pcm_hw_params_set_access (handle, hwParams, SND_PCM_ACCESS_RW_INTERLEAVED) >= 0) // works better for plughw..
  171. isInterleaved = true;
  172. else if (snd_pcm_hw_params_set_access (handle, hwParams, SND_PCM_ACCESS_RW_NONINTERLEAVED) >= 0)
  173. isInterleaved = false;
  174. else
  175. {
  176. jassertfalse;
  177. return false;
  178. }
  179. enum { isFloatBit = 1 << 16, isLittleEndianBit = 1 << 17, onlyUseLower24Bits = 1 << 18 };
  180. const int formatsToTry[] = { SND_PCM_FORMAT_FLOAT_LE, 32 | isFloatBit | isLittleEndianBit,
  181. SND_PCM_FORMAT_FLOAT_BE, 32 | isFloatBit,
  182. SND_PCM_FORMAT_S32_LE, 32 | isLittleEndianBit,
  183. SND_PCM_FORMAT_S32_BE, 32,
  184. SND_PCM_FORMAT_S24_3LE, 24 | isLittleEndianBit,
  185. SND_PCM_FORMAT_S24_3BE, 24,
  186. SND_PCM_FORMAT_S24_LE, 32 | isLittleEndianBit | onlyUseLower24Bits,
  187. SND_PCM_FORMAT_S16_LE, 16 | isLittleEndianBit,
  188. SND_PCM_FORMAT_S16_BE, 16 };
  189. bitDepth = 0;
  190. for (int i = 0; i < numElementsInArray (formatsToTry); i += 2)
  191. {
  192. if (snd_pcm_hw_params_set_format (handle, hwParams, (_snd_pcm_format) formatsToTry [i]) >= 0)
  193. {
  194. const int type = formatsToTry [i + 1];
  195. bitDepth = type & 255;
  196. converter = createConverter (isInput, bitDepth,
  197. (type & isFloatBit) != 0,
  198. (type & isLittleEndianBit) != 0,
  199. (type & onlyUseLower24Bits) != 0,
  200. numChannels,
  201. isInterleaved);
  202. break;
  203. }
  204. }
  205. if (bitDepth == 0)
  206. {
  207. error = "device doesn't support a compatible PCM format";
  208. JUCE_ALSA_LOG ("Error: " + error);
  209. return false;
  210. }
  211. int dir = 0;
  212. unsigned int periods = 4;
  213. snd_pcm_uframes_t samplesPerPeriod = (snd_pcm_uframes_t) bufferSize;
  214. if (JUCE_ALSA_FAILED (snd_pcm_hw_params_set_rate_near (handle, hwParams, &sampleRate, 0))
  215. || JUCE_ALSA_FAILED (snd_pcm_hw_params_set_channels (handle, hwParams, (unsigned int ) numChannels))
  216. || JUCE_ALSA_FAILED (snd_pcm_hw_params_set_periods_near (handle, hwParams, &periods, &dir))
  217. || JUCE_ALSA_FAILED (snd_pcm_hw_params_set_period_size_near (handle, hwParams, &samplesPerPeriod, &dir))
  218. || JUCE_ALSA_FAILED (snd_pcm_hw_params (handle, hwParams)))
  219. {
  220. return false;
  221. }
  222. snd_pcm_uframes_t frames = 0;
  223. if (JUCE_ALSA_FAILED (snd_pcm_hw_params_get_period_size (hwParams, &frames, &dir))
  224. || JUCE_ALSA_FAILED (snd_pcm_hw_params_get_periods (hwParams, &periods, &dir)))
  225. latency = 0;
  226. else
  227. latency = (int) frames * ((int) periods - 1); // (this is the method JACK uses to guess the latency..)
  228. JUCE_ALSA_LOG ("frames: " << (int) frames << ", periods: " << (int) periods
  229. << ", samplesPerPeriod: " << (int) samplesPerPeriod);
  230. snd_pcm_sw_params_t* swParams;
  231. snd_pcm_sw_params_alloca (&swParams);
  232. snd_pcm_uframes_t boundary;
  233. if (JUCE_ALSA_FAILED (snd_pcm_sw_params_current (handle, swParams))
  234. || JUCE_ALSA_FAILED (snd_pcm_sw_params_get_boundary (swParams, &boundary))
  235. || JUCE_ALSA_FAILED (snd_pcm_sw_params_set_silence_threshold (handle, swParams, 0))
  236. || JUCE_ALSA_FAILED (snd_pcm_sw_params_set_silence_size (handle, swParams, boundary))
  237. || JUCE_ALSA_FAILED (snd_pcm_sw_params_set_start_threshold (handle, swParams, samplesPerPeriod))
  238. || JUCE_ALSA_FAILED (snd_pcm_sw_params_set_stop_threshold (handle, swParams, boundary))
  239. || JUCE_ALSA_FAILED (snd_pcm_sw_params (handle, swParams)))
  240. {
  241. return false;
  242. }
  243. #if JUCE_ALSA_LOGGING
  244. // enable this to dump the config of the devices that get opened
  245. snd_output_t* out;
  246. snd_output_stdio_attach (&out, stderr, 0);
  247. snd_pcm_hw_params_dump (hwParams, out);
  248. snd_pcm_sw_params_dump (swParams, out);
  249. #endif
  250. numChannelsRunning = numChannels;
  251. return true;
  252. }
  253. //==============================================================================
  254. bool writeToOutputDevice (AudioSampleBuffer& outputChannelBuffer, const int numSamples)
  255. {
  256. jassert (numChannelsRunning <= outputChannelBuffer.getNumChannels());
  257. float* const* const data = outputChannelBuffer.getArrayOfWritePointers();
  258. snd_pcm_sframes_t numDone = 0;
  259. if (isInterleaved)
  260. {
  261. scratch.ensureSize ((size_t) ((int) sizeof (float) * numSamples * numChannelsRunning), false);
  262. for (int i = 0; i < numChannelsRunning; ++i)
  263. converter->convertSamples (scratch.getData(), i, data[i], 0, numSamples);
  264. numDone = snd_pcm_writei (handle, scratch.getData(), (snd_pcm_uframes_t) numSamples);
  265. }
  266. else
  267. {
  268. for (int i = 0; i < numChannelsRunning; ++i)
  269. converter->convertSamples (data[i], data[i], numSamples);
  270. numDone = snd_pcm_writen (handle, (void**) data, (snd_pcm_uframes_t) numSamples);
  271. }
  272. if (numDone < 0)
  273. {
  274. if (numDone == -(EPIPE))
  275. underrunCount++;
  276. if (JUCE_ALSA_FAILED (snd_pcm_recover (handle, (int) numDone, 1 /* silent */)))
  277. return false;
  278. }
  279. if (numDone < numSamples)
  280. JUCE_ALSA_LOG ("Did not write all samples: numDone: " << numDone << ", numSamples: " << numSamples);
  281. return true;
  282. }
  283. bool readFromInputDevice (AudioSampleBuffer& inputChannelBuffer, const int numSamples)
  284. {
  285. jassert (numChannelsRunning <= inputChannelBuffer.getNumChannels());
  286. float* const* const data = inputChannelBuffer.getArrayOfWritePointers();
  287. if (isInterleaved)
  288. {
  289. scratch.ensureSize ((size_t) ((int) sizeof (float) * numSamples * numChannelsRunning), false);
  290. scratch.fillWith (0); // (not clearing this data causes warnings in valgrind)
  291. snd_pcm_sframes_t num = snd_pcm_readi (handle, scratch.getData(), (snd_pcm_uframes_t) numSamples);
  292. if (num < 0)
  293. {
  294. if (num == -(EPIPE))
  295. overrunCount++;
  296. if (JUCE_ALSA_FAILED (snd_pcm_recover (handle, (int) num, 1 /* silent */)))
  297. return false;
  298. }
  299. if (num < numSamples)
  300. JUCE_ALSA_LOG ("Did not read all samples: num: " << num << ", numSamples: " << numSamples);
  301. for (int i = 0; i < numChannelsRunning; ++i)
  302. converter->convertSamples (data[i], 0, scratch.getData(), i, numSamples);
  303. }
  304. else
  305. {
  306. snd_pcm_sframes_t num = snd_pcm_readn (handle, (void**) data, (snd_pcm_uframes_t) numSamples);
  307. if (num < 0)
  308. {
  309. if (num == -(EPIPE))
  310. overrunCount++;
  311. if (JUCE_ALSA_FAILED (snd_pcm_recover (handle, (int) num, 1 /* silent */)))
  312. return false;
  313. }
  314. if (num < numSamples)
  315. JUCE_ALSA_LOG ("Did not read all samples: num: " << num << ", numSamples: " << numSamples);
  316. for (int i = 0; i < numChannelsRunning; ++i)
  317. converter->convertSamples (data[i], data[i], numSamples);
  318. }
  319. return true;
  320. }
  321. //==============================================================================
  322. snd_pcm_t* handle;
  323. String error;
  324. int bitDepth, numChannelsRunning, latency;
  325. int underrunCount = 0, overrunCount = 0;
  326. private:
  327. //==============================================================================
  328. String deviceID;
  329. const bool isInput;
  330. bool isInterleaved;
  331. MemoryBlock scratch;
  332. ScopedPointer<AudioData::Converter> converter;
  333. //==============================================================================
  334. template <class SampleType>
  335. struct ConverterHelper
  336. {
  337. static AudioData::Converter* createConverter (const bool forInput, const bool isLittleEndian, const int numInterleavedChannels, bool interleaved)
  338. {
  339. if (interleaved)
  340. return create<AudioData::Interleaved> (forInput, isLittleEndian, numInterleavedChannels);
  341. return create<AudioData::NonInterleaved> (forInput, isLittleEndian, numInterleavedChannels);
  342. }
  343. private:
  344. template <class InterleavedType>
  345. static AudioData::Converter* create (const bool forInput, const bool isLittleEndian, const int numInterleavedChannels)
  346. {
  347. if (forInput)
  348. {
  349. typedef AudioData::Pointer <AudioData::Float32, AudioData::NativeEndian, AudioData::NonInterleaved, AudioData::NonConst> DestType;
  350. if (isLittleEndian)
  351. return new AudioData::ConverterInstance <AudioData::Pointer <SampleType, AudioData::LittleEndian, InterleavedType, AudioData::Const>, DestType> (numInterleavedChannels, 1);
  352. return new AudioData::ConverterInstance <AudioData::Pointer <SampleType, AudioData::BigEndian, InterleavedType, AudioData::Const>, DestType> (numInterleavedChannels, 1);
  353. }
  354. typedef AudioData::Pointer <AudioData::Float32, AudioData::NativeEndian, AudioData::NonInterleaved, AudioData::Const> SourceType;
  355. if (isLittleEndian)
  356. return new AudioData::ConverterInstance <SourceType, AudioData::Pointer <SampleType, AudioData::LittleEndian, InterleavedType, AudioData::NonConst> > (1, numInterleavedChannels);
  357. return new AudioData::ConverterInstance <SourceType, AudioData::Pointer <SampleType, AudioData::BigEndian, InterleavedType, AudioData::NonConst> > (1, numInterleavedChannels);
  358. }
  359. };
  360. static AudioData::Converter* createConverter (bool forInput, int bitDepth,
  361. bool isFloat, bool isLittleEndian, bool useOnlyLower24Bits,
  362. int numInterleavedChannels,
  363. bool interleaved)
  364. {
  365. JUCE_ALSA_LOG ("format: bitDepth=" << bitDepth << ", isFloat=" << (int) isFloat
  366. << ", isLittleEndian=" << (int) isLittleEndian << ", numChannels=" << numInterleavedChannels);
  367. if (isFloat) return ConverterHelper <AudioData::Float32>::createConverter (forInput, isLittleEndian, numInterleavedChannels, interleaved);
  368. if (bitDepth == 16) return ConverterHelper <AudioData::Int16> ::createConverter (forInput, isLittleEndian, numInterleavedChannels, interleaved);
  369. if (bitDepth == 24) return ConverterHelper <AudioData::Int24> ::createConverter (forInput, isLittleEndian, numInterleavedChannels, interleaved);
  370. jassert (bitDepth == 32);
  371. if (useOnlyLower24Bits)
  372. return ConverterHelper <AudioData::Int24in32>::createConverter (forInput, isLittleEndian, numInterleavedChannels, interleaved);
  373. return ConverterHelper <AudioData::Int32>::createConverter (forInput, isLittleEndian, numInterleavedChannels, interleaved);
  374. }
  375. //==============================================================================
  376. bool failed (const int errorNum)
  377. {
  378. if (errorNum >= 0)
  379. return false;
  380. error = snd_strerror (errorNum);
  381. JUCE_ALSA_LOG ("ALSA error: " << error);
  382. return true;
  383. }
  384. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ALSADevice)
  385. };
  386. //==============================================================================
  387. class ALSAThread : public Thread
  388. {
  389. public:
  390. ALSAThread (const String& inputDeviceID, const String& outputDeviceID)
  391. : Thread ("Juce ALSA"),
  392. sampleRate (0),
  393. bufferSize (0),
  394. outputLatency (0),
  395. inputLatency (0),
  396. callback (0),
  397. inputId (inputDeviceID),
  398. outputId (outputDeviceID),
  399. numCallbacks (0),
  400. audioIoInProgress (false),
  401. inputChannelBuffer (1, 1),
  402. outputChannelBuffer (1, 1)
  403. {
  404. initialiseRatesAndChannels();
  405. }
  406. ~ALSAThread()
  407. {
  408. close();
  409. }
  410. void open (BigInteger inputChannels,
  411. BigInteger outputChannels,
  412. const double newSampleRate,
  413. const int newBufferSize)
  414. {
  415. close();
  416. error.clear();
  417. sampleRate = newSampleRate;
  418. bufferSize = newBufferSize;
  419. int maxInputsRequested = inputChannels.getHighestBit() + 1;
  420. maxInputsRequested = jmax ((int) minChansIn, jmin ((int) maxChansIn, maxInputsRequested));
  421. inputChannelBuffer.setSize (maxInputsRequested, bufferSize);
  422. inputChannelBuffer.clear();
  423. inputChannelDataForCallback.clear();
  424. currentInputChans.clear();
  425. if (inputChannels.getHighestBit() >= 0)
  426. {
  427. for (int i = 0; i < maxInputsRequested; ++i)
  428. {
  429. if (inputChannels[i])
  430. {
  431. inputChannelDataForCallback.add (inputChannelBuffer.getReadPointer (i));
  432. currentInputChans.setBit (i);
  433. }
  434. }
  435. }
  436. ensureMinimumNumBitsSet (outputChannels, (int) minChansOut);
  437. int maxOutputsRequested = outputChannels.getHighestBit() + 1;
  438. maxOutputsRequested = jmax ((int) minChansOut, jmin ((int) maxChansOut, maxOutputsRequested));
  439. outputChannelBuffer.setSize (maxOutputsRequested, bufferSize);
  440. outputChannelBuffer.clear();
  441. outputChannelDataForCallback.clear();
  442. currentOutputChans.clear();
  443. if (outputChannels.getHighestBit() >= 0)
  444. {
  445. for (int i = 0; i < maxOutputsRequested; ++i)
  446. {
  447. if (outputChannels[i])
  448. {
  449. outputChannelDataForCallback.add (outputChannelBuffer.getWritePointer (i));
  450. currentOutputChans.setBit (i);
  451. }
  452. }
  453. }
  454. if (outputChannelDataForCallback.size() > 0 && outputId.isNotEmpty())
  455. {
  456. outputDevice = new ALSADevice (outputId, false);
  457. if (outputDevice->error.isNotEmpty())
  458. {
  459. error = outputDevice->error;
  460. outputDevice = nullptr;
  461. return;
  462. }
  463. if (! outputDevice->setParameters ((unsigned int) sampleRate,
  464. jlimit ((int) minChansOut, (int) maxChansOut,
  465. currentOutputChans.getHighestBit() + 1),
  466. bufferSize))
  467. {
  468. error = outputDevice->error;
  469. outputDevice = nullptr;
  470. return;
  471. }
  472. outputLatency = outputDevice->latency;
  473. }
  474. if (inputChannelDataForCallback.size() > 0 && inputId.isNotEmpty())
  475. {
  476. inputDevice = new ALSADevice (inputId, true);
  477. if (inputDevice->error.isNotEmpty())
  478. {
  479. error = inputDevice->error;
  480. inputDevice = nullptr;
  481. return;
  482. }
  483. ensureMinimumNumBitsSet (currentInputChans, (int) minChansIn);
  484. if (! inputDevice->setParameters ((unsigned int) sampleRate,
  485. jlimit ((int) minChansIn, (int) maxChansIn, currentInputChans.getHighestBit() + 1),
  486. bufferSize))
  487. {
  488. error = inputDevice->error;
  489. inputDevice = nullptr;
  490. return;
  491. }
  492. inputLatency = inputDevice->latency;
  493. }
  494. if (outputDevice == nullptr && inputDevice == nullptr)
  495. {
  496. error = "no channels";
  497. return;
  498. }
  499. if (outputDevice != nullptr && inputDevice != nullptr)
  500. snd_pcm_link (outputDevice->handle, inputDevice->handle);
  501. if (inputDevice != nullptr && JUCE_ALSA_FAILED (snd_pcm_prepare (inputDevice->handle)))
  502. return;
  503. if (outputDevice != nullptr && JUCE_ALSA_FAILED (snd_pcm_prepare (outputDevice->handle)))
  504. return;
  505. startThread (9);
  506. int count = 1000;
  507. while (numCallbacks == 0)
  508. {
  509. sleep (5);
  510. if (--count < 0 || ! isThreadRunning())
  511. {
  512. error = "device didn't start";
  513. break;
  514. }
  515. }
  516. }
  517. void close()
  518. {
  519. if (isThreadRunning())
  520. {
  521. // problem: when pulseaudio is suspended (with pasuspend) , the ALSAThread::run is just stuck in
  522. // snd_pcm_writei -- no error, no nothing it just stays stuck. So the only way I found to exit "nicely"
  523. // (that is without the "killing thread by force" of stopThread) , is to just call snd_pcm_close from
  524. // here which will cause the thread to resume, and exit
  525. signalThreadShouldExit();
  526. const int callbacksToStop = numCallbacks;
  527. if ((! waitForThreadToExit (400)) && audioIoInProgress && numCallbacks == callbacksToStop)
  528. {
  529. JUCE_ALSA_LOG ("Thread is stuck in i/o.. Is pulseaudio suspended?");
  530. if (outputDevice != nullptr) outputDevice->closeNow();
  531. if (inputDevice != nullptr) inputDevice->closeNow();
  532. }
  533. }
  534. stopThread (6000);
  535. inputDevice = nullptr;
  536. outputDevice = nullptr;
  537. inputChannelBuffer.setSize (1, 1);
  538. outputChannelBuffer.setSize (1, 1);
  539. numCallbacks = 0;
  540. }
  541. void setCallback (AudioIODeviceCallback* const newCallback) noexcept
  542. {
  543. const ScopedLock sl (callbackLock);
  544. callback = newCallback;
  545. }
  546. void run() override
  547. {
  548. while (! threadShouldExit())
  549. {
  550. if (inputDevice != nullptr && inputDevice->handle != nullptr)
  551. {
  552. if (outputDevice == nullptr || outputDevice->handle == nullptr)
  553. {
  554. JUCE_ALSA_FAILED (snd_pcm_wait (inputDevice->handle, 2000));
  555. if (threadShouldExit())
  556. break;
  557. snd_pcm_sframes_t avail = snd_pcm_avail_update (inputDevice->handle);
  558. if (avail < 0)
  559. JUCE_ALSA_FAILED (snd_pcm_recover (inputDevice->handle, (int) avail, 0));
  560. }
  561. audioIoInProgress = true;
  562. if (! inputDevice->readFromInputDevice (inputChannelBuffer, bufferSize))
  563. {
  564. JUCE_ALSA_LOG ("Read failure");
  565. break;
  566. }
  567. audioIoInProgress = false;
  568. }
  569. if (threadShouldExit())
  570. break;
  571. {
  572. const ScopedLock sl (callbackLock);
  573. ++numCallbacks;
  574. if (callback != nullptr)
  575. {
  576. callback->audioDeviceIOCallback (inputChannelDataForCallback.getRawDataPointer(),
  577. inputChannelDataForCallback.size(),
  578. outputChannelDataForCallback.getRawDataPointer(),
  579. outputChannelDataForCallback.size(),
  580. bufferSize);
  581. }
  582. else
  583. {
  584. for (int i = 0; i < outputChannelDataForCallback.size(); ++i)
  585. zeromem (outputChannelDataForCallback[i], sizeof (float) * (size_t) bufferSize);
  586. }
  587. }
  588. if (outputDevice != nullptr && outputDevice->handle != nullptr)
  589. {
  590. JUCE_ALSA_FAILED (snd_pcm_wait (outputDevice->handle, 2000));
  591. if (threadShouldExit())
  592. break;
  593. snd_pcm_sframes_t avail = snd_pcm_avail_update (outputDevice->handle);
  594. if (avail < 0)
  595. JUCE_ALSA_FAILED (snd_pcm_recover (outputDevice->handle, (int) avail, 0));
  596. audioIoInProgress = true;
  597. if (! outputDevice->writeToOutputDevice (outputChannelBuffer, bufferSize))
  598. {
  599. JUCE_ALSA_LOG ("write failure");
  600. break;
  601. }
  602. audioIoInProgress = false;
  603. }
  604. }
  605. audioIoInProgress = false;
  606. }
  607. int getBitDepth() const noexcept
  608. {
  609. if (outputDevice != nullptr)
  610. return outputDevice->bitDepth;
  611. if (inputDevice != nullptr)
  612. return inputDevice->bitDepth;
  613. return 16;
  614. }
  615. int getXRunCount() const noexcept
  616. {
  617. int result = 0;
  618. if (outputDevice != nullptr)
  619. result += outputDevice->underrunCount;
  620. if (inputDevice != nullptr)
  621. result += inputDevice->overrunCount;
  622. return result;
  623. }
  624. //==============================================================================
  625. String error;
  626. double sampleRate;
  627. int bufferSize, outputLatency, inputLatency;
  628. BigInteger currentInputChans, currentOutputChans;
  629. Array<double> sampleRates;
  630. StringArray channelNamesOut, channelNamesIn;
  631. AudioIODeviceCallback* callback;
  632. private:
  633. //==============================================================================
  634. const String inputId, outputId;
  635. ScopedPointer<ALSADevice> outputDevice, inputDevice;
  636. int numCallbacks;
  637. bool audioIoInProgress;
  638. CriticalSection callbackLock;
  639. AudioSampleBuffer inputChannelBuffer, outputChannelBuffer;
  640. Array<const float*> inputChannelDataForCallback;
  641. Array<float*> outputChannelDataForCallback;
  642. unsigned int minChansOut, maxChansOut;
  643. unsigned int minChansIn, maxChansIn;
  644. bool failed (const int errorNum)
  645. {
  646. if (errorNum >= 0)
  647. return false;
  648. error = snd_strerror (errorNum);
  649. JUCE_ALSA_LOG ("ALSA error: " << error);
  650. return true;
  651. }
  652. void initialiseRatesAndChannels()
  653. {
  654. sampleRates.clear();
  655. channelNamesOut.clear();
  656. channelNamesIn.clear();
  657. minChansOut = 0;
  658. maxChansOut = 0;
  659. minChansIn = 0;
  660. maxChansIn = 0;
  661. unsigned int dummy = 0;
  662. getDeviceProperties (inputId, dummy, dummy, minChansIn, maxChansIn, sampleRates, false, true);
  663. getDeviceProperties (outputId, minChansOut, maxChansOut, dummy, dummy, sampleRates, true, false);
  664. for (unsigned int i = 0; i < maxChansOut; ++i)
  665. channelNamesOut.add ("channel " + String ((int) i + 1));
  666. for (unsigned int i = 0; i < maxChansIn; ++i)
  667. channelNamesIn.add ("channel " + String ((int) i + 1));
  668. }
  669. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ALSAThread)
  670. };
  671. //==============================================================================
  672. class ALSAAudioIODevice : public AudioIODevice
  673. {
  674. public:
  675. ALSAAudioIODevice (const String& deviceName,
  676. const String& deviceTypeName,
  677. const String& inputDeviceID,
  678. const String& outputDeviceID)
  679. : AudioIODevice (deviceName, deviceTypeName),
  680. inputId (inputDeviceID),
  681. outputId (outputDeviceID),
  682. isOpen_ (false),
  683. isStarted (false),
  684. internal (inputDeviceID, outputDeviceID)
  685. {
  686. }
  687. ~ALSAAudioIODevice()
  688. {
  689. close();
  690. }
  691. StringArray getOutputChannelNames() override { return internal.channelNamesOut; }
  692. StringArray getInputChannelNames() override { return internal.channelNamesIn; }
  693. Array<double> getAvailableSampleRates() override { return internal.sampleRates; }
  694. Array<int> getAvailableBufferSizes() override
  695. {
  696. Array<int> r;
  697. int n = 16;
  698. for (int i = 0; i < 50; ++i)
  699. {
  700. r.add (n);
  701. n += n < 64 ? 16
  702. : (n < 512 ? 32
  703. : (n < 1024 ? 64
  704. : (n < 2048 ? 128 : 256)));
  705. }
  706. return r;
  707. }
  708. int getDefaultBufferSize() override { return 512; }
  709. String open (const BigInteger& inputChannels,
  710. const BigInteger& outputChannels,
  711. double sampleRate,
  712. int bufferSizeSamples) override
  713. {
  714. close();
  715. if (bufferSizeSamples <= 0)
  716. bufferSizeSamples = getDefaultBufferSize();
  717. if (sampleRate <= 0)
  718. {
  719. for (int i = 0; i < internal.sampleRates.size(); ++i)
  720. {
  721. double rate = internal.sampleRates[i];
  722. if (rate >= 44100)
  723. {
  724. sampleRate = rate;
  725. break;
  726. }
  727. }
  728. }
  729. internal.open (inputChannels, outputChannels,
  730. sampleRate, bufferSizeSamples);
  731. isOpen_ = internal.error.isEmpty();
  732. return internal.error;
  733. }
  734. void close() override
  735. {
  736. stop();
  737. internal.close();
  738. isOpen_ = false;
  739. }
  740. bool isOpen() override { return isOpen_; }
  741. bool isPlaying() override { return isStarted && internal.error.isEmpty(); }
  742. String getLastError() override { return internal.error; }
  743. int getCurrentBufferSizeSamples() override { return internal.bufferSize; }
  744. double getCurrentSampleRate() override { return internal.sampleRate; }
  745. int getCurrentBitDepth() override { return internal.getBitDepth(); }
  746. BigInteger getActiveOutputChannels() const override { return internal.currentOutputChans; }
  747. BigInteger getActiveInputChannels() const override { return internal.currentInputChans; }
  748. int getOutputLatencyInSamples() override { return internal.outputLatency; }
  749. int getInputLatencyInSamples() override { return internal.inputLatency; }
  750. int getXRunCount() const noexcept override { return internal.getXRunCount(); }
  751. void start (AudioIODeviceCallback* callback) override
  752. {
  753. if (! isOpen_)
  754. callback = nullptr;
  755. if (callback != nullptr)
  756. callback->audioDeviceAboutToStart (this);
  757. internal.setCallback (callback);
  758. isStarted = (callback != nullptr);
  759. }
  760. void stop() override
  761. {
  762. AudioIODeviceCallback* const oldCallback = internal.callback;
  763. start (nullptr);
  764. if (oldCallback != nullptr)
  765. oldCallback->audioDeviceStopped();
  766. }
  767. String inputId, outputId;
  768. private:
  769. bool isOpen_, isStarted;
  770. ALSAThread internal;
  771. };
  772. //==============================================================================
  773. class ALSAAudioIODeviceType : public AudioIODeviceType
  774. {
  775. public:
  776. ALSAAudioIODeviceType (bool onlySoundcards, const String &deviceTypeName)
  777. : AudioIODeviceType (deviceTypeName),
  778. hasScanned (false),
  779. listOnlySoundcards (onlySoundcards)
  780. {
  781. #if ! JUCE_ALSA_LOGGING
  782. snd_lib_error_set_handler (&silentErrorHandler);
  783. #endif
  784. }
  785. ~ALSAAudioIODeviceType()
  786. {
  787. #if ! JUCE_ALSA_LOGGING
  788. snd_lib_error_set_handler (nullptr);
  789. #endif
  790. snd_config_update_free_global(); // prevent valgrind from screaming about alsa leaks
  791. }
  792. //==============================================================================
  793. void scanForDevices()
  794. {
  795. if (hasScanned)
  796. return;
  797. hasScanned = true;
  798. inputNames.clear();
  799. inputIds.clear();
  800. outputNames.clear();
  801. outputIds.clear();
  802. JUCE_ALSA_LOG ("scanForDevices()");
  803. if (listOnlySoundcards)
  804. enumerateAlsaSoundcards();
  805. else
  806. enumerateAlsaPCMDevices();
  807. inputNames.appendNumbersToDuplicates (false, true);
  808. outputNames.appendNumbersToDuplicates (false, true);
  809. }
  810. StringArray getDeviceNames (bool wantInputNames) const
  811. {
  812. jassert (hasScanned); // need to call scanForDevices() before doing this
  813. return wantInputNames ? inputNames : outputNames;
  814. }
  815. int getDefaultDeviceIndex (bool forInput) const
  816. {
  817. jassert (hasScanned); // need to call scanForDevices() before doing this
  818. const int idx = (forInput ? inputIds : outputIds).indexOf ("default");
  819. return idx >= 0 ? idx : 0;
  820. }
  821. bool hasSeparateInputsAndOutputs() const { return true; }
  822. int getIndexOfDevice (AudioIODevice* device, bool asInput) const
  823. {
  824. jassert (hasScanned); // need to call scanForDevices() before doing this
  825. if (ALSAAudioIODevice* d = dynamic_cast<ALSAAudioIODevice*> (device))
  826. return asInput ? inputIds.indexOf (d->inputId)
  827. : outputIds.indexOf (d->outputId);
  828. return -1;
  829. }
  830. AudioIODevice* createDevice (const String& outputDeviceName,
  831. const String& inputDeviceName)
  832. {
  833. jassert (hasScanned); // need to call scanForDevices() before doing this
  834. const int inputIndex = inputNames.indexOf (inputDeviceName);
  835. const int outputIndex = outputNames.indexOf (outputDeviceName);
  836. String deviceName (outputIndex >= 0 ? outputDeviceName
  837. : inputDeviceName);
  838. if (inputIndex >= 0 || outputIndex >= 0)
  839. return new ALSAAudioIODevice (deviceName, getTypeName(),
  840. inputIds [inputIndex],
  841. outputIds [outputIndex]);
  842. return nullptr;
  843. }
  844. private:
  845. //==============================================================================
  846. StringArray inputNames, outputNames, inputIds, outputIds;
  847. bool hasScanned, listOnlySoundcards;
  848. bool testDevice (const String &id, const String &outputName, const String &inputName)
  849. {
  850. unsigned int minChansOut = 0, maxChansOut = 0;
  851. unsigned int minChansIn = 0, maxChansIn = 0;
  852. Array<double> rates;
  853. bool isInput = inputName.isNotEmpty(), isOutput = outputName.isNotEmpty();
  854. getDeviceProperties (id, minChansOut, maxChansOut, minChansIn, maxChansIn, rates, isOutput, isInput);
  855. isInput = maxChansIn > 0;
  856. isOutput = maxChansOut > 0;
  857. if ((isInput || isOutput) && rates.size() > 0)
  858. {
  859. JUCE_ALSA_LOG ("testDevice: '" << id.toUTF8().getAddress() << "' -> isInput: "
  860. << (int) isInput << ", isOutput: " << (int) isOutput);
  861. if (isInput)
  862. {
  863. inputNames.add (inputName);
  864. inputIds.add (id);
  865. }
  866. if (isOutput)
  867. {
  868. outputNames.add (outputName);
  869. outputIds.add (id);
  870. }
  871. return isInput || isOutput;
  872. }
  873. return false;
  874. }
  875. void enumerateAlsaSoundcards()
  876. {
  877. snd_ctl_t* handle = nullptr;
  878. snd_ctl_card_info_t* info = nullptr;
  879. snd_ctl_card_info_alloca (&info);
  880. int cardNum = -1;
  881. while (outputIds.size() + inputIds.size() <= 64)
  882. {
  883. snd_card_next (&cardNum);
  884. if (cardNum < 0)
  885. break;
  886. if (JUCE_CHECKED_RESULT (snd_ctl_open (&handle, ("hw:" + String (cardNum)).toUTF8(), SND_CTL_NONBLOCK)) >= 0)
  887. {
  888. if (JUCE_CHECKED_RESULT (snd_ctl_card_info (handle, info)) >= 0)
  889. {
  890. String cardId (snd_ctl_card_info_get_id (info));
  891. if (cardId.removeCharacters ("0123456789").isEmpty())
  892. cardId = String (cardNum);
  893. String cardName = snd_ctl_card_info_get_name (info);
  894. if (cardName.isEmpty())
  895. cardName = cardId;
  896. int device = -1;
  897. snd_pcm_info_t* pcmInfo;
  898. snd_pcm_info_alloca (&pcmInfo);
  899. for (;;)
  900. {
  901. if (snd_ctl_pcm_next_device (handle, &device) < 0 || device < 0)
  902. break;
  903. snd_pcm_info_set_device (pcmInfo, (unsigned int) device);
  904. for (unsigned int subDevice = 0, nbSubDevice = 1; subDevice < nbSubDevice; ++subDevice)
  905. {
  906. snd_pcm_info_set_subdevice (pcmInfo, subDevice);
  907. snd_pcm_info_set_stream (pcmInfo, SND_PCM_STREAM_CAPTURE);
  908. const bool isInput = (snd_ctl_pcm_info (handle, pcmInfo) >= 0);
  909. snd_pcm_info_set_stream (pcmInfo, SND_PCM_STREAM_PLAYBACK);
  910. const bool isOutput = (snd_ctl_pcm_info (handle, pcmInfo) >= 0);
  911. if (! (isInput || isOutput))
  912. continue;
  913. if (nbSubDevice == 1)
  914. nbSubDevice = snd_pcm_info_get_subdevices_count (pcmInfo);
  915. String id, name;
  916. if (nbSubDevice == 1)
  917. {
  918. id << "hw:" << cardId << "," << device;
  919. name << cardName << ", " << snd_pcm_info_get_name (pcmInfo);
  920. }
  921. else
  922. {
  923. id << "hw:" << cardId << "," << device << "," << (int) subDevice;
  924. name << cardName << ", " << snd_pcm_info_get_name (pcmInfo)
  925. << " {" << snd_pcm_info_get_subdevice_name (pcmInfo) << "}";
  926. }
  927. JUCE_ALSA_LOG ("Soundcard ID: " << id << ", name: '" << name
  928. << ", isInput:" << (int) isInput
  929. << ", isOutput:" << (int) isOutput << "\n");
  930. if (isInput)
  931. {
  932. inputNames.add (name);
  933. inputIds.add (id);
  934. }
  935. if (isOutput)
  936. {
  937. outputNames.add (name);
  938. outputIds.add (id);
  939. }
  940. }
  941. }
  942. }
  943. JUCE_CHECKED_RESULT (snd_ctl_close (handle));
  944. }
  945. }
  946. }
  947. /* Enumerates all ALSA output devices (as output by the command aplay -L)
  948. Does not try to open the devices (with "testDevice" for example),
  949. so that it also finds devices that are busy and not yet available.
  950. */
  951. void enumerateAlsaPCMDevices()
  952. {
  953. void** hints = nullptr;
  954. if (JUCE_CHECKED_RESULT (snd_device_name_hint (-1, "pcm", &hints)) == 0)
  955. {
  956. for (char** h = (char**) hints; *h; ++h)
  957. {
  958. const String id (hintToString (*h, "NAME"));
  959. const String description (hintToString (*h, "DESC"));
  960. const String ioid (hintToString (*h, "IOID"));
  961. JUCE_ALSA_LOG ("ID: " << id << "; desc: " << description << "; ioid: " << ioid);
  962. String ss = id.fromFirstOccurrenceOf ("=", false, false)
  963. .upToFirstOccurrenceOf (",", false, false);
  964. if (id.isEmpty()
  965. || id.startsWith ("default:") || id.startsWith ("sysdefault:")
  966. || id.startsWith ("plughw:") || id == "null")
  967. continue;
  968. String name (description.replace ("\n", "; "));
  969. if (name.isEmpty())
  970. name = id;
  971. bool isOutput = (ioid != "Input");
  972. bool isInput = (ioid != "Output");
  973. // alsa is stupid here, it advertises dmix and dsnoop as input/output devices, but
  974. // opening dmix as input, or dsnoop as output will trigger errors..
  975. isInput = isInput && ! id.startsWith ("dmix");
  976. isOutput = isOutput && ! id.startsWith ("dsnoop");
  977. if (isInput)
  978. {
  979. inputNames.add (name);
  980. inputIds.add (id);
  981. }
  982. if (isOutput)
  983. {
  984. outputNames.add (name);
  985. outputIds.add (id);
  986. }
  987. }
  988. snd_device_name_free_hint (hints);
  989. }
  990. // sometimes the "default" device is not listed, but it is nice to see it explicitely in the list
  991. if (! outputIds.contains ("default"))
  992. testDevice ("default", "Default ALSA Output", "Default ALSA Input");
  993. // same for the pulseaudio plugin
  994. if (! outputIds.contains ("pulse"))
  995. testDevice ("pulse", "Pulseaudio output", "Pulseaudio input");
  996. // make sure the default device is listed first, and followed by the pulse device (if present)
  997. int idx = outputIds.indexOf ("pulse");
  998. outputIds.move (idx, 0);
  999. outputNames.move (idx, 0);
  1000. idx = inputIds.indexOf ("pulse");
  1001. inputIds.move (idx, 0);
  1002. inputNames.move (idx, 0);
  1003. idx = outputIds.indexOf ("default");
  1004. outputIds.move (idx, 0);
  1005. outputNames.move (idx, 0);
  1006. idx = inputIds.indexOf ("default");
  1007. inputIds.move (idx, 0);
  1008. inputNames.move (idx, 0);
  1009. }
  1010. static String hintToString (const void* hints, const char* type)
  1011. {
  1012. char* const hint = snd_device_name_get_hint (hints, type);
  1013. const String s (String::fromUTF8 (hint));
  1014. ::free (hint);
  1015. return s;
  1016. }
  1017. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ALSAAudioIODeviceType)
  1018. };
  1019. }
  1020. //==============================================================================
  1021. AudioIODeviceType* createAudioIODeviceType_ALSA_Soundcards()
  1022. {
  1023. return new ALSAAudioIODeviceType (true, "ALSA HW");
  1024. }
  1025. AudioIODeviceType* createAudioIODeviceType_ALSA_PCMDevices()
  1026. {
  1027. return new ALSAAudioIODeviceType (false, "ALSA");
  1028. }
  1029. AudioIODeviceType* AudioIODeviceType::createAudioIODeviceType_ALSA()
  1030. {
  1031. return createAudioIODeviceType_ALSA_PCMDevices();
  1032. }
  1033. } // namespace juce