Skip to content

Commit ccb590a

Browse files
committed
Added mem16 to twrwasmmem and switched 16bit PCM function in audio library to use it
1 parent 5fd1a53 commit ccb590a

File tree

2 files changed

+10
-11
lines changed

2 files changed

+10
-11
lines changed

source/twr-ts/twrlibaudio.ts

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -119,14 +119,13 @@ export default class twrLibAudio extends twrLibrary {
119119

120120
for (let channel = 0; channel < numChannels; channel++) {
121121
const channelBuff = arrayBuffer.getChannelData(channel);
122-
const startPos = dataPtr/1.0 + channel*singleChannelDataLen*2.0; //using collections of two from mem8 to represent 16 bits
122+
const startPos = dataPtr/2.0 + channel*singleChannelDataLen;
123123

124-
const dataBuff = mod.wasmMem.mem8.slice(startPos, startPos + singleChannelDataLen*2.0);
124+
const dataBuff = mod.wasmMem.mem16.slice(startPos, startPos + singleChannelDataLen);
125125

126126
for (let i = 0; i < singleChannelDataLen*2; i += 2) {
127-
const val = dataBuff[i]+dataBuff[i+1]*256;
128127
//convert 16-bit PCM to float
129-
channelBuff[i] = val > 32767 ? (val - 65536)/32768 : val/32768;
128+
channelBuff[i] = dataBuff[i] > 32767 ? (dataBuff[i] - 65536)/32768 : dataBuff[i]/32768;
130129
}
131130
}
132131

@@ -240,15 +239,12 @@ export default class twrLibAudio extends twrLibrary {
240239
internalGet16bitPCMPart2(mod: IWasmModuleAsync|IWasmModule, buffer: AudioBuffer, bufferPtr: number) {
241240
for (let channel = 0; channel < buffer.numberOfChannels; channel++) {
242241
let data = buffer.getChannelData(channel);
243-
const startPos = bufferPtr + channel*buffer.length*2.0; //2 bytes per short
244-
const retBuffer = mod.wasmMem.mem8.slice(startPos, buffer.length * 2.0);
242+
const startPos = bufferPtr/2.0 + channel*buffer.length;
243+
const retBuffer = mod.wasmMem.mem16.slice(startPos, buffer.length);
245244

246245
for (let i = 0; i < buffer.length; i++) {
247-
248-
const val = data[i] < 0 ? 65536 + data[i] * 32768 : data[i] * 32768;
249-
250-
retBuffer[i] = val%256; //byte 1
251-
retBuffer[i+1] = Math.round(val/256); //byte 2
246+
//nergative values will automatically be converted to unsigned when assigning to retBuffer
247+
retBuffer[i] = Math.round(data[i] * 32768);
252248
}
253249
}
254250
}

source/twr-ts/twrwasmmem.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {codePageUTF8, codePage1252, codePageASCII, to1252, toASCII} from "./twrl
44
export interface IWasmMemoryBase {
55
memory:WebAssembly.Memory;
66
mem8:Uint8Array;
7+
mem16:Uint16Array;
78
mem32:Uint32Array;
89
memF:Float32Array;
910
memD:Float64Array;
@@ -46,13 +47,15 @@ export interface IWasmMemoryAsync extends IWasmMemoryBase {
4647
export class twrWasmMemoryBase implements IWasmMemoryBase {
4748
memory:WebAssembly.Memory;
4849
mem8:Uint8Array;
50+
mem16:Uint16Array;
4951
mem32:Uint32Array;
5052
memF:Float32Array;
5153
memD:Float64Array;
5254

5355
constructor(memory:WebAssembly.Memory) {
5456
this.memory=memory;
5557
this.mem8 = new Uint8Array(memory.buffer);
58+
this.mem16 = new Uint16Array(memory.buffer);
5659
this.mem32 = new Uint32Array(memory.buffer);
5760
this.memF = new Float32Array(memory.buffer);
5861
this.memD = new Float64Array(memory.buffer);

0 commit comments

Comments
 (0)