audio_i2s.cpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764
  1. /**
  2. * Copyright (C) 2023 saybur
  3. * Copyright (C) 2024-2025 Rabbit Hole Computing™
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program.  If not, see <https://www.gnu.org/licenses/>.
  17. **/
  18. #ifdef ENABLE_AUDIO_OUTPUT_I2S
  19. #include <SdFat.h>
  20. #include <stdbool.h>
  21. #include <hardware/dma.h>
  22. #include <hardware/irq.h>
  23. #include <hardware/pio.h>
  24. #include <pico/multicore.h>
  25. #include "audio_i2s.h"
  26. #include <CUEParser.h>
  27. #include "timings_RP2MCU.h"
  28. #include "ZuluSCSI_audio.h"
  29. // #include "ZuluIDE_config.h"
  30. #include "ZuluSCSI_log.h"
  31. #include "ZuluSCSI_platform.h"
  32. // #include "ide_imagefile.h"
  33. // #include "ide_atapi.h"
  34. #include <ZuluI2S.h>
  35. extern SdFs SD;
  36. I2S i2s;
  37. static FsFile audio_parent;
  38. static FsFile audio_file;
  39. static FsFile *cuesheet_file;
  40. static CUEParser * g_cue_parser = nullptr;
  41. static char g_cuesheet[4096];
  42. // True is using the same filenames for the bin/cue, false if using a directory with multiple bin/wav files
  43. static bool single_bin_file = false;
  44. // DMA configuration info
  45. static dma_channel_config snd_dma_a_cfg;
  46. static dma_channel_config snd_dma_b_cfg;
  47. // some chonky buffers to store audio samples,
  48. // output and sample buffers are the same memory
  49. #define AUDIO_OUT_BUFFER_SIZE (AUDIO_BUFFER_SIZE / 4)
  50. static uint32_t out_len_a = AUDIO_OUT_BUFFER_SIZE;
  51. static uint32_t out_len_b = AUDIO_OUT_BUFFER_SIZE;
  52. static uint32_t * out_len = &out_len_a;
  53. static uint32_t output_buf_a[AUDIO_OUT_BUFFER_SIZE];
  54. static uint32_t output_buf_b[AUDIO_OUT_BUFFER_SIZE];
  55. static uint8_t *sample_buf_a = (uint8_t*) output_buf_a;
  56. static uint8_t *sample_buf_b = (uint8_t*) output_buf_b;
  57. // tracking for the state of the above buffers
  58. enum bufstate { STALE, FILLING, PROCESSING, READY };
  59. static volatile bufstate sbufst_a = STALE;
  60. static volatile bufstate sbufst_b = STALE;
  61. enum bufselect { A, B };
  62. static bufselect sbufsel = A;
  63. // tracking for audio playback
  64. static uint8_t audio_owner; // SCSI ID or 0xFF when idle
  65. static bool audio_idle = true;
  66. static bool audio_playing = false;
  67. static volatile bool audio_paused = false;
  68. static uint64_t fpos;
  69. static uint32_t fleft;
  70. static uint64_t gap_length = 0;
  71. static bool last_track_reached = false;
  72. static bool within_gap = false;
  73. static uint32_t gap_read = 0;
  74. static CUETrackInfo current_track = {0};
  75. // historical playback status information
  76. static audio_status_code audio_last_status[8] = {ASC_NO_STATUS, ASC_NO_STATUS, ASC_NO_STATUS, ASC_NO_STATUS,
  77. ASC_NO_STATUS, ASC_NO_STATUS, ASC_NO_STATUS, ASC_NO_STATUS};
  78. // volume information for targets
  79. static volatile uint16_t volumes[8] = {
  80. DEFAULT_VOLUME_LEVEL_2CH, DEFAULT_VOLUME_LEVEL_2CH, DEFAULT_VOLUME_LEVEL_2CH, DEFAULT_VOLUME_LEVEL_2CH,
  81. DEFAULT_VOLUME_LEVEL_2CH, DEFAULT_VOLUME_LEVEL_2CH, DEFAULT_VOLUME_LEVEL_2CH, DEFAULT_VOLUME_LEVEL_2CH
  82. };
  83. static volatile uint16_t channel[8] = {
  84. AUDIO_CHANNEL_ENABLE_MASK, AUDIO_CHANNEL_ENABLE_MASK, AUDIO_CHANNEL_ENABLE_MASK, AUDIO_CHANNEL_ENABLE_MASK,
  85. AUDIO_CHANNEL_ENABLE_MASK, AUDIO_CHANNEL_ENABLE_MASK, AUDIO_CHANNEL_ENABLE_MASK, AUDIO_CHANNEL_ENABLE_MASK
  86. };
  87. // mechanism for cleanly stopping DMA units
  88. static volatile bool audio_stopping = false;
  89. /*
  90. * I2S format is directly compatible to CD 16-bit audio with left and right channels
  91. * The only encoding needed is adjusting the volume and muting if one of the channels
  92. * is disabled.
  93. */
  94. static void snd_encode(int16_t* samples, int16_t* output_buf, uint16_t len) {
  95. uint8_t vol_r = 0, vol_l = 0;
  96. vol_l = (uint8_t)volumes[audio_owner];
  97. vol_r = (uint8_t)(volumes[audio_owner] >> 8);
  98. uint16_t chn = channel[audio_owner] & AUDIO_CHANNEL_ENABLE_MASK;
  99. if (!(chn >> 8)) vol_r = 0; // right
  100. if (!(chn & 0xFF)) vol_l = 0; // left
  101. int16_t temp = 0;
  102. for (uint16_t i = 0; i < len; i++ )
  103. {
  104. if (samples == nullptr)
  105. output_buf[i] = 0;
  106. else
  107. {
  108. if (i % 2 == 0)
  109. {
  110. temp = output_buf[i+1];
  111. output_buf[i+1] = (int16_t)(((int64_t)samples[i]) * (vol_l) * g_scsi_settings.getSystem()->maxVolume / 25500) ;
  112. }
  113. else
  114. {
  115. output_buf[i-1] = (int16_t)(((int64_t)temp) * (vol_r) * g_scsi_settings.getSystem()->maxVolume / 25500);
  116. }
  117. }
  118. }
  119. }
  120. // functions for passing to Core1
  121. static void snd_process_a() {
  122. snd_encode((int16_t *)(sample_buf_a), (int16_t*)(output_buf_a), AUDIO_BUFFER_SIZE/2);
  123. sbufst_a = READY;
  124. }
  125. static void snd_process_b() {
  126. snd_encode((int16_t *)sample_buf_b, (int16_t*)(output_buf_b), AUDIO_BUFFER_SIZE/2);
  127. sbufst_b = READY;
  128. }
  129. /**********************************************************************************************
  130. * Sets up playback via side effect for last_track_reached, within_gap, fpos and fleft, gap_read
  131. * \param start - start of playback in lba
  132. * \param length - length of playback in lba
  133. * \param continued - true if updating values while audio is being played
  134. * - false if setting up for the first time
  135. **********************************************************************************************/
  136. static bool setup_playback(uint8_t id, uint32_t start, uint32_t length, bool continued)
  137. {
  138. static uint32_t last_length = 0;
  139. static uint32_t last_start = 0;
  140. static uint8_t last_track_number = 0;
  141. if (!continued)
  142. {
  143. last_start = start;
  144. last_length = length;
  145. last_track_number = 0;
  146. }
  147. // read in the first track and report errors
  148. const CUETrackInfo *find_track_info;
  149. // Init globals
  150. within_gap = false;
  151. last_track_reached = false;
  152. gap_length = 0;
  153. gap_read = 0;
  154. uint64_t file_size = 0;
  155. CUETrackInfo track_info = {0};
  156. uint32_t start_of_next_track = 0;
  157. int file_index = -1;
  158. g_cue_parser->restart();
  159. while ((find_track_info = g_cue_parser->next_track(file_size)) != nullptr )
  160. {
  161. if (!single_bin_file)
  162. {
  163. // opening the file for getting file size
  164. if (find_track_info->file_index != file_index)
  165. {
  166. if (!(audio_parent.isDir() && audio_file.open(&audio_parent, find_track_info->filename, O_RDONLY)))
  167. {
  168. dbgmsg("------ Audio playback - could not open the next track's bin file: ", find_track_info->filename);
  169. audio_file.close();
  170. return false;
  171. }
  172. file_index = find_track_info->file_index;
  173. }
  174. }
  175. file_size = audio_file.size();
  176. if (continued)
  177. {
  178. // looking up the next track
  179. if (find_track_info->track_number < last_track_number + 1)
  180. continue;
  181. if (find_track_info->track_number == last_track_number + 1)
  182. {
  183. // set start to the new track because the last track has finished
  184. start = find_track_info->track_start;
  185. }
  186. }
  187. if (start < find_track_info->track_start)
  188. {
  189. // start began in the last track, stop looping
  190. start_of_next_track = find_track_info->track_start;
  191. break;
  192. }
  193. track_info = *find_track_info;
  194. }
  195. if (!single_bin_file)
  196. {
  197. if (!(audio_parent.isDir() && audio_file.open(&audio_parent, track_info.filename, O_RDONLY)))
  198. {
  199. dbgmsg("------ Audio playback - could not open the current track's bin file: ", track_info.filename);
  200. audio_file.close();
  201. return false;
  202. }
  203. }
  204. if (find_track_info == nullptr)
  205. {
  206. // if the loop completed without breaking
  207. last_track_reached = true;
  208. if (track_info.track_number == 0)
  209. {
  210. dbgmsg("------ Audio continued playback could not find specified track");
  211. return false;
  212. }
  213. }
  214. // test if the current or new audio file is open or can be opened
  215. if (single_bin_file && !audio_file.isOpen())
  216. {
  217. dbgmsg("------ Audio playback - CD's bin file is not open");
  218. return false;
  219. }
  220. if (track_info.track_mode != CUETrack_AUDIO)
  221. {
  222. dbgmsg("------ Audio playback - track not CD Audio");
  223. return false;
  224. }
  225. if (continued)
  226. {
  227. // adjust length for new track
  228. length = last_length - (start - last_start);
  229. last_length = length;
  230. last_start = start;
  231. }
  232. last_track_number = track_info.track_number;
  233. // find the offset within the current audio file
  234. uint64_t offset = track_info.file_offset;
  235. if (start >= track_info.data_start)
  236. {
  237. // add to the offset the current playback position
  238. offset += (start - track_info.data_start) * (uint64_t)track_info.sector_length;
  239. }
  240. else if (track_info.unstored_pregap_length != 0 && start >= track_info.data_start - track_info.unstored_pregap_length)
  241. {
  242. // Start is within the pregap position, offset is not increased due to no file data is being played
  243. gap_length = (start - track_info.data_start) *(uint64_t) track_info.sector_length;
  244. // offset += 0;
  245. within_gap = true;
  246. gap_read = 0;
  247. }
  248. else
  249. {
  250. // Get data from stored pregap (INDEX 0), which is in the file before trackinfo.file_offset.
  251. uint32_t seek_back = (track_info.data_start - start) * track_info.sector_length;
  252. if (seek_back > offset)
  253. {
  254. logmsg("WARNING: Host attempted CD read at sector ", start, "+", length,
  255. " pregap request ", (int)seek_back, " exceeded available ", (int)offset, " for track ", track_info.track_number,
  256. " (possible .cue file issue)");
  257. offset = 0;
  258. return false;
  259. }
  260. else
  261. {
  262. offset -= seek_back;
  263. }
  264. }
  265. if (start_of_next_track != 0)
  266. {
  267. // There is a next track
  268. if (start + length < start_of_next_track)
  269. {
  270. // playback ends before the next track
  271. if (within_gap)
  272. // adjust length unplayed file data within gap
  273. fleft = (length - track_info.unstored_pregap_length) * (uint64_t)track_info.sector_length;
  274. else
  275. fleft = length * (uint64_t)track_info.sector_length;
  276. last_track_reached = true;
  277. }
  278. else
  279. {
  280. // playback continues after this track
  281. if (within_gap)
  282. fleft = (start_of_next_track - track_info.data_start) * (uint64_t)track_info.sector_length;
  283. else
  284. fleft = (start_of_next_track - start) * (uint64_t)track_info.sector_length;
  285. last_track_reached = false;
  286. }
  287. }
  288. else
  289. {
  290. // if playback is with current bin file and there are no more tracks
  291. volatile uint64_t size_of_playback;
  292. volatile uint32_t start_lba = start;
  293. size_of_playback = (start_lba + length - track_info.data_start) * (uint64_t)track_info.sector_length ;
  294. volatile uint64_t last_track_byte_length = audio_file.size() - track_info.file_offset;
  295. if (size_of_playback <= last_track_byte_length)
  296. {
  297. if (within_gap)
  298. fleft = (length - (track_info.data_start - start)) * track_info.sector_length;
  299. else
  300. fleft = length * track_info.sector_length;
  301. last_track_reached = true;
  302. }
  303. else
  304. {
  305. dbgmsg("------ Audio playback - length ", (int) length ,", beyond the last file in cue ");
  306. return false;
  307. }
  308. }
  309. current_track = track_info;
  310. fpos = offset;
  311. return true;
  312. }
  313. // Allows execution on Core1 via function pointers. Each function can take
  314. // no parameters and should return nothing, operating via side-effects only.
  315. static void core1_handler() {
  316. while (1) {
  317. void (*function)() = (void (*)()) multicore_fifo_pop_blocking();
  318. (*function)();
  319. }
  320. }
  321. /* ------------------------------------------------------------------------ */
  322. /* ---------- VISIBLE FUNCTIONS ------------------------------------------- */
  323. /* ------------------------------------------------------------------------ */
  324. extern "C"
  325. {
  326. void audio_dma_irq() {
  327. // Using dma irq raw register access, because the 2.1.0 pico-sdk function seem to cause issues
  328. if (dma_hw->intr & (1 << SOUND_DMA_CHA)) {
  329. dma_hw->ints2 = (1 << SOUND_DMA_CHA);
  330. sbufst_a = STALE;
  331. if (audio_stopping) {
  332. channel_config_set_chain_to(&snd_dma_a_cfg, SOUND_DMA_CHA);
  333. }
  334. dma_channel_configure(SOUND_DMA_CHA,
  335. &snd_dma_a_cfg,
  336. i2s.getPioFIFOAddr(),
  337. output_buf_a,
  338. out_len_a / 4,
  339. false);
  340. } else if (dma_hw->intr & (1 << SOUND_DMA_CHB)) {
  341. dma_hw->ints2 = (1 << SOUND_DMA_CHB);
  342. sbufst_b = STALE;
  343. if (audio_stopping) {
  344. channel_config_set_chain_to(&snd_dma_b_cfg, SOUND_DMA_CHB);
  345. }
  346. dma_channel_configure(SOUND_DMA_CHB,
  347. &snd_dma_b_cfg,
  348. i2s.getPioFIFOAddr(),
  349. output_buf_b,
  350. out_len_b / 4,
  351. false);
  352. }
  353. }
  354. }
  355. bool audio_is_active() {
  356. return !audio_idle;
  357. }
  358. bool audio_is_playing(uint8_t id) {
  359. // return audio_playing;
  360. return audio_owner == (id & 7) && audio_playing;
  361. }
  362. void audio_setup() {
  363. // setup GPIOs
  364. pio_gpio_init(I2S_PIO_HW, GPIO_I2S_BCLK);
  365. pio_gpio_init(I2S_PIO_HW, GPIO_I2S_WS);
  366. pio_gpio_init(I2S_PIO_HW, GPIO_I2S_DOUT);
  367. // setup Arduino-Pico I2S library
  368. i2s.setBCLK(GPIO_I2S_BCLK);
  369. i2s.setDATA(GPIO_I2S_DOUT);
  370. i2s.setBitsPerSample(16);
  371. i2s.setDivider(g_zuluscsi_timings->audio.clk_div_pio, 0);
  372. i2s.begin(I2S_PIO_HW, I2S_PIO_SM);
  373. dma_channel_claim(SOUND_DMA_CHA);
  374. dma_channel_claim(SOUND_DMA_CHB);
  375. irq_set_exclusive_handler(I2S_DMA_IRQ_NUM, audio_dma_irq);
  376. irq_set_enabled(I2S_DMA_IRQ_NUM, true);
  377. irq_clear(I2S_DMA_IRQ_NUM);
  378. logmsg("Starting Core1 for audio");
  379. multicore_launch_core1(core1_handler);
  380. }
  381. void audio_poll() {
  382. if (audio_idle) return;
  383. static bool set_pause_buf = true;
  384. if (audio_paused)
  385. {
  386. if (set_pause_buf)
  387. {
  388. memset(output_buf_a, 0, sizeof(output_buf_a));
  389. memset(output_buf_b, 0, sizeof(output_buf_b));
  390. }
  391. set_pause_buf = false;
  392. return;
  393. }
  394. set_pause_buf = true;
  395. if (last_track_reached && fleft == 0 && sbufst_a == STALE && sbufst_b == STALE) {
  396. // out of data and ready to stop
  397. audio_stop(audio_owner);
  398. return;
  399. } else if (last_track_reached && fleft == 0) {
  400. // out of data to read but still working on remainder
  401. return;
  402. } else if (!audio_file.isOpen()) {
  403. // closed elsewhere, maybe disk ejected?
  404. dbgmsg("------ Playback stop due to closed file");
  405. audio_stop(audio_owner);
  406. return;
  407. }
  408. if (fleft == 0)
  409. {
  410. if (!setup_playback(audio_owner, 0, 0, true))
  411. {
  412. dbgmsg("------ Playback stopped because of error loading next track");
  413. audio_stop(audio_owner);
  414. return;
  415. }
  416. }
  417. // are new audio samples needed from the memory card?
  418. uint8_t* audiobuf;
  419. if (sbufst_a == STALE) {
  420. sbufst_a = FILLING;
  421. audiobuf = sample_buf_a;
  422. out_len = &out_len_a;
  423. } else if (sbufst_b == STALE) {
  424. sbufst_b = FILLING;
  425. audiobuf = sample_buf_b;
  426. out_len = &out_len_b;
  427. } else {
  428. // no data needed this time
  429. return;
  430. }
  431. platform_set_sd_callback(NULL, NULL);
  432. uint16_t toRead = AUDIO_BUFFER_SIZE;
  433. uint16_t gap_to_read = AUDIO_BUFFER_SIZE;
  434. if (within_gap)
  435. {
  436. if (gap_length < gap_to_read) gap_to_read = gap_length;
  437. memset(audiobuf, 0, AUDIO_BUFFER_SIZE);
  438. gap_read += gap_to_read;
  439. *out_len = gap_to_read;
  440. if (gap_read >= gap_length)
  441. {
  442. within_gap = false;
  443. gap_read = 0;
  444. gap_length = 0;
  445. }
  446. }
  447. else
  448. {
  449. if (fleft < toRead) toRead = fleft;
  450. if (audio_file.position() != fpos) {
  451. // should be uncommon due to SCSI command restrictions on devices
  452. // playing audio; if this is showing up in logs a different approach
  453. // will be needed to avoid seek performance issues on FAT32 vols
  454. dbgmsg("------ Audio seek required");
  455. if (!audio_file.seek(fpos)) {
  456. logmsg("------ Audio error, unable to seek to ", fpos);
  457. }
  458. }
  459. if (audio_file.read(audiobuf, toRead) != toRead) {
  460. logmsg("------ Audio sample data read error");
  461. }
  462. *out_len = toRead;
  463. fpos += toRead;
  464. fleft -= toRead;
  465. }
  466. if (sbufst_a == FILLING) {
  467. sbufst_a = PROCESSING;
  468. multicore_fifo_push_blocking((uintptr_t) &snd_process_a);
  469. } else if (sbufst_b == FILLING) {
  470. sbufst_b = PROCESSING;
  471. multicore_fifo_push_blocking((uintptr_t) &snd_process_b);
  472. }
  473. }
  474. bool audio_play(uint8_t owner, image_config_t* img, uint32_t start, uint32_t length, bool swap) {
  475. // Per Annex C terminate playback immediately if already in progress on
  476. // the current target. Non-current targets may also get their audio
  477. // interrupted later due to hardware limitations
  478. // stop any existing playback first
  479. if (!audio_idle)
  480. audio_stop(audio_owner);
  481. if(!img->cuesheetfile && img->cuesheetfile.isOpen())
  482. {
  483. logmsg("Error attempting to play CD Audio with no cue/bin image(s)");
  484. return false;
  485. }
  486. if (img->bin_container.isOpen() && img->bin_container.isDir())
  487. {
  488. audio_parent.close();
  489. audio_file.close();
  490. audio_parent = img->bin_container;
  491. single_bin_file = false;
  492. }
  493. else if (img->bin_container.isOpen())
  494. {
  495. audio_parent.close();
  496. audio_file.close();
  497. audio_file = img->bin_container;
  498. single_bin_file = true;
  499. }
  500. else
  501. return false;
  502. if (&img->cuesheetfile != cuesheet_file)
  503. {
  504. cuesheet_file = &img->cuesheetfile;
  505. cuesheet_file->seek(0);
  506. cuesheet_file->read(g_cuesheet, sizeof(g_cuesheet));
  507. delete g_cue_parser;
  508. g_cue_parser = new CUEParser(g_cuesheet);
  509. }
  510. // dbgmsg("Request to play ('", file, "':", start, ":", end, ")");
  511. // verify audio file is present and inputs are (somewhat) sane
  512. platform_set_sd_callback(NULL, NULL);
  513. // truncate playback end to end of file
  514. // we will not consider this to be an error at the moment
  515. // \todo reimplement
  516. // if (end > len) {
  517. // dbgmsg("------ Truncate audio play request end ", end, " to file size ", len);
  518. // end = len;
  519. //
  520. audio_owner = owner;
  521. if(!setup_playback(owner, start, length, false))
  522. return false;
  523. if (length == 0)
  524. {
  525. audio_last_status[owner] = ASC_NO_STATUS;
  526. audio_paused = false;
  527. audio_playing = false;
  528. audio_idle = true;
  529. return true;
  530. }
  531. audio_last_status[owner] = ASC_PLAYING;
  532. audio_paused = false;
  533. audio_playing = true;
  534. audio_idle = false;
  535. // read in initial sample buffers
  536. if (within_gap)
  537. {
  538. sbufst_a = READY;
  539. sbufst_b = READY;
  540. memset(output_buf_a, 0, sizeof(output_buf_a));
  541. memset(output_buf_b, 0, sizeof(output_buf_b));
  542. }
  543. else
  544. {
  545. sbufst_a = STALE;
  546. sbufst_b = STALE;
  547. sbufsel = B;
  548. audio_poll();
  549. sbufsel = A;
  550. audio_poll();
  551. }
  552. // setup the two DMA units to hand-off to each other
  553. // to maintain a stable bitstream these need to run without interruption
  554. snd_dma_a_cfg = dma_channel_get_default_config(SOUND_DMA_CHA);
  555. channel_config_set_transfer_data_size(&snd_dma_a_cfg, DMA_SIZE_32);
  556. channel_config_set_dreq(&snd_dma_a_cfg, i2s.getPioDreq());
  557. channel_config_set_read_increment(&snd_dma_a_cfg, true);
  558. channel_config_set_chain_to(&snd_dma_a_cfg, SOUND_DMA_CHB);
  559. channel_config_set_high_priority(&snd_dma_a_cfg, true);
  560. dma_channel_configure(SOUND_DMA_CHA, &snd_dma_a_cfg, i2s.getPioFIFOAddr(),
  561. output_buf_a, AUDIO_OUT_BUFFER_SIZE, false);
  562. hw_set_bits(&dma_hw->inte2, 1 << SOUND_DMA_CHA );
  563. // dma_irqn_set_channel_enabled(I2S_DMA_IRQ_NUM, SOUND_DMA_CHA, true);
  564. // dma_channel_set_irq0_enabled(SOUND_DMA_CHA, true);
  565. snd_dma_b_cfg = dma_channel_get_default_config(SOUND_DMA_CHB);
  566. channel_config_set_transfer_data_size(&snd_dma_b_cfg, DMA_SIZE_32);
  567. channel_config_set_dreq(&snd_dma_b_cfg, i2s.getPioDreq());
  568. channel_config_set_read_increment(&snd_dma_b_cfg, true);
  569. channel_config_set_chain_to(&snd_dma_b_cfg, SOUND_DMA_CHA);
  570. channel_config_set_high_priority(&snd_dma_b_cfg, true);
  571. dma_channel_configure(SOUND_DMA_CHB, &snd_dma_b_cfg, i2s.getPioFIFOAddr(),
  572. output_buf_b, AUDIO_OUT_BUFFER_SIZE, false);
  573. hw_set_bits(&dma_hw->inte2, 1 << SOUND_DMA_CHB );
  574. // dma_irqn_set_channel_enabled(I2S_DMA_IRQ_NUM, SOUND_DMA_CHB, true);
  575. // dma_channel_set_irq0_enabled(SOUND_DMA_CHB, true);
  576. // ready to go
  577. dma_channel_start(SOUND_DMA_CHA);
  578. return true;
  579. }
  580. bool audio_set_paused(uint8_t id, bool paused) {
  581. if (audio_idle) return false;
  582. else if (audio_paused && paused) return false;
  583. else if (!audio_paused && !paused) return false;
  584. audio_paused = paused;
  585. if (paused) {
  586. audio_last_status[id] = ASC_PAUSED;
  587. } else {
  588. audio_last_status[id] = ASC_PLAYING;
  589. }
  590. return true;
  591. }
  592. void audio_stop(uint8_t id) {
  593. if (audio_idle || (id & 7) != audio_owner) return;
  594. memset(&current_track, 0, sizeof(current_track));
  595. memset(output_buf_a, 0, sizeof(output_buf_a));
  596. memset(output_buf_b, 0, sizeof(output_buf_b));
  597. // then indicate that the streams should no longer chain to one another
  598. // and wait for them to shut down naturally
  599. audio_stopping = true;
  600. while (dma_channel_is_busy(SOUND_DMA_CHA)) tight_loop_contents();
  601. while (dma_channel_is_busy(SOUND_DMA_CHB)) tight_loop_contents();
  602. // \todo check if I2S pio is done
  603. // The way to check is the I2S pio is done would be to check
  604. // if the fifo is empty and the PIO's program counter is at the first instruction
  605. // while (spi_is_busy(AUDIO_SPI)) tight_loop_contents();
  606. audio_stopping = false;
  607. dma_channel_abort(SOUND_DMA_CHA);
  608. dma_channel_abort(SOUND_DMA_CHB);
  609. // idle the subsystem
  610. audio_last_status[id] = ASC_COMPLETED;
  611. audio_paused = false;
  612. audio_playing = false;
  613. audio_idle = true;
  614. audio_file.close();
  615. }
  616. audio_status_code audio_get_status_code(uint8_t id) {
  617. audio_status_code tmp = audio_last_status[id];
  618. if (tmp == ASC_COMPLETED || tmp == ASC_ERRORED) {
  619. audio_last_status[id] = ASC_NO_STATUS;
  620. }
  621. return tmp;
  622. }
  623. uint16_t audio_get_volume(uint8_t id) {
  624. return volumes[id];
  625. }
  626. void audio_set_volume(uint8_t id, uint16_t vol)
  627. {
  628. volumes[id] = vol;
  629. }
  630. uint16_t audio_get_channel(uint8_t id) {
  631. return channel[id];
  632. }
  633. void audio_set_channel(uint8_t id, uint16_t chn) {
  634. channel[id] = chn;
  635. }
  636. uint32_t audio_get_lba_position()
  637. {
  638. if (current_track.track_number != 0 && audio_file.isOpen())
  639. {
  640. // We need the file position from the start of the track,
  641. // current_track.file_offset equivalent to data_start (index 1 in cue file)
  642. // index0_offset is the adjustment to current_track.file_offset
  643. // to make it equivalent to current_track.track_start (index 0 in cue file)
  644. uint64_t index0_offset = (current_track.data_start - current_track.track_start) * current_track.sector_length;
  645. return current_track.track_start + (audio_file.position() - (current_track.file_offset - index0_offset)) / current_track.sector_length;
  646. }
  647. else
  648. {
  649. return 0;
  650. }
  651. }
  652. void audio_set_cue_parser(CUEParser *cue_parser, FsFile* file)
  653. {
  654. g_cue_parser = cue_parser;
  655. if (file != nullptr)
  656. {
  657. char filename[MAX_FILE_PATH] = {0};
  658. if (file->isFile())
  659. {
  660. file->getName(filename, sizeof(filename));
  661. audio_file.open(filename, O_RDONLY);
  662. single_bin_file = true;
  663. }
  664. else if (file->isDir())
  665. {
  666. file->getName(filename, sizeof(filename));
  667. audio_parent.open(filename, O_RDONLY);
  668. single_bin_file = false;
  669. }
  670. }
  671. }
  672. uint64_t audio_get_file_position()
  673. {
  674. return fpos;
  675. }
  676. void audio_set_file_position(uint8_t id, uint32_t lba)
  677. {
  678. setup_playback(id, lba, 0, false);
  679. }
  680. #endif // ENABLE_AUDIO_OUTPUT_SPDIF