Skip to content

Commit 4e8c04b

Browse files
committed
Tue 10 Mar 2020 18:22:52 EDT - volume fix
1 parent e0eab5c commit 4e8c04b

File tree

91 files changed

+3649
-138
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

91 files changed

+3649
-138
lines changed

Assets/retro-esp32/systems/fav.gif

-30 Bytes
Loading
751 Bytes
Loading

Assets/retro-esp32/systems/recent.gif

-27 Bytes
Loading
3 Bytes
Loading
3 Bytes
Loading
298 Bytes
Loading
Lines changed: 192 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,192 @@
1+
#include "odroid_audio.h"
2+
3+
#include "odroid_settings.h"
4+
5+
#include "freertos/FreeRTOS.h"
6+
#include "esp_system.h"
7+
#include "driver/i2s.h"
8+
#include "driver/rtc_io.h"
9+
10+
11+
12+
#define I2S_NUM (I2S_NUM_0)
13+
#define BUILTIN_DAC_ENABLED 1
14+
15+
static float Volume = 1.0f;
16+
static odroid_volume_level volumeLevel = ODROID_VOLUME_LEVEL3;
17+
static int volumeLevels[] = {0, 60, 125, 187, 250, 375, 500, 750, 1000};
18+
static int audio_sample_rate;
19+
20+
21+
odroid_volume_level odroid_audio_volume_get()
22+
{
23+
return volumeLevel;
24+
}
25+
26+
void odroid_audio_volume_set(odroid_volume_level value)
27+
{
28+
if (value >= ODROID_VOLUME_LEVEL_COUNT)
29+
{
30+
printf("odroid_audio_volume_set: value out of range (%d)\n", value);
31+
abort();
32+
}
33+
34+
volumeLevel = value;
35+
Volume = (float)volumeLevels[value] * 0.001f;
36+
}
37+
38+
void odroid_audio_volume_change()
39+
{
40+
int level = (volumeLevel + 1) % ODROID_VOLUME_LEVEL_COUNT;
41+
odroid_audio_volume_set(level);
42+
43+
odroid_settings_Volume_set(level);
44+
}
45+
46+
void odroid_audio_init(int sample_rate)
47+
{
48+
audio_sample_rate = sample_rate;
49+
50+
// NOTE: buffer needs to be adjusted per AUDIO_SAMPLE_RATE
51+
# if BUILTIN_DAC_ENABLED
52+
53+
i2s_config_t i2s_config = {
54+
//.mode = I2S_MODE_MASTER | I2S_MODE_TX, // Only TX
55+
.mode = I2S_MODE_MASTER | I2S_MODE_TX | I2S_MODE_DAC_BUILT_IN,
56+
.sample_rate = audio_sample_rate,
57+
.bits_per_sample = 16,
58+
.channel_format = I2S_CHANNEL_FMT_RIGHT_LEFT, //2-channels
59+
.communication_format = I2S_COMM_FORMAT_I2S_MSB,
60+
//.communication_format = I2S_COMM_FORMAT_PCM,
61+
.dma_buf_count = 4,
62+
//.dma_buf_len = 1472 / 2, // (368samples * 2ch * 2(short)) = 1472
63+
.dma_buf_len = 512, // (416samples * 2ch * 2(short)) = 1664
64+
.intr_alloc_flags = ESP_INTR_FLAG_LEVEL1, //Interrupt level 1
65+
.use_apll = 0 //1
66+
};
67+
68+
i2s_driver_install(I2S_NUM, &i2s_config, 0, NULL);
69+
70+
i2s_set_pin(I2S_NUM, NULL);
71+
i2s_set_dac_mode(/*I2S_DAC_CHANNEL_LEFT_EN*/ I2S_DAC_CHANNEL_BOTH_EN);
72+
73+
#else
74+
75+
i2s_config_t i2s_config = {
76+
.mode = I2S_MODE_MASTER | I2S_MODE_TX, // Only TX
77+
.sample_rate = audio_sample_rate,
78+
.bits_per_sample = 16,
79+
.channel_format = I2S_CHANNEL_FMT_RIGHT_LEFT, //2-channels
80+
.communication_format = I2S_COMM_FORMAT_I2S | I2S_COMM_FORMAT_I2S_MSB,
81+
.dma_buf_count = 4,
82+
//.dma_buf_len = 1472 / 2, // (368samples * 2ch * 2(short)) = 1472
83+
.dma_buf_len = 512, // (416samples * 2ch * 2(short)) = 1664
84+
.intr_alloc_flags = ESP_INTR_FLAG_LEVEL1, //Interrupt level 1
85+
.use_apll = 1
86+
};
87+
88+
i2s_driver_install(I2S_NUM, &i2s_config, 0, NULL);
89+
90+
91+
i2s_pin_config_t pin_config = {
92+
.bck_io_num = 26,
93+
.ws_io_num = 25,
94+
.data_out_num = 27,
95+
.data_in_num = -1 //Not used
96+
};
97+
i2s_set_pin(I2S_NUM, &pin_config);
98+
99+
#endif
100+
101+
odroid_volume_level level = odroid_settings_Volume_get();
102+
odroid_audio_volume_set(level);
103+
}
104+
105+
void odroid_audio_terminate()
106+
{
107+
i2s_zero_dma_buffer(I2S_NUM);
108+
i2s_stop(I2S_NUM);
109+
110+
i2s_start(I2S_NUM);
111+
112+
113+
esp_err_t err = rtc_gpio_init(GPIO_NUM_25);
114+
err = rtc_gpio_init(GPIO_NUM_26);
115+
if (err != ESP_OK)
116+
{
117+
abort();
118+
}
119+
120+
err = rtc_gpio_set_direction(GPIO_NUM_25, RTC_GPIO_MODE_OUTPUT_ONLY);
121+
err = rtc_gpio_set_direction(GPIO_NUM_26, RTC_GPIO_MODE_OUTPUT_ONLY);
122+
if (err != ESP_OK)
123+
{
124+
abort();
125+
}
126+
127+
err = rtc_gpio_set_level(GPIO_NUM_25, 0);
128+
err = rtc_gpio_set_level(GPIO_NUM_26, 0);
129+
if (err != ESP_OK)
130+
{
131+
abort();
132+
}
133+
}
134+
135+
void odroid_audio_submit(short* stereoAudioBuffer, int frameCount)
136+
{
137+
short currentAudioSampleCount = frameCount * 2;
138+
139+
#if BUILTIN_DAC_ENABLED
140+
// Convert for built in DAC
141+
for (short i = 0; i < currentAudioSampleCount; i += 2)
142+
{
143+
int32_t dac0;
144+
int32_t dac1;
145+
146+
if (Volume == 0.0f)
147+
{
148+
// Disable amplifier
149+
dac0 = 0;
150+
dac1 = 0;
151+
}
152+
else
153+
{
154+
dac1 = 0x8000;
155+
156+
// Down mix stero to mono
157+
int32_t sample = stereoAudioBuffer[i];
158+
sample += stereoAudioBuffer[i + 1];
159+
sample >>= 1;
160+
161+
// Apply volume
162+
sample *= Volume;
163+
164+
// Convert to unsigned
165+
dac0 = sample + 0x8000;
166+
}
167+
168+
stereoAudioBuffer[i] = (int16_t)dac1;
169+
stereoAudioBuffer[i + 1] = (int16_t)dac0;
170+
}
171+
172+
#endif
173+
174+
int len = currentAudioSampleCount * sizeof(int16_t);
175+
int count = i2s_write_bytes(I2S_NUM, (const char *)stereoAudioBuffer, len, portMAX_DELAY);
176+
if (count != len)
177+
{
178+
printf("i2s_write_bytes: count (%d) != len (%d)\n", count, len);
179+
abort();
180+
}
181+
}
182+
183+
void odroid_audio_submit_zero()
184+
{
185+
esp_err_t err = i2s_zero_dma_buffer(I2S_NUM);
186+
if (err != ESP_OK) abort();
187+
}
188+
189+
int odroid_audio_sample_rate_get()
190+
{
191+
return audio_sample_rate;
192+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#pragma once
2+
3+
4+
#define ODROID_VOLUME_LEVEL_COUNT (9)
5+
6+
typedef enum
7+
{
8+
ODROID_VOLUME_LEVEL0 = 0,
9+
ODROID_VOLUME_LEVEL1 = 1,
10+
ODROID_VOLUME_LEVEL2 = 2,
11+
ODROID_VOLUME_LEVEL3 = 3,
12+
ODROID_VOLUME_LEVEL4 = 4,
13+
ODROID_VOLUME_LEVEL5 = 5,
14+
ODROID_VOLUME_LEVEL6 = 6,
15+
ODROID_VOLUME_LEVEL7 = 7,
16+
ODROID_VOLUME_LEVEL8 = 8,
17+
18+
_ODROID_VOLUME_FILLER = 0xffffffff
19+
} odroid_volume_level;
20+
21+
22+
odroid_volume_level odroid_audio_volume_get();
23+
void odroid_audio_volume_set(odroid_volume_level value);
24+
void odroid_audio_volume_change();
25+
void odroid_audio_init(int sample_rate);
26+
void odroid_audio_terminate();
27+
void odroid_audio_submit(short* stereoAudioBuffer, int frameCount);
28+
void odroid_audio_submit_zero();
29+
int odroid_audio_sample_rate_get();

0 commit comments

Comments
 (0)