@@ -6,15 +6,6 @@ using namespace espp;
66// Audio Functions //
77// //////////////////////
88
9- static TaskHandle_t play_audio_task_handle_ = NULL ;
10-
11- static bool IRAM_ATTR audio_tx_sent_callback (i2s_chan_handle_t handle, i2s_event_data_t *event,
12- void *user_ctx) {
13- // notify the main task that we're done
14- vTaskNotifyGiveFromISR (play_audio_task_handle_, NULL );
15- return true ;
16- }
17-
189bool TDeck::initialize_i2s (uint32_t default_audio_rate) {
1910 logger_.info (" initializing i2s driver" );
2011 logger_.debug (" Using newer I2S standard" );
@@ -65,12 +56,6 @@ bool TDeck::initialize_i2s(uint32_t default_audio_rate) {
6556
6657 audio_tx_stream = xStreamBufferCreate (buffer_size * 4 , 0 );
6758
68- play_audio_task_handle_ = xTaskGetCurrentTaskHandle ();
69-
70- memset (&audio_tx_callbacks_, 0 , sizeof (audio_tx_callbacks_));
71- audio_tx_callbacks_.on_sent = audio_tx_sent_callback;
72- i2s_channel_register_event_callback (audio_tx_handle, &audio_tx_callbacks_, NULL );
73-
7459 xStreamBufferReset (audio_tx_stream);
7560
7661 ESP_ERROR_CHECK (i2s_channel_enable (audio_tx_handle));
@@ -125,13 +110,13 @@ bool TDeck::audio_task_callback(std::mutex &m, std::condition_variable &cv, bool
125110 // pointers to the buffer to the correct positions.
126111 int16_t *ptr = reinterpret_cast <int16_t *>(buffer);
127112 for (int i = 0 ; i < (available / 2 ); ++i) {
128- int16_t sample = ptr[i];
129- ptr[i] = static_cast <int16_t >((sample * volume_) / 100 . 0f );
113+ int32_t sample = ( ptr[i] * volume_) / 100 . 0f ;
114+ ptr[i] = static_cast <int16_t >(std::clamp< int32_t > (sample, INT16_MIN , INT16_MAX ) );
130115 }
131116 }
132117
133118 // write the buffer to the I2S channel
134- i2s_channel_write (audio_tx_handle, buffer, available , NULL , portMAX_DELAY);
119+ i2s_channel_write (audio_tx_handle, buffer, buffer_size , NULL , portMAX_DELAY);
135120 }
136121 return false ; // don't stop the task
137122}
@@ -164,11 +149,6 @@ void TDeck::audio_sample_rate(uint32_t sample_rate) {
164149void TDeck::play_audio (const std::vector<uint8_t > &data) { play_audio (data.data (), data.size ()); }
165150
166151void TDeck::play_audio (const uint8_t *data, uint32_t num_bytes) {
167- play_audio_task_handle_ = xTaskGetCurrentTaskHandle ();
168- if (has_sound) {
169- ulTaskNotifyTake (pdTRUE, portMAX_DELAY);
170- }
171152 // don't block here
172153 xStreamBufferSendFromISR (audio_tx_stream, data, num_bytes, NULL );
173- has_sound = true ;
174154}
0 commit comments