-
-
Notifications
You must be signed in to change notification settings - Fork 122
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c456186
commit f5ca1ff
Showing
1 changed file
with
120 additions
and
121 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,141 +1,140 @@ | ||
import {Invoke} from './utils.mjs'; | ||
import {duration, isNote, midiToFreq, noteToMidi, Pattern} from '@strudel.cycles/core'; | ||
import {getAudioContext, getEnvelope} from '@strudel.cycles/webaudio'; | ||
import { Invoke } from './utils.mjs'; | ||
import { duration, isNote, midiToFreq, noteToMidi, Pattern } from '@strudel.cycles/core'; | ||
import { getAudioContext, getEnvelope } from '@strudel.cycles/webaudio'; | ||
|
||
export const desktopAudio = async (value, deadline, hapDuration) => { | ||
const ac = getAudioContext(); | ||
if (typeof value !== 'object') { | ||
throw new Error( | ||
`expected hap.value to be an object, but got "${value}". Hint: append .note() or .s() to the end`, | ||
'error', | ||
); | ||
} | ||
const ac = getAudioContext(); | ||
if (typeof value !== 'object') { | ||
throw new Error( | ||
`expected hap.value to be an object, but got "${value}". Hint: append .note() or .s() to the end`, | ||
'error', | ||
); | ||
} | ||
|
||
let t = ac.currentTime + deadline; | ||
let t = ac.currentTime + deadline; | ||
|
||
let { | ||
note = 'C3', | ||
s = 'triangle', | ||
bank = '', | ||
source, | ||
gain = 0.8, | ||
// low pass | ||
cutoff = 8000, | ||
resonance = 1, | ||
// high pass | ||
hcutoff = 0, | ||
hresonance = 1, | ||
// band pass | ||
bandf = 0, | ||
bandq = 1, | ||
// | ||
coarse, | ||
crush, | ||
shape, | ||
pan, | ||
vowel, | ||
delay = 0, | ||
delayfeedback = 0.5, | ||
delaytime = 0.25, | ||
orbit = 1, | ||
room, | ||
size = 2, | ||
velocity = 1, | ||
analyze, // analyser wet | ||
fft = 8, // fftSize 0 - 10 | ||
speed = 1, // sample playback speed | ||
begin = 0, | ||
end = 1, | ||
loop = 0, | ||
loopBegin = 0, | ||
loopEnd = 1, | ||
attack = 0.001, | ||
decay = 0.05, | ||
sustain = 1, | ||
release = 0.001, | ||
lpattack = 0.0001, | ||
lpdecay = 0.2, | ||
lpsustain = 0.6, | ||
lprelease = 0.2, | ||
lpenv = 1, | ||
hpattack = 0.0001, | ||
hpdecay = 0.2, | ||
hpsustain = 0.6, | ||
hprelease = 0.2, | ||
hpenv = 1, | ||
bpattack = 0.0001, | ||
bpdecay = 0.2, | ||
bpsustain = 0.6, | ||
bprelease = 0.2, | ||
bpenv = 1, | ||
} = value; | ||
let { | ||
note = 'C3', | ||
s = 'triangle', | ||
bank = '', | ||
source, | ||
gain = 0.8, | ||
// low pass | ||
cutoff = 8000, | ||
resonance = 1, | ||
// high pass | ||
hcutoff = 0, | ||
hresonance = 1, | ||
// band pass | ||
bandf = 0, | ||
bandq = 1, | ||
// | ||
coarse, | ||
crush, | ||
shape, | ||
pan, | ||
vowel, | ||
delay = 0, | ||
delayfeedback = 0.5, | ||
delaytime = 0.25, | ||
orbit = 1, | ||
room, | ||
size = 2, | ||
velocity = 1, | ||
analyze, // analyser wet | ||
fft = 8, // fftSize 0 - 10 | ||
speed = 1, // sample playback speed | ||
begin = 0, | ||
end = 1, | ||
loop = 0, | ||
loopBegin = 0, | ||
loopEnd = 1, | ||
attack = 0.001, | ||
decay = 0.05, | ||
sustain = 1, | ||
release = 0.001, | ||
lpattack = 0.0001, | ||
lpdecay = 0.2, | ||
lpsustain = 0.6, | ||
lprelease = 0.2, | ||
lpenv = 1, | ||
hpattack = 0.0001, | ||
hpdecay = 0.2, | ||
hpsustain = 0.6, | ||
hprelease = 0.2, | ||
hpenv = 1, | ||
bpattack = 0.0001, | ||
bpdecay = 0.2, | ||
bpsustain = 0.6, | ||
bprelease = 0.2, | ||
bpenv = 1, | ||
} = value; | ||
|
||
value.duration = hapDuration; | ||
if (isNote(note)) { | ||
note = noteToMidi(note); | ||
} | ||
value.duration = hapDuration; | ||
if (isNote(note)) { | ||
note = noteToMidi(note); | ||
} | ||
|
||
if (delay !== 0) { | ||
delay = Math.abs(delay); | ||
delayfeedback = Math.abs(delayfeedback); | ||
delaytime = Math.abs(delaytime); | ||
} | ||
if (delay !== 0) { | ||
delay = Math.abs(delay); | ||
delayfeedback = Math.abs(delayfeedback); | ||
delaytime = Math.abs(delaytime); | ||
} | ||
|
||
let adsr_on = attack !== 0.001 || decay !== 0.05 || sustain !== 1 || release !== 0.001 ? 1 : 0; | ||
let adsr_on = attack !== 0.001 || decay !== 0.05 || sustain !== 1 || release !== 0.001 ? 1 : 0; | ||
|
||
const packages = { | ||
loop: [loop, loopBegin, loopEnd], | ||
delay: [delay, delayfeedback, delaytime], | ||
lpf: [cutoff, resonance], | ||
hpf: [hcutoff, hresonance], | ||
bpf: [bandf, bandq], | ||
adsr: [attack, decay, sustain, release, adsr_on], | ||
lpenv: [lpattack, lpdecay, lpsustain, lprelease, lpenv], | ||
hpenv: [hpattack, hpdecay, hpsustain, hprelease, hpenv], | ||
bpenv: [bpattack, bpdecay, bpsustain, bprelease, bpenv] | ||
}; | ||
const packages = { | ||
loop: [loop, loopBegin, loopEnd], | ||
delay: [delay, delayfeedback, delaytime], | ||
lpf: [cutoff, resonance], | ||
hpf: [hcutoff, hresonance], | ||
bpf: [bandf, bandq], | ||
adsr: [attack, decay, sustain, release, adsr_on], | ||
lpenv: [lpattack, lpdecay, lpsustain, lprelease, lpenv], | ||
hpenv: [hpattack, hpdecay, hpsustain, hprelease, hpenv], | ||
bpenv: [bpattack, bpdecay, bpsustain, bprelease, bpenv], | ||
}; | ||
|
||
const offset = (t - getAudioContext().currentTime) * 1000; | ||
const roundedOffset = Math.round(offset); | ||
const messagesfromjs = []; | ||
const offset = (t - getAudioContext().currentTime) * 1000; | ||
const roundedOffset = Math.round(offset); | ||
const messagesfromjs = []; | ||
|
||
messagesfromjs.push({ | ||
note: midiToFreq(note), | ||
offset: roundedOffset, | ||
waveform: s, | ||
bank: bank, | ||
lpf: packages.lpf, | ||
hpf: packages.hpf, | ||
bpf: packages.bpf, | ||
duration: hapDuration, | ||
velocity: velocity, | ||
delay: packages.delay, | ||
speed: speed, | ||
begin: begin, | ||
end: end, | ||
looper: packages.loop, | ||
adsr: packages.adsr, | ||
lpenv: packages.lpenv, | ||
hpenv: packages.hpenv, | ||
bpenv: packages.bpenv, | ||
}); | ||
|
||
messagesfromjs.push({ | ||
note: midiToFreq(note), | ||
offset: roundedOffset, | ||
waveform: s, | ||
bank: bank, | ||
lpf: packages.lpf, | ||
hpf: packages.hpf, | ||
bpf: packages.bpf, | ||
duration: hapDuration, | ||
velocity: velocity, | ||
delay: packages.delay, | ||
speed: speed, | ||
begin: begin, | ||
end: end, | ||
looper: packages.loop, | ||
adsr: packages.adsr, | ||
lpenv: packages.lpenv, | ||
hpenv: packages.hpenv, | ||
bpenv: packages.bpenv, | ||
if (messagesfromjs.length) { | ||
setTimeout(() => { | ||
Invoke('sendwebaudio', { messagesfromjs }); | ||
}); | ||
|
||
if (messagesfromjs.length) { | ||
setTimeout(() => { | ||
Invoke('sendwebaudio', {messagesfromjs}); | ||
}); | ||
} | ||
} | ||
}; | ||
const hap2value = (hap) => { | ||
hap.ensureObjectValue(); | ||
return {...hap.value, velocity: hap.context.velocity}; | ||
hap.ensureObjectValue(); | ||
return { ...hap.value, velocity: hap.context.velocity }; | ||
}; | ||
export const webaudioDesktopOutputTrigger = (t, hap, ct, cps) => | ||
desktopAudio(hap2value(hap), t - ct, hap.duration / cps, cps); | ||
desktopAudio(hap2value(hap), t - ct, hap.duration / cps, cps); | ||
export const webaudioDesktopOutput = (hap, deadline, hapDuration) => | ||
desktopAudio(hap2value(hap), deadline, hapDuration); | ||
desktopAudio(hap2value(hap), deadline, hapDuration); | ||
|
||
Pattern.prototype.webaudio = function () { | ||
return this.onTrigger(webaudioDesktopOutputTrigger); | ||
return this.onTrigger(webaudioDesktopOutputTrigger); | ||
}; |