Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

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