3 #include "AudioTools/CoreAudio/AudioStreams.h"
4 #include "AudioTools/CoreAudio/AudioI2S/I2SStream.h"
5 #include "mtb_wm8960.h"
85 if (!mtb_wm8960_activate()){
86 LOGE(
"mtb_wm8960_activate");
89 if (!configure_clocking()){
90 LOGE(
"configure_clocking");
107 mtb_wm8960_deactivate();
119 void setVolumeIn(
float vol){
120 adjustInputVolume(vol);
123 void setVolumeOut(
float vol){
124 setOutputVolume(vol);
136 size_t readBytes (uint8_t *data,
size_t size)
override{
140 size_t write (
const uint8_t *data,
size_t size)
override {
141 return i2s.
write(data, size);
150 void adjustInputVolume(
float vol){
154 }
else if (vol<0.0f){
160 int vol_int =
map(volume_in*100, 0, 100, 0 ,30);
161 mtb_wm8960_adjust_input_volume(vol_int);
164 void setOutputVolume(
float vol){
168 }
else if (vol<0.0f){
174 int vol_int = volume_out==0.0? 0 :
map(volume_out*100, 0, 100, 30 ,0x7F);
175 mtb_wm8960_set_output_volume(vol_int);
179 mtb_wm8960_set_write_retry_count(cfg.i2c_retry_count);
181 mtb_wm8960_set_wire(cfg.wire);
184 if (cfg.features==-1){
187 cfg.features = WM8960_FEATURE_MICROPHONE1;
190 cfg.features = WM8960_FEATURE_HEADPHONE | WM8960_FEATURE_SPEAKER;
193 cfg.features = WM8960_FEATURE_MICROPHONE1 | WM8960_FEATURE_HEADPHONE | WM8960_FEATURE_SPEAKER;
196 LOGW(
"Setup features: %d", cfg.features);
198 return mtb_wm8960_init(cfg.features);
201 bool configure_clocking(){
202 if (cfg.vs1053_mclk_hz==0){
204 cfg.vs1053_mclk_hz = 512 * cfg.sample_rate;
206 if (!mtb_wm8960_configure_clocking(cfg.vs1053_mclk_hz, cfg.vs1053_enable_pll, sampleRate(cfg.sample_rate), wordLength(cfg.bits_per_sample),
modeMasterSlave(cfg.is_master))){
207 LOGE(
"mtb_wm8960_configure_clocking");
213 mtb_wm8960_adc_dac_sample_rate_t sampleRate(
int rate){
216 return WM8960_ADC_DAC_SAMPLE_RATE_48_KHZ;
218 return WM8960_ADC_DAC_SAMPLE_RATE_44_1_KHZ;
220 return WM8960_ADC_DAC_SAMPLE_RATE_32_KHZ;
222 return WM8960_ADC_DAC_SAMPLE_RATE_24_KHZ;
224 return WM8960_ADC_DAC_SAMPLE_RATE_22_05_KHZ;
226 return WM8960_ADC_DAC_SAMPLE_RATE_16_KHZ;
228 return WM8960_ADC_DAC_SAMPLE_RATE_12_KHZ;
230 return WM8960_ADC_DAC_SAMPLE_RATE_11_025_KHZ;
232 return WM8960_ADC_DAC_SAMPLE_RATE_8_018_KHZ;
234 return WM8960_ADC_DAC_SAMPLE_RATE_8_KHZ;
236 LOGE(
"Unsupported rate: %d",rate);
237 return WM8960_ADC_DAC_SAMPLE_RATE_44_1_KHZ;
241 mtb_wm8960_word_length_t wordLength(
int bits){
244 return WM8960_WL_16BITS;
246 return WM8960_WL_20BITS;
248 return WM8960_WL_24BITS;
250 return WM8960_WL_32BITS;
252 LOGE(
"Unsupported bits: %d", bits);
253 return WM8960_WL_16BITS;
259 return microcontroller_is_master ? WM8960_MODE_SLAVE : WM8960_MODE_MASTER;
262 void volumeError(
float vol){
263 LOGE(
"Invalid volume %f", vol);
RxTxMode
The Microcontroller is the Audio Source (TX_MODE) or Audio Sink (RX_MODE). RXTX_MODE is Source and Si...
Definition: AudioTypes.h:28