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.

effect_midside.cpp 3.7KB

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