Skip to content

Commit

Permalink
add extra fillSampleBufferWithNumSamples to exported wasm methods
Browse files Browse the repository at this point in the history
  • Loading branch information
petersalomonsen committed Feb 10, 2024
1 parent 79c25e7 commit 23bccaf
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ class UpperKeys extends MidiVoice {

class LinearVoice extends MidiVoice {
pos: f32 = 0.0;
nextframe(): void {
nextframe(): void {
const val = this.pos / 128;
this.channel.signal.add(val, val);
this.pos++;
Expand Down Expand Up @@ -686,17 +686,17 @@ describe("midisynth", () => {
for (let n = 0; n < 64; n++) {
expect<f32>(samplebuffer[n]).toBe((n / 128.0) as f32);
}

for (let n = 64; n < 128; n++) {
expect<f32>(samplebuffer[n]).toBe(0);
}

fillSampleBufferWithNumSamples(64);

for (let n = 0; n < 64; n++) {
expect<f32>(samplebuffer[n]).toBe(((n+64) / 128.0) as f32);
expect<f32>(samplebuffer[n]).toBe(((n + 64) / 128.0) as f32);
}

for (let n = 64; n < 128; n++) {
expect<f32>(samplebuffer[n]).toBe(0);
}
Expand Down
26 changes: 13 additions & 13 deletions wasmaudioworklet/synth1/assembly/midi/midisynth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ export class MidiChannel {
if (voice.note === note) {
if (this.controllerValues[CONTROL_SUSTAIN] >= 64) {
this.sustainedVoices[note] = voice;
} else {
} else {
voice.noteoff();
}
break;
Expand Down Expand Up @@ -142,15 +142,15 @@ export class MidiChannel {
}
if (oldestVoice !== null) {
const voice = (oldestVoice as MidiVoice);
for (let n = 0;n<sampleBufferFrames;n++) {
for (let n = 0; n < sampleBufferFrames; n++) {
voice.nextframe();
const fact: f32 = ((sampleBufferFrames as f32) - (n as f32)) / (sampleBufferFrames as f32);
this.voiceTransitionBuffer[n<<1] += this.signal.left * fact;
this.voiceTransitionBuffer[(n<<1) + 1] += this.signal.right * fact;
this.voiceTransitionBuffer[n << 1] += this.signal.left * fact;
this.voiceTransitionBuffer[(n << 1) + 1] += this.signal.right * fact;
this.signal.clear();
}
voice.activationCount = voiceActivationCount++;
this.removeFromSustainedVoices(voice);
this.removeFromSustainedVoices(voice);
}
return oldestVoice;
}
Expand Down Expand Up @@ -237,10 +237,10 @@ export function shortmessage(val1: u8, val2: u8, val3: u8): void {
}

export function getActiveVoicesStatusSnapshot(): usize {
for (let n=0;n<activeVoices.length;n++) {
for (let n = 0; n < activeVoices.length; n++) {
const activeVoicesStatusSnapshotIndex = n * 3;
if (activeVoices[n] != null) {
const voice = (activeVoices[n] as MidiVoice);
if (activeVoices[n] != null) {
const voice = (activeVoices[n] as MidiVoice);
activeVoicesStatusSnapshot[activeVoicesStatusSnapshotIndex] = voice.channelNo;
activeVoicesStatusSnapshot[activeVoicesStatusSnapshotIndex + 1] = voice.note;
activeVoicesStatusSnapshot[activeVoicesStatusSnapshotIndex + 2] = voice.velocity;
Expand Down Expand Up @@ -303,11 +303,11 @@ export function fillSampleBufferWithNumSamples(numSamples: i32): void {

channelsignal.left += midichannel.voiceTransitionBuffer[voiceTransitionBufferNdx];
midichannel.voiceTransitionBuffer[voiceTransitionBufferNdx] = 0;
channelsignal.right +=midichannel.voiceTransitionBuffer[voiceTransitionBufferNdx+1];
midichannel.voiceTransitionBuffer[voiceTransitionBufferNdx+1] = 0;
midichannel.preprocess();
channelsignal.right += midichannel.voiceTransitionBuffer[voiceTransitionBufferNdx + 1];
midichannel.voiceTransitionBuffer[voiceTransitionBufferNdx + 1] = 0;

midichannel.preprocess();

channelsignal.left *= midichannel.pan.leftLevel * midichannel.volume;
channelsignal.right *= midichannel.pan.rightLevel * midichannel.volume;

Expand Down
1 change: 1 addition & 0 deletions wasmaudioworklet/synth1/assembly/mixes/globalimports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ export { sampleBufferFrames } from '../midi/midisynth';
export { sampleBufferBytesPerChannel } from '../midi/midisynth';
export { sampleBufferChannels } from '../midi/midisynth';
export { samplebuffer } from '../midi/midisynth';
export { freeverb } from '../midi/midisynth';
export { outputline } from '../midi/midisynth';
export { MidiChannel } from '../midi/midisynth';
export { MidiVoice } from '../midi/midisynth';
Expand Down
2 changes: 1 addition & 1 deletion wasmaudioworklet/synth1/browsercompilerwebworker.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ function createWebAssemblySongData(song, mode = EXPORT_MODE_MIDISYNTH_MULTIPART_
export const midipartschedule: MidiSequencerPartSchedule[] = [new MidiSequencerPartSchedule(0, 0)];
`;
assemblyscriptsynthsources[wasi_main_src] = `
export { fillSampleBuffer, samplebuffer, allNotesOff, shortmessage, getActiveVoicesStatusSnapshot } from './midi/midisynth';
export { fillSampleBuffer, fillSampleBufferWithNumSamples, samplebuffer, allNotesOff, shortmessage, getActiveVoicesStatusSnapshot } from './midi/midisynth';
export { seek, playEventsAndFillSampleBuffer, currentTimeMillis } from './midi/sequencer/midisequencer';
import { midipartschedule } from './midi/sequencer/midiparts';
Expand Down

Large diffs are not rendered by default.

0 comments on commit 23bccaf

Please sign in to comment.