Hi, I was trying to rewrite some code and then I started getting an error
Uncaught TypeError: Cannot read property 'decodeAudioData' of undefined at FileReader.reader.onload (lvl.js:131)
on this line
this.context.decodeAudioData(fileEvent.target.result, calcTempo);
How I can fix it?
function Lvl(song){
this.Engine = Matter.Engine;
this.World = Matter.World;
this.Bodies = Matter.Bodies;
this.engine;
this.world;
//physObj
this.walls = [];
this.player;
this.snake = undefined;
//par for obj
this.playerRadius = 45;
//array for draw
this.drawebleObjects = [];
this.screenSize = createVector(windowWidth,windowHeight);
this.canvas;
this.bar;
this.isStarted = false;
this.ground;
this.up;
this.left;
this.right;
this.root;
this.loadingTicks;
this.create = function(){
this.root = document.documentElement;
this.engine = this.Engine.create();
this.world = this.engine.world;
this.GetMt();
this.ground = new Rect(createVector(windowWidth/2,windowHeight),windowWidth,5,0,this.engine,this.Bodies,this.world,this.World,{isStatic: true});
this.up = new Rect(createVector(windowWidth/2,0),windowWidth,5,0,this.engine,this.Bodies,this.world,this.World,{isStatic: true});
this.left = new Rect(createVector(0,windowHeight/2),5,windowHeight,0,this.engine,this.Bodies,this.world,this.World,{isStatic: true});
this.right = new Rect(createVector(windowWidth,windowHeight/2),5,windowHeight,0,this.engine,this.Bodies,this.world,this.World,{isStatic: true});
this.bar = new BonusBar(createVector(0,0),color(153,102,204),10,1);
this.Engine.run(this.engine);
}
this.show = function(){
background(28);
if(this.isStarted){
if(this.player == undefined){
this.player = new Krug(createVector(this.screenSize.x/2,100),this.playerRadius,0,{frictionAir: 0,friction:0,restitution:1,inertia: Infinity,isStatic:false});
this.snake = new Snake();
this.snake.setup();
}
if(this.bar._progress<5){
this.snake.setEasing();
}
noStroke();
this.player.show();
this.snake.show();
this.bar.show();
noStroke();
if(mouseIsPressed){
if(mouseButton == CENTER){
this.snake.createSegment(this.snake.x[this.snake.x.length-1],this.snake.y[this.snake.y.length-1]);
}
else if(mouseButton == LEFT){
if(this.snake!=undefined){
if(this.bar._progress>=100)
{
this.bar.rst();
this.snake.easing=0.5;
}
}
}
}
textAlign(BOTTOM,CENTER);
fill(255);
textSize(20);
text('BPM: '+mt.tempo,50,50);
}
else{
if(this.loadingTicks<6) this.loadingTicks++;
else this.loadingTicks = 0;
let strL = '';
for(let i = 0;i<this.loadingTicks;i++){
strL=strL+'.';
}
textAlign(LEFT, CENTER);
fill(220);
textSize(32);
text('LOADING '+strL, windowWidth/2-((7*32)/2), windowHeight/2);
}
}
//get bpm
this.context = new AudioContext();
this.mt;
this.GetMt = function(){
async function pasteAudio() {
var blob = await (await fetch('scripts/testSound.mp3')).blob();
dt = new DataTransfer();
dt.items.add(new File([blob], 'scripts/testSound.mp3', {type: 'audio/mpeg'}));
var files = dt.files;
if (files.length == 0) return;
var reader = new FileReader();
reader.onload = function(fileEvent) {
this.context.decodeAudioData(fileEvent.target.result, calcTempo);
}
reader.readAsArrayBuffer(files[0]);
}
pasteAudio();
var calcTempo = function (buffer) {
var audioData = [];
// Take the average of the two channels
if (buffer.numberOfChannels == 2) {
var channel1Data = buffer.getChannelData(0);
var channel2Data = buffer.getChannelData(1);
var length = channel1Data.length;
for (var i = 0; i < length; i++) {
audioData[i] = (channel1Data[i] + channel2Data[i]) / 2;
}
} else {
audioData = buffer.getChannelData(0);
}
this.mt = new MusicTempo(audioData);
let wave1Spd = map(mt.tempo,0,350,1,25);
let wave2Spd = map(mt.tempo,0,350,1,20);
let wave3Spd = map(mt.tempo,0,350,1,15);
root.style.setProperty('--spd', wave1Spd+"s");
root.style.setProperty('--spd2', wave2Spd+"s");
root.style.setProperty('--spd3', wave3Spd+"s");
console.log(mt.tempo);
this.isStarted = true;
this.song.play();
/*const audio = new Audio()
audio.src = 'scripts/testSound.mp3'
audio.play();
textAlign(CENTER);
textSize(32);
fill(200);
text('bpm~ '+mt.tempo, windowWidth/2, windowHeight/2);*/
}
return true;
}
}
thanks for reading)