Skip to content

Commit

Permalink
test variable size blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
petersalomonsen committed Feb 9, 2024
1 parent 57e7985 commit 79c25e7
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 4 deletions.
60 changes: 57 additions & 3 deletions wasmaudioworklet/synth1/assembly/__tests__/midi/midisynth.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { samplebuffer, sampleBufferFrames, playActiveVoices, cleanupInactiveVoices, shortmessage, activeVoices, MidiVoice, midichannels, MidiChannel, numActiveVoices, fillSampleBuffer, allNotesOff, getActiveVoicesStatusSnapshot } from '../../midi/midisynth';
import { freeverb, samplebuffer, sampleBufferFrames, playActiveVoices, cleanupInactiveVoices, shortmessage, activeVoices, MidiVoice, midichannels, MidiChannel, numActiveVoices, fillSampleBuffer, allNotesOff, getActiveVoicesStatusSnapshot, fillSampleBufferWithNumSamples } from '../../midi/midisynth';
import { SineOscillator } from '../../synth/sineoscillator.class';
import { Envelope, EnvelopeState } from '../../synth/envelope.class';
import { notefreq } from '../../synth/note';
Expand All @@ -7,8 +7,6 @@ import { Pan } from '../../synth/pan.class';

let signal: f32 = 0;

midichannels[1] = midichannels[0];

class TestMidiInstrument extends MidiVoice {
osc: SineOscillator = new SineOscillator();
env: Envelope = new Envelope(0.1, 0.0, 1.0, 0.1);
Expand Down Expand Up @@ -117,6 +115,18 @@ class UpperKeys extends MidiVoice {
}
}

class LinearVoice extends MidiVoice {
pos: f32 = 0.0;
nextframe(): void {
const val = this.pos / 128;
this.channel.signal.add(val, val);
this.pos++;
if (this.pos === 128.0) {
this.pos = 0;
}
}
}

describe("midisynth", () => {
it("should activate and deactivate one midivoice", () => {
const channel = midichannels[0] = new MidiChannel(1, (channel: MidiChannel) => new TestMidiInstrument(channel));
Expand Down Expand Up @@ -654,4 +664,48 @@ describe("midisynth", () => {
expect<u8>(load<u8>(activeVoicesShapshotLocation + 7)).toBe(0);
expect<u8>(load<u8>(activeVoicesShapshotLocation + 8)).toBe(0);
});
it("should be able to fill variable sized blocks in the sample buffer", () => {
expect<i32>(numActiveVoices).toBe(0, 'should be no active voices');
freeverb.set_wet(0.0);
const channel = new MidiChannel(1, (channel: MidiChannel) => new LinearVoice(channel));
midichannels[0] = channel;
channel.volume = 1.0;
channel.pan.leftLevel = 1.0;
channel.pan.rightLevel = 1.0;

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

shortmessage(0x90, 69, 100);

expect<i32>(numActiveVoices).toBe(1, 'should be one active voice');

fillSampleBufferWithNumSamples(64);
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);
}

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

shortmessage(0x90, 69, 0);

fillSampleBufferWithNumSamples(64);
for (let n = 0; n < samplebuffer.length; n++) {
expect<f32>(samplebuffer[n]).toBe(0, 'signal should be quiet');
}
});
});
2 changes: 1 addition & 1 deletion wasmaudioworklet/synth1/assembly/midi/midisynth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ const CONTROL_REVERB: u8 = 91;

const mainline = new StereoSignal();
const reverbline = new StereoSignal();
const freeverb = new Freeverb();
export const freeverb = new Freeverb();
export const outputline = new StereoSignal();
export class MidiChannel {
controllerValues: StaticArray<u8> = new StaticArray<u8>(128);
Expand Down

0 comments on commit 79c25e7

Please sign in to comment.