Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

mixer.cpp 4.0KB

10 lat temu
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /* Audio Library for Teensy 3.X
  2. * Copyright (c) 2014, Paul Stoffregen, paul@pjrc.com
  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 "mixer.h"
  27. #include "utility/dspinst.h"
  28. #if defined(KINETISK)
  29. #define MULTI_UNITYGAIN 65536
  30. static void applyGain(int16_t *data, int32_t mult)
  31. {
  32. uint32_t *p = (uint32_t *)data;
  33. const uint32_t *end = (uint32_t *)(data + AUDIO_BLOCK_SAMPLES);
  34. do {
  35. uint32_t tmp32 = *p; // read 2 samples from *data
  36. int32_t val1 = signed_multiply_32x16b(mult, tmp32);
  37. int32_t val2 = signed_multiply_32x16t(mult, tmp32);
  38. val1 = signed_saturate_rshift(val1, 16, 0);
  39. val2 = signed_saturate_rshift(val2, 16, 0);
  40. *p++ = pack_16b_16b(val2, val1);
  41. } while (p < end);
  42. }
  43. static void applyGainThenAdd(int16_t *data, const int16_t *in, int32_t mult)
  44. {
  45. uint32_t *dst = (uint32_t *)data;
  46. const uint32_t *src = (uint32_t *)in;
  47. const uint32_t *end = (uint32_t *)(data + AUDIO_BLOCK_SAMPLES);
  48. if (mult == MULTI_UNITYGAIN) {
  49. do {
  50. uint32_t tmp32 = *dst;
  51. *dst++ = signed_add_16_and_16(tmp32, *src++);
  52. tmp32 = *dst;
  53. *dst++ = signed_add_16_and_16(tmp32, *src++);
  54. } while (dst < end);
  55. } else {
  56. do {
  57. uint32_t tmp32 = *src++; // read 2 samples from *data
  58. int32_t val1 = signed_multiply_32x16b(mult, tmp32);
  59. int32_t val2 = signed_multiply_32x16t(mult, tmp32);
  60. val1 = signed_saturate_rshift(val1, 16, 0);
  61. val2 = signed_saturate_rshift(val2, 16, 0);
  62. tmp32 = pack_16b_16b(val2, val1);
  63. uint32_t tmp32b = *dst;
  64. *dst++ = signed_add_16_and_16(tmp32, tmp32b);
  65. } while (dst < end);
  66. }
  67. }
  68. #elif defined(KINETISL)
  69. #define MULTI_UNITYGAIN 256
  70. static void applyGain(int16_t *data, int32_t mult)
  71. {
  72. const int16_t *end = data + AUDIO_BLOCK_SAMPLES;
  73. do {
  74. int32_t val = *data * mult;
  75. *data++ = signed_saturate_rshift(val, 16, 0);
  76. } while (data < end);
  77. }
  78. static void applyGainThenAdd(int16_t *dst, const int16_t *src, int32_t mult)
  79. {
  80. const int16_t *end = dst + AUDIO_BLOCK_SAMPLES;
  81. if (mult == MULTI_UNITYGAIN) {
  82. do {
  83. int32_t val = *dst + *src++;
  84. *dst++ = signed_saturate_rshift(val, 16, 0);
  85. } while (dst < end);
  86. } else {
  87. do {
  88. int32_t val = *dst + ((*src++ * mult) >> 8); // overflow possible??
  89. *dst++ = signed_saturate_rshift(val, 16, 0);
  90. } while (dst < end);
  91. }
  92. }
  93. #endif
  94. void AudioMixer4::update(void)
  95. {
  96. audio_block_t *in, *out=NULL;
  97. unsigned int channel;
  98. for (channel=0; channel < 4; channel++) {
  99. if (!out) {
  100. out = receiveWritable(channel);
  101. if (out) {
  102. int32_t mult = multiplier[channel];
  103. if (mult != MULTI_UNITYGAIN) applyGain(out->data, mult);
  104. }
  105. } else {
  106. in = receiveReadOnly(channel);
  107. if (in) {
  108. applyGainThenAdd(out->data, in->data, multiplier[channel]);
  109. release(in);
  110. }
  111. }
  112. }
  113. if (out) {
  114. transmit(out);
  115. release(out);
  116. }
  117. }