0

I'm unsure what the problem is here, but here is my code

var btotal = [];
var songarray = [];
function whileplaying() {
    var playingSound = soundManager.getSoundById('aSound');
    for (var i=0;i<8;i++) {
        var ttotal = 0;
        for (var n=0;n<32;n++) {
            var eblock = (i*32)+n;
            ttotal = ttotal+(playingSound.eqData.left[eblock]*100);
        }
        ttotal = ttotal/32;
        btotal[i] = ttotal;
    }
    console.log(btotal);
    songarray.push(btotal);
}

To explain, while the sound is playing I get an array with a length of 8, summing up the EQ values of that frequency block. At the end I append this array to songarray, so the theory is that songarray is an array containing the sound's EQ data in arrays.

The problem is that when I then get the value of songarray, it has however many arrays in it that I expected, but they are all identical, and are all what the last data point was, the latest value of btotal.

So this code would run, and console would display (for example):

[42.743750000000006, 2.98125, 0.10625000000000001, 0, 0, 0, 0, 0]
[38.859374999999986, 2.8, 0.09375, 0, 0, 0, 0, 0]
[56.26874999999998, 21.831250000000004, 3.853125, 0.340625, 0, 0, 0, 0]
[46.459374999999994, 19.584374999999998, 1.4, 0, 0, 0, 0, 0]
[38.08125, 11.8, 1.0750000000000002, 0, 0, 0, 0, 0] 

However the contents of song array goes like:

0: Array[8]
0: 38.08125
1: 11.8
2: 1.0750000000000002
3: 0
4: 0
5: 0
6: 0
7: 0
length: 8
__proto__: Array[0]
1: Array[8]
0: 38.08125
1: 11.8
2: 1.0750000000000002
3: 0
4: 0
5: 0
6: 0
7: 0
length: 8
__proto__: Array[0]

Which are all the contents of the latest btotal array. This leads me to believe that each entry in songarray merely points to btotal. So how do I make it so that each array in songarray is the value of btotal at the time I append it, and not merely a pointer?

2
  • Your playingSound variable is the exact same in every iteration as you set it outside the loop, and never change it. Did you expect it to magically change? Commented Dec 7, 2012 at 0:24
  • Sorry, I didn't explain that. What it returns is a sound instance in SoundManager, which data can be retrieved from. whileplaying() is called every 50ms Commented Dec 7, 2012 at 0:45

2 Answers 2

1

Define btotal within the scope of the function:

var songarray = [];
function whileplaying() {
    var btotal = [];
    var playingSound = soundManager.getSoundById('aSound');
    for (var i=0;i<8;i++) {
        var ttotal = 0;
        for (var n=0;n<32;n++) {
            var eblock = (i*32)+n;
            ttotal = ttotal+(playingSound.eqData.left[eblock]*100);
        }
        ttotal = ttotal/32;
        btotal[i] = ttotal;
    }
    console.log(btotal);
    songarray.push(btotal);
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much, worked perfectly. I'm still fairly new to javascript, intricacies of some things are lost on me.
1

You probably want to copy the array.

Instead of

songarray.push(btotal);

use

songarray.push(btotal.slice(0));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.