Skip to content

Commit b94f33e

Browse files
committed
Resample
1 parent dfb4b29 commit b94f33e

File tree

4 files changed

+29
-16
lines changed

4 files changed

+29
-16
lines changed

examples/sandbox/basic-a2dp-i2s/basic-a2dp-i2s.ino renamed to examples/sandbox/basic-a2dp-resample-i2s/basic-a2dp-resample-i2s.ino

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,9 @@ void setup() {
3333

3434
// setup output
3535
auto cfg = i2s.defaultConfig();
36-
cfg.pin_data = 23;
36+
cfg.pin_bck = 26;
37+
cfg.pin_ws = 25;
38+
cfg.pin_data = 22;
3739
cfg.sample_rate = a2dp_sink.sample_rate()*2;
3840
cfg.channels = 2;
3941
cfg.bits_per_sample = 16;

src/AudioExperiments/Resample.h

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -40,23 +40,27 @@ class Resample : public AudioStreamX {
4040
return 0;
4141
}
4242
size_t bytes = 0;
43+
size_t result = 0;
4344
int sample_count = byte_count / sizeof(T);
4445
if (factor>1){
4546
allocateBuffer(sample_count*factor);
4647
bytes = upsample((T*)src, buffer, sample_count, channels, factor) * sizeof(T);
47-
p_out->write((uint8_t*)buffer, bytes);
48+
result = p_out->write((uint8_t*)buffer, bytes) / factor;
4849
} else if (factor<1){
49-
allocateBuffer(sample_count/abs(factor));
50-
bytes = downsample((T*)src, buffer , sample_count, channels, abs(factor)) * sizeof(T);
51-
p_out->write((uint8_t*)buffer, bytes);
50+
int abs_factor = abs(factor);
51+
allocateBuffer(sample_count/abs_factor);
52+
bytes = downsample((T*)src, buffer , sample_count, channels, abs_factor) * sizeof(T);
53+
result = p_out->write((uint8_t*)buffer, bytes) * abs_factor;
5254
} else {
53-
bytes = p_out->write(src, byte_count);
55+
result = p_out->write(src, byte_count);
5456
}
55-
return bytes;
57+
return result;
5658
}
5759

60+
/// Determines the available bytes from the final source stream
5861
int available() override { return p_in!=nullptr ? p_in->available() : 0; }
5962

63+
/// Reads the up/downsampled bytes
6064
size_t readBytes(uint8_t *src, size_t length) override {
6165
if (p_in==nullptr) return 0;
6266
if (length%channels!=0){
@@ -95,30 +99,37 @@ class Resample : public AudioStreamX {
9599
if (len>buffer_size){
96100
if (buffer!=nullptr) delete []buffer;
97101
buffer = new T[len];
102+
buffer_size = len;
98103
}
99104
if (last_end==nullptr){
100105
last_end = new T[channels];
101106
}
102107
}
103108

104-
size_t downsample(T *from,T *to, int size, int channels, int factor ){
105-
if (size%factor!=0){
109+
// reduces the sampes by the indicated factor.
110+
size_t downsample(T *from,T *to, int sample_count, int channels, int factor ){
111+
if (sample_count%factor!=0){
106112
LOGE("Incompatible buffer length for down sampling. If must be a factor of %d", factor);
107113
return 0;
108114
}
109-
for (int16_t j=0; j<size-factor; j+=channels){
115+
int frame_count = sample_count / channels;
116+
size_t result = 0;
117+
for (int16_t j=0; j<frame_count; j+=factor){
110118
long total[channels];
111119
for (int8_t ch=0; ch<channels; ch++){
112-
for (int16_t f=0;f<factor;f++){
113-
total[j+ch] += from[j+ch+(f*channels)];
120+
total[ch]=0;
121+
int pos = j+ch;
122+
for (int16_t f=0; f<factor; f++){
123+
total[ch] += *p_data(j+f, ch, from);
114124
}
115-
to[j+ch] = total[j+ch] / factor;
125+
*p_data(j/factor, ch, to) = total[ch] / factor;
126+
result++;
116127
}
117128
}
118-
return size/factor;
129+
return result;
119130
}
120131

121-
/// We interpolate the missing samples
132+
/// Increases the samples by the indicated factor: We interpolate the missing samples
122133
size_t upsample(T *from, T* to, int sample_count, int channels, int factor ){
123134
int frame_count = sample_count/channels;
124135
size_t result = 0;

tests/resample/resample.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ uint8_t channels = 2; // The stream will ha
88
SineWaveGenerator<int16_t> sineWave(32000); // subclass of SoundGenerator with max amplitude of 32000
99
GeneratedSoundStream<int16_t> sound(sineWave); // Stream generated from sine wave
1010
CsvStream<int16_t> csv(Serial, channels); // Output to Serial
11-
Resample<int16_t> out(csv, channels, 2); // We double the output sample rate
11+
Resample<int16_t> out(csv, channels, -2); // We double the output sample rate
1212
StreamCopy copier(out, sound); // copies sound to out
1313

1414
// Arduino Setup

0 commit comments

Comments
 (0)