ZuluSCSI_audio.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. /**
  2. * ZuluSCSI™ - Copyright (c) 2022-2025 Rabbit Hole Computing™
  3. *
  4. * ZuluSCSI™ firmware is licensed under the GPL version 3 or any later version. 
  5. *
  6. * https://www.gnu.org/licenses/gpl-3.0.html
  7. * ----
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version. 
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16. * GNU General Public License for more details. 
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program.  If not, see <https://www.gnu.org/licenses/>.
  20. **/
  21. #pragma once
  22. #include <stdint.h>
  23. #include "ZuluSCSI_disk.h"
  24. /*
  25. * Starting volume level for audio output, with 0 being muted and 255 being
  26. * max volume. SCSI-2 says this should be 25% of maximum by default, MMC-1
  27. * says 100%. Testing shows this tends to be obnoxious at high volumes, so
  28. * go with SCSI-2.
  29. *
  30. * This implementation uses the high byte for output port 1 and the low byte
  31. * for port 0. The two values are averaged to determine final volume level.
  32. */
  33. #define DEFAULT_VOLUME_LEVEL 0x3F
  34. #define DEFAULT_VOLUME_LEVEL_2CH DEFAULT_VOLUME_LEVEL << 8 | DEFAULT_VOLUME_LEVEL
  35. /*
  36. * Defines the 'enable' masks for the two audio output ports of each device.
  37. * If this mask is matched with audio_get_channel() the relevant port will
  38. * have audio output to it, otherwise it will be muted, regardless of the
  39. * volume level.
  40. */
  41. #define AUDIO_CHANNEL_ENABLE_MASK 0x0201
  42. /*
  43. * Status codes for audio playback, matching the SCSI 'audio status codes'.
  44. *
  45. * The first two are for a live condition and will be returned repeatedly. The
  46. * following two reflect a historical condition and are only returned once.
  47. */
  48. enum audio_status_code {
  49. ASC_PLAYING = 0x11,
  50. ASC_PAUSED = 0x12,
  51. ASC_COMPLETED = 0x13,
  52. ASC_ERRORED = 0x14,
  53. ASC_NO_STATUS = 0x15
  54. };
  55. /**
  56. * Indicates whether there is an active playback event for a given target.
  57. *
  58. * Note: this does not consider pause/resume events: even if audio is paused
  59. * this will indicate playback is in progress.
  60. *
  61. * \param owner The SCSI ID to check.
  62. * \return True if playback in progress, false if playback idle.
  63. */
  64. bool audio_is_playing(uint8_t id);
  65. #if defined(ENABLE_AUDIO_OUTPUT) && !defined(ZULUSCSI_BLASTER)
  66. /**
  67. * Begins audio playback for a file.
  68. *
  69. * \param owner The SCSI ID that initiated this playback operation.
  70. * \param img Pointer to the image containing PCM samples to play.
  71. * \param start Byte offset within file where playback will begin, inclusive.
  72. * \param end Byte offset within file where playback will end, exclusive.
  73. * \param swap If false, little-endian sample order, otherwise big-endian.
  74. * \return True if successful, false otherwise.
  75. */
  76. bool audio_play(uint8_t owner, image_config_t* img, uint64_t start, uint64_t end, bool swap);
  77. #elif defined(ENABLE_AUDIO_OUTPUT_I2S) && defined(ZULUSCSI_BLASTER)
  78. /**
  79. * Begins audio playback for a file.
  80. *
  81. * \param owner The SCSI ID that initiated this playback operation.
  82. * \param img Pointer to the image container that can load PCM samples to play.
  83. * \param start LBA offset within file where playback will begin, inclusive.
  84. * \param length LBA length of play .
  85. * \param swap If false, little-endian sample order, otherwise big-endian.
  86. * \return True if successful, false otherwise.
  87. */
  88. bool audio_play(uint8_t owner, image_config_t* img, uint32_t start, uint32_t length, bool swap);
  89. /**
  90. * Begins audio playback for a file using the track and index.
  91. *
  92. * \param owner The SCSI ID that initiated this playback operation.
  93. * \param img Pointer to the image container that can load PCM samples to play.
  94. * \param start_track Starting track
  95. * \param start_index Starting index
  96. * \param end_track LBA offset within file where playback will begin, inclusive.
  97. * \param end_index LBA length of play .
  98. * \return True if successful, false otherwise.
  99. */
  100. bool audio_play_track_index(uint8_t owner, image_config_t* img, uint8_t start_track, uint8_t start_index, uint8_t end_track, uint8_t end_index);
  101. #endif
  102. /**
  103. * Pauses audio playback. This may be delayed slightly to allow sample buffers
  104. * to purge.
  105. *
  106. * \param id The SCSI ID to pause audio playback on.
  107. * \param pause If true, pause, otherwise resume.
  108. * \return True if operation changed audio output, false if no change.
  109. */
  110. bool audio_set_paused(uint8_t id, bool pause);
  111. /**
  112. * Stops audio playback.
  113. *
  114. * \param id The SCSI ID to stop audio playback on.
  115. */
  116. void audio_stop(uint8_t id);
  117. /**
  118. * Provides SCSI 'audio status code' for the given target. Depending on the
  119. * code this operation may produce side-effects, see the enum for details.
  120. *
  121. * \param id The SCSI ID to provide status codes for.
  122. * \return The matching audio status code.
  123. */
  124. audio_status_code audio_get_status_code(uint8_t id);
  125. /**
  126. * Gets the current volume level for a target. This is a pair of 8-bit values
  127. * ranging from 0-255 that are averaged together to determine the final output
  128. * level, where 0 is muted and 255 is maximum volume. The high byte corresponds
  129. * to 0x0E channel 1 and the low byte to 0x0E channel 0. See the spec's mode
  130. * page documentation for more details.
  131. *
  132. * \param id SCSI ID to provide volume for.
  133. * \return The matching volume level.
  134. */
  135. uint16_t audio_get_volume(uint8_t id);
  136. /**
  137. * Sets the volume level for a target, as above. See 0x0E mode page for more.
  138. *
  139. * \param id SCSI ID to set volume for.
  140. * \param vol The new volume level.
  141. */
  142. void audio_set_volume(uint8_t id, uint16_t vol);
  143. /**
  144. * Gets the 0x0E channel information for both audio ports. The high byte
  145. * corresponds to port 1 and the low byte to port 0. If the bits defined in
  146. * AUDIO_CHANNEL_ENABLE_MASK are not set for the respective ports, that
  147. * output will be muted, regardless of volume set.
  148. *
  149. * \param id SCSI ID to provide channel information for.
  150. * \return The channel information.
  151. */
  152. uint16_t audio_get_channel(uint8_t id);
  153. /**
  154. * Sets the 0x0E channel information for a target, as above. See 0x0E mode
  155. * page for more.
  156. *
  157. * \param id SCSI ID to set channel information for.
  158. * \param chn The new channel information.
  159. */
  160. void audio_set_channel(uint8_t id, uint16_t chn);
  161. /**
  162. * Gets the byte position in the audio image
  163. *
  164. * \return byte position in the audio image
  165. */
  166. uint64_t audio_get_file_position();
  167. #ifdef ZULUSCSI_BLASTER
  168. /**
  169. * Gets the LBA position in the audio image
  170. *
  171. * \return LBA position in the audio image
  172. */
  173. uint32_t audio_get_lba_position();
  174. #endif
  175. /**
  176. * Sets the playback position in the audio image via the lba
  177. *
  178. */
  179. void audio_set_file_position(uint8_t id, uint32_t lba);