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.

240 lines
6.9KB

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