Skip to content

Commit 3c2a54a

Browse files
committed
STM32 PWM output and AnalogRead
1 parent 1deb0bd commit 3c2a54a

File tree

8 files changed

+243
-144
lines changed

8 files changed

+243
-144
lines changed

examples/examples-stream/streams-adc-serial/streams-adc-serial.ino

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
#include "Arduino.h"
1010
#include "AudioTools.h"
1111

12-
const uint16_t sample_rate = 44100;
12+
const uint16_t sample_rate = 8000;
1313
AnalogAudioStream in;
1414
int channels = 1;
1515
CsvStream<int16_t> out(Serial, channels); // ASCII output stream

examples/examples-stream/streams-generator-pwm/streams-generator-pwm.ino

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
//int pins[] = {22, 23};
1212
int channels = 1;
13-
uint16_t sample_rate=22000;
13+
uint16_t sample_rate=8000;
1414
SineWaveGenerator<int16_t> sineWave(32000); // subclass of SoundGenerator with max amplitude of 32000
1515
GeneratedSoundStream<int16_t> sound(sineWave); // Stream generated from sine wave
1616
PWMAudioStream pwm;

src/AudioAnalog/AnalogAudioArduino.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99

1010
namespace audio_tools {
1111

12-
1312
/**
1413
* @brief Configuration for Analog Reader
1514
* @author Phil Schatzmann
@@ -39,6 +38,9 @@ class AnalogAudioStream : public AudioStreamX {
3938

4039
AnalogConfig defaultConfig(RxTxMode mode=RX_MODE) {
4140
AnalogConfig cfg;
41+
if (mode!=RX_MODE){
42+
LOGE("mode not supported");
43+
}
4244
return cfg;
4345
}
4446

src/AudioCodecs/CodecWAV.h

Lines changed: 70 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -42,16 +42,12 @@ const char* wav_mime = "audio/wav";
4242
*/
4343
class WAVHeader {
4444
public:
45-
WAVHeader(){
46-
};
47-
48-
void begin(uint8_t* buffer, size_t len){
49-
LOGI("WAVHeader len: %u",(unsigned) len);
45+
WAVHeader() = default;
5046

51-
this->buffer = buffer;
52-
this->len = len;
53-
this->data_pos = 0l;
54-
47+
/// Call begin when header data is complete
48+
void begin(){
49+
LOGI("WAVHeader::begin: %u",(unsigned) len);
50+
this->data_pos = 0l;
5551
memset((void*)&headerInfo, 0, sizeof(WAVAudioInfo));
5652
while (!eof()) {
5753
uint32_t tag, tag2, length;
@@ -125,31 +121,38 @@ class WAVHeader {
125121
seek(length, SEEK_CUR);
126122
}
127123
}
128-
logInfo();
129-
return;
124+
logInfo();
125+
len = 0;
126+
}
127+
128+
/// Resetss the len
129+
void end(){
130+
len = 0;
130131
}
131132

133+
/// Adds data to the 44 byte wav header data buffer
134+
int write(uint8_t* data, size_t data_len){
135+
int write_len = min(data_len, 44 - len);
136+
memmove(buffer, data+len, write_len);
137+
len+=write_len;
138+
LOGI("WAVHeader::write: %u -> %d -> %d",(unsigned) data_len, write_len, len);
139+
return write_len;
140+
}
141+
142+
/// Returns true if the header is complete (with 44 bytes)
143+
bool isDataComplete() {
144+
return len==44;
145+
}
132146

133147
// provides the AudioInfo
134148
WAVAudioInfo &audioInfo() {
135149
return headerInfo;
136150
}
137151

138-
// provides access to the sound data for the first record
139-
bool soundData(uint8_t* &data, size_t &len){
140-
if (sound_pos > 0){
141-
data = buffer + sound_pos;
142-
len = max((long) (this->len - sound_pos),0l);
143-
sound_pos = 0;
144-
return true;
145-
}
146-
return false;
147-
}
148-
149152
protected:
150153
struct WAVAudioInfo headerInfo;
151-
uint8_t* buffer;
152-
size_t len;
154+
uint8_t buffer[44];
155+
size_t len=0;
153156
size_t data_pos = 0;
154157
size_t sound_pos = 0;
155158

@@ -304,49 +307,56 @@ class WAVDecoder : public AudioDecoder {
304307
size_t result = 0;
305308
if (active) {
306309
if (isFirst){
307-
header.begin((uint8_t*)in_ptr, in_size);
308-
uint8_t *sound_ptr;
309-
size_t len;
310-
if (header.soundData(sound_ptr, len)){
311-
isFirst = false;
312-
isValid = header.audioInfo().is_valid;
313-
314-
LOGI("WAV sample_rate: %d", header.audioInfo().sample_rate);
315-
LOGI("WAV data_length: %u", (unsigned) header.audioInfo().data_length);
316-
LOGI("WAV is_streamed: %d", header.audioInfo().is_streamed);
317-
LOGI("WAV is_valid: %s", header.audioInfo().is_valid ? "true" : "false");
318-
319-
// check format
320-
int format = header.audioInfo().format;
321-
isValid = format == WAV_FORMAT_PCM;
322-
if (format != WAV_FORMAT_PCM){
323-
LOGE("WAV format not supported: %d", format);
324-
isValid = false;
325-
} else {
326-
// update sampling rate if the target supports it
327-
AudioBaseInfo bi;
328-
bi.sample_rate = header.audioInfo().sample_rate;
329-
bi.channels = header.audioInfo().channels;
330-
bi.bits_per_sample = header.audioInfo().bits_per_sample;
331-
// we provide some functionality so that we could check if the destination supports the requested format
332-
if (audioBaseInfoSupport!=nullptr){
333-
isValid = audioBaseInfoSupport->validate(bi);
334-
if (isValid){
335-
LOGI("isValid: %s", isValid ? "true":"false");
336-
audioBaseInfoSupport->setAudioInfo(bi);
337-
// write prm data from first record
338-
LOGI("WAVDecoder writing first sound data");
339-
result = out->write(sound_ptr, len);
340-
} else {
341-
LOGE("isValid: %s", isValid ? "true":"false");
342-
}
310+
// we expect at least the full header
311+
int written = header.write((uint8_t*)in_ptr, in_size);
312+
if (!header.isDataComplete()){
313+
return in_size;
314+
}
315+
// parse header
316+
header.begin();
317+
318+
size_t len = in_size - written;
319+
uint8_t *sound_ptr = (uint8_t *) in_ptr + written;
320+
isFirst = false;
321+
isValid = header.audioInfo().is_valid;
322+
323+
LOGI("WAV sample_rate: %d", header.audioInfo().sample_rate);
324+
LOGI("WAV data_length: %u", (unsigned) header.audioInfo().data_length);
325+
LOGI("WAV is_streamed: %d", header.audioInfo().is_streamed);
326+
LOGI("WAV is_valid: %s", header.audioInfo().is_valid ? "true" : "false");
327+
328+
// check format
329+
int format = header.audioInfo().format;
330+
isValid = format == WAV_FORMAT_PCM;
331+
if (format != WAV_FORMAT_PCM){
332+
LOGE("WAV format not supported: %d", format);
333+
isValid = false;
334+
} else {
335+
// update sampling rate if the target supports it
336+
AudioBaseInfo bi;
337+
bi.sample_rate = header.audioInfo().sample_rate;
338+
bi.channels = header.audioInfo().channels;
339+
bi.bits_per_sample = header.audioInfo().bits_per_sample;
340+
// we provide some functionality so that we could check if the destination supports the requested format
341+
if (audioBaseInfoSupport!=nullptr){
342+
isValid = audioBaseInfoSupport->validate(bi);
343+
if (isValid){
344+
LOGI("isValid: %s", isValid ? "true":"false");
345+
audioBaseInfoSupport->setAudioInfo(bi);
346+
// write prm data from first record
347+
LOGI("WAVDecoder writing first sound data");
348+
result = out->write(sound_ptr, len);
349+
} else {
350+
LOGE("isValid: %s", isValid ? "true":"false");
343351
}
344352
}
345353
}
354+
346355
} else if (isValid) {
347356
result = out->write((uint8_t*)in_ptr, in_size);
348357
}
349358
}
359+
header.end();
350360
return result;
351361
}
352362

src/AudioConfig.h

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -110,11 +110,11 @@
110110
#endif
111111

112112
#ifndef PWM_BUFFERS
113-
#define PWM_BUFFERS 50
113+
#define PWM_BUFFERS 40
114114
#endif
115115

116-
#ifndef PWM_FREQUENCY
117-
#define PWM_FREQUENCY 60000
116+
#ifndef PWM_AUDIO_FREQUENCY
117+
#define PWM_AUDIO_FREQUENCY 30000
118118
#endif
119119

120120

@@ -410,12 +410,12 @@ typedef uint32_t eps32_i2s_sample_rate_type;
410410
#define ADC_BUFFERS 20
411411

412412
#define PIN_ADC_START PA0
413-
#define PIN_PWM_START 6
414-
#define PIN_I2S_BCK 1
415-
#define PIN_I2S_WS PIN_I2S_BCK+1
416-
#define PIN_I2S_DATA_IN 3
417-
#define PIN_I2S_DATA_OUT 3
418-
#define PIN_I2S_MUTE 4
413+
#define PIN_PWM_START PA6
414+
#define PIN_I2S_BCK -1
415+
#define PIN_I2S_WS -1
416+
#define PIN_I2S_DATA_IN -1
417+
#define PIN_I2S_DATA_OUT -1
418+
#define PIN_I2S_MUTE -1
419419
#define SOFT_MUTE_VALUE LOW
420420
#define PIN_CS 10
421421
#endif

src/AudioPWM/AudioPWM.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include "AudioPWM/PWMAudioESP32.h"
55
#include "AudioPWM/PWMAudioRP2040.h"
66
#include "AudioPWM/PWMAudioMBED.h"
7+
#include "AudioPWM/PWMAudioSTM32.h"
78
// this is experimental at the moment
89
#include "AudioPWM/PWMAudioAVR.h"
910
#endif

0 commit comments

Comments
 (0)