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.

94 lines
3.6KB

  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 "effect_midside.h"
  27. void AudioEffectMidSide::update(void)
  28. {
  29. audio_block_t *blocka, *blockb;
  30. uint32_t *pa, *pb, *end;
  31. uint32_t a12, a34; //, a56, a78;
  32. uint32_t b12, b34; //, b56, b78;
  33. blocka = receiveWritable(0); // left (encoding) or mid (decoding)
  34. blockb = receiveWritable(1); // right (encoding) or side (decoding)
  35. if (!blocka || !blockb) {
  36. if (blocka) release(blocka); // maybe an extra if statement here but if one
  37. if (blockb) release(blockb); // of the blocks is NULL then it's trouble anyway
  38. return;
  39. }
  40. #if defined(KINETISK)
  41. pa = (uint32_t *)(blocka->data);
  42. pb = (uint32_t *)(blockb->data);
  43. end = pa + AUDIO_BLOCK_SAMPLES/2;
  44. if (encoding) {
  45. while (pa < end) {
  46. // mid[i] = (blocka[i] + blockb[i])/2; // div2 to prevent overflows
  47. // side[i] = (blocka[i] - blockb[i])/2; // div2 to prevent overflows
  48. // L[i] = (mid[i] + side[i])/2;
  49. // R[i] = (mid[i] - side[i])/2;
  50. a12 = signed_halving_add_16_and_16(*pa, *pb); // mid12
  51. a34 = signed_halving_add_16_and_16(*(pa+1), *(pb+1)); //mid34
  52. b12 = signed_halving_subtract_16_and_16(*pa, *pb); // side12
  53. b34 = signed_halving_subtract_16_and_16(*(pa+1), *(pb+1)); // side34
  54. *pa++ = a12;
  55. *pa++ = a34;
  56. *pb++ = b12;
  57. *pb++ = b34;
  58. }
  59. } else {
  60. while (pa < end) {
  61. // L[i] = mid[i] + side[i]);
  62. // R[i] = mid[i] - side[i]);
  63. // Because the /2 has already been applied in the encoding,
  64. // we shouldn't have to add it here.
  65. // However... because of the posibility that the user has
  66. // modified mid or side such that
  67. // it could overflow, we have to:
  68. // a) preventively do a (x/2+y/2)*2 again, causing bit reduction
  69. // or b) perform saturation or something.
  70. // While (b) could produce artifacts if saturated, I'll go for
  71. // that option to preserve precision
  72. // QADD16 and QSUB16 perform saturating add/sub
  73. a12 = signed_add_16_and_16(*pa, *pb); // left12
  74. a34 = signed_add_16_and_16(*(pa+1), *(pb+1)); // left34
  75. b12 = signed_subtract_16_and_16(*pa, *pb); // right12
  76. b34 = signed_subtract_16_and_16(*(pa+1), *(pb+1)); // right34
  77. *pa++ = a12;
  78. *pa++ = a34;
  79. *pb++ = b12;
  80. *pb++ = b34;
  81. }
  82. }
  83. transmit(blocka, 0); // mid (encoding) or left (decoding)
  84. transmit(blockb, 1); // side (encoding) or right (decoding)
  85. #endif
  86. release(blocka);
  87. release(blockb);
  88. }