|
| 1 | +import "./assets/Superpowered.js"; |
| 2 | + |
| 3 | +var webaudioManager = null; // The SuperpoweredWebAudio helper class managing Web Audio for us. |
| 4 | +var Superpowered = null; // A Superpowered instance. |
| 5 | +var audioNode = null; // This example uses one audio node only. |
| 6 | +var content = null; // The <div> displaying everything. |
| 7 | +var pitchShift = 0; // The current pitch shift value. |
| 8 | +var currentPath = null; |
| 9 | +var pbPerc = null; |
| 10 | + |
| 11 | +function changePitchShift(e) { |
| 12 | + // limiting the new pitch shift value |
| 13 | + // let value = parseInt(e.target.value); |
| 14 | + // pitchShift += value; |
| 15 | + |
| 16 | + pitchShift = Math.min(12, Math.max(-12, pitchShift + parseInt(e.target.value))); |
| 17 | + |
| 18 | + // if (pitchShift < -12) pitchShift = -12; else if (pitchShift > 12) pitchShift = 12; |
| 19 | + // displaying the value |
| 20 | + document.getElementById('pitch-shift-display').textContent = ' pitch shift: ' + ((pitchShift < 1) ? pitchShift : '+' + pitchShift) + ' '; |
| 21 | + // sending the new value to the audio node |
| 22 | + audioNode.sendMessageToAudioScope({ 'pitchShift': pitchShift }); |
| 23 | +} |
| 24 | + |
| 25 | +// on change by the rate slider |
| 26 | +function changeRate() { |
| 27 | + // displaying the new rate |
| 28 | + let value = document.getElementById('rateSlider').value, text; |
| 29 | + if (value == 10000) text = 'original tempo'; |
| 30 | + else if (value < 10000) text = '-' + (100 - value / 100).toPrecision(2) + '%'; |
| 31 | + else text = '+' + (value / 100 - 100).toPrecision(2) + '%'; |
| 32 | + document.getElementById('rateDisplay').textContent = text; |
| 33 | + // sending the new rate to the audio node |
| 34 | + audioNode.sendMessageToAudioScope({ rate: value }); |
| 35 | +} |
| 36 | + |
| 37 | +function changePitchBend(e) { |
| 38 | + console.log(Number(document.getElementById('holdMsSelect').value)); |
| 39 | + const value = e.target.value; |
| 40 | + audioNode.sendMessageToAudioScope({ |
| 41 | + 'pitchBend': true, |
| 42 | + maxPercent: Math.abs(value/100), |
| 43 | + bendStretch: 0, |
| 44 | + faster: value < 0 ? 0 : 1, |
| 45 | + holdMs: Number(document.getElementById('holdMsSelect').value) |
| 46 | + }); |
| 47 | +} |
| 48 | + |
| 49 | +// double click on the rate slider |
| 50 | +function changeRateDbl() { |
| 51 | + document.getElementById('rateSlider').value = 10000; |
| 52 | + changeRate(); |
| 53 | +} |
| 54 | + |
| 55 | +// double click on the rate slider |
| 56 | +function changeBendDbl() { |
| 57 | + document.getElementById('pitchBend').value = 0; |
| 58 | + audioNode.sendMessageToAudioScope({ |
| 59 | + 'pitchBend': true, |
| 60 | + maxPercent: 0, |
| 61 | + bendStretch: 0, |
| 62 | + faster: 0, |
| 63 | + holdMs: Number(document.getElementById('holdMsSelect').value) |
| 64 | + }); |
| 65 | +} |
| 66 | + |
| 67 | +// click on play/pause |
| 68 | +function togglePlayback(e) { |
| 69 | + let button = document.getElementById('playPause'); |
| 70 | + if (button.value == 1) { |
| 71 | + button.value = 0; |
| 72 | + button.textContent = 'Play audio'; |
| 73 | + webaudioManager.audioContext.suspend(); |
| 74 | + } else { |
| 75 | + button.value = 1; |
| 76 | + button.textContent = 'Pause audio'; |
| 77 | + webaudioManager.audioContext.resume(); |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +function onMessageFromAudioScope(message) { |
| 82 | + if (message.loaded) { |
| 83 | + // UI: innerHTML may be ugly but keeps this example small |
| 84 | + content.innerHTML = '\ |
| 85 | + <button id="playPause" value="0">Play audio</button>\ |
| 86 | + <h3>Pitch bend holdMs</h3>\ |
| 87 | + <select id="holdMsSelect"><option>40</option><option>200</option><option>300</option><option>600</option><option>1000</option></select><span>ms</span>\ |
| 88 | + <h3>Pitch bend percentage</h3>\ |
| 89 | + <div style="width: 100%; display: flex; justify-content: space-between;"><span>-30%</span><span>0%</span><span>+30%</span></div>\ |
| 90 | + <input id="pitchBend" type="range" min="-30" max="30" value="0" style="width: 100%">\ |
| 91 | + <div style="overflow: hidden; border-radius: 5px; background: #909090; width: 100%; postion: relative;" id="bend-container"><div style="width: 50%; height: 10px; background: black;" id="bend-value"></div></div>\ |
| 92 | + <div style="text-align: center;"><span><span id="pitch-bend-percentage">100</span>%</span></div><br />\ |
| 93 | + <button id="reset-bend">Reset pitch bend</button>\ |
| 94 | + <h3>Playback rate</h3>\ |
| 95 | + <span id="rateDisplay">original tempo</span>\ |
| 96 | + <div style="width: 100%; display: flex; justify-content: space-between;"><span>-50%</span><span>+100%</span></div>\ |
| 97 | + <input id="rateSlider" type="range" min="5000" max="20000" value="10000" style="width: 100%">\ |
| 98 | + <button id="reset-rate">Reset playback rate</button> <br /><br />\ |
| 99 | + <div>\ |
| 100 | + <button id="pitchMinus" value="-1">-</button>\ |
| 101 | + <span id="pitch-shift-display"> pitch shift: 0 </span>\ |
| 102 | + <button id="pitchPlus" value="1">+</button>\ |
| 103 | + </div>\ |
| 104 | + '; |
| 105 | + document.getElementById('rateSlider').addEventListener('input', changeRate); |
| 106 | + document.getElementById('pitchBend').addEventListener('input', changePitchBend); |
| 107 | + document.getElementById('pitchBend').addEventListener('dblclick', changeBendDbl); |
| 108 | + document.getElementById('rateSlider').addEventListener('dblclick', changeRateDbl); |
| 109 | + document.getElementById('reset-bend').addEventListener('click', changeBendDbl); |
| 110 | + document.getElementById('reset-rate').addEventListener('click', changeRateDbl); |
| 111 | + document.getElementById('pitchMinus').addEventListener('click', changePitchShift); |
| 112 | + document.getElementById('pitchPlus').addEventListener('click', changePitchShift); |
| 113 | + document.getElementById('playPause').addEventListener('click', togglePlayback); |
| 114 | + pbPerc = document.getElementById('pitch-bend-percentage'); |
| 115 | + } |
| 116 | + if (message.pitchBendDetails && document.getElementById('bend-value')) { |
| 117 | + if (pbPerc && (typeof message.pitchBendDetails.currentPitchBend !== 'undefined')) { |
| 118 | + pbPerc.textContent = message.pitchBendDetails.currentPitchBend * 100; |
| 119 | + document.getElementById('bend-value').style.width = convertRange(message.pitchBendDetails.currentPitchBend * 100, [70, 130], [0, 100]) + '%'; |
| 120 | + document.getElementById('bend-value').style.background = message.pitchBendDetails.currentPitchBend === 1 ? 'black' : message.pitchBendDetails.currentPitchBend < 1 ? 'red' : 'green'; |
| 121 | + } |
| 122 | + } |
| 123 | +} |
| 124 | + |
| 125 | +function convertRange( value, r1, r2 ) { |
| 126 | + return ( value - r1[ 0 ] ) * ( r2[ 1 ] - r2[ 0 ] ) / ( r1[ 1 ] - r1[ 0 ] ) + r2[ 0 ]; |
| 127 | +} |
| 128 | + |
| 129 | +function requestPitchBendDetails() { |
| 130 | + audioNode.sendMessageToAudioScope({ requestPitchBend: true }); |
| 131 | + requestAnimationFrame(requestPitchBendDetails) |
| 132 | +} |
| 133 | + |
| 134 | +// when the START button is clicked |
| 135 | +async function start() { |
| 136 | + webaudioManager = new SuperpoweredWebAudio(44100, Superpowered); |
| 137 | + currentPath = window.location.href.substring(0, window.location.href.lastIndexOf('/')); |
| 138 | + audioNode = await webaudioManager.createAudioNodeAsync(window.location.href + '/assets/processor.js?date=' + Date.now(), 'MyProcessor', onMessageFromAudioScope); |
| 139 | + // audioNode -> audioContext.destination (audio output) |
| 140 | + webaudioManager.audioContext.suspend(); |
| 141 | + audioNode.connect(webaudioManager.audioContext.destination); |
| 142 | + |
| 143 | + // start polling of pitch bend details from audioworklet |
| 144 | + requestAnimationFrame(requestPitchBendDetails) |
| 145 | +} |
| 146 | + |
| 147 | +async function loadFromMainThread() { |
| 148 | + Superpowered.downloadAndDecode(window.location.href + '/assets/track.mp3', audioNode); |
| 149 | +} |
| 150 | + |
| 151 | +async function loadJS() { |
| 152 | + Superpowered = await SuperpoweredGlue.Instantiate('ExampleLicenseKey-WillExpire-OnNextUpdate', `${window.location.href}/assets/superpowered-npm.wasm`); |
| 153 | + |
| 154 | + // display the START button |
| 155 | + content = document.getElementById('content'); |
| 156 | + content.innerHTML = `<div> |
| 157 | + <button id="startApplication">Start</button> |
| 158 | + </div>`; |
| 159 | + document.getElementById('startApplication').addEventListener('click', loadFromMainThread); |
| 160 | + start(); |
| 161 | +} |
| 162 | + |
| 163 | +loadJS(); |
0 commit comments