audio.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500
  1. /**
  2. * Copyright (C) 2023 saybur
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation, either version 3 of the License, or
  7. * (at your option) any later version. 
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12. * GNU General Public License for more details. 
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program.  If not, see <https://www.gnu.org/licenses/>.
  16. **/
  17. #ifdef ENABLE_AUDIO_OUTPUT
  18. #include <SdFat.h>
  19. #include <stdbool.h>
  20. #include <hardware/dma.h>
  21. #include <hardware/irq.h>
  22. #include <hardware/spi.h>
  23. #include <pico/multicore.h>
  24. #include "audio.h"
  25. #include "BlueSCSI_config.h"
  26. #include "BlueSCSI_log.h"
  27. #include "BlueSCSI_platform.h"
  28. #ifdef __cplusplus
  29. extern "C" {
  30. #endif
  31. extern SdFs SD;
  32. // Table with the number of '1' bits for each index.
  33. // Used for SP/DIF parity calculations.
  34. // Placed in SRAM5 for the second core to use with reduced contention.
  35. const uint8_t snd_parity[256] __attribute__((aligned(256), section(".scratch_y.snd_parity"))) = {
  36. 0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4,
  37. 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
  38. 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
  39. 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
  40. 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
  41. 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
  42. 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
  43. 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
  44. 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
  45. 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
  46. 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
  47. 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
  48. 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
  49. 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
  50. 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
  51. 4, 5, 5, 6, 5, 6, 6, 7, 5, 6, 6, 7, 6, 7, 7, 8, };
  52. /*
  53. * Precomputed biphase-mark patterns for data. For an 8-bit value this has
  54. * 16-bits in MSB-first order for the correct high/low transitions to
  55. * represent the data, given an output clocking rate twice the bitrate (so the
  56. * bits '11' or '00' reflect a zero and '10' or '01' represent a one). Each
  57. * value below starts with a '1' and will need to be inverted if the last bit
  58. * of the previous mask was also a '1'. These values can be written to an
  59. * appropriately configured SPI peripheral to blast biphase data at a
  60. * receiver.
  61. *
  62. * To facilitate fast lookups this table should be put in SRAM with low
  63. * contention, aligned to an apppropriate boundry.
  64. */
  65. const uint16_t biphase[256] __attribute__((aligned(512), section(".scratch_y.biphase"))) = {
  66. 0xCCCC, 0xB333, 0xD333, 0xACCC, 0xCB33, 0xB4CC, 0xD4CC, 0xAB33,
  67. 0xCD33, 0xB2CC, 0xD2CC, 0xAD33, 0xCACC, 0xB533, 0xD533, 0xAACC,
  68. 0xCCB3, 0xB34C, 0xD34C, 0xACB3, 0xCB4C, 0xB4B3, 0xD4B3, 0xAB4C,
  69. 0xCD4C, 0xB2B3, 0xD2B3, 0xAD4C, 0xCAB3, 0xB54C, 0xD54C, 0xAAB3,
  70. 0xCCD3, 0xB32C, 0xD32C, 0xACD3, 0xCB2C, 0xB4D3, 0xD4D3, 0xAB2C,
  71. 0xCD2C, 0xB2D3, 0xD2D3, 0xAD2C, 0xCAD3, 0xB52C, 0xD52C, 0xAAD3,
  72. 0xCCAC, 0xB353, 0xD353, 0xACAC, 0xCB53, 0xB4AC, 0xD4AC, 0xAB53,
  73. 0xCD53, 0xB2AC, 0xD2AC, 0xAD53, 0xCAAC, 0xB553, 0xD553, 0xAAAC,
  74. 0xCCCB, 0xB334, 0xD334, 0xACCB, 0xCB34, 0xB4CB, 0xD4CB, 0xAB34,
  75. 0xCD34, 0xB2CB, 0xD2CB, 0xAD34, 0xCACB, 0xB534, 0xD534, 0xAACB,
  76. 0xCCB4, 0xB34B, 0xD34B, 0xACB4, 0xCB4B, 0xB4B4, 0xD4B4, 0xAB4B,
  77. 0xCD4B, 0xB2B4, 0xD2B4, 0xAD4B, 0xCAB4, 0xB54B, 0xD54B, 0xAAB4,
  78. 0xCCD4, 0xB32B, 0xD32B, 0xACD4, 0xCB2B, 0xB4D4, 0xD4D4, 0xAB2B,
  79. 0xCD2B, 0xB2D4, 0xD2D4, 0xAD2B, 0xCAD4, 0xB52B, 0xD52B, 0xAAD4,
  80. 0xCCAB, 0xB354, 0xD354, 0xACAB, 0xCB54, 0xB4AB, 0xD4AB, 0xAB54,
  81. 0xCD54, 0xB2AB, 0xD2AB, 0xAD54, 0xCAAB, 0xB554, 0xD554, 0xAAAB,
  82. 0xCCCD, 0xB332, 0xD332, 0xACCD, 0xCB32, 0xB4CD, 0xD4CD, 0xAB32,
  83. 0xCD32, 0xB2CD, 0xD2CD, 0xAD32, 0xCACD, 0xB532, 0xD532, 0xAACD,
  84. 0xCCB2, 0xB34D, 0xD34D, 0xACB2, 0xCB4D, 0xB4B2, 0xD4B2, 0xAB4D,
  85. 0xCD4D, 0xB2B2, 0xD2B2, 0xAD4D, 0xCAB2, 0xB54D, 0xD54D, 0xAAB2,
  86. 0xCCD2, 0xB32D, 0xD32D, 0xACD2, 0xCB2D, 0xB4D2, 0xD4D2, 0xAB2D,
  87. 0xCD2D, 0xB2D2, 0xD2D2, 0xAD2D, 0xCAD2, 0xB52D, 0xD52D, 0xAAD2,
  88. 0xCCAD, 0xB352, 0xD352, 0xACAD, 0xCB52, 0xB4AD, 0xD4AD, 0xAB52,
  89. 0xCD52, 0xB2AD, 0xD2AD, 0xAD52, 0xCAAD, 0xB552, 0xD552, 0xAAAD,
  90. 0xCCCA, 0xB335, 0xD335, 0xACCA, 0xCB35, 0xB4CA, 0xD4CA, 0xAB35,
  91. 0xCD35, 0xB2CA, 0xD2CA, 0xAD35, 0xCACA, 0xB535, 0xD535, 0xAACA,
  92. 0xCCB5, 0xB34A, 0xD34A, 0xACB5, 0xCB4A, 0xB4B5, 0xD4B5, 0xAB4A,
  93. 0xCD4A, 0xB2B5, 0xD2B5, 0xAD4A, 0xCAB5, 0xB54A, 0xD54A, 0xAAB5,
  94. 0xCCD5, 0xB32A, 0xD32A, 0xACD5, 0xCB2A, 0xB4D5, 0xD4D5, 0xAB2A,
  95. 0xCD2A, 0xB2D5, 0xD2D5, 0xAD2A, 0xCAD5, 0xB52A, 0xD52A, 0xAAD5,
  96. 0xCCAA, 0xB355, 0xD355, 0xACAA, 0xCB55, 0xB4AA, 0xD4AA, 0xAB55,
  97. 0xCD55, 0xB2AA, 0xD2AA, 0xAD55, 0xCAAA, 0xB555, 0xD555, 0xAAAA };
  98. /*
  99. * Biphase frame headers for SP/DIF, including the special bit framing
  100. * errors used to detect (sub)frame start conditions. See above table
  101. * for details.
  102. */
  103. const uint16_t x_preamble = 0xE2CC;
  104. const uint16_t y_preamble = 0xE4CC;
  105. const uint16_t z_preamble = 0xE8CC;
  106. // DMA configuration info
  107. static dma_channel_config snd_dma_a_cfg;
  108. static dma_channel_config snd_dma_b_cfg;
  109. // some chonky buffers to store audio samples
  110. static uint8_t sample_buf_a[AUDIO_BUFFER_SIZE];
  111. static uint8_t sample_buf_b[AUDIO_BUFFER_SIZE];
  112. // tracking for the state of the above buffers
  113. enum bufstate { STALE, FILLING, READY };
  114. static volatile bufstate sbufst_a = STALE;
  115. static volatile bufstate sbufst_b = STALE;
  116. enum bufselect { A, B };
  117. static bufselect sbufsel = A;
  118. static uint16_t sbufpos = 0;
  119. static uint8_t sbufswap = 0;
  120. // buffers for storing biphase patterns
  121. #define SAMPLE_CHUNK_SIZE 1024 // ~5.8ms
  122. #define WIRE_BUFFER_SIZE (SAMPLE_CHUNK_SIZE * 2)
  123. static uint16_t wire_buf_a[WIRE_BUFFER_SIZE];
  124. static uint16_t wire_buf_b[WIRE_BUFFER_SIZE];
  125. // tracking for audio playback
  126. static bool audio_active = false;
  127. static volatile bool audio_stopping = false;
  128. static FsFile audio_file;
  129. static uint32_t fleft;
  130. // trackers for the below function call
  131. static uint16_t sfcnt = 0; // sub-frame count; 2 per frame, 192 frames/block
  132. static uint8_t invert = 0; // biphase encode help: set if last wire bit was '1'
  133. /*
  134. * Translates 16-bit stereo sound samples to biphase wire patterns for the
  135. * SPI peripheral. Produces 8 patterns (128 bits, or 1 SP/DIF frame) per pair
  136. * of input samples. Provided length is the total number of sample bytes present,
  137. * _twice_ the number of samples (little-endian order assumed)
  138. *
  139. * This function operates with side-effects and is not safe to call from both
  140. * cores. It must also be called in the same order data is intended to be
  141. * output.
  142. */
  143. static void snd_encode(uint8_t* samples, uint16_t* wire_patterns, uint16_t len, uint8_t swap) {
  144. uint16_t widx = 0;
  145. for (uint16_t i = 0; i < len; i += 2) {
  146. uint32_t sample = 0;
  147. uint8_t parity = 0;
  148. if (samples != NULL) {
  149. if (swap) {
  150. sample = samples[i + 1] + (samples[i] << 8);
  151. } else {
  152. sample = samples[i] + (samples[i + 1] << 8);
  153. }
  154. // determine parity, simplified to one lookup via an XOR
  155. parity = (sample >> 8) ^ sample;
  156. parity = snd_parity[parity];
  157. /*
  158. * Shift sample into the correct bit positions of the sub-frame. This
  159. * would normally be << 12, but with my DACs I've had persistent issues
  160. * with signal clipping when sending data in the highest bit position.
  161. */
  162. sample = sample << 11;
  163. if (sample & 0x04000000) {
  164. // handle two's complement
  165. sample |= 0x08000000;
  166. parity++;
  167. }
  168. }
  169. // if needed, establish even parity with P bit
  170. if (parity % 2) sample |= 0x80000000;
  171. // translate sample into biphase encoding
  172. // first is low 8 bits: preamble and 4 least-significant bits of
  173. // 24-bit audio, pre-encoded as all '0' due to 16-bit samples
  174. uint16_t wp;
  175. if (sfcnt == 0) {
  176. wp = z_preamble; // left channel, block start
  177. } else if (sfcnt % 2) {
  178. wp = y_preamble; // right channel
  179. } else {
  180. wp = x_preamble; // left channel, not block start
  181. }
  182. if (invert) wp = ~wp;
  183. invert = wp & 1;
  184. wire_patterns[widx++] = wp;
  185. // next 8 bits (only high 4 have data)
  186. wp = biphase[(uint8_t) (sample >> 8)];
  187. if (invert) wp = ~wp;
  188. invert = wp & 1;
  189. wire_patterns[widx++] = wp;
  190. // next 8 again, all audio data
  191. wp = biphase[(uint8_t) (sample >> 16)];
  192. if (invert) wp = ~wp;
  193. invert = wp & 1;
  194. wire_patterns[widx++] = wp;
  195. // final 8, low 4 audio data and high 4 control bits
  196. wp = biphase[(uint8_t) (sample >> 24)];
  197. if (invert) wp = ~wp;
  198. invert = wp & 1;
  199. wire_patterns[widx++] = wp;
  200. // increment subframe counter for next pass
  201. sfcnt++;
  202. if (sfcnt == 384) sfcnt = 0; // if true, block complete
  203. }
  204. }
  205. // functions for passing to Core1
  206. static void snd_process_a() {
  207. if (sbufsel == A) {
  208. if (sbufst_a == READY) {
  209. snd_encode(sample_buf_a + sbufpos, wire_buf_a, SAMPLE_CHUNK_SIZE, sbufswap);
  210. sbufpos += SAMPLE_CHUNK_SIZE;
  211. if (sbufpos >= AUDIO_BUFFER_SIZE) {
  212. sbufsel = B;
  213. sbufpos = 0;
  214. sbufst_a = STALE;
  215. }
  216. } else {
  217. snd_encode(NULL, wire_buf_a, SAMPLE_CHUNK_SIZE, sbufswap);
  218. }
  219. } else {
  220. if (sbufst_b == READY) {
  221. snd_encode(sample_buf_b + sbufpos, wire_buf_a, SAMPLE_CHUNK_SIZE, sbufswap);
  222. sbufpos += SAMPLE_CHUNK_SIZE;
  223. if (sbufpos >= AUDIO_BUFFER_SIZE) {
  224. sbufsel = A;
  225. sbufpos = 0;
  226. sbufst_b = STALE;
  227. }
  228. } else {
  229. snd_encode(NULL, wire_buf_a, SAMPLE_CHUNK_SIZE, sbufswap);
  230. }
  231. }
  232. }
  233. static void snd_process_b() {
  234. // clone of above for the other wire buffer
  235. if (sbufsel == A) {
  236. if (sbufst_a == READY) {
  237. snd_encode(sample_buf_a + sbufpos, wire_buf_b, SAMPLE_CHUNK_SIZE, sbufswap);
  238. sbufpos += SAMPLE_CHUNK_SIZE;
  239. if (sbufpos >= AUDIO_BUFFER_SIZE) {
  240. sbufsel = B;
  241. sbufpos = 0;
  242. sbufst_a = STALE;
  243. }
  244. } else {
  245. snd_encode(NULL, wire_buf_b, SAMPLE_CHUNK_SIZE, sbufswap);
  246. }
  247. } else {
  248. if (sbufst_b == READY) {
  249. snd_encode(sample_buf_b + sbufpos, wire_buf_b, SAMPLE_CHUNK_SIZE, sbufswap);
  250. sbufpos += SAMPLE_CHUNK_SIZE;
  251. if (sbufpos >= AUDIO_BUFFER_SIZE) {
  252. sbufsel = A;
  253. sbufpos = 0;
  254. sbufst_b = STALE;
  255. }
  256. } else {
  257. snd_encode(NULL, wire_buf_b, SAMPLE_CHUNK_SIZE, sbufswap);
  258. }
  259. }
  260. }
  261. // Allows execution on Core1 via function pointers. Each function can take
  262. // no parameters and should return nothing, operating via side-effects only.
  263. static void core1_handler() {
  264. while (1) {
  265. void (*function)() = (void (*)()) multicore_fifo_pop_blocking();
  266. (*function)();
  267. }
  268. }
  269. /* ------------------------------------------------------------------------ */
  270. /* ---------- VISIBLE FUNCTIONS ------------------------------------------- */
  271. /* ------------------------------------------------------------------------ */
  272. void audio_dma_irq() {
  273. if (dma_hw->intr & (1 << SOUND_DMA_CHA)) {
  274. dma_hw->ints0 = (1 << SOUND_DMA_CHA);
  275. multicore_fifo_push_blocking((uintptr_t) &snd_process_a);
  276. if (audio_stopping) {
  277. channel_config_set_chain_to(&snd_dma_a_cfg, SOUND_DMA_CHA);
  278. }
  279. dma_channel_configure(SOUND_DMA_CHA,
  280. &snd_dma_a_cfg,
  281. &(spi_get_hw(AUDIO_SPI)->dr),
  282. &wire_buf_a,
  283. WIRE_BUFFER_SIZE,
  284. false);
  285. } else if (dma_hw->intr & (1 << SOUND_DMA_CHB)) {
  286. dma_hw->ints0 = (1 << SOUND_DMA_CHB);
  287. multicore_fifo_push_blocking((uintptr_t) &snd_process_b);
  288. if (audio_stopping) {
  289. channel_config_set_chain_to(&snd_dma_b_cfg, SOUND_DMA_CHB);
  290. }
  291. dma_channel_configure(SOUND_DMA_CHB,
  292. &snd_dma_b_cfg,
  293. &(spi_get_hw(AUDIO_SPI)->dr),
  294. &wire_buf_b,
  295. WIRE_BUFFER_SIZE,
  296. false);
  297. }
  298. }
  299. bool audio_is_active() {
  300. return audio_active;
  301. }
  302. void audio_setup() {
  303. // setup SPI to blast SP/DIF data over the TX pin
  304. spi_set_baudrate(AUDIO_SPI, 5644800); // will be slightly wrong, ~0.03% slow
  305. hw_write_masked(&spi_get_hw(AUDIO_SPI)->cr0,
  306. 0x1F, // TI mode with 16 bits
  307. SPI_SSPCR0_DSS_BITS | SPI_SSPCR0_FRF_BITS);
  308. spi_get_hw(AUDIO_SPI)->dmacr = SPI_SSPDMACR_TXDMAE_BITS;
  309. hw_set_bits(&spi_get_hw(AUDIO_SPI)->cr1, SPI_SSPCR1_SSE_BITS);
  310. dma_channel_claim(SOUND_DMA_CHA);
  311. dma_channel_claim(SOUND_DMA_CHB);
  312. log("Starting Core1 for audio");
  313. multicore_launch_core1(core1_handler);
  314. }
  315. void audio_poll() {
  316. if (!audio_active) return;
  317. if (fleft == 0 && sbufst_a == STALE && sbufst_b == STALE) {
  318. // out of data and ready to stop
  319. audio_stop();
  320. return;
  321. } else if (fleft == 0) {
  322. // out of data to read but still working on remainder
  323. return;
  324. }
  325. // are new audio samples needed from the memory card?
  326. uint8_t* audiobuf;
  327. if (sbufst_a == STALE) {
  328. sbufst_a = FILLING;
  329. audiobuf = sample_buf_a;
  330. } else if (sbufst_b == STALE) {
  331. sbufst_b = FILLING;
  332. audiobuf = sample_buf_b;
  333. } else {
  334. // no data needed this time
  335. return;
  336. }
  337. platform_set_sd_callback(NULL, NULL);
  338. uint16_t toRead = AUDIO_BUFFER_SIZE;
  339. if (fleft < toRead) toRead = fleft;
  340. if (audio_file.read(audiobuf, toRead) != toRead) {
  341. log("Audio sample data underrun");
  342. }
  343. fleft -= toRead;
  344. if (sbufst_a == FILLING) {
  345. sbufst_a = READY;
  346. } else if (sbufst_b == FILLING) {
  347. sbufst_b = READY;
  348. }
  349. }
  350. bool audio_play(const char* file, uint64_t start, uint64_t end, bool swap) {
  351. // stop any existing playback first
  352. if (audio_active) audio_stop();
  353. // debuglog("Request to play ('", file, "':", start, ":", end, ")");
  354. // verify audio file is present and inputs are (somewhat) sane
  355. if (start >= end) {
  356. log("Invalid range for audio (", start, ":", end, ")");
  357. return false;
  358. }
  359. platform_set_sd_callback(NULL, NULL);
  360. audio_file = SD.open(file, O_RDONLY);
  361. if (!audio_file.isOpen()) {
  362. log("Unable to open file for audio playback: ", file);
  363. return false;
  364. }
  365. uint64_t len = audio_file.size();
  366. if (start > len || end > len) {
  367. log("File '", file, "' playback request (",
  368. start, ":", end, ":", len, ") outside bounds");
  369. audio_file.close();
  370. return false;
  371. }
  372. fleft = end - start;
  373. if (fleft <= 2 * AUDIO_BUFFER_SIZE) {
  374. log("File '", file, "' playback request (",
  375. start, ":", end, ") too short");
  376. audio_file.close();
  377. return false;
  378. }
  379. // read in initial sample buffers
  380. if (!audio_file.seek(start)) {
  381. log("Sample file (", file, ") failed start seek to ", start);
  382. audio_file.close();
  383. return false;
  384. }
  385. if (audio_file.read(sample_buf_a, AUDIO_BUFFER_SIZE) != AUDIO_BUFFER_SIZE) {
  386. log("File '", file, "' playback start returned fewer bytes than allowed");
  387. audio_file.close();
  388. return false;
  389. }
  390. if (audio_file.read(sample_buf_b, AUDIO_BUFFER_SIZE) != AUDIO_BUFFER_SIZE) {
  391. log("File '", file, "' playback start returned fewer bytes than allowed");
  392. audio_file.close();
  393. return false;
  394. }
  395. // prepare initial tracking state
  396. fleft -= AUDIO_BUFFER_SIZE * 2;
  397. sbufsel = A;
  398. sbufpos = 0;
  399. sbufswap = swap;
  400. sbufst_a = READY;
  401. sbufst_b = READY;
  402. // prepare the wire buffers
  403. for (uint16_t i = 0; i < WIRE_BUFFER_SIZE; i++) {
  404. wire_buf_a[i] = 0;
  405. wire_buf_b[i] = 0;
  406. }
  407. sfcnt = 0;
  408. invert = 0;
  409. // setup the two DMA units to hand-off to each other
  410. // to maintain a stable bitstream these need to run without interruption
  411. snd_dma_a_cfg = dma_channel_get_default_config(SOUND_DMA_CHA);
  412. channel_config_set_transfer_data_size(&snd_dma_a_cfg, DMA_SIZE_16);
  413. channel_config_set_dreq(&snd_dma_a_cfg, spi_get_dreq(AUDIO_SPI, true));
  414. channel_config_set_read_increment(&snd_dma_a_cfg, true);
  415. channel_config_set_chain_to(&snd_dma_a_cfg, SOUND_DMA_CHB);
  416. // version of pico-sdk lacks channel_config_set_high_priority()
  417. snd_dma_a_cfg.ctrl |= DMA_CH0_CTRL_TRIG_HIGH_PRIORITY_BITS;
  418. dma_channel_configure(SOUND_DMA_CHA, &snd_dma_a_cfg, &(spi_get_hw(AUDIO_SPI)->dr),
  419. &wire_buf_a, WIRE_BUFFER_SIZE, false);
  420. dma_channel_set_irq0_enabled(SOUND_DMA_CHA, true);
  421. snd_dma_b_cfg = dma_channel_get_default_config(SOUND_DMA_CHB);
  422. channel_config_set_transfer_data_size(&snd_dma_b_cfg, DMA_SIZE_16);
  423. channel_config_set_dreq(&snd_dma_b_cfg, spi_get_dreq(AUDIO_SPI, true));
  424. channel_config_set_read_increment(&snd_dma_b_cfg, true);
  425. channel_config_set_chain_to(&snd_dma_b_cfg, SOUND_DMA_CHA);
  426. snd_dma_b_cfg.ctrl |= DMA_CH0_CTRL_TRIG_HIGH_PRIORITY_BITS;
  427. dma_channel_configure(SOUND_DMA_CHB, &snd_dma_b_cfg, &(spi_get_hw(AUDIO_SPI)->dr),
  428. &wire_buf_b, WIRE_BUFFER_SIZE, false);
  429. dma_channel_set_irq0_enabled(SOUND_DMA_CHB, true);
  430. // ready to go
  431. dma_channel_start(SOUND_DMA_CHA);
  432. audio_active = true;
  433. return true;
  434. }
  435. void audio_stop() {
  436. if (!audio_active) return;
  437. // to help mute external hardware, send a bunch of '0' samples prior to
  438. // halting the datastream; easiest way to do this is invalidating the
  439. // sample buffers, same as if there was a sample data underrun
  440. sbufst_a = STALE;
  441. sbufst_b = STALE;
  442. // then indicate that the streams should no longer chain to one another
  443. // and wait for them to shut down naturally
  444. audio_stopping = true;
  445. while (dma_channel_is_busy(SOUND_DMA_CHA)) tight_loop_contents();
  446. while (dma_channel_is_busy(SOUND_DMA_CHB)) tight_loop_contents();
  447. while (spi_is_busy(AUDIO_SPI)) tight_loop_contents();
  448. audio_stopping = false;
  449. // idle the subsystem
  450. if (audio_file.isOpen()) {
  451. audio_file.close();
  452. }
  453. audio_active = false;
  454. }
  455. #ifdef __cplusplus
  456. }
  457. #endif
  458. #endif // ENABLE_AUDIO_OUTPUT