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_AudioDataConverters.cpp 21KB

11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600
  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2013 - Raw Material Software Ltd.
  5. Permission is granted to use this software under the terms of either:
  6. a) the GPL v2 (or any later version)
  7. b) the Affero GPL v3
  8. Details of these licenses can be found at: www.gnu.org/licenses
  9. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  11. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  12. ------------------------------------------------------------------------------
  13. To release a closed-source product which uses JUCE, commercial licenses are
  14. available: visit www.juce.com for more information.
  15. ==============================================================================
  16. */
  17. void AudioDataConverters::convertFloatToInt16LE (const float* source, void* dest, int numSamples, const int destBytesPerSample)
  18. {
  19. const double maxVal = (double) 0x7fff;
  20. char* intData = static_cast <char*> (dest);
  21. if (dest != (void*) source || destBytesPerSample <= 4)
  22. {
  23. for (int i = 0; i < numSamples; ++i)
  24. {
  25. *(uint16*) intData = ByteOrder::swapIfBigEndian ((uint16) (short) roundToInt (jlimit (-maxVal, maxVal, maxVal * source[i])));
  26. intData += destBytesPerSample;
  27. }
  28. }
  29. else
  30. {
  31. intData += destBytesPerSample * numSamples;
  32. for (int i = numSamples; --i >= 0;)
  33. {
  34. intData -= destBytesPerSample;
  35. *(uint16*) intData = ByteOrder::swapIfBigEndian ((uint16) (short) roundToInt (jlimit (-maxVal, maxVal, maxVal * source[i])));
  36. }
  37. }
  38. }
  39. void AudioDataConverters::convertFloatToInt16BE (const float* source, void* dest, int numSamples, const int destBytesPerSample)
  40. {
  41. const double maxVal = (double) 0x7fff;
  42. char* intData = static_cast <char*> (dest);
  43. if (dest != (void*) source || destBytesPerSample <= 4)
  44. {
  45. for (int i = 0; i < numSamples; ++i)
  46. {
  47. *(uint16*) intData = ByteOrder::swapIfLittleEndian ((uint16) (short) roundToInt (jlimit (-maxVal, maxVal, maxVal * source[i])));
  48. intData += destBytesPerSample;
  49. }
  50. }
  51. else
  52. {
  53. intData += destBytesPerSample * numSamples;
  54. for (int i = numSamples; --i >= 0;)
  55. {
  56. intData -= destBytesPerSample;
  57. *(uint16*) intData = ByteOrder::swapIfLittleEndian ((uint16) (short) roundToInt (jlimit (-maxVal, maxVal, maxVal * source[i])));
  58. }
  59. }
  60. }
  61. void AudioDataConverters::convertFloatToInt24LE (const float* source, void* dest, int numSamples, const int destBytesPerSample)
  62. {
  63. const double maxVal = (double) 0x7fffff;
  64. char* intData = static_cast <char*> (dest);
  65. if (dest != (void*) source || destBytesPerSample <= 4)
  66. {
  67. for (int i = 0; i < numSamples; ++i)
  68. {
  69. ByteOrder::littleEndian24BitToChars (roundToInt (jlimit (-maxVal, maxVal, maxVal * source[i])), intData);
  70. intData += destBytesPerSample;
  71. }
  72. }
  73. else
  74. {
  75. intData += destBytesPerSample * numSamples;
  76. for (int i = numSamples; --i >= 0;)
  77. {
  78. intData -= destBytesPerSample;
  79. ByteOrder::littleEndian24BitToChars (roundToInt (jlimit (-maxVal, maxVal, maxVal * source[i])), intData);
  80. }
  81. }
  82. }
  83. void AudioDataConverters::convertFloatToInt24BE (const float* source, void* dest, int numSamples, const int destBytesPerSample)
  84. {
  85. const double maxVal = (double) 0x7fffff;
  86. char* intData = static_cast <char*> (dest);
  87. if (dest != (void*) source || destBytesPerSample <= 4)
  88. {
  89. for (int i = 0; i < numSamples; ++i)
  90. {
  91. ByteOrder::bigEndian24BitToChars (roundToInt (jlimit (-maxVal, maxVal, maxVal * source[i])), intData);
  92. intData += destBytesPerSample;
  93. }
  94. }
  95. else
  96. {
  97. intData += destBytesPerSample * numSamples;
  98. for (int i = numSamples; --i >= 0;)
  99. {
  100. intData -= destBytesPerSample;
  101. ByteOrder::bigEndian24BitToChars (roundToInt (jlimit (-maxVal, maxVal, maxVal * source[i])), intData);
  102. }
  103. }
  104. }
  105. void AudioDataConverters::convertFloatToInt32LE (const float* source, void* dest, int numSamples, const int destBytesPerSample)
  106. {
  107. const double maxVal = (double) 0x7fffffff;
  108. char* intData = static_cast <char*> (dest);
  109. if (dest != (void*) source || destBytesPerSample <= 4)
  110. {
  111. for (int i = 0; i < numSamples; ++i)
  112. {
  113. *(uint32*)intData = ByteOrder::swapIfBigEndian ((uint32) roundToInt (jlimit (-maxVal, maxVal, maxVal * source[i])));
  114. intData += destBytesPerSample;
  115. }
  116. }
  117. else
  118. {
  119. intData += destBytesPerSample * numSamples;
  120. for (int i = numSamples; --i >= 0;)
  121. {
  122. intData -= destBytesPerSample;
  123. *(uint32*)intData = ByteOrder::swapIfBigEndian ((uint32) roundToInt (jlimit (-maxVal, maxVal, maxVal * source[i])));
  124. }
  125. }
  126. }
  127. void AudioDataConverters::convertFloatToInt32BE (const float* source, void* dest, int numSamples, const int destBytesPerSample)
  128. {
  129. const double maxVal = (double) 0x7fffffff;
  130. char* intData = static_cast <char*> (dest);
  131. if (dest != (void*) source || destBytesPerSample <= 4)
  132. {
  133. for (int i = 0; i < numSamples; ++i)
  134. {
  135. *(uint32*)intData = ByteOrder::swapIfLittleEndian ((uint32) roundToInt (jlimit (-maxVal, maxVal, maxVal * source[i])));
  136. intData += destBytesPerSample;
  137. }
  138. }
  139. else
  140. {
  141. intData += destBytesPerSample * numSamples;
  142. for (int i = numSamples; --i >= 0;)
  143. {
  144. intData -= destBytesPerSample;
  145. *(uint32*)intData = ByteOrder::swapIfLittleEndian ((uint32) roundToInt (jlimit (-maxVal, maxVal, maxVal * source[i])));
  146. }
  147. }
  148. }
  149. void AudioDataConverters::convertFloatToFloat32LE (const float* source, void* dest, int numSamples, const int destBytesPerSample)
  150. {
  151. jassert (dest != (void*) source || destBytesPerSample <= 4); // This op can't be performed on in-place data!
  152. char* d = static_cast <char*> (dest);
  153. for (int i = 0; i < numSamples; ++i)
  154. {
  155. *(float*) d = source[i];
  156. #if JUCE_BIG_ENDIAN
  157. *(uint32*) d = ByteOrder::swap (*(uint32*) d);
  158. #endif
  159. d += destBytesPerSample;
  160. }
  161. }
  162. void AudioDataConverters::convertFloatToFloat32BE (const float* source, void* dest, int numSamples, const int destBytesPerSample)
  163. {
  164. jassert (dest != (void*) source || destBytesPerSample <= 4); // This op can't be performed on in-place data!
  165. char* d = static_cast <char*> (dest);
  166. for (int i = 0; i < numSamples; ++i)
  167. {
  168. *(float*) d = source[i];
  169. #if JUCE_LITTLE_ENDIAN
  170. *(uint32*) d = ByteOrder::swap (*(uint32*) d);
  171. #endif
  172. d += destBytesPerSample;
  173. }
  174. }
  175. //==============================================================================
  176. void AudioDataConverters::convertInt16LEToFloat (const void* const source, float* const dest, int numSamples, const int srcBytesPerSample)
  177. {
  178. const float scale = 1.0f / 0x7fff;
  179. const char* intData = static_cast <const char*> (source);
  180. if (source != (void*) dest || srcBytesPerSample >= 4)
  181. {
  182. for (int i = 0; i < numSamples; ++i)
  183. {
  184. dest[i] = scale * (short) ByteOrder::swapIfBigEndian (*(uint16*)intData);
  185. intData += srcBytesPerSample;
  186. }
  187. }
  188. else
  189. {
  190. intData += srcBytesPerSample * numSamples;
  191. for (int i = numSamples; --i >= 0;)
  192. {
  193. intData -= srcBytesPerSample;
  194. dest[i] = scale * (short) ByteOrder::swapIfBigEndian (*(uint16*)intData);
  195. }
  196. }
  197. }
  198. void AudioDataConverters::convertInt16BEToFloat (const void* const source, float* const dest, int numSamples, const int srcBytesPerSample)
  199. {
  200. const float scale = 1.0f / 0x7fff;
  201. const char* intData = static_cast <const char*> (source);
  202. if (source != (void*) dest || srcBytesPerSample >= 4)
  203. {
  204. for (int i = 0; i < numSamples; ++i)
  205. {
  206. dest[i] = scale * (short) ByteOrder::swapIfLittleEndian (*(uint16*)intData);
  207. intData += srcBytesPerSample;
  208. }
  209. }
  210. else
  211. {
  212. intData += srcBytesPerSample * numSamples;
  213. for (int i = numSamples; --i >= 0;)
  214. {
  215. intData -= srcBytesPerSample;
  216. dest[i] = scale * (short) ByteOrder::swapIfLittleEndian (*(uint16*)intData);
  217. }
  218. }
  219. }
  220. void AudioDataConverters::convertInt24LEToFloat (const void* const source, float* const dest, int numSamples, const int srcBytesPerSample)
  221. {
  222. const float scale = 1.0f / 0x7fffff;
  223. const char* intData = static_cast <const char*> (source);
  224. if (source != (void*) dest || srcBytesPerSample >= 4)
  225. {
  226. for (int i = 0; i < numSamples; ++i)
  227. {
  228. dest[i] = scale * (short) ByteOrder::littleEndian24Bit (intData);
  229. intData += srcBytesPerSample;
  230. }
  231. }
  232. else
  233. {
  234. intData += srcBytesPerSample * numSamples;
  235. for (int i = numSamples; --i >= 0;)
  236. {
  237. intData -= srcBytesPerSample;
  238. dest[i] = scale * (short) ByteOrder::littleEndian24Bit (intData);
  239. }
  240. }
  241. }
  242. void AudioDataConverters::convertInt24BEToFloat (const void* const source, float* const dest, int numSamples, const int srcBytesPerSample)
  243. {
  244. const float scale = 1.0f / 0x7fffff;
  245. const char* intData = static_cast <const char*> (source);
  246. if (source != (void*) dest || srcBytesPerSample >= 4)
  247. {
  248. for (int i = 0; i < numSamples; ++i)
  249. {
  250. dest[i] = scale * (short) ByteOrder::bigEndian24Bit (intData);
  251. intData += srcBytesPerSample;
  252. }
  253. }
  254. else
  255. {
  256. intData += srcBytesPerSample * numSamples;
  257. for (int i = numSamples; --i >= 0;)
  258. {
  259. intData -= srcBytesPerSample;
  260. dest[i] = scale * (short) ByteOrder::bigEndian24Bit (intData);
  261. }
  262. }
  263. }
  264. void AudioDataConverters::convertInt32LEToFloat (const void* const source, float* const dest, int numSamples, const int srcBytesPerSample)
  265. {
  266. const float scale = 1.0f / 0x7fffffff;
  267. const char* intData = static_cast <const char*> (source);
  268. if (source != (void*) dest || srcBytesPerSample >= 4)
  269. {
  270. for (int i = 0; i < numSamples; ++i)
  271. {
  272. dest[i] = scale * (int) ByteOrder::swapIfBigEndian (*(uint32*) intData);
  273. intData += srcBytesPerSample;
  274. }
  275. }
  276. else
  277. {
  278. intData += srcBytesPerSample * numSamples;
  279. for (int i = numSamples; --i >= 0;)
  280. {
  281. intData -= srcBytesPerSample;
  282. dest[i] = scale * (int) ByteOrder::swapIfBigEndian (*(uint32*) intData);
  283. }
  284. }
  285. }
  286. void AudioDataConverters::convertInt32BEToFloat (const void* const source, float* const dest, int numSamples, const int srcBytesPerSample)
  287. {
  288. const float scale = 1.0f / 0x7fffffff;
  289. const char* intData = static_cast <const char*> (source);
  290. if (source != (void*) dest || srcBytesPerSample >= 4)
  291. {
  292. for (int i = 0; i < numSamples; ++i)
  293. {
  294. dest[i] = scale * (int) ByteOrder::swapIfLittleEndian (*(uint32*) intData);
  295. intData += srcBytesPerSample;
  296. }
  297. }
  298. else
  299. {
  300. intData += srcBytesPerSample * numSamples;
  301. for (int i = numSamples; --i >= 0;)
  302. {
  303. intData -= srcBytesPerSample;
  304. dest[i] = scale * (int) ByteOrder::swapIfLittleEndian (*(uint32*) intData);
  305. }
  306. }
  307. }
  308. void AudioDataConverters::convertFloat32LEToFloat (const void* const source, float* const dest, int numSamples, const int srcBytesPerSample)
  309. {
  310. const char* s = static_cast <const char*> (source);
  311. for (int i = 0; i < numSamples; ++i)
  312. {
  313. dest[i] = *(float*)s;
  314. #if JUCE_BIG_ENDIAN
  315. uint32* const d = (uint32*) (dest + i);
  316. *d = ByteOrder::swap (*d);
  317. #endif
  318. s += srcBytesPerSample;
  319. }
  320. }
  321. void AudioDataConverters::convertFloat32BEToFloat (const void* const source, float* const dest, int numSamples, const int srcBytesPerSample)
  322. {
  323. const char* s = static_cast <const char*> (source);
  324. for (int i = 0; i < numSamples; ++i)
  325. {
  326. dest[i] = *(float*)s;
  327. #if JUCE_LITTLE_ENDIAN
  328. uint32* const d = (uint32*) (dest + i);
  329. *d = ByteOrder::swap (*d);
  330. #endif
  331. s += srcBytesPerSample;
  332. }
  333. }
  334. //==============================================================================
  335. void AudioDataConverters::convertFloatToFormat (const DataFormat destFormat,
  336. const float* const source,
  337. void* const dest,
  338. const int numSamples)
  339. {
  340. switch (destFormat)
  341. {
  342. case int16LE: convertFloatToInt16LE (source, dest, numSamples); break;
  343. case int16BE: convertFloatToInt16BE (source, dest, numSamples); break;
  344. case int24LE: convertFloatToInt24LE (source, dest, numSamples); break;
  345. case int24BE: convertFloatToInt24BE (source, dest, numSamples); break;
  346. case int32LE: convertFloatToInt32LE (source, dest, numSamples); break;
  347. case int32BE: convertFloatToInt32BE (source, dest, numSamples); break;
  348. case float32LE: convertFloatToFloat32LE (source, dest, numSamples); break;
  349. case float32BE: convertFloatToFloat32BE (source, dest, numSamples); break;
  350. default: jassertfalse; break;
  351. }
  352. }
  353. void AudioDataConverters::convertFormatToFloat (const DataFormat sourceFormat,
  354. const void* const source,
  355. float* const dest,
  356. const int numSamples)
  357. {
  358. switch (sourceFormat)
  359. {
  360. case int16LE: convertInt16LEToFloat (source, dest, numSamples); break;
  361. case int16BE: convertInt16BEToFloat (source, dest, numSamples); break;
  362. case int24LE: convertInt24LEToFloat (source, dest, numSamples); break;
  363. case int24BE: convertInt24BEToFloat (source, dest, numSamples); break;
  364. case int32LE: convertInt32LEToFloat (source, dest, numSamples); break;
  365. case int32BE: convertInt32BEToFloat (source, dest, numSamples); break;
  366. case float32LE: convertFloat32LEToFloat (source, dest, numSamples); break;
  367. case float32BE: convertFloat32BEToFloat (source, dest, numSamples); break;
  368. default: jassertfalse; break;
  369. }
  370. }
  371. //==============================================================================
  372. void AudioDataConverters::interleaveSamples (const float** const source,
  373. float* const dest,
  374. const int numSamples,
  375. const int numChannels)
  376. {
  377. for (int chan = 0; chan < numChannels; ++chan)
  378. {
  379. int i = chan;
  380. const float* src = source [chan];
  381. for (int j = 0; j < numSamples; ++j)
  382. {
  383. dest [i] = src [j];
  384. i += numChannels;
  385. }
  386. }
  387. }
  388. void AudioDataConverters::deinterleaveSamples (const float* const source,
  389. float** const dest,
  390. const int numSamples,
  391. const int numChannels)
  392. {
  393. for (int chan = 0; chan < numChannels; ++chan)
  394. {
  395. int i = chan;
  396. float* dst = dest [chan];
  397. for (int j = 0; j < numSamples; ++j)
  398. {
  399. dst [j] = source [i];
  400. i += numChannels;
  401. }
  402. }
  403. }
  404. //==============================================================================
  405. #if JUCE_UNIT_TESTS
  406. class AudioConversionTests : public UnitTest
  407. {
  408. public:
  409. AudioConversionTests() : UnitTest ("Audio data conversion") {}
  410. template <class F1, class E1, class F2, class E2>
  411. struct Test5
  412. {
  413. static void test (UnitTest& unitTest, Random& r)
  414. {
  415. test (unitTest, false, r);
  416. test (unitTest, true, r);
  417. }
  418. static void test (UnitTest& unitTest, bool inPlace, Random& r)
  419. {
  420. const int numSamples = 2048;
  421. int32 original [numSamples], converted [numSamples], reversed [numSamples];
  422. {
  423. AudioData::Pointer<F1, E1, AudioData::NonInterleaved, AudioData::NonConst> d (original);
  424. bool clippingFailed = false;
  425. for (int i = 0; i < numSamples / 2; ++i)
  426. {
  427. d.setAsFloat (r.nextFloat() * 2.2f - 1.1f);
  428. if (! d.isFloatingPoint())
  429. clippingFailed = d.getAsFloat() > 1.0f || d.getAsFloat() < -1.0f || clippingFailed;
  430. ++d;
  431. d.setAsInt32 (r.nextInt());
  432. ++d;
  433. }
  434. unitTest.expect (! clippingFailed);
  435. }
  436. // convert data from the source to dest format..
  437. ScopedPointer<AudioData::Converter> conv (new AudioData::ConverterInstance <AudioData::Pointer<F1, E1, AudioData::NonInterleaved, AudioData::Const>,
  438. AudioData::Pointer<F2, E2, AudioData::NonInterleaved, AudioData::NonConst> >());
  439. conv->convertSamples (inPlace ? reversed : converted, original, numSamples);
  440. // ..and back again..
  441. conv = new AudioData::ConverterInstance <AudioData::Pointer<F2, E2, AudioData::NonInterleaved, AudioData::Const>,
  442. AudioData::Pointer<F1, E1, AudioData::NonInterleaved, AudioData::NonConst> >();
  443. if (! inPlace)
  444. zeromem (reversed, sizeof (reversed));
  445. conv->convertSamples (reversed, inPlace ? reversed : converted, numSamples);
  446. {
  447. int biggestDiff = 0;
  448. AudioData::Pointer<F1, E1, AudioData::NonInterleaved, AudioData::Const> d1 (original);
  449. AudioData::Pointer<F1, E1, AudioData::NonInterleaved, AudioData::Const> d2 (reversed);
  450. const int errorMargin = 2 * AudioData::Pointer<F1, E1, AudioData::NonInterleaved, AudioData::Const>::get32BitResolution()
  451. + AudioData::Pointer<F2, E2, AudioData::NonInterleaved, AudioData::Const>::get32BitResolution();
  452. for (int i = 0; i < numSamples; ++i)
  453. {
  454. biggestDiff = jmax (biggestDiff, std::abs (d1.getAsInt32() - d2.getAsInt32()));
  455. ++d1;
  456. ++d2;
  457. }
  458. unitTest.expect (biggestDiff <= errorMargin);
  459. }
  460. }
  461. };
  462. template <class F1, class E1, class FormatType>
  463. struct Test3
  464. {
  465. static void test (UnitTest& unitTest, Random& r)
  466. {
  467. Test5 <F1, E1, FormatType, AudioData::BigEndian>::test (unitTest, r);
  468. Test5 <F1, E1, FormatType, AudioData::LittleEndian>::test (unitTest, r);
  469. }
  470. };
  471. template <class FormatType, class Endianness>
  472. struct Test2
  473. {
  474. static void test (UnitTest& unitTest, Random& r)
  475. {
  476. Test3 <FormatType, Endianness, AudioData::Int8>::test (unitTest, r);
  477. Test3 <FormatType, Endianness, AudioData::UInt8>::test (unitTest, r);
  478. Test3 <FormatType, Endianness, AudioData::Int16>::test (unitTest, r);
  479. Test3 <FormatType, Endianness, AudioData::Int24>::test (unitTest, r);
  480. Test3 <FormatType, Endianness, AudioData::Int32>::test (unitTest, r);
  481. Test3 <FormatType, Endianness, AudioData::Float32>::test (unitTest, r);
  482. }
  483. };
  484. template <class FormatType>
  485. struct Test1
  486. {
  487. static void test (UnitTest& unitTest, Random& r)
  488. {
  489. Test2 <FormatType, AudioData::BigEndian>::test (unitTest, r);
  490. Test2 <FormatType, AudioData::LittleEndian>::test (unitTest, r);
  491. }
  492. };
  493. void runTest()
  494. {
  495. Random r = getRandom();
  496. beginTest ("Round-trip conversion: Int8");
  497. Test1 <AudioData::Int8>::test (*this, r);
  498. beginTest ("Round-trip conversion: Int16");
  499. Test1 <AudioData::Int16>::test (*this, r);
  500. beginTest ("Round-trip conversion: Int24");
  501. Test1 <AudioData::Int24>::test (*this, r);
  502. beginTest ("Round-trip conversion: Int32");
  503. Test1 <AudioData::Int32>::test (*this, r);
  504. beginTest ("Round-trip conversion: Float32");
  505. Test1 <AudioData::Float32>::test (*this, r);
  506. }
  507. };
  508. static AudioConversionTests audioConversionUnitTests;
  509. #endif