audio.h 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. * Handler for DMA interrupts
  31. *
  32. * This is called from scsi_dma_irq() in scsi_accel_rp2040.cpp. That is
  33. * obviously a silly way to handle things. However, using
  34. * irq_add_shared_handler() causes a lockup, likely due to pico-sdk issue #724
  35. * fixed in 1.3.1. Current builds use pico-sdk 1.3.0 and are affected by
  36. * the bug. To work around the problem the above exclusive handler
  37. * delegates to this function if its normal mask is not matched.
  38. */
  39. void audio_dma_irq();
  40. /**
  41. * Indicates if the audio subsystem is actively streaming, including if it is
  42. * sending silent data during sample stall events.
  43. *
  44. * \return true if audio streaming is active, false otherwise.
  45. */
  46. bool audio_is_active();
  47. /**
  48. * Initializes the audio subsystem. Should be called only once, toward the end
  49. * of platform_late_init().
  50. */
  51. void audio_setup();
  52. /**
  53. * Called from platform_poll() to fill sample buffer(s) if needed.
  54. */
  55. void audio_poll();
  56. /**
  57. * Begins audio playback for a file.
  58. *
  59. * \param file Path of a file containing PCM samples to play.
  60. * \param start Byte offset within file where playback will begin, inclusive.
  61. * \param end Byte offset within file where playback will end, exclusive.
  62. * \param swap If false, little-endian sample order, otherwise big-endian.
  63. * \return True if successful, false otherwise.
  64. */
  65. bool audio_play(const char* file, uint64_t start, uint64_t end, bool swap);
  66. /**
  67. * Stops audio playback.
  68. */
  69. void audio_stop();
  70. #ifdef __cplusplus
  71. }
  72. #endif
  73. #endif // ENABLE_AUDIO_OUTPUT