audio.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. #pragma once
  18. #ifdef ENABLE_AUDIO_OUTPUT
  19. #include <Arduino.h>
  20. #ifdef __cplusplus
  21. extern "C" {
  22. #endif
  23. // audio subsystem DMA channels
  24. #define SOUND_DMA_CHA 6
  25. #define SOUND_DMA_CHB 7
  26. // size of the two audio sample buffers, in bytes
  27. // these must be divisible by 1024
  28. #define AUDIO_BUFFER_SIZE 8192 // ~46.44ms
  29. /*
  30. * Status codes for audio playback, matching the SCSI 'audio status codes'.
  31. *
  32. * The first two are for a live condition and will be returned repeatedly. The
  33. * following two reflect a historical condition and are only returned once.
  34. */
  35. enum audio_status_code {
  36. ASC_PLAYING = 0x11,
  37. ASC_PAUSED = 0x12,
  38. ASC_COMPLETED = 0x13,
  39. ASC_ERRORED = 0x14,
  40. ASC_NO_STATUS = 0x15
  41. };
  42. /**
  43. * Handler for DMA interrupts
  44. *
  45. * This is called from scsi_dma_irq() in scsi_accel_rp2040.cpp. That is
  46. * obviously a silly way to handle things. However, using
  47. * irq_add_shared_handler() causes a lockup, likely due to pico-sdk issue #724
  48. * fixed in 1.3.1. Current builds use pico-sdk 1.3.0 and are affected by
  49. * the bug. To work around the problem the above exclusive handler
  50. * delegates to this function if its normal mask is not matched.
  51. */
  52. void audio_dma_irq();
  53. /**
  54. * Indicates if the audio subsystem is actively streaming, including if it is
  55. * sending silent data during sample stall events.
  56. *
  57. * \return true if audio streaming is active, false otherwise.
  58. */
  59. bool audio_is_active();
  60. /**
  61. * \return true if audio streaming is paused, false otherwise.
  62. */
  63. bool audio_is_paused();
  64. /**
  65. * \return the owner value passed to the _play() call, or 0xFF if no owner.
  66. */
  67. uint8_t audio_get_owner();
  68. /**
  69. * Initializes the audio subsystem. Should be called only once, toward the end
  70. * of platform_late_init().
  71. */
  72. void audio_setup();
  73. /**
  74. * Called from platform_poll() to fill sample buffer(s) if needed.
  75. */
  76. void audio_poll();
  77. /**
  78. * Begins audio playback for a file.
  79. *
  80. * \param owner The SCSI ID that initiated this playback operation.
  81. * \param file Path of a file containing PCM samples to play.
  82. * \param start Byte offset within file where playback will begin, inclusive.
  83. * \param end Byte offset within file where playback will end, exclusive.
  84. * \param swap If false, little-endian sample order, otherwise big-endian.
  85. * \return True if successful, false otherwise.
  86. */
  87. bool audio_play(uint8_t owner, const char* file, uint64_t start, uint64_t end, bool swap);
  88. /**
  89. * Pauses audio playback. This may be delayed slightly to allow sample buffers
  90. * to purge.
  91. *
  92. * \param pause If true, pause, otherwise resume.
  93. * \return True if operation changed audio output, false if no change.
  94. */
  95. bool audio_set_paused(bool pause);
  96. /**
  97. * Stops audio playback.
  98. */
  99. void audio_stop();
  100. /**
  101. * Provides SCSI 'audio status code' for the given target. Depending on the
  102. * code this operation may produce side-effects, see the enum for details.
  103. *
  104. * \param id The SCSI ID to provide status codes for.
  105. * \return The matching audio status code.
  106. */
  107. audio_status_code audio_get_status_code(uint8_t id);
  108. /**
  109. * Provides the number of sample bytes read in during an audio_play() call.
  110. * This can be combined with an (external) starting offset to determine
  111. * virtual CD positioning information. This is only an approximation since
  112. * this tracker is always at the end of the most recently read sample data.
  113. *
  114. * This is intentionally not cleared by audio_stop(): audio_play() events will
  115. * reset this information.
  116. *
  117. * \param id The SCSI ID target to return data for.
  118. * \return The number of bytes read in during a playback.
  119. */
  120. uint32_t audio_get_bytes_read(uint8_t id);
  121. /**
  122. * Clears the byte counter in the above call. This is insensitive to whether
  123. * audio playback is occurring but is safe to call in any event.
  124. *
  125. * \param id The SCSI ID target to return data for.
  126. */
  127. void audio_clear_bytes_read(uint8_t id);
  128. #ifdef __cplusplus
  129. }
  130. #endif
  131. #endif // ENABLE_AUDIO_OUTPUT