|
| 1 | +/* global BaseAudioContext, AudioContext, webkitAudioContext, AudioParam */ |
| 2 | + |
| 3 | +import { isEmpty, prop, compose, not, clamp, isNil, reject, append, equals, lt, __, gte, either, filter, both, reduce, max, pluck, unless, find, propEq, min, gt, last, has, all, props, add } from 'ramda' |
| 4 | +import { getLinearRampToValueAtTime, getExponentialRampToValueAtTime, getTargetValueAtTime, getValueCurveAtTime } from 'pseudo-audio-param/lib/expr.js' |
| 5 | + |
| 6 | +const AudioContextClass = isNil(window.BaseAudioContext) ? (isNil(window.AudioContext) ? webkitAudioContext : AudioContext) : BaseAudioContext |
| 7 | + |
| 8 | +const maxAll = reduce(max, -Infinity) |
| 9 | +const minAll = reduce(min, Infinity) |
| 10 | + |
| 11 | +const findLastChangeBeforeTime = (scheduledChanges, time) => { |
| 12 | + const targetTimeOfLastChange = compose( |
| 13 | + maxAll, |
| 14 | + filter(lt(__, time)), |
| 15 | + pluck('targetTime') |
| 16 | + )(scheduledChanges) |
| 17 | + |
| 18 | + return find(propEq('targetTime', targetTimeOfLastChange), scheduledChanges) |
| 19 | +} |
| 20 | + |
| 21 | +const findFirstChangeAfterTime = (scheduledChanges, time) => { |
| 22 | + const targetTimeOfLastChange = compose( |
| 23 | + minAll, |
| 24 | + filter(gt(__, time)), |
| 25 | + pluck('targetTime') |
| 26 | + )(scheduledChanges) |
| 27 | + |
| 28 | + return find(propEq('targetTime', targetTimeOfLastChange), scheduledChanges) |
| 29 | +} |
| 30 | + |
| 31 | +const getTargetValueOfChange = scheduledChange => { |
| 32 | + if (scheduledChange.method === 'setValueCurveAtTime') { |
| 33 | + return last(scheduledChange.params[0]) |
| 34 | + } else { |
| 35 | + return scheduledChange.params[0] |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +const evaluateSchedulement = (scheduledChanges, initialValue, initialTime, endTime = Infinity) => { |
| 40 | + const lastChangeBeforeTime = findLastChangeBeforeTime(scheduledChanges, endTime) |
| 41 | + const firstChangeAfterTime = findFirstChangeAfterTime(scheduledChanges, endTime) |
| 42 | + |
| 43 | + let value = isNil(lastChangeBeforeTime) ? initialValue : getTargetValueOfChange(lastChangeBeforeTime) |
| 44 | + if (!isNil(firstChangeAfterTime)) { |
| 45 | + const endTimeOfLastChange = isNil(lastChangeBeforeTime) ? initialTime : lastChangeBeforeTime.targetTime |
| 46 | + switch (firstChangeAfterTime.method) { |
| 47 | + case 'linearRampToValueAtTime': |
| 48 | + value = getLinearRampToValueAtTime(endTime, value, getTargetValueOfChange(firstChangeAfterTime), endTimeOfLastChange, firstChangeAfterTime.targetTime) |
| 49 | + break |
| 50 | + case 'exponentialRampToValueAtTime': |
| 51 | + value = getExponentialRampToValueAtTime(endTime, value, getTargetValueOfChange(firstChangeAfterTime), endTimeOfLastChange, firstChangeAfterTime.targetTime) |
| 52 | + break |
| 53 | + case 'setTargetAtTime': |
| 54 | + value = getTargetValueAtTime(endTime, value, firstChangeAfterTime.params[0], firstChangeAfterTime.params[1], firstChangeAfterTime.params[2]) |
| 55 | + break |
| 56 | + case 'setValueCurveAtTime': |
| 57 | + value = getValueCurveAtTime(endTime, firstChangeAfterTime.params[0], firstChangeAfterTime.params[1], firstChangeAfterTime.params[2]) |
| 58 | + break |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + return value |
| 63 | +} |
| 64 | + |
| 65 | +const scheduleChange = (audioParam, method, params, targetTime) => { |
| 66 | + const now = audioParam._ctx.currentTime |
| 67 | + |
| 68 | + const outdatedSchedulements = filter(compose( |
| 69 | + both( |
| 70 | + gte(__, audioParam._valueWasLastSetAt), |
| 71 | + lt(__, now) |
| 72 | + ), |
| 73 | + prop('targetTime') |
| 74 | + ))(audioParam._scheduledChanges) |
| 75 | + |
| 76 | + if (!isEmpty(outdatedSchedulements)) { |
| 77 | + audioParam._valueWasLastSetAt = compose( |
| 78 | + maxAll, |
| 79 | + pluck('targetTime') |
| 80 | + )(outdatedSchedulements) |
| 81 | + audioParam._value = evaluateSchedulement(outdatedSchedulements, audioParam._value, audioParam._valueWasLastSetAt) |
| 82 | + } |
| 83 | + |
| 84 | + audioParam._scheduledChanges = compose( |
| 85 | + unless( |
| 86 | + () => method === 'cancelScheduledValues', |
| 87 | + append({ |
| 88 | + method, |
| 89 | + params, |
| 90 | + targetTime: clamp(now, Infinity, targetTime) |
| 91 | + }) |
| 92 | + ), |
| 93 | + reject(compose( |
| 94 | + either( |
| 95 | + (method === 'cancelScheduledValues' ? gte(__, targetTime) : equals(__, targetTime)), |
| 96 | + lt(__, now) |
| 97 | + ), |
| 98 | + prop('targetTime') |
| 99 | + )) |
| 100 | + )(audioParam._scheduledChanges) |
| 101 | +} |
| 102 | + |
| 103 | +// gotChangesScheduled :: audioParam -> bool |
| 104 | +const gotChangesScheduled = compose( |
| 105 | + not, |
| 106 | + isEmpty, |
| 107 | + prop('_scheduledChanges') |
| 108 | +) |
| 109 | + |
| 110 | +const getValueAtTime = (audioParam, time) => { |
| 111 | + if (gotChangesScheduled(audioParam)) { |
| 112 | + return evaluateSchedulement(audioParam._scheduledChanges, audioParam._value, audioParam._valueWasLastSetAt, time) |
| 113 | + } else { |
| 114 | + return audioParam._value |
| 115 | + } |
| 116 | +} |
| 117 | + |
| 118 | +// The AudioContext, on which the createX function was called is not accessible from the created AudioNode's params. |
| 119 | +// This will bind the AudioContext to the AudioParam's _ctx property. |
| 120 | +// |
| 121 | +// Example: |
| 122 | +// const osc = ctx.createOscillator() |
| 123 | +// console.log(osc.frequency._ctx === ctx) // true |
| 124 | +const bindContextToParams = (creatorName, params) => { |
| 125 | + const originalFn = AudioContextClass.prototype[creatorName] |
| 126 | + if (!isNil(originalFn)) { |
| 127 | + AudioContextClass.prototype[creatorName] = function (...args) { |
| 128 | + const ctx = this |
| 129 | + const node = originalFn.apply(ctx, args) |
| 130 | + params.forEach(param => { |
| 131 | + const audioParam = node[param] |
| 132 | + audioParam._ctx = ctx |
| 133 | + audioParam._value = audioParam.value |
| 134 | + audioParam._valueWasLastSetAt = 0 |
| 135 | + audioParam._scheduledChanges = [] |
| 136 | + |
| 137 | + // ramps don't take effect, until there was at least one scheduled change |
| 138 | + // audioParam._hadFinishedSchedulement = false // TODO: when to set this to true? |
| 139 | + }) |
| 140 | + return node |
| 141 | + } |
| 142 | + } |
| 143 | +} |
| 144 | + |
| 145 | +const bindSchedulerToParamMethod = (methodName, timeArgIndexes = []) => { |
| 146 | + const originalFn = AudioParam.prototype[methodName] |
| 147 | + if (!isNil(originalFn)) { |
| 148 | + AudioParam.prototype[methodName] = function (...args) { |
| 149 | + const audioParam = this |
| 150 | + let targetTime = Infinity |
| 151 | + if (!isEmpty(timeArgIndexes) && all(has(__, args), timeArgIndexes)) { |
| 152 | + targetTime = reduce(add, 0, props(timeArgIndexes, args)) |
| 153 | + } |
| 154 | + scheduleChange(audioParam, methodName, args, targetTime) |
| 155 | + originalFn.apply(audioParam, args) |
| 156 | + return audioParam |
| 157 | + } |
| 158 | + } |
| 159 | +} |
| 160 | + |
| 161 | +// older Firefox versions always return the defaultValue when reading the value from an AudioParam |
| 162 | +// the correct current value can be read from audioParam._value |
| 163 | +const hijackParamValueSetter = () => { |
| 164 | + const descriptor = Object.getOwnPropertyDescriptor(AudioParam.prototype, 'value') |
| 165 | + const originalSetter = descriptor.set |
| 166 | + descriptor.set = function (newValue) { |
| 167 | + const audioParam = this |
| 168 | + // value change gets ignored in Firefox and Safari, if there are changes scheduled |
| 169 | + if (!gotChangesScheduled(audioParam)) { |
| 170 | + audioParam._value = clamp(audioParam.minValue, audioParam.maxValue, newValue) |
| 171 | + audioParam._valueWasLastSetAt = audioParam._ctx.currentTime |
| 172 | + originalSetter.call(audioParam, newValue) |
| 173 | + } |
| 174 | + } |
| 175 | + Object.defineProperty(AudioParam.prototype, 'value', descriptor) |
| 176 | +} |
| 177 | + |
| 178 | +export { |
| 179 | + scheduleChange, |
| 180 | + gotChangesScheduled, |
| 181 | + getValueAtTime, |
| 182 | + bindContextToParams, |
| 183 | + bindSchedulerToParamMethod, |
| 184 | + hijackParamValueSetter |
| 185 | +} |
0 commit comments