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.

604 lines
21KB

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