Skip to content

Commit

Permalink
Adding basic ziffers
Browse files Browse the repository at this point in the history
  • Loading branch information
amiika committed Aug 10, 2023
1 parent 1ff0d46 commit 9bec7f2
Show file tree
Hide file tree
Showing 6 changed files with 107 additions and 168 deletions.
2 changes: 2 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
{
}
16 changes: 16 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"postcss": "^8.4.27",
"tailwindcss": "^3.3.3",
"tone": "^14.8.49",
"zifferjs": "github:amiika/zifferjs",
"zzfx": "^1.2.0"
}
}
32 changes: 32 additions & 0 deletions src/API.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import { Editor } from "./main";
import { scale } from './Scales';
import { tryEvaluate } from "./Evaluator";
import { MidiConnection } from "./IO/MidiConnection";
import { next } from "zifferjs";

// @ts-ignore
import { webaudioOutput, samples } from '@strudel.cycles/webaudio';

Expand Down Expand Up @@ -222,6 +224,16 @@ export class UserAPI {
this.MidiConnection.sendMidiNote(note, channel, velocity, duration)
}

public zn(input: string, options: object = {}): void {
const node = next(input, options);
const channel = options.channel ? options.channel : 0;
const velocity = options.velocity ? options.velocity : 100;
const sustain = options.sustain ? options.sustain : 0.5;
if(node.bend) this.MidiConnection.sendPitchBend(node.bend, channel);
this.MidiConnection.sendMidiNote(node.note, channel, velocity, sustain);
if(node.bend) this.MidiConnection.sendPitchBend(8192, channel);
}

public sysex(data: Array<number>): void {
/**
* Sends a MIDI sysex message to the current MIDI output.
Expand All @@ -231,6 +243,19 @@ export class UserAPI {
this.MidiConnection.sendSysExMessage(data)
}

public pitch_bend(value: number, channel: number): void {
/**
* Sends a MIDI pitch bend to the current MIDI output.
*
* @param value - The value of the pitch bend
* @param channel - The MIDI channel to send the pitch bend on
*
* @returns The value of the pitch bend
*/
this.MidiConnection.sendPitchBend(value, channel)
}


public program_change(program: number, channel: number): void {
/**
* Sends a MIDI program change to the current MIDI output.
Expand Down Expand Up @@ -704,6 +729,13 @@ export class UserAPI {
return final_pulses.some(p => p == true)
}

z(string: String, options: object = {}): void {
const parsed = zget(string, options);
if(options && options.s) {
sound(options.s);
}
}

stop(): void {
/**
* Stops the clock.
Expand Down
25 changes: 25 additions & 0 deletions src/IO/MidiConnection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ export class MidiConnection{
*
*/
const output = this.midiOutputs[this.currentOutputIndex];
noteNumber = Math.min(Math.max(noteNumber, 0), 127);
if (output) {
const noteOnMessage = [0x90 + channel, noteNumber, velocity];
const noteOffMessage = [0x80 + channel, noteNumber, 0];
Expand Down Expand Up @@ -138,6 +139,30 @@ export class MidiConnection{
}
}

public sendPitchBend(value: number, channel: number): void {
/**
* Sends a MIDI Pitch Bend message to the currently selected MIDI output.
*
* @param value MIDI pitch bend value (0-16383)
* @param channel MIDI channel (0-15)
*
*/
if (value < 0 || value > 16383) {
console.error('Invalid pitch bend value. Value must be in the range 0-16383.');
}
if (channel < 0 || channel > 15) {
console.error('Invalid MIDI channel. Channel must be in the range 0-15.');
}
const output = this.midiOutputs[this.currentOutputIndex];
if (output) {
const lsb = value & 0x7F;
const msb = (value >> 7) & 0x7F;
output.send([0xE0 | channel, lsb, msb]);
} else {
console.error('MIDI output not available.');
}
}

public sendProgramChange(programNumber: number, channel: number): void {
/**
* Sends a MIDI Program Change message to the currently selected MIDI output.
Expand Down
Loading

0 comments on commit 9bec7f2

Please sign in to comment.