PlatformIO package of the Teensy core framework compatible with GCC 10 & C++20
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

effect_delay_ext.cpp 8.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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 <Arduino.h>
  27. #include "effect_delay_ext.h"
  28. //#define INTERNAL_TEST
  29. // While 20 MHz (Teensy actually uses 16 MHz in most cases) and even 24 MHz
  30. // have worked well in testing at room temperature with 3.3V power, to fully
  31. // meet all the worst case timing specs, the SPI clock low time would need
  32. // to be 40ns (12.5 MHz clock) for the single chip case and 51ns (9.8 MHz
  33. // clock) for the 6-chip memoryboard with 74LCX126 buffers.
  34. //
  35. // Timing analysis and info is here:
  36. // https://forum.pjrc.com/threads/29276-Limits-of-delay-effect-in-audio-library?p=97506&viewfull=1#post97506
  37. #define SPISETTING SPISettings(20000000, MSBFIRST, SPI_MODE0)
  38. // Use these with the audio adaptor board (should be adjustable by the user...)
  39. #define SPIRAM_MOSI_PIN 7
  40. #define SPIRAM_MISO_PIN 12
  41. #define SPIRAM_SCK_PIN 14
  42. #define SPIRAM_CS_PIN 6
  43. #define MEMBOARD_CS0_PIN 2
  44. #define MEMBOARD_CS1_PIN 3
  45. #define MEMBOARD_CS2_PIN 4
  46. void AudioEffectDelayExternal::update(void)
  47. {
  48. audio_block_t *block;
  49. uint32_t n, channel, read_offset;
  50. // grab incoming data and put it into the memory
  51. block = receiveReadOnly();
  52. if (memory_type >= AUDIO_MEMORY_UNDEFINED) {
  53. // ignore input and do nothing if undefined memory type
  54. release(block);
  55. return;
  56. }
  57. if (block) {
  58. if (head_offset + AUDIO_BLOCK_SAMPLES <= memory_length) {
  59. // a single write is enough
  60. write(head_offset, AUDIO_BLOCK_SAMPLES, block->data);
  61. head_offset += AUDIO_BLOCK_SAMPLES;
  62. } else {
  63. // write wraps across end-of-memory
  64. n = memory_length - head_offset;
  65. write(head_offset, n, block->data);
  66. head_offset = AUDIO_BLOCK_SAMPLES - n;
  67. write(0, head_offset, block->data + n);
  68. }
  69. release(block);
  70. } else {
  71. // if no input, store zeros, so later playback will
  72. // not be random garbage previously stored in memory
  73. if (head_offset + AUDIO_BLOCK_SAMPLES <= memory_length) {
  74. zero(head_offset, AUDIO_BLOCK_SAMPLES);
  75. head_offset += AUDIO_BLOCK_SAMPLES;
  76. } else {
  77. n = memory_length - head_offset;
  78. zero(head_offset, n);
  79. head_offset = AUDIO_BLOCK_SAMPLES - n;
  80. zero(0, head_offset);
  81. }
  82. }
  83. // transmit the delayed outputs
  84. for (channel = 0; channel < 8; channel++) {
  85. if (!(activemask & (1<<channel))) continue;
  86. block = allocate();
  87. if (!block) continue;
  88. // compute the delayed location where we read
  89. if (delay_length[channel] <= head_offset) {
  90. read_offset = head_offset - delay_length[channel];
  91. } else {
  92. read_offset = memory_length + head_offset - delay_length[channel];
  93. }
  94. if (read_offset + AUDIO_BLOCK_SAMPLES <= memory_length) {
  95. // a single read will do it
  96. read(read_offset, AUDIO_BLOCK_SAMPLES, block->data);
  97. } else {
  98. // read wraps across end-of-memory
  99. n = memory_length - read_offset;
  100. read(read_offset, n, block->data);
  101. read(0, AUDIO_BLOCK_SAMPLES - n, block->data + n);
  102. }
  103. transmit(block, channel);
  104. release(block);
  105. }
  106. }
  107. uint32_t AudioEffectDelayExternal::allocated[2] = {0, 0};
  108. void AudioEffectDelayExternal::initialize(AudioEffectDelayMemoryType_t type, uint32_t samples)
  109. {
  110. uint32_t memsize, avail;
  111. activemask = 0;
  112. head_offset = 0;
  113. memory_type = type;
  114. SPI.setMOSI(SPIRAM_MOSI_PIN);
  115. SPI.setMISO(SPIRAM_MISO_PIN);
  116. SPI.setSCK(SPIRAM_SCK_PIN);
  117. SPI.begin();
  118. if (type == AUDIO_MEMORY_23LC1024) {
  119. #ifdef INTERNAL_TEST
  120. memsize = 8000;
  121. #else
  122. memsize = 65536;
  123. #endif
  124. pinMode(SPIRAM_CS_PIN, OUTPUT);
  125. digitalWriteFast(SPIRAM_CS_PIN, HIGH);
  126. } else if (type == AUDIO_MEMORY_MEMORYBOARD) {
  127. memsize = 393216;
  128. pinMode(MEMBOARD_CS0_PIN, OUTPUT);
  129. pinMode(MEMBOARD_CS1_PIN, OUTPUT);
  130. pinMode(MEMBOARD_CS2_PIN, OUTPUT);
  131. digitalWriteFast(MEMBOARD_CS0_PIN, LOW);
  132. digitalWriteFast(MEMBOARD_CS1_PIN, LOW);
  133. digitalWriteFast(MEMBOARD_CS2_PIN, LOW);
  134. } else if (type == AUDIO_MEMORY_CY15B104) {
  135. #ifdef INTERNAL_TEST
  136. memsize = 8000;
  137. #else
  138. memsize = 262144;
  139. #endif
  140. pinMode(SPIRAM_CS_PIN, OUTPUT);
  141. digitalWriteFast(SPIRAM_CS_PIN, HIGH);
  142. } else {
  143. return;
  144. }
  145. avail = memsize - allocated[type];
  146. if (avail < AUDIO_BLOCK_SAMPLES*2+1) {
  147. memory_type = AUDIO_MEMORY_UNDEFINED;
  148. return;
  149. }
  150. if (samples > avail) samples = avail;
  151. memory_begin = allocated[type];
  152. allocated[type] += samples;
  153. memory_length = samples;
  154. zero(0, memory_length);
  155. }
  156. #ifdef INTERNAL_TEST
  157. static int16_t testmem[8000]; // testing only
  158. #endif
  159. void AudioEffectDelayExternal::read(uint32_t offset, uint32_t count, int16_t *data)
  160. {
  161. uint32_t addr = memory_begin + offset;
  162. #ifdef INTERNAL_TEST
  163. while (count) { *data++ = testmem[addr++]; count--; } // testing only
  164. #else
  165. if (memory_type == AUDIO_MEMORY_23LC1024 ||
  166. memory_type == AUDIO_MEMORY_CY15B104) {
  167. addr *= 2;
  168. SPI.beginTransaction(SPISETTING);
  169. digitalWriteFast(SPIRAM_CS_PIN, LOW);
  170. SPI.transfer16((0x03 << 8) | (addr >> 16));
  171. SPI.transfer16(addr & 0xFFFF);
  172. while (count) {
  173. *data++ = (int16_t)(SPI.transfer16(0));
  174. count--;
  175. }
  176. digitalWriteFast(SPIRAM_CS_PIN, HIGH);
  177. SPI.endTransaction();
  178. } else if (memory_type == AUDIO_MEMORY_MEMORYBOARD) {
  179. SPI.beginTransaction(SPISETTING);
  180. while (count) {
  181. uint32_t chip = (addr >> 16) + 1;
  182. digitalWriteFast(MEMBOARD_CS0_PIN, chip & 1);
  183. digitalWriteFast(MEMBOARD_CS1_PIN, chip & 2);
  184. digitalWriteFast(MEMBOARD_CS2_PIN, chip & 4);
  185. uint32_t chipaddr = (addr & 0xFFFF) << 1;
  186. SPI.transfer16((0x03 << 8) | (chipaddr >> 16));
  187. SPI.transfer16(chipaddr & 0xFFFF);
  188. uint32_t num = 0x10000 - (addr & 0xFFFF);
  189. if (num > count) num = count;
  190. count -= num;
  191. addr += num;
  192. do {
  193. *data++ = (int16_t)(SPI.transfer16(0));
  194. } while (--num > 0);
  195. }
  196. digitalWriteFast(MEMBOARD_CS0_PIN, LOW);
  197. digitalWriteFast(MEMBOARD_CS1_PIN, LOW);
  198. digitalWriteFast(MEMBOARD_CS2_PIN, LOW);
  199. SPI.endTransaction();
  200. }
  201. #endif
  202. }
  203. void AudioEffectDelayExternal::write(uint32_t offset, uint32_t count, const int16_t *data)
  204. {
  205. uint32_t addr = memory_begin + offset;
  206. #ifdef INTERNAL_TEST
  207. while (count) { testmem[addr++] = *data++; count--; } // testing only
  208. #else
  209. if (memory_type == AUDIO_MEMORY_23LC1024) {
  210. addr *= 2;
  211. SPI.beginTransaction(SPISETTING);
  212. digitalWriteFast(SPIRAM_CS_PIN, LOW);
  213. SPI.transfer16((0x02 << 8) | (addr >> 16));
  214. SPI.transfer16(addr & 0xFFFF);
  215. while (count) {
  216. int16_t w = 0;
  217. if (data) w = *data++;
  218. SPI.transfer16(w);
  219. count--;
  220. }
  221. digitalWriteFast(SPIRAM_CS_PIN, HIGH);
  222. SPI.endTransaction();
  223. } else if (memory_type == AUDIO_MEMORY_CY15B104) {
  224. addr *= 2;
  225. SPI.beginTransaction(SPISETTING);
  226. digitalWriteFast(SPIRAM_CS_PIN, LOW);
  227. SPI.transfer(0x06); //write-enable before every write
  228. digitalWriteFast(SPIRAM_CS_PIN, HIGH);
  229. asm volatile ("NOP\n NOP\n NOP\n NOP\n NOP\n NOP\n");
  230. digitalWriteFast(SPIRAM_CS_PIN, LOW);
  231. SPI.transfer16((0x02 << 8) | (addr >> 16));
  232. SPI.transfer16(addr & 0xFFFF);
  233. while (count) {
  234. int16_t w = 0;
  235. if (data) w = *data++;
  236. SPI.transfer16(w);
  237. count--;
  238. }
  239. digitalWriteFast(SPIRAM_CS_PIN, HIGH);
  240. SPI.endTransaction();
  241. } else if (memory_type == AUDIO_MEMORY_MEMORYBOARD) {
  242. SPI.beginTransaction(SPISETTING);
  243. while (count) {
  244. uint32_t chip = (addr >> 16) + 1;
  245. digitalWriteFast(MEMBOARD_CS0_PIN, chip & 1);
  246. digitalWriteFast(MEMBOARD_CS1_PIN, chip & 2);
  247. digitalWriteFast(MEMBOARD_CS2_PIN, chip & 4);
  248. uint32_t chipaddr = (addr & 0xFFFF) << 1;
  249. SPI.transfer16((0x02 << 8) | (chipaddr >> 16));
  250. SPI.transfer16(chipaddr & 0xFFFF);
  251. uint32_t num = 0x10000 - (addr & 0xFFFF);
  252. if (num > count) num = count;
  253. count -= num;
  254. addr += num;
  255. do {
  256. int16_t w = 0;
  257. if (data) w = *data++;
  258. SPI.transfer16(w);
  259. } while (--num > 0);
  260. }
  261. digitalWriteFast(MEMBOARD_CS0_PIN, LOW);
  262. digitalWriteFast(MEMBOARD_CS1_PIN, LOW);
  263. digitalWriteFast(MEMBOARD_CS2_PIN, LOW);
  264. SPI.endTransaction();
  265. }
  266. #endif
  267. }