@@ -40,23 +40,27 @@ class Resample : public AudioStreamX {
40
40
return 0 ;
41
41
}
42
42
size_t bytes = 0 ;
43
+ size_t result = 0 ;
43
44
int sample_count = byte_count / sizeof (T);
44
45
if (factor>1 ){
45
46
allocateBuffer (sample_count*factor);
46
47
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 ;
48
49
} 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;
52
54
} else {
53
- bytes = p_out->write (src, byte_count);
55
+ result = p_out->write (src, byte_count);
54
56
}
55
- return bytes ;
57
+ return result ;
56
58
}
57
59
60
+ // / Determines the available bytes from the final source stream
58
61
int available () override { return p_in!=nullptr ? p_in->available () : 0 ; }
59
62
63
+ // / Reads the up/downsampled bytes
60
64
size_t readBytes (uint8_t *src, size_t length) override {
61
65
if (p_in==nullptr ) return 0 ;
62
66
if (length%channels!=0 ){
@@ -95,30 +99,37 @@ class Resample : public AudioStreamX {
95
99
if (len>buffer_size){
96
100
if (buffer!=nullptr ) delete [] buffer;
97
101
buffer = new T[len];
102
+ buffer_size = len;
98
103
}
99
104
if (last_end==nullptr ){
100
105
last_end = new T[channels];
101
106
}
102
107
}
103
108
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 ){
106
112
LOGE (" Incompatible buffer length for down sampling. If must be a factor of %d" , factor);
107
113
return 0 ;
108
114
}
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){
110
118
long total[channels];
111
119
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);
114
124
}
115
- to[j+ch] = total[j+ch] / factor;
125
+ *p_data (j/factor, ch, to) = total[ch] / factor;
126
+ result++;
116
127
}
117
128
}
118
- return size/factor ;
129
+ return result ;
119
130
}
120
131
121
- // / We interpolate the missing samples
132
+ // / Increases the samples by the indicated factor: We interpolate the missing samples
122
133
size_t upsample (T *from, T* to, int sample_count, int channels, int factor ){
123
134
int frame_count = sample_count/channels;
124
135
size_t result = 0 ;
0 commit comments