1
1
#include <freertos/FreeRTOS.h>
2
2
#include <freertos/semphr.h>
3
+ #include <driver/gpio.h>
3
4
#include <driver/i2s.h>
4
- #include <driver/dac.h>
5
5
#include <string.h>
6
6
#include <unistd.h>
7
7
8
8
#include "rg_system.h"
9
9
#include "rg_audio.h"
10
10
11
+ #if RG_AUDIO_USE_SPEAKER
12
+ #include <driver/dac.h>
13
+ #endif
14
+
11
15
static int audioSink = -1 ;
12
16
static int audioSampleRate = 0 ;
13
17
static int audioFilter = 0 ;
@@ -19,10 +23,10 @@ static int64_t dummyBusyUntil = 0;
19
23
static const rg_sink_t sinks [] = {
20
24
{RG_AUDIO_SINK_DUMMY , 0 , "Dummy" },
21
25
#if RG_AUDIO_USE_SPEAKER
22
- {RG_AUDIO_SINK_SPEAKER , 0 , "Speaker" },
26
+ {RG_AUDIO_SINK_I2S_DAC , 0 , "Speaker" },
23
27
#endif
24
28
#if RG_AUDIO_USE_EXT_DAC
25
- {RG_AUDIO_SINK_EXT_DAC , 0 , "Ext DAC" },
29
+ {RG_AUDIO_SINK_I2S_EXT , 0 , "Ext DAC" },
26
30
#endif
27
31
// {RG_AUDIO_SINK_BT_A2DP, 0, "Bluetooth"},
28
32
};
@@ -50,19 +54,6 @@ void rg_audio_init(int sampleRate)
50
54
{
51
55
RG_ASSERT (audioSink == -1 , "Audio sink already initialized!" );
52
56
53
- i2s_config_t i2s_config = {
54
- .mode = I2S_MODE_MASTER | I2S_MODE_TX | I2S_MODE_DAC_BUILT_IN ,
55
- .sample_rate = sampleRate ,
56
- .bits_per_sample = 16 ,
57
- .channel_format = I2S_CHANNEL_FMT_RIGHT_LEFT ,
58
- .communication_format = I2S_COMM_FORMAT_STAND_MSB ,
59
- .dma_buf_count = 3 , // Goal is to have ~800 samples over 2-8 buffers (3x270 or 5x180 are pretty good)
60
- .dma_buf_len = 270 , // The unit is stereo samples (4 bytes) (optimize for 533 usage)
61
- .intr_alloc_flags = 0 , // ESP_INTR_FLAG_LEVEL1
62
- .use_apll = 0
63
- };
64
- esp_err_t ret = ESP_FAIL ;
65
-
66
57
if (audioDevLock == NULL )
67
58
audioDevLock = xSemaphoreCreateMutex ();
68
59
@@ -74,18 +65,35 @@ void rg_audio_init(int sampleRate)
74
65
audioVolume = (int )rg_settings_get_number (NS_GLOBAL , SETTING_VOLUME , audioVolume );
75
66
// audioMuted = false;
76
67
77
- if (audioSink == RG_AUDIO_SINK_SPEAKER )
68
+ i2s_config_t i2s_config = {
69
+ .mode = I2S_MODE_MASTER | I2S_MODE_TX ,
70
+ .sample_rate = sampleRate ,
71
+ .bits_per_sample = 16 ,
72
+ .channel_format = I2S_CHANNEL_FMT_RIGHT_LEFT ,
73
+ .communication_format = I2S_COMM_FORMAT_STAND_I2S ,
74
+ .intr_alloc_flags = 0 , // ESP_INTR_FLAG_LEVEL1
75
+ .dma_buf_count = 3 , // Goal is to have ~800 samples over 2-8 buffers (3x270 or 5x180 are pretty good)
76
+ .dma_buf_len = 270 , // The unit is stereo samples (4 bytes) (optimize for 533 usage)
77
+ .use_apll = true, // External DAC may care about accuracy
78
+ };
79
+ esp_err_t ret = ESP_FAIL ;
80
+
81
+ if (audioSink == RG_AUDIO_SINK_I2S_DAC )
78
82
{
83
+ #if RG_AUDIO_USE_SPEAKER
84
+ i2s_config .mode = I2S_MODE_MASTER | I2S_MODE_TX | I2S_MODE_DAC_BUILT_IN ;
85
+ i2s_config .communication_format = I2S_COMM_FORMAT_STAND_MSB ;
86
+ i2s_config .use_apll = false;
79
87
if ((ret = i2s_driver_install (I2S_NUM_0 , & i2s_config , 0 , NULL )) == ESP_OK )
80
88
{
81
89
ret = i2s_set_dac_mode (SPEAKER_DAC_MODE );
82
90
}
91
+ #else
92
+ RG_LOGE ("This device does not support internal DAC mode!\n" );
93
+ #endif
83
94
}
84
- else if (audioSink == RG_AUDIO_SINK_EXT_DAC )
95
+ else if (audioSink == RG_AUDIO_SINK_I2S_EXT )
85
96
{
86
- i2s_config .mode = I2S_MODE_MASTER | I2S_MODE_TX ;
87
- i2s_config .communication_format = I2S_COMM_FORMAT_STAND_I2S ;
88
- i2s_config .use_apll = 1 ;
89
97
if ((ret = i2s_driver_install (I2S_NUM_0 , & i2s_config , 0 , NULL )) == ESP_OK )
90
98
{
91
99
ret = i2s_set_pin (I2S_NUM_0 , & (i2s_pin_config_t ) {
@@ -111,6 +119,7 @@ void rg_audio_init(int sampleRate)
111
119
{
112
120
RG_LOGI ("Audio ready. sink='%s', samplerate=%d, volume=%d\n" ,
113
121
rg_audio_get_sink ()-> name , audioSampleRate , audioVolume );
122
+ // RG_LOGI("Real I2S samplerate: %f\n", i2s_get_clk(I2S_NUM_0));
114
123
}
115
124
else
116
125
{
@@ -127,16 +136,20 @@ void rg_audio_deinit(void)
127
136
// We'll go ahead even if we can't acquire the lock...
128
137
ACQUIRE_DEVICE (1000 );
129
138
130
- if (audioSink == RG_AUDIO_SINK_SPEAKER )
139
+ if (audioSink == RG_AUDIO_SINK_I2S_DAC )
131
140
{
141
+ #if RG_AUDIO_USE_SPEAKER
132
142
i2s_driver_uninstall (I2S_NUM_0 );
133
143
if (SPEAKER_DAC_MODE & I2S_DAC_CHANNEL_RIGHT_EN )
134
144
dac_output_disable (DAC_CHANNEL_1 );
135
145
if (SPEAKER_DAC_MODE & I2S_DAC_CHANNEL_LEFT_EN )
136
146
dac_output_disable (DAC_CHANNEL_2 );
137
147
dac_i2s_disable ();
148
+ #else
149
+ RG_LOGE ("This device does not support internal DAC mode!\n" );
150
+ #endif
138
151
}
139
- else if (audioSink == RG_AUDIO_SINK_EXT_DAC )
152
+ else if (audioSink == RG_AUDIO_SINK_I2S_EXT )
140
153
{
141
154
i2s_driver_uninstall (I2S_NUM_0 );
142
155
gpio_reset_pin (RG_GPIO_SND_I2S_BCK );
@@ -190,7 +203,7 @@ void rg_audio_submit(int16_t *stereoAudioBuffer, size_t frameCount)
190
203
dummyBusyUntil = rg_system_timer () + ((audioSampleRate * 1000 ) / sampleCount );
191
204
written = bufferSize ;
192
205
}
193
- else if (audioSink == RG_AUDIO_SINK_SPEAKER )
206
+ else if (audioSink == RG_AUDIO_SINK_I2S_DAC )
194
207
{
195
208
// In speaker mode we use dac left and right as a single channel
196
209
// to increase resolution.
@@ -227,7 +240,7 @@ void rg_audio_submit(int16_t *stereoAudioBuffer, size_t frameCount)
227
240
}
228
241
i2s_write (I2S_NUM_0 , stereoAudioBuffer , bufferSize , & written , 1000 );
229
242
}
230
- else if (audioSink == RG_AUDIO_SINK_EXT_DAC )
243
+ else if (audioSink == RG_AUDIO_SINK_I2S_EXT )
231
244
{
232
245
for (size_t i = 0 ; i < sampleCount ; ++ i )
233
246
{
@@ -299,8 +312,8 @@ void rg_audio_set_mute(bool mute)
299
312
return ;
300
313
301
314
if (RG_GPIO_SND_AMP_ENABLE != GPIO_NUM_NC )
302
- gpio_set_level (RG_GPIO_SND_AMP_ENABLE , mute ? 0 : 1 );
303
- if (audioSink == RG_AUDIO_SINK_SPEAKER || audioSink == RG_AUDIO_SINK_EXT_DAC )
315
+ gpio_set_level (RG_GPIO_SND_AMP_ENABLE , ! mute );
316
+ if (audioSink == RG_AUDIO_SINK_I2S_DAC || audioSink == RG_AUDIO_SINK_I2S_EXT )
304
317
i2s_zero_dma_buffer (I2S_NUM_0 );
305
318
306
319
audioMuted = mute ;
@@ -320,12 +333,12 @@ void rg_audio_set_sample_rate(int sampleRate)
320
333
if (!ACQUIRE_DEVICE (1000 ))
321
334
return ;
322
335
323
- if (audioSink == RG_AUDIO_SINK_SPEAKER || audioSink == RG_AUDIO_SINK_EXT_DAC )
336
+ if (audioSink == RG_AUDIO_SINK_I2S_DAC || audioSink == RG_AUDIO_SINK_I2S_EXT )
324
337
{
325
338
RG_LOGI ("i2s_set_sample_rates(%d)\n" , sampleRate );
326
339
i2s_set_sample_rates (I2S_NUM_0 , sampleRate );
327
340
}
328
- audioSampleRate = sampleRate ;
329
341
342
+ audioSampleRate = sampleRate ;
330
343
RELEASE_DEVICE ();
331
344
}
0 commit comments