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.

190 lines
4.8KB

  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 "play_memory.h"
  27. #include "utility/dspinst.h"
  28. void AudioPlayMemory::play(const unsigned int *data)
  29. {
  30. uint32_t format;
  31. playing = 0;
  32. prior = 0;
  33. format = *data++;
  34. next = data;
  35. length = format & 0xFFFFFF;
  36. playing = format >> 24;
  37. }
  38. void AudioPlayMemory::stop(void)
  39. {
  40. playing = 0;
  41. }
  42. extern "C" {
  43. extern const int16_t ulaw_decode_table[256];
  44. };
  45. void AudioPlayMemory::update(void)
  46. {
  47. audio_block_t *block;
  48. const unsigned int *in;
  49. int16_t *out;
  50. uint32_t tmp32, consumed;
  51. int16_t s0, s1, s2, s3, s4;
  52. int i;
  53. if (!playing) return;
  54. block = allocate();
  55. if (block == NULL) return;
  56. //Serial.write('.');
  57. out = block->data;
  58. in = next;
  59. s0 = prior;
  60. switch (playing) {
  61. case 0x01: // u-law encoded, 44100 Hz
  62. for (i=0; i < AUDIO_BLOCK_SAMPLES; i += 4) {
  63. tmp32 = *in++;
  64. *out++ = ulaw_decode_table[(tmp32 >> 0) & 255];
  65. *out++ = ulaw_decode_table[(tmp32 >> 8) & 255];
  66. *out++ = ulaw_decode_table[(tmp32 >> 16) & 255];
  67. *out++ = ulaw_decode_table[(tmp32 >> 24) & 255];
  68. }
  69. consumed = 128;
  70. break;
  71. case 0x81: // 16 bit PCM, 44100 Hz
  72. for (i=0; i < AUDIO_BLOCK_SAMPLES; i += 2) {
  73. tmp32 = *in++;
  74. *out++ = (int16_t)(tmp32 & 65535);
  75. *out++ = (int16_t)(tmp32 >> 16);
  76. }
  77. consumed = 128;
  78. break;
  79. case 0x02: // u-law encoded, 22050 Hz
  80. for (i=0; i < AUDIO_BLOCK_SAMPLES; i += 8) {
  81. tmp32 = *in++;
  82. s1 = ulaw_decode_table[(tmp32 >> 0) & 255];
  83. s2 = ulaw_decode_table[(tmp32 >> 8) & 255];
  84. s3 = ulaw_decode_table[(tmp32 >> 16) & 255];
  85. s4 = ulaw_decode_table[(tmp32 >> 24) & 255];
  86. *out++ = (s0 + s1) >> 1;
  87. *out++ = s1;
  88. *out++ = (s1 + s2) >> 1;
  89. *out++ = s2;
  90. *out++ = (s2 + s3) >> 1;
  91. *out++ = s3;
  92. *out++ = (s3 + s4) >> 1;
  93. *out++ = s4;
  94. s0 = s4;
  95. }
  96. consumed = 64;
  97. break;
  98. case 0x82: // 16 bits PCM, 22050 Hz
  99. for (i=0; i < AUDIO_BLOCK_SAMPLES; i += 4) {
  100. tmp32 = *in++;
  101. s1 = (int16_t)(tmp32 & 65535);
  102. s2 = (int16_t)(tmp32 >> 16);
  103. *out++ = (s0 + s1) >> 1;
  104. *out++ = s1;
  105. *out++ = (s1 + s2) >> 1;
  106. *out++ = s2;
  107. s0 = s2;
  108. }
  109. consumed = 64;
  110. break;
  111. case 0x03: // u-law encoded, 11025 Hz
  112. for (i=0; i < AUDIO_BLOCK_SAMPLES; i += 16) {
  113. tmp32 = *in++;
  114. s1 = ulaw_decode_table[(tmp32 >> 0) & 255];
  115. s2 = ulaw_decode_table[(tmp32 >> 8) & 255];
  116. s3 = ulaw_decode_table[(tmp32 >> 16) & 255];
  117. s4 = ulaw_decode_table[(tmp32 >> 24) & 255];
  118. *out++ = (s0 * 3 + s1) >> 2;
  119. *out++ = (s0 + s1) >> 1;
  120. *out++ = (s0 + s1 * 3) >> 2;
  121. *out++ = s1;
  122. *out++ = (s1 * 3 + s2) >> 2;
  123. *out++ = (s1 + s2) >> 1;
  124. *out++ = (s1 + s2 * 3) >> 2;
  125. *out++ = s2;
  126. *out++ = (s2 * 3 + s3) >> 2;
  127. *out++ = (s2 + s3) >> 1;
  128. *out++ = (s2 + s3 * 3) >> 2;
  129. *out++ = s3;
  130. *out++ = (s3 * 3 + s4) >> 2;
  131. *out++ = (s3 + s4) >> 1;
  132. *out++ = (s3 + s4 * 3) >> 2;
  133. *out++ = s4;
  134. s0 = s4;
  135. }
  136. consumed = 32;
  137. break;
  138. case 0x83: // 16 bit PCM, 11025 Hz
  139. for (i=0; i < AUDIO_BLOCK_SAMPLES; i += 8) {
  140. tmp32 = *in++;
  141. s1 = (int16_t)(tmp32 & 65535);
  142. s2 = (int16_t)(tmp32 >> 16);
  143. *out++ = (s0 * 3 + s1) >> 2;
  144. *out++ = (s0 + s1) >> 1;
  145. *out++ = (s0 + s1 * 3) >> 2;
  146. *out++ = s1;
  147. *out++ = (s1 * 3 + s2) >> 2;
  148. *out++ = (s1 + s2) >> 1;
  149. *out++ = (s1 + s2 * 3) >> 2;
  150. *out++ = s2;
  151. s0 = s2;
  152. }
  153. consumed = 32;
  154. break;
  155. default:
  156. release(block);
  157. playing = 0;
  158. return;
  159. }
  160. prior = s0;
  161. next = in;
  162. if (length > consumed) {
  163. length -= consumed;
  164. } else {
  165. playing = 0;
  166. }
  167. transmit(block);
  168. release(block);
  169. }