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 3.2KB

10 lat temu
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. void applyGain(int16_t *data, int32_t mult)
  29. {
  30. uint32_t *p = (uint32_t *)data;
  31. const uint32_t *end = (uint32_t *)(data + AUDIO_BLOCK_SAMPLES);
  32. do {
  33. uint32_t tmp32 = *p; // read 2 samples from *data
  34. int32_t val1 = signed_multiply_32x16b(mult, tmp32);
  35. int32_t val2 = signed_multiply_32x16t(mult, tmp32);
  36. val1 = signed_saturate_rshift(val1, 16, 0);
  37. val2 = signed_saturate_rshift(val2, 16, 0);
  38. *p++ = pack_16x16(val2, val1);
  39. } while (p < end);
  40. }
  41. // page 133
  42. void applyGainThenAdd(int16_t *data, const int16_t *in, int32_t mult)
  43. {
  44. uint32_t *dst = (uint32_t *)data;
  45. const uint32_t *src = (uint32_t *)in;
  46. const uint32_t *end = (uint32_t *)(data + AUDIO_BLOCK_SAMPLES);
  47. if (mult == 65536) {
  48. do {
  49. uint32_t tmp32 = *dst;
  50. *dst++ = signed_add_16_and_16(tmp32, *src++);
  51. tmp32 = *dst;
  52. *dst++ = signed_add_16_and_16(tmp32, *src++);
  53. } while (dst < end);
  54. } else {
  55. do {
  56. uint32_t tmp32 = *src++; // read 2 samples from *data
  57. int32_t val1 = signed_multiply_32x16b(mult, tmp32);
  58. int32_t val2 = signed_multiply_32x16t(mult, tmp32);
  59. val1 = signed_saturate_rshift(val1, 16, 0);
  60. val2 = signed_saturate_rshift(val2, 16, 0);
  61. tmp32 = pack_16x16(val2, val1);
  62. uint32_t tmp32b = *dst;
  63. *dst++ = signed_add_16_and_16(tmp32, tmp32b);
  64. } while (dst < end);
  65. }
  66. }
  67. void AudioMixer4::update(void)
  68. {
  69. audio_block_t *in, *out=NULL;
  70. unsigned int channel;
  71. for (channel=0; channel < 4; channel++) {
  72. if (!out) {
  73. out = receiveWritable(channel);
  74. if (out) {
  75. int32_t mult = multiplier[channel];
  76. if (mult != 65536) applyGain(out->data, mult);
  77. }
  78. } else {
  79. in = receiveReadOnly(channel);
  80. if (in) {
  81. applyGainThenAdd(out->data, in->data, multiplier[channel]);
  82. release(in);
  83. }
  84. }
  85. }
  86. if (out) {
  87. transmit(out);
  88. release(out);
  89. }
  90. }