audio_output.h 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. #pragma once
  2. #include "esp_err.h"
  3. #include "freertos/FreeRTOS.h"
  4. /**
  5. * Output channel mode. LEFT/RIGHT route the chosen source channel to both
  6. * speakers; STEREO (default) plays the normal left/right mix.
  7. */
  8. typedef enum {
  9. AUDIO_CHANNEL_STEREO = 0,
  10. AUDIO_CHANNEL_LEFT,
  11. AUDIO_CHANNEL_RIGHT,
  12. } audio_channel_mode_t;
  13. /**
  14. * Initialize the audio output backend (I2S / SPDIF / USB UAC).
  15. */
  16. esp_err_t audio_output_init(void);
  17. /**
  18. * Start the audio playback task.
  19. */
  20. void audio_output_start(void);
  21. /**
  22. * Flush output buffers (clears stale audio on pause/seek).
  23. */
  24. void audio_output_flush(void);
  25. /**
  26. * Stop the AirPlay playback task (for yielding I2S to another source)
  27. */
  28. void audio_output_stop(void);
  29. // Playback lock for prompt tones
  30. void audio_output_lock(void);
  31. void audio_output_unlock(void);
  32. /**
  33. * Write raw PCM data to the I2S output.
  34. * Can be used by any audio source (BT A2DP, etc.) when the AirPlay
  35. * playback task is stopped.
  36. *
  37. * @param data PCM data buffer (interleaved stereo, 16-bit)
  38. * @param bytes Number of bytes to write
  39. * @param wait Maximum ticks to wait for I2S DMA space
  40. * @return ESP_OK on success
  41. */
  42. esp_err_t audio_output_write(const void *data, size_t bytes, TickType_t wait);
  43. /**
  44. * Change the I2S sample rate (e.g. when BT negotiates 48 kHz)
  45. *
  46. * @param rate Sample rate in Hz (e.g. 44100, 48000)
  47. */
  48. void audio_output_set_sample_rate(uint32_t rate);
  49. /**
  50. * Notify the output of the source sample rate (from AirPlay ANNOUNCE).
  51. * The resampler is re-initialized if the rate changes.
  52. */
  53. void audio_output_set_source_rate(int rate);
  54. /**
  55. * Return the I2S DMA pipeline latency in microseconds.
  56. *
  57. * This is computed from the DMA descriptor count and frame count
  58. * (both set at init time) divided by the output sample rate — i.e.
  59. * (dma_desc_num × dma_frame_num × 1 000 000) / sample_rate
  60. *
  61. * Using this value instead of a hard-coded constant means the latency
  62. * stays correct if the DMA config or sample rate is ever changed.
  63. */
  64. uint32_t audio_output_get_hardware_latency_us(void);
  65. /**
  66. * Cycle the output channel mode: STEREO -> LEFT -> RIGHT -> STEREO.
  67. * @return the new mode after cycling.
  68. */
  69. audio_channel_mode_t audio_output_cycle_channel_mode(void);
  70. /**
  71. * Get the current output channel mode.
  72. */
  73. audio_channel_mode_t audio_output_get_channel_mode(void);