Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

effect_midside.cpp 3.6KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /* Audio Library for Teensy 3.X
  2. * Copyright (c) 2015, Hedde Bosman
  3. *
  4. * Development of this audio library was funded by PJRC.COM, LLC by sales of
  5. * Teensy and Audio Adaptor boards. Please support PJRC's efforts to develop
  6. * open source software by purchasing Teensy or other PJRC products.
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy
  9. * of this software and associated documentation files (the "Software"), to deal
  10. * in the Software without restriction, including without limitation the rights
  11. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. * copies of the Software, and to permit persons to whom the Software is
  13. * furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice, development funding notice, and this permission
  16. * notice shall be included in all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. * THE SOFTWARE.
  25. */
  26. #include <Arduino.h>
  27. #include "effect_midside.h"
  28. void AudioEffectMidSide::update(void)
  29. {
  30. audio_block_t *blocka, *blockb;
  31. uint32_t *pa, *pb, *end;
  32. uint32_t a12, a34; //, a56, a78;
  33. uint32_t b12, b34; //, b56, b78;
  34. blocka = receiveWritable(0); // left (encoding) or mid (decoding)
  35. blockb = receiveWritable(1); // right (encoding) or side (decoding)
  36. if (!blocka || !blockb) {
  37. if (blocka) release(blocka); // maybe an extra if statement here but if one
  38. if (blockb) release(blockb); // of the blocks is NULL then it's trouble anyway
  39. return;
  40. }
  41. #if defined(KINETISK)
  42. pa = (uint32_t *)(blocka->data);
  43. pb = (uint32_t *)(blockb->data);
  44. end = pa + AUDIO_BLOCK_SAMPLES/2;
  45. if (encoding) {
  46. while (pa < end) {
  47. // mid[i] = (blocka[i] + blockb[i])/2; // div2 to prevent overflows
  48. // side[i] = (blocka[i] - blockb[i])/2; // div2 to prevent overflows
  49. // L[i] = (mid[i] + side[i])/2;
  50. // R[i] = (mid[i] - side[i])/2;
  51. a12 = signed_halving_add_16_and_16(*pa, *pb); // mid12
  52. a34 = signed_halving_add_16_and_16(*(pa+1), *(pb+1)); //mid34
  53. b12 = signed_halving_subtract_16_and_16(*pa, *pb); // side12
  54. b34 = signed_halving_subtract_16_and_16(*(pa+1), *(pb+1)); // side34
  55. *pa++ = a12;
  56. *pa++ = a34;
  57. *pb++ = b12;
  58. *pb++ = b34;
  59. }
  60. } else {
  61. while (pa < end) {
  62. // L[i] = mid[i] + side[i]);
  63. // R[i] = mid[i] - side[i]);
  64. // Because the /2 has already been applied in the encoding,
  65. // we shouldn't have to add it here.
  66. // However... because of the posibility that the user has
  67. // modified mid or side such that
  68. // it could overflow, we have to:
  69. // a) preventively do a (x/2+y/2)*2 again, causing bit reduction
  70. // or b) perform saturation or something.
  71. // While (b) could produce artifacts if saturated, I'll go for
  72. // that option to preserve precision
  73. // QADD16 and QSUB16 perform saturating add/sub
  74. a12 = signed_add_16_and_16(*pa, *pb); // left12
  75. a34 = signed_add_16_and_16(*(pa+1), *(pb+1)); // left34
  76. b12 = signed_subtract_16_and_16(*pa, *pb); // right12
  77. b34 = signed_subtract_16_and_16(*(pa+1), *(pb+1)); // right34
  78. *pa++ = a12;
  79. *pa++ = a34;
  80. *pb++ = b12;
  81. *pb++ = b34;
  82. }
  83. }
  84. transmit(blocka, 0); // mid (encoding) or left (decoding)
  85. transmit(blockb, 1); // side (encoding) or right (decoding)
  86. #endif
  87. release(blocka);
  88. release(blockb);
  89. }