arduino-audio-tools
Loading...
Searching...
No Matches
WM8960Stream.h
Go to the documentation of this file.
1#pragma once
2
5#include "mtb_wm8960.h" // https://github.com/pschatzmann/arduino-wm8960
6
7namespace audio_tools {
8
14class WM8960Config : public I2SConfig {
15 public:
18 sample_rate = 44100;
19 channels = 2;
20 bits_per_sample = 16;
21 }
23 float default_volume = 0.6;
25 bool vs1053_enable_pll = true;
29 TwoWire* wire = nullptr;
31 bool vs1053_dump = false;
34 // optional features: use bitmask with
35 // WM8960_FEATURE_MICROPHONE,WM8960_FEATURE_HEADPHONE,WM8960_FEATURE_SPEAKER
37};
38
50class WM8960Stream : public AudioStream {
51 public:
52 WM8960Stream() = default;
53
55 TRACED();
56 WM8960Config c(mode);
57 return c;
58 }
59
62 cfg = c;
63 begin(c);
64 }
65
67 cfg.copyFrom(c);
68 begin(cfg);
69 }
70
72 bool begin() { return begin(cfg); }
73
75 bool begin(WM8960Config config) {
76 TRACEI();
77 cfg = config;
78
79 // setup wm8960
80 if (!init(cfg.rx_tx_mode)) {
81 LOGE("init");
82 return false;
83 }
85 if (!mtb_wm8960_activate()) {
86 LOGE("mtb_wm8960_activate");
87 return false;
88 }
89 if (!configure_clocking()) {
90 LOGE("configure_clocking");
91 return false;
92 }
93 if (config.vs1053_dump) {
95 }
96
97 // setup output
98 i2s.begin(cfg);
99
100 return true;
101 }
102
104 void end() {
105 TRACEI();
106 i2s.end();
109 }
110
112 bool setVolume(float vol) {
113 // make sure that value is between 0 and 1
116 return true;
117 }
118
120
122
124 float volumeIn() { return volume_in; }
125
126 float volumeOut() { return volume_out; }
127
128 size_t readBytes(uint8_t* data, size_t size) override {
129 return i2s.readBytes(data, size);
130 }
131
132 size_t write(const uint8_t* data, size_t size) override {
133 return i2s.write(data, size);
134 }
135
136 protected:
141
142 void adjustInputVolume(float vol) {
143 if (vol > 1.0f) {
144 volume_in = 1.0f;
146 } else if (vol < 0.0f) {
147 volume_in = 0.0f;
149 } else {
150 volume_in = vol;
151 }
152 int vol_int = map(volume_in * 100, 0, 100, 0, 30);
154 }
155
156 void setOutputVolume(float vol) {
157 if (vol > 1.0f) {
158 volume_out = 1.0f;
160 } else if (vol < 0.0f) {
161 volume_out = 0.0f;
163 } else {
164 volume_out = vol;
165 }
166 int vol_int =
167 volume_out == 0.0 ? 0 : map(volume_out * 100, 0, 100, 30, 0x7F);
169 }
170
171 bool init(RxTxMode mode) {
173 // define wire object
175
176 // init features if not defined depending on mode
177 if (cfg.features == -1) {
178 switch (mode) {
179 case RX_MODE:
181 break;
182 case TX_MODE:
184 break;
185 case RXTX_MODE:
188 break;
189 }
190 LOGW("Setup features: %d", cfg.features);
191 }
193 }
194
196 if (cfg.vs1053_mclk_hz == 0) {
197 // just pick a multiple of the sample rate
199 }
204 LOGE("mtb_wm8960_configure_clocking");
205 return false;
206 }
207 return true;
208 }
209
211 switch (rate) {
212 case 48000:
214 case 44100:
216 case 32000:
218 case 24000:
220 case 22050:
222 case 16000:
224 case 12000:
226 case 11025:
228 case 8018:
230 case 8000:
232 default:
233 LOGE("Unsupported rate: %d", rate);
235 }
236 }
237
239 switch (bits) {
240 case 16:
241 return WM8960_WL_16BITS;
242 case 20:
243 return WM8960_WL_20BITS;
244 case 24:
245 return WM8960_WL_24BITS;
246 case 32:
247 return WM8960_WL_32BITS;
248 default:
249 LOGE("Unsupported bits: %d", bits);
250 return WM8960_WL_16BITS;
251 }
252 }
253
258
259 void volumeError(float vol) { LOGE("Invalid volume %f", vol); }
260};
261
262} // namespace audio_tools
long map(long x, long in_min, long in_max, long out_min, long out_max)
Maps input to output values.
Definition Arduino.h:182
#define LOGW(...)
Definition AudioLoggerIDF.h:29
#define TRACEI()
Definition AudioLoggerIDF.h:32
#define TRACED()
Definition AudioLoggerIDF.h:31
#define LOGE(...)
Definition AudioLoggerIDF.h:30
Base class for all Audio Streams. It support the boolean operator to test if the object is ready with...
Definition BaseStream.h:120
Configuration for ESP32 legacy i2s.
Definition I2SConfigESP32.h:24
RxTxMode rx_tx_mode
public settings
Definition I2SConfigESP32.h:60
bool is_master
Definition I2SConfigESP32.h:63
We support the Stream interface for the I2S access. In addition we allow a separate mute pin which mi...
Definition I2SStream.h:40
bool begin()
Definition I2SStream.h:60
virtual size_t readBytes(uint8_t *data, size_t len) override
Reads the audio data.
Definition I2SStream.h:131
virtual size_t write(const uint8_t *data, size_t len)
Writes the audio data to I2S.
Definition I2SStream.h:124
void end()
Stops the I2S interface.
Definition I2SStream.h:91
Configuration for WM8960.
Definition WM8960Stream.h:14
WM8960Config(RxTxMode mode)
Definition WM8960Stream.h:17
bool vs1053_enable_pll
enalbel pll for wm8960 - default is true
Definition WM8960Stream.h:25
int8_t features
Definition WM8960Stream.h:36
TwoWire * wire
Define wire if we do not use the default Wire object.
Definition WM8960Stream.h:29
uint32_t i2c_retry_count
Number of i2c write retry on fail: 0 = endless until success.
Definition WM8960Stream.h:33
WM8960Config()
Definition WM8960Stream.h:16
bool vs1053_dump
Dump registers.
Definition WM8960Stream.h:31
uint32_t vs1053_mclk_hz
masterclock rate for wm8960 - default is 0
Definition WM8960Stream.h:27
float default_volume
Volume that is used on start (range 0.0 to 1.0)
Definition WM8960Stream.h:23
Stream for reading and writing audio data using the WM8960 Codec Chip You need to install https://git...
Definition WM8960Stream.h:50
mtb_wm8960_mode_t modeMasterSlave(bool microcontroller_is_master)
if microcontroller is master then module is slave
Definition WM8960Stream.h:255
float volumeIn()
provides the volume
Definition WM8960Stream.h:124
size_t write(const uint8_t *data, size_t size) override
Definition WM8960Stream.h:132
mtb_wm8960_word_length_t wordLength(int bits)
Definition WM8960Stream.h:238
float volume_in
Definition WM8960Stream.h:139
void setAudioInfo(AudioInfo c)
Defines the input AudioInfo.
Definition WM8960Stream.h:66
I2SStream i2s
Definition WM8960Stream.h:138
void setOutputVolume(float vol)
Definition WM8960Stream.h:156
bool begin()
Starts with the default config or restarts.
Definition WM8960Stream.h:72
void setVolumeOut(float vol)
Definition WM8960Stream.h:121
mtb_wm8960_adc_dac_sample_rate_t sampleRate(int rate)
Definition WM8960Stream.h:210
bool begin(WM8960Config config)
Starts with the indicated configuration.
Definition WM8960Stream.h:75
void adjustInputVolume(float vol)
Definition WM8960Stream.h:142
float volume_out
Definition WM8960Stream.h:140
void volumeError(float vol)
Definition WM8960Stream.h:259
void setAudioInfo(WM8960Config c)
defines the default configuration that is used with the next begin()
Definition WM8960Stream.h:61
void end()
Stops the processing and releases the memory.
Definition WM8960Stream.h:104
float volumeOut()
Definition WM8960Stream.h:126
bool init(RxTxMode mode)
Definition WM8960Stream.h:171
bool configure_clocking()
Definition WM8960Stream.h:195
void setVolumeIn(float vol)
Definition WM8960Stream.h:119
bool setVolume(float vol)
Sets both input and output volume value (from 0 to 1.0)
Definition WM8960Stream.h:112
WM8960Config cfg
Definition WM8960Stream.h:137
size_t readBytes(uint8_t *data, size_t size) override
Definition WM8960Stream.h:128
WM8960Config defaultConfig(RxTxMode mode=TX_MODE)
Definition WM8960Stream.h:54
RxTxMode
The Microcontroller is the Audio Source (TX_MODE) or Audio Sink (RX_MODE). RXTX_MODE is Source and Si...
Definition AudioTypes.h:26
@ RXTX_MODE
Definition AudioTypes.h:26
@ TX_MODE
Definition AudioTypes.h:26
@ RX_MODE
Definition AudioTypes.h:26
Generic Implementation of sound input and output for desktop environments using portaudio.
Definition AudioCodecsBase.h:10
size_t writeData(Print *p_out, T *data, int samples, int maxSamples=512)
Definition AudioTypes.h:508
Basic Audio information which drives e.g. I2S.
Definition AudioTypes.h:51
void copyFrom(AudioInfo info)
Same as set.
Definition AudioTypes.h:101
sample_rate_t sample_rate
Sample Rate: e.g 44100.
Definition AudioTypes.h:53
uint16_t channels
Number of channels: 2=stereo, 1=mono.
Definition AudioTypes.h:55
uint8_t bits_per_sample
Number of bits per sample (int16_t = 16 bits)
Definition AudioTypes.h:57