Skip to content

Commit 5fd1a53

Browse files
committed
Changed how float PCM is converted to 8, 16, and 32bit PCM
1 parent 7f82acf commit 5fd1a53

File tree

2 files changed

+9
-9
lines changed

2 files changed

+9
-9
lines changed

examples/tests-audio/tests-audio.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -352,7 +352,7 @@ void internal_test_case(int test, void* extra, bool full, enum CallType typ) {
352352
float* noise = generate_random_noise(total_len);
353353
char* noise_char = (char*)malloc(sizeof(char) * total_len);
354354
for (long i = 0; i < total_len; i++) {
355-
noise_char[i] = (char)floor((noise[i] > 0 ? noise[i]/127 : noise[i]/128) + 0.5); //0.5 is added to make it round
355+
noise_char[i] = (char)floor(noise[i]/128 + 0.5); //0.5 is added to make it round
356356
}
357357
free(noise);
358358

@@ -371,7 +371,7 @@ void internal_test_case(int test, void* extra, bool full, enum CallType typ) {
371371
float* noise = generate_random_noise(total_len);
372372
short* noise_char = (short*)malloc(sizeof(short) * total_len);
373373
for (long i = 0; i < total_len; i++) {
374-
noise_char[i] = (short)floor((noise[i] > 0 ? noise[i]/32767 : noise[i]/32768) + 0.5); //0.5 is added to make it round
374+
noise_char[i] = (short)floor(noise[i]/32768 + 0.5); //0.5 is added to make it round
375375
}
376376
free(noise);
377377

@@ -390,7 +390,7 @@ void internal_test_case(int test, void* extra, bool full, enum CallType typ) {
390390
float* noise = generate_random_noise(total_len);
391391
int* noise_char = (int*)malloc(sizeof(int) * total_len);
392392
for (long i = 0; i < total_len; i++) {
393-
noise_char[i] = (int)floor((noise[i] > 0 ? noise[i]/2147483648 : noise[i]/2147483648) + 0.5); //0.5 is added to make it round
393+
noise_char[i] = (int)floor(noise[i]/2147483648 + 0.5); //0.5 is added to make it round
394394
}
395395
free(noise);
396396

source/twr-ts/twrlibaudio.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ export default class twrLibAudio extends twrLibrary {
107107
for (let i = 0; i < singleChannelDataLen; i++) {
108108
//convert 8-bit PCM to float
109109
//data is signed, so it will also need to find the negatives
110-
channelBuff[i] = dataBuff[i] > 127 ? (dataBuff[i] - 256)/128 : dataBuff[i]/127;
110+
channelBuff[i] = dataBuff[i] > 127 ? (dataBuff[i] - 256)/128 : dataBuff[i]/128;
111111
}
112112
}
113113

@@ -126,7 +126,7 @@ export default class twrLibAudio extends twrLibrary {
126126
for (let i = 0; i < singleChannelDataLen*2; i += 2) {
127127
const val = dataBuff[i]+dataBuff[i+1]*256;
128128
//convert 16-bit PCM to float
129-
channelBuff[i] = val > 32767 ? (val - 65536)/32768 : val/32767;
129+
channelBuff[i] = val > 32767 ? (val - 65536)/32768 : val/32768;
130130
}
131131
}
132132

@@ -144,7 +144,7 @@ export default class twrLibAudio extends twrLibrary {
144144

145145
for (let i = 0; i < singleChannelDataLen; i++) {
146146
//convert 32-bit PCM to float
147-
channelBuff[i] = dataBuff[i] > 2147483647 ? (dataBuff[i] - 4294967296)/2147483648 : dataBuff[i]/2147483647;
147+
channelBuff[i] = dataBuff[i] > 2147483647 ? (dataBuff[i] - 4294967296)/2147483648 : dataBuff[i]/2147483648;
148148
}
149149
}
150150

@@ -224,7 +224,7 @@ export default class twrLibAudio extends twrLibrary {
224224

225225
for (let i = 0; i < buffer.length; i++) {
226226
//nergative values will automatically be converted to unsigned when assigning to retBuffer
227-
retBuffer[i] = Math.round(data[i] < 0 ? data[i] * 128 : data[i] * 127);
227+
retBuffer[i] = Math.round(data[i] * 128);
228228
}
229229
}
230230
}
@@ -245,7 +245,7 @@ export default class twrLibAudio extends twrLibrary {
245245

246246
for (let i = 0; i < buffer.length; i++) {
247247

248-
const val = data[i] < 0 ? 65536 + data[i] * 32768 : data[i] * 32767;
248+
const val = data[i] < 0 ? 65536 + data[i] * 32768 : data[i] * 32768;
249249

250250
retBuffer[i] = val%256; //byte 1
251251
retBuffer[i+1] = Math.round(val/256); //byte 2
@@ -269,7 +269,7 @@ export default class twrLibAudio extends twrLibrary {
269269

270270
for (let i = 0; i < buffer.length; i++) {
271271
//nergative values will automatically be converted to unsigned when assigning to retBuffer
272-
retBuffer[i] = Math.round(data[i] < 0 ? data[i] * 2147483648 : data[i] * 2147483647);
272+
retBuffer[i] = Math.round(data[i] * 2147483648);
273273
}
274274
}
275275
}

0 commit comments

Comments
 (0)