|
| 1 | +import { request } from 'http' |
1 | 2 |
|
2 | | -const p5 = require('p5') |
| 3 | +let canvasElement = document.getElementById('canvas') |
| 4 | +let canvasContext = canvasElement.getContext('2d') |
| 5 | +let WIDTH = canvasElement.width |
| 6 | +let flag = true |
3 | 7 |
|
4 | | -let sketch = function(p) { |
5 | | - p.setup = function() { |
6 | | - p.createCanvas(100, 100) |
7 | | - p.background(255) |
| 8 | +// define online and offline audio context |
| 9 | +let audioCtx = new AudioContext() |
| 10 | + |
| 11 | +let analyser = audioCtx.createAnalyser() |
| 12 | +analyser.smoothingTimeConstant = 0.0 |
| 13 | + |
| 14 | +analyser.fftSize = 1024 |
| 15 | +let bufferLength = analyser.frequencyBinCount |
| 16 | +let eightBufferLength = 8 * bufferLength + 1 |
| 17 | +let dataArray = new Uint8Array(bufferLength) |
| 18 | + |
| 19 | +// setup the imageDataFrame early |
| 20 | +let imageDataFrame = canvasContext.createImageData(2, canvasElement.height) |
| 21 | + |
| 22 | +for (var index = 0; index < imageDataFrame.data.length * 4; index += 8) { |
| 23 | + imageDataFrame.data[index] = imageDataFrame.data[index + 6] = 0 |
| 24 | + imageDataFrame.data[index + 3] = imageDataFrame.data[ |
| 25 | + index + 4 |
| 26 | + ] = imageDataFrame.data[index + 5] = imageDataFrame.data[index + 7] = 255 |
| 27 | +} |
| 28 | + |
| 29 | + |
| 30 | + |
| 31 | +// use XHR to load an audio track, and |
| 32 | +// decodeAudioData to decode it and OfflineAudioContext to render it |
| 33 | + |
| 34 | +function getData() { |
| 35 | + // var reader = new FileReader() |
| 36 | + // reader.onload = function(ev) {} |
| 37 | + |
| 38 | + let request = new XMLHttpRequest() |
| 39 | + let printLog = (a, b) => console.log(JSON.stringify(a), b) |
| 40 | + request.open('GET', rootPath, true) |
| 41 | + request.addEventListener('progress', printLog) |
| 42 | + request.addEventListener('load', printLog) |
| 43 | + request.addEventListener('error', printLog) |
| 44 | + request.addEventListener('abort', printLog) |
| 45 | + request.responseType = 'arraybuffer' |
| 46 | + |
| 47 | + request.onload = () => { |
| 48 | + var audioData = request.response |
| 49 | + |
| 50 | + audioCtx.decodeAudioData(audioData, buffer => { |
| 51 | + console.log(buffer.length) |
| 52 | + let myBuffer = buffer |
| 53 | + |
| 54 | + let offlineCtx = new OfflineAudioContext(2, buffer.length, 44100) |
| 55 | + let source = offlineCtx.createBufferSource() |
| 56 | + source.buffer = myBuffer |
| 57 | + source.connect(offlineCtx.destination) |
| 58 | + source.start() |
| 59 | + //source.loop = true; |
| 60 | + |
| 61 | + offlineCtx |
| 62 | + .startRendering() |
| 63 | + .then(renderedBuffer => { |
| 64 | + console.log('Rendering completed successfully') |
| 65 | + |
| 66 | + var song = audioCtx.createBufferSource() |
| 67 | + song.buffer = renderedBuffer |
| 68 | + console.log(JSON.stringify(renderedBuffer)) |
| 69 | + |
| 70 | + song.connect(audioCtx.destination) |
| 71 | + song.connect(analyser) |
| 72 | + // // play.onclick = function() { |
| 73 | + song.playbackRate.value = 5 |
| 74 | + song.start() |
| 75 | + // } |
| 76 | + song.onended = function(e) { |
| 77 | + flag = false |
| 78 | + console.log('finished') |
| 79 | + } |
| 80 | + |
| 81 | + analyser.getByteFrequencyData(dataArray) |
| 82 | + console.log(Array.apply([], dataArray).join(",")); |
| 83 | + draw() |
| 84 | + }) |
| 85 | + .catch(err => { |
| 86 | + console.log('Rendering failed: ' + err) |
| 87 | + // Note: The promise should reject when startRendering is called a second time on an OfflineAudioContext |
| 88 | + }) |
| 89 | + }) |
8 | 90 | } |
| 91 | + request.send() |
9 | 92 | } |
10 | 93 |
|
11 | | -new p5(sketch, 'sketch') |
12 | | -console.log('sketch here') |
| 94 | +// Run getData to start the process off |
| 95 | + |
| 96 | +getData() |
| 97 | + |
| 98 | +let x= 0 |
| 99 | +function draw() { |
| 100 | + if(flag) requestAnimationFrame(draw) |
| 101 | + analyser.getByteFrequencyData(dataArray) |
| 102 | + |
| 103 | + |
| 104 | + // for(var index = bufferLength, y = 0; index > 0; --index, y += 8) |
| 105 | + for ( |
| 106 | + var index = 0, y = eightBufferLength; |
| 107 | + index < bufferLength; |
| 108 | + ++index, y -= 8 |
| 109 | + ) { |
| 110 | + imageDataFrame.data[y] = imageDataFrame.data[y + 1] = dataArray[index] |
| 111 | + // increment by 8, as to not interfere with the playhead (second coloumn of pixels at x=1) |
| 112 | + } |
| 113 | + |
| 114 | + canvasContext.putImageData(imageDataFrame, x, 0) |
| 115 | + |
| 116 | + if (x < WIDTH) { |
| 117 | + x++ |
| 118 | + } else { |
| 119 | + x = 0 |
| 120 | + } |
| 121 | +} |
0 commit comments