Compare commits

...

5 Commits

Author SHA1 Message Date
Felix Roos 5a42adc366 Merge remote-tracking branch 'origin/main' into objectify-midi 2022-10-29 17:55:53 +02:00
Felix Roos cd3765b8fa Merge remote-tracking branch 'origin/main' into objectify-midi 2022-09-25 21:17:29 +02:00
Felix Roos 787ba9872f Merge remote-tracking branch 'origin/main' into objectify-midi 2022-09-22 19:20:31 +02:00
Felix Roos fd2c7fb4bb allow non notes 2022-08-16 23:12:49 +02:00
Felix Roos 50c84a973c object support for midi #192 2022-08-16 22:31:04 +02:00
3 changed files with 54 additions and 46 deletions
+3 -5
View File
@@ -1287,7 +1287,7 @@ export class Pattern {
}
// sets absolute duration of haps
_duration(value) {
/* _duration(value) {
return this.withHapSpan((span) => new TimeSpan(span.begin, span.begin.add(value)));
}
@@ -1301,7 +1301,7 @@ export class Pattern {
*/
_legato(value) {
return this.withHapSpan((span) => new TimeSpan(span.begin, span.begin.add(span.end.sub(span.begin).mul(value))));
}
}
/**
*
@@ -1315,6 +1315,7 @@ export class Pattern {
_velocity(velocity) {
return this._withContext((context) => ({ ...context, velocity: (context.velocity || 1) * velocity }));
}
*/
/**
* Makes the sample fit the given number of cycles by changing the speed.
@@ -1501,18 +1502,15 @@ Pattern.prototype.patternified = [
'chop',
'color',
'cpm',
'duration',
'early',
'fast',
'jux',
'late',
'legato',
'linger',
'ply',
'segment',
'striate',
'slow',
'velocity',
];
// aliases
+7
View File
@@ -130,3 +130,10 @@ export function curry(func, overload) {
}
return fn;
}
export function objectify(value) {
if (typeof value === 'object' && !Array.isArray(value)) {
return value; // do nothing if its already an object
}
return { value };
}
+44 -41
View File
@@ -4,10 +4,8 @@ Copyright (C) 2022 Strudel contributors - see <https://github.com/tidalcycles/st
This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. You should have received a copy of the GNU Affero General Public License along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
import { isNote } from 'tone';
import _WebMidi from 'webmidi';
import { Pattern, isPattern } from '@strudel.cycles/core';
import { Tone } from '@strudel.cycles/tone';
import { Pattern, isPattern, isNote, getPlayableNoteValue, objectify } from '@strudel.cycles/core';
// if you use WebMidi from outside of this package, make sure to import that instance:
export const WebMidi = _WebMidi;
@@ -32,53 +30,58 @@ const outputByName = (name) => WebMidi.getOutputByName(name);
// Pattern.prototype.midi = function (output: string | number, channel = 1) {
Pattern.prototype.midi = function (output, channel = 1) {
if (isPattern(output?.constructor?.name)) {
if (output?.constructor?.name && isPattern(output?.constructor?.name)) {
throw new Error(
`.midi does not accept Pattern input. Make sure to pass device name with single quotes. Example: .midi('${
WebMidi.outputs?.[0]?.name || 'IAC Driver Bus 1'
}')`,
);
}
return this._withHap((hap) => {
// const onTrigger = (time: number, hap: any) => {
const onTrigger = (time, hap) => {
let note = hap.value;
const velocity = hap.context?.velocity ?? 0.9;
if (!isNote(note)) {
throw new Error('not a note: ' + note);
}
if (!WebMidi.enabled) {
throw new Error(`🎹 WebMidi is not enabled. Supported Browsers: https://caniuse.com/?search=webmidi`);
}
if (!WebMidi.outputs.length) {
throw new Error(`🔌 No MIDI devices found. Connect a device or enable IAC Driver.`);
}
let device;
if (typeof output === 'number') {
device = WebMidi.outputs[output];
} else if (typeof output === 'string') {
device = outputByName(output);
} else {
device = WebMidi.outputs[0];
}
if (!device) {
throw new Error(
`🔌 MIDI device '${output ? output : ''}' not found. Use one of ${WebMidi.outputs
.map((o) => `'${o.name}'`)
.join(' | ')}`,
);
}
// console.log('midi', value, output);
const timingOffset = WebMidi.time - Tone.getContext().currentTime * 1000;
time = time * 1000 + timingOffset;
// const inMs = '+' + (time - Tone.getContext().currentTime) * 1000;
// await enableWebMidi()
return this.onTrigger((time, hap, currentTime) => {
let note;
try {
note = getPlayableNoteValue(hap);
} catch (err) {
// dont care if no note is found as we can still send cvs
}
const { velocity, nrpnn, nrpv, ccn, ccv, legato, duration: durationValue } = objectify(hap.value);
if (!WebMidi.enabled) {
throw new Error(`🎹 WebMidi is not enabled. Supported Browsers: https://caniuse.com/?search=webmidi`);
}
if (!WebMidi.outputs.length) {
throw new Error(`🔌 No MIDI devices found. Connect a device or enable IAC Driver.`);
}
let device;
if (typeof output === 'number') {
device = WebMidi.outputs[output];
} else if (typeof output === 'string') {
device = outputByName(output);
} else {
device = WebMidi.outputs[0];
}
if (!device) {
throw new Error(
`🔌 MIDI device '${output ? output : ''}' not found. Use one of ${WebMidi.outputs
.map((o) => `'${o.name}'`)
.join(' | ')}`,
);
}
// console.log('midi', value, output);
const deadline = (time - currentTime) * 1000;
time = WebMidi.time + deadline;
/* const timingOffset = WebMidi.time - currentTime * 1000;
time = time * 1000 + timingOffset; */
const duration = (durationValue ?? hap.duration.valueOf()) * (legato ?? 1) * 1000 - 5;
if (note) {
device.playNote(note, channel, {
time,
duration: hap.duration.valueOf() * 1000 - 5,
duration,
velocity,
});
};
return hap.setContext({ ...hap.context, onTrigger });
}
if (ccn && ccv) {
device.sendControlChange(ccn, ccv, channel, { time });
}
});
};