Adding basic ziffers

This commit is contained in:
2023-08-11 00:37:01 +03:00
parent 1ff0d46c8d
commit 9bec7f20ef
6 changed files with 107 additions and 168 deletions

View File

@ -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';
@ -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.
@ -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.
@ -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.

View File

@ -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];
@ -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.