Skip to content

Commit 9913454

Browse files
committed
Resample
1 parent b94f33e commit 9913454

File tree

1 file changed

+18
-11
lines changed

1 file changed

+18
-11
lines changed

src/AudioExperiments/Resample.h

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -66,23 +66,26 @@ class Resample : public AudioStreamX {
6666
if (length%channels!=0){
6767
length = length / channels * channels;
6868
}
69-
size_t bytes = 0;
70-
int size = length / channels / sizeof(T);
69+
size_t byte_count = 0;
7170
if (factor>1){
7271
int read_len = length/factor;
73-
allocateBuffer(read_len);
72+
int sample_count = read_len / sizeof(T);
73+
allocateBuffer(sample_count);
7474
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);
7677
} else if (factor<1){
7778
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);
8082
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);
8285
} else {
83-
bytes = p_in->readBytes(src, length);
86+
byte_count = p_in->readBytes(src, length);
8487
}
85-
return bytes;
88+
return byte_count;
8689
}
8790

8891

@@ -95,6 +98,7 @@ class Resample : public AudioStreamX {
9598
int factor = 1;
9699
int buffer_size = 0;
97100

101+
// allocates a buffer; len is specified in samples
98102
void allocateBuffer(int len) {
99103
if (len>buffer_size){
100104
if (buffer!=nullptr) delete []buffer;
@@ -112,6 +116,7 @@ class Resample : public AudioStreamX {
112116
LOGE("Incompatible buffer length for down sampling. If must be a factor of %d", factor);
113117
return 0;
114118
}
119+
int to_pos=0;
115120
int frame_count = sample_count / channels;
116121
size_t result = 0;
117122
for (int16_t j=0; j<frame_count; j+=factor){
@@ -122,7 +127,8 @@ class Resample : public AudioStreamX {
122127
for (int16_t f=0; f<factor; f++){
123128
total[ch] += *p_data(j+f, ch, from);
124129
}
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;
126132
result++;
127133
}
128134
}
@@ -145,7 +151,8 @@ class Resample : public AudioStreamX {
145151
result++;
146152
for (int16_t f=1;f<factor;f++){
147153
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;
149156
result++;
150157
}
151158
}

0 commit comments

Comments
 (0)