Skip to content

Commit 595d8d0

Browse files
committed
2 parents 4301286 + 646a9c7 commit 595d8d0

File tree

10 files changed

+61
-15
lines changed

10 files changed

+61
-15
lines changed

examples/examples-vs1053/streams-generator-vs1053/streams-generator-vs1053.ino

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
#include "AudioTools.h"
1515
#include "AudioTools/AudioLibs/VS1053Stream.h"
1616

17-
AudioInfo info(44100, 2, 16);
17+
AudioInfo info(16000, 2, 16);
1818
SineWaveGenerator<int16_t> sineWave(32000); // subclass of SoundGenerator with max amplitude of 32000
1919
GeneratedSoundStream<int16_t> sound(sineWave); // Stream generated from sine wave
2020
VS1053Stream out; // VS1053 output

src/AudioTools/AudioLibs/VS1053Stream.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,9 @@ class VS1053Config : public AudioInfo {
6868
* audio module. If you want to write encoded data set is_encoded_data = true in
6969
* the configuration. Many VS1053 modules also have a built in microphone that
7070
* can be used for recording: set the mode to RX_MODE to use this.
71-
*
71+
*
7272
* Depends on https://github.com/pschatzmann/arduino-vs1053
73-
*
73+
*
7474
* @ingroup io
7575
* @author Phil Schatzmann
7676
* @copyright GPLv3
@@ -197,6 +197,9 @@ class VS1053Stream : public AudioStream, public VolumeSupport {
197197
result = false;
198198
break;
199199
}
200+
201+
// log error on failure
202+
if (!result) LOGE("begin failed");
200203
return result;
201204
}
202205

src/AudioTools/CoreAudio/BaseConverter.h

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -993,17 +993,18 @@ class ChannelAvgT : public BaseConverter {
993993
* @ingroup convert
994994
* @tparam T
995995
*/
996-
template <typename T = int16_t>
996+
template <typename T = int16_t, typename SumT = float>
997997
class ChannelMixer : public BaseConverter {
998998
public:
999999
ChannelMixer(int channels = 2) { this->channels = channels; }
1000-
size_t convert(uint8_t *target, uint8_t *src, size_t size) {
1001-
T *srcT = (T *)src;
1002-
T *targetT = (T *)target;
1000+
size_t convert(uint8_t *data, size_t size) {
1001+
if (channels <= 1) return size; // No mixing needed for single channel
1002+
T *srcT = (T *)data;
1003+
T *targetT = (T *)data;
10031004
int samples = size / sizeof(T);
10041005
assert(samples % channels == 0);
10051006
for (int j = 0; j < samples; j += channels) {
1006-
float sum = 0;
1007+
SumT sum = 0;
10071008
for (int ch = 0; ch < channels; ch++) {
10081009
sum += srcT[j + ch];
10091010
}

src/AudioTools/Disk/AudioSourceSD.h

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ class AudioSourceSD : public AudioSource {
7272
}
7373

7474
void end() {
75+
file.close();
7576
SD.end();
7677
is_sd_setup = false;
7778
}
@@ -82,17 +83,19 @@ class AudioSourceSD : public AudioSource {
8283
}
8384

8485
virtual Stream *selectStream(int index) override {
85-
LOGI("selectStream: %d", index);
86-
idx_pos = index;
87-
file_name = idx[index];
88-
if (file_name==nullptr) return nullptr;
89-
LOGI("Using file %s", file_name);
90-
file = SD.open(file_name);
91-
return file ? &file : nullptr;
86+
LOGI("selectStream SD: %d", index);
87+
if (index > -1) { // avoid invalid position
88+
idx_pos = index;
89+
}
90+
return selectStream(idx[idx_pos]);
9291
}
9392

9493
virtual Stream *selectStream(const char *path) override {
9594
file.close();
95+
if (path == nullptr) {
96+
LOGE("Filename is null")
97+
return nullptr;
98+
}
9699
file = SD.open(path);
97100
file_name = file.name();
98101
LOGI("-> selectStream: %s", path);

src/AudioTools/Disk/AudioSourceSDFAT.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,8 @@ class AudioSourceSDFAT : public AudioSource {
8787
}
8888

8989
void end() {
90+
file.close();
91+
9092
if (is_sd_setup) {
9193
TRACEI();
9294
#ifdef ESP32

src/AudioTools/Disk/AudioSourceSDMMC.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@ class AudioSourceSDMMC : public AudioSource {
3838
setup_index = setupIndex;
3939
}
4040

41+
virtual ~AudioSourceSDMMC() {
42+
end();
43+
}
44+
4145
virtual bool begin() override {
4246
TRACED();
4347
if (!is_sd_setup) {
@@ -53,6 +57,7 @@ class AudioSourceSDMMC : public AudioSource {
5357
}
5458

5559
void end() {
60+
file.close();
5661
SD_MMC.end();
5762
is_sd_setup = false;
5863
}
@@ -65,6 +70,7 @@ class AudioSourceSDMMC : public AudioSource {
6570

6671
virtual Stream *selectStream(int index) override {
6772
LOGI("selectStream: %d", index);
73+
file.close();
6874
idx_pos = index;
6975
file_name = idx[index];
7076
if (file_name==nullptr) return nullptr;

src/AudioTools/Disk/AudioSourceSPIFFS.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ class AudioSourceSPIFFS : public AudioSource {
2020
start_path = startFilePath;
2121
exension = ext;
2222
}
23+
24+
virtual ~AudioSourceSPIFFS() {
25+
end();
26+
}
2327

2428
virtual bool begin() override {
2529
TRACED();
@@ -40,6 +44,7 @@ class AudioSourceSPIFFS : public AudioSource {
4044
}
4145

4246
void end() {
47+
file.close();
4348
SPIFFS.end();
4449
is_sd_setup = false;
4550
}
@@ -51,6 +56,7 @@ class AudioSourceSPIFFS : public AudioSource {
5156

5257
virtual Stream *selectStream(int index) override {
5358
LOGI("selectStream: %d", index);
59+
file.close();
5460
idx_pos = index;
5561
file_name = idx[index];
5662
if (file_name==nullptr) return nullptr;

src/AudioTools/Disk/AudioSourceSTD.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,18 @@ class AudioSourceSTD : public AudioSource {
2525
timeout_auto_next_value = 600000;
2626
}
2727

28+
virtual ~AudioSourceSTD() {
29+
end();
30+
}
31+
2832
virtual bool begin() override {
2933
TRACED();
3034
idx_pos = 0;
3135
return true;
3236
}
3337

3438
virtual void end() {
39+
file.close();
3540
}
3641

3742
virtual Stream *nextStream(int offset = 1) override {

src/AudioTools/Disk/AudioSourceURL.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@ class AudioSourceURL : public AudioSource {
2626
this->pos = startPos - 1;
2727
this->timeout_auto_next_value = 20000;
2828
}
29+
30+
virtual ~AudioSourceURL() {
31+
end();
32+
}
33+
2934

3035
/// Setup Wifi URL
3136
virtual bool begin() override {
@@ -34,6 +39,11 @@ class AudioSourceURL : public AudioSource {
3439
return true;
3540
}
3641

42+
void end() {
43+
if (started) actual_stream->end();
44+
started = false;
45+
}
46+
3747
/// Opens the selected url from the array
3848
Stream* selectStream(int idx) override {
3949
pos = idx;

src/AudioTools/Disk/AudioSourceVFS.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,19 @@ class AudioSourceVFS : public AudioSource {
3636
exension = ext;
3737
}
3838

39+
virtual ~AudioSourceVFS() {
40+
end();
41+
}
42+
43+
3944
virtual bool begin() override {
4045
TRACED();
4146
idx_pos = 0;
4247
return (p_vfs) ? p_vfs->begin() : false;
4348
}
4449

4550
virtual void end() {
51+
file.close();
4652
if (p_vfs) p_vfs->end();
4753
}
4854

@@ -64,6 +70,10 @@ class AudioSourceVFS : public AudioSource {
6470

6571
virtual Stream *selectStream(const char *path) override {
6672
file.close();
73+
if (path == nullptr) {
74+
LOGE("Filename is null")
75+
return nullptr;
76+
}
6777
assert(p_vfs != nullptr);
6878
file = p_vfs->open(path);
6979
file_name = file.name();

0 commit comments

Comments
 (0)