Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

256 lines
7.6KB

  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 "effect_delay_ext.h"
  27. //#define INTERNAL_TEST
  28. // Use these with the audio adaptor board (should be adjustable by the user...)
  29. #define SPIRAM_MOSI_PIN 7
  30. #define SPIRAM_MISO_PIN 12
  31. #define SPIRAM_SCK_PIN 14
  32. #define SPIRAM_CS_PIN 6
  33. #define MEMBOARD_CS0_PIN 2
  34. #define MEMBOARD_CS1_PIN 3
  35. #define MEMBOARD_CS2_PIN 4
  36. void AudioEffectDelayExternal::update(void)
  37. {
  38. audio_block_t *block;
  39. uint32_t n, channel, read_offset;
  40. // grab incoming data and put it into the memory
  41. block = receiveReadOnly();
  42. if (memory_type >= AUDIO_MEMORY_UNDEFINED) {
  43. // ignore input and do nothing if undefined memory type
  44. release(block);
  45. return;
  46. }
  47. if (block) {
  48. if (head_offset + AUDIO_BLOCK_SAMPLES <= memory_length) {
  49. // a single write is enough
  50. write(head_offset, AUDIO_BLOCK_SAMPLES, block->data);
  51. head_offset += AUDIO_BLOCK_SAMPLES;
  52. } else {
  53. // write wraps across end-of-memory
  54. n = memory_length - head_offset;
  55. write(head_offset, n, block->data);
  56. head_offset = AUDIO_BLOCK_SAMPLES - n;
  57. write(0, head_offset, block->data + n);
  58. }
  59. release(block);
  60. } else {
  61. // if no input, store zeros, so later playback will
  62. // not be random garbage previously stored in memory
  63. if (head_offset + AUDIO_BLOCK_SAMPLES <= memory_length) {
  64. zero(head_offset, AUDIO_BLOCK_SAMPLES);
  65. head_offset += AUDIO_BLOCK_SAMPLES;
  66. } else {
  67. n = memory_length - head_offset;
  68. zero(head_offset, n);
  69. head_offset = AUDIO_BLOCK_SAMPLES - n;
  70. zero(0, head_offset);
  71. }
  72. }
  73. // transmit the delayed outputs
  74. for (channel = 0; channel < 8; channel++) {
  75. if (!(activemask & (1<<channel))) continue;
  76. block = allocate();
  77. if (!block) continue;
  78. // compute the delayed location where we read
  79. if (delay_length[channel] <= head_offset) {
  80. read_offset = head_offset - delay_length[channel];
  81. } else {
  82. read_offset = memory_length + head_offset - delay_length[channel];
  83. }
  84. if (read_offset + AUDIO_BLOCK_SAMPLES <= memory_length) {
  85. // a single read will do it
  86. read(read_offset, AUDIO_BLOCK_SAMPLES, block->data);
  87. } else {
  88. // read wraps across end-of-memory
  89. n = memory_length - read_offset;
  90. read(read_offset, n, block->data);
  91. read(0, AUDIO_BLOCK_SAMPLES - n, block->data + n);
  92. }
  93. transmit(block, channel);
  94. release(block);
  95. }
  96. }
  97. uint32_t AudioEffectDelayExternal::allocated[2] = {0, 0};
  98. void AudioEffectDelayExternal::initialize(AudioEffectDelayMemoryType_t type, uint32_t samples)
  99. {
  100. uint32_t memsize, avail;
  101. activemask = 0;
  102. head_offset = 0;
  103. memory_type = type;
  104. if (type == AUDIO_MEMORY_23LC1024) {
  105. #ifdef INTERNAL_TEST
  106. memsize = 8000;
  107. #else
  108. memsize = 65536;
  109. #endif
  110. pinMode(SPIRAM_CS_PIN, OUTPUT);
  111. digitalWriteFast(SPIRAM_CS_PIN, HIGH);
  112. } else if (type == AUDIO_MEMORY_MEMORYBOARD) {
  113. memsize = 393216;
  114. pinMode(MEMBOARD_CS0_PIN, OUTPUT);
  115. pinMode(MEMBOARD_CS1_PIN, OUTPUT);
  116. pinMode(MEMBOARD_CS2_PIN, OUTPUT);
  117. digitalWriteFast(MEMBOARD_CS0_PIN, LOW);
  118. digitalWriteFast(MEMBOARD_CS1_PIN, LOW);
  119. digitalWriteFast(MEMBOARD_CS2_PIN, LOW);
  120. } else {
  121. return;
  122. }
  123. avail = memsize - allocated[type];
  124. if (avail < AUDIO_BLOCK_SAMPLES*2+1) {
  125. memory_type = AUDIO_MEMORY_UNDEFINED;
  126. return;
  127. }
  128. if (samples > avail) samples = avail;
  129. memory_begin = allocated[type];
  130. allocated[type] += samples;
  131. memory_length = samples;
  132. SPI.setMOSI(SPIRAM_MOSI_PIN);
  133. SPI.setMISO(SPIRAM_MISO_PIN);
  134. SPI.setSCK(SPIRAM_SCK_PIN);
  135. SPI.begin();
  136. zero(0, memory_length);
  137. }
  138. #ifdef INTERNAL_TEST
  139. static int16_t testmem[8000]; // testing only
  140. #endif
  141. #define SPISETTING SPISettings(20000000, MSBFIRST, SPI_MODE0)
  142. void AudioEffectDelayExternal::read(uint32_t offset, uint32_t count, int16_t *data)
  143. {
  144. uint32_t addr = memory_begin + offset;
  145. #ifdef INTERNAL_TEST
  146. while (count) { *data++ = testmem[addr++]; count--; } // testing only
  147. #else
  148. if (memory_type == AUDIO_MEMORY_23LC1024) {
  149. addr *= 2;
  150. SPI.beginTransaction(SPISETTING);
  151. digitalWriteFast(SPIRAM_CS_PIN, LOW);
  152. SPI.transfer16((0x03 << 8) | (addr >> 16));
  153. SPI.transfer16(addr & 0xFFFF);
  154. while (count) {
  155. *data++ = (int16_t)(SPI.transfer16(0));
  156. count--;
  157. }
  158. digitalWriteFast(SPIRAM_CS_PIN, HIGH);
  159. SPI.endTransaction();
  160. } else if (memory_type == AUDIO_MEMORY_MEMORYBOARD) {
  161. SPI.beginTransaction(SPISETTING);
  162. while (count) {
  163. uint32_t chip = (addr >> 16) + 1;
  164. digitalWriteFast(MEMBOARD_CS0_PIN, chip & 1);
  165. digitalWriteFast(MEMBOARD_CS1_PIN, chip & 2);
  166. digitalWriteFast(MEMBOARD_CS2_PIN, chip & 4);
  167. uint32_t chipaddr = (addr & 0xFFFF) << 1;
  168. SPI.transfer16((0x03 << 8) | (chipaddr >> 16));
  169. SPI.transfer16(chipaddr & 0xFFFF);
  170. uint32_t num = 0x10000 - (addr & 0xFFFF);
  171. if (num > count) num = count;
  172. count -= num;
  173. addr += num;
  174. do {
  175. *data++ = (int16_t)(SPI.transfer16(0));
  176. } while (--num > 0);
  177. }
  178. digitalWriteFast(MEMBOARD_CS0_PIN, LOW);
  179. digitalWriteFast(MEMBOARD_CS1_PIN, LOW);
  180. digitalWriteFast(MEMBOARD_CS2_PIN, LOW);
  181. SPI.endTransaction();
  182. }
  183. #endif
  184. }
  185. void AudioEffectDelayExternal::write(uint32_t offset, uint32_t count, const int16_t *data)
  186. {
  187. uint32_t addr = memory_begin + offset;
  188. #ifdef INTERNAL_TEST
  189. while (count) { testmem[addr++] = *data++; count--; } // testing only
  190. #else
  191. if (memory_type == AUDIO_MEMORY_23LC1024) {
  192. addr *= 2;
  193. SPI.beginTransaction(SPISETTING);
  194. digitalWriteFast(SPIRAM_CS_PIN, LOW);
  195. SPI.transfer16((0x02 << 8) | (addr >> 16));
  196. SPI.transfer16(addr & 0xFFFF);
  197. while (count) {
  198. int16_t w = 0;
  199. if (data) w = *data++;
  200. SPI.transfer16(w);
  201. count--;
  202. }
  203. digitalWriteFast(SPIRAM_CS_PIN, HIGH);
  204. SPI.endTransaction();
  205. } else if (memory_type == AUDIO_MEMORY_MEMORYBOARD) {
  206. SPI.beginTransaction(SPISETTING);
  207. while (count) {
  208. uint32_t chip = (addr >> 16) + 1;
  209. digitalWriteFast(MEMBOARD_CS0_PIN, chip & 1);
  210. digitalWriteFast(MEMBOARD_CS1_PIN, chip & 2);
  211. digitalWriteFast(MEMBOARD_CS2_PIN, chip & 4);
  212. uint32_t chipaddr = (addr & 0xFFFF) << 1;
  213. SPI.transfer16((0x02 << 8) | (chipaddr >> 16));
  214. SPI.transfer16(chipaddr & 0xFFFF);
  215. uint32_t num = 0x10000 - (addr & 0xFFFF);
  216. if (num > count) num = count;
  217. count -= num;
  218. addr += num;
  219. do {
  220. int16_t w = 0;
  221. if (data) w = *data++;
  222. SPI.transfer16(w);
  223. } while (--num > 0);
  224. }
  225. digitalWriteFast(MEMBOARD_CS0_PIN, LOW);
  226. digitalWriteFast(MEMBOARD_CS1_PIN, LOW);
  227. digitalWriteFast(MEMBOARD_CS2_PIN, LOW);
  228. SPI.endTransaction();
  229. }
  230. #endif
  231. }