@@ -66,23 +66,26 @@ class Resample : public AudioStreamX {
66
66
if (length%channels!=0 ){
67
67
length = length / channels * channels;
68
68
}
69
- size_t bytes = 0 ;
70
- int size = length / channels / sizeof (T);
69
+ size_t byte_count = 0 ;
71
70
if (factor>1 ){
72
71
int read_len = length/factor;
73
- allocateBuffer (read_len);
72
+ int sample_count = read_len / sizeof (T);
73
+ allocateBuffer (sample_count);
74
74
read_len = p_in->readBytes ((uint8_t *)buffer, read_len);
75
- bytes = upsample (buffer,(T*)src, read_len, channels, factor) * sizeof (T);
75
+ sample_count = read_len / sizeof (T);
76
+ byte_count = upsample (buffer,(T*)src, sample_count, channels, factor) * sizeof (T);
76
77
} else if (factor<1 ){
77
78
int abs_factor = abs (factor);
78
- int read_len = length*factor;
79
- allocateBuffer (read_len);
79
+ int read_len = length * abs_factor;
80
+ int sample_count = read_len / sizeof (T);
81
+ allocateBuffer (sample_count);
80
82
read_len = p_in->readBytes ((uint8_t *)buffer, read_len);
81
- bytes = downsample (buffer,(T*)src, read_len, channels, abs_factor) * sizeof (T);
83
+ sample_count = read_len / sizeof (T);
84
+ byte_count = downsample (buffer,(T*)src, sample_count, channels, abs_factor) * sizeof (T);
82
85
} else {
83
- bytes = p_in->readBytes (src, length);
86
+ byte_count = p_in->readBytes (src, length);
84
87
}
85
- return bytes ;
88
+ return byte_count ;
86
89
}
87
90
88
91
@@ -95,6 +98,7 @@ class Resample : public AudioStreamX {
95
98
int factor = 1 ;
96
99
int buffer_size = 0 ;
97
100
101
+ // allocates a buffer; len is specified in samples
98
102
void allocateBuffer (int len) {
99
103
if (len>buffer_size){
100
104
if (buffer!=nullptr ) delete [] buffer;
@@ -112,6 +116,7 @@ class Resample : public AudioStreamX {
112
116
LOGE (" Incompatible buffer length for down sampling. If must be a factor of %d" , factor);
113
117
return 0 ;
114
118
}
119
+ int to_pos=0 ;
115
120
int frame_count = sample_count / channels;
116
121
size_t result = 0 ;
117
122
for (int16_t j=0 ; j<frame_count; j+=factor){
@@ -122,7 +127,8 @@ class Resample : public AudioStreamX {
122
127
for (int16_t f=0 ; f<factor; f++){
123
128
total[ch] += *p_data (j+f, ch, from);
124
129
}
125
- *p_data (j/factor, ch, to) = total[ch] / factor;
130
+ to_pos = j/factor;
131
+ *p_data (to_pos, ch, to) = total[ch] / factor;
126
132
result++;
127
133
}
128
134
}
@@ -145,7 +151,8 @@ class Resample : public AudioStreamX {
145
151
result++;
146
152
for (int16_t f=1 ;f<factor;f++){
147
153
pos = ((frame_pos+1 )*factor)+f;
148
- *p_data (pos, ch, to) = actual_data + (diff*f);
154
+ T tmp = actual_data + (diff*f);
155
+ *p_data (pos, ch, to) = tmp;
149
156
result++;
150
157
}
151
158
}
0 commit comments