audio.cpp 22 KB

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