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.

265 lines
8.1KB

  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. // While 20 MHz (Teensy actually uses 16 MHz in most cases) and even 24 MHz
  143. // have worked well in testing at room temperature with 3.3V power, to fully
  144. // meet all the worst case timing specs, the SPI clock low time would need
  145. // to be 40ns (12.5 MHz clock) for the single chip case and 51ns (9.8 MHz
  146. // clock) for the 6-chip memoryboard with 74LCX126 buffers.
  147. //
  148. // Timing analysis and info is here:
  149. // https://forum.pjrc.com/threads/29276-Limits-of-delay-effect-in-audio-library?p=97506&viewfull=1#post97506
  150. void AudioEffectDelayExternal::read(uint32_t offset, uint32_t count, int16_t *data)
  151. {
  152. uint32_t addr = memory_begin + offset;
  153. #ifdef INTERNAL_TEST
  154. while (count) { *data++ = testmem[addr++]; count--; } // testing only
  155. #else
  156. if (memory_type == AUDIO_MEMORY_23LC1024) {
  157. addr *= 2;
  158. SPI.beginTransaction(SPISETTING);
  159. digitalWriteFast(SPIRAM_CS_PIN, LOW);
  160. SPI.transfer16((0x03 << 8) | (addr >> 16));
  161. SPI.transfer16(addr & 0xFFFF);
  162. while (count) {
  163. *data++ = (int16_t)(SPI.transfer16(0));
  164. count--;
  165. }
  166. digitalWriteFast(SPIRAM_CS_PIN, HIGH);
  167. SPI.endTransaction();
  168. } else if (memory_type == AUDIO_MEMORY_MEMORYBOARD) {
  169. SPI.beginTransaction(SPISETTING);
  170. while (count) {
  171. uint32_t chip = (addr >> 16) + 1;
  172. digitalWriteFast(MEMBOARD_CS0_PIN, chip & 1);
  173. digitalWriteFast(MEMBOARD_CS1_PIN, chip & 2);
  174. digitalWriteFast(MEMBOARD_CS2_PIN, chip & 4);
  175. uint32_t chipaddr = (addr & 0xFFFF) << 1;
  176. SPI.transfer16((0x03 << 8) | (chipaddr >> 16));
  177. SPI.transfer16(chipaddr & 0xFFFF);
  178. uint32_t num = 0x10000 - (addr & 0xFFFF);
  179. if (num > count) num = count;
  180. count -= num;
  181. addr += num;
  182. do {
  183. *data++ = (int16_t)(SPI.transfer16(0));
  184. } while (--num > 0);
  185. }
  186. digitalWriteFast(MEMBOARD_CS0_PIN, LOW);
  187. digitalWriteFast(MEMBOARD_CS1_PIN, LOW);
  188. digitalWriteFast(MEMBOARD_CS2_PIN, LOW);
  189. SPI.endTransaction();
  190. }
  191. #endif
  192. }
  193. void AudioEffectDelayExternal::write(uint32_t offset, uint32_t count, const int16_t *data)
  194. {
  195. uint32_t addr = memory_begin + offset;
  196. #ifdef INTERNAL_TEST
  197. while (count) { testmem[addr++] = *data++; count--; } // testing only
  198. #else
  199. if (memory_type == AUDIO_MEMORY_23LC1024) {
  200. addr *= 2;
  201. SPI.beginTransaction(SPISETTING);
  202. digitalWriteFast(SPIRAM_CS_PIN, LOW);
  203. SPI.transfer16((0x02 << 8) | (addr >> 16));
  204. SPI.transfer16(addr & 0xFFFF);
  205. while (count) {
  206. int16_t w = 0;
  207. if (data) w = *data++;
  208. SPI.transfer16(w);
  209. count--;
  210. }
  211. digitalWriteFast(SPIRAM_CS_PIN, HIGH);
  212. SPI.endTransaction();
  213. } else if (memory_type == AUDIO_MEMORY_MEMORYBOARD) {
  214. SPI.beginTransaction(SPISETTING);
  215. while (count) {
  216. uint32_t chip = (addr >> 16) + 1;
  217. digitalWriteFast(MEMBOARD_CS0_PIN, chip & 1);
  218. digitalWriteFast(MEMBOARD_CS1_PIN, chip & 2);
  219. digitalWriteFast(MEMBOARD_CS2_PIN, chip & 4);
  220. uint32_t chipaddr = (addr & 0xFFFF) << 1;
  221. SPI.transfer16((0x02 << 8) | (chipaddr >> 16));
  222. SPI.transfer16(chipaddr & 0xFFFF);
  223. uint32_t num = 0x10000 - (addr & 0xFFFF);
  224. if (num > count) num = count;
  225. count -= num;
  226. addr += num;
  227. do {
  228. int16_t w = 0;
  229. if (data) w = *data++;
  230. SPI.transfer16(w);
  231. } while (--num > 0);
  232. }
  233. digitalWriteFast(MEMBOARD_CS0_PIN, LOW);
  234. digitalWriteFast(MEMBOARD_CS1_PIN, LOW);
  235. digitalWriteFast(MEMBOARD_CS2_PIN, LOW);
  236. SPI.endTransaction();
  237. }
  238. #endif
  239. }