dimanche 1 janvier 2017

Javascript error with if statement inside of a for loop/array insertion and construction

Backstory and Context: I will do my best to explain what I am working on, where the problem is that I have identified, and my thought process for my current code. Bear with me as I will try to provide as much detail as possible. Note that I am still learning Quartz Composer and am rather unfamiliar with Javascript syntax.

I am working with Quartz Composer to modify the visual equalizer template that comes with the program. Essentially, my audio feed gets input into the program where it is processed as 16 audio frequency bands that are stored in an array. Each of these 16 bands corresponds to the magnitude of the frequency in that range, and will ultimately display on a "bar graph" with the various equalizer levels. This part is all well and good.

My task requires that I have more than 16 bars in my graph. Since I cannot split the audio any further without turning to a somewhat complicated external process, I figured I could just fake my way through by inserting fake bar and values in between the actual audio bars that would average or evenly space themselves in between the true audio bar values.

For example, suppose that my input array looked as such "A B C" where "A," "B," and "C" are some audio value. I declare in my code a certain "insertion" value that determines how many fake bars are to be added in between each of my true audio values. In the case of "A B C," suppose that the insertion value were set to 0. No bars are to be inserted, thus the output array is "A B C." Suppose that the insertion value is set to 1. 1 bar is to be inserted in between each of the true values, returning an array of "A (A+B)/2 B (B+C)/2 C." As such, if the insertion value is set to 2, two values will be placed between A and B such that they are evenly spaced in between, aka 1/3 and 2/3rds of the way between A and B respectively.

Relevant Code:

var array = new Array();
function (__structure outputStructure) main (__structure inputStructure, __number insertions, __number time) {
        var result = new Object();
        
        /* check if the input array for the sound bands is null or not */ 
        if (inputStructure != null) {
                
                /* keep track of the number of times that a value is inserted so that, upon the counter being reset, a value from the original array can be inserted */
                var counter = 0;
                
                /* keep track of which index location the original array value is to be pulled from */
                var inputPlace = 0;
                
                /* build a new array that inserts a number of values equal to the value of the insertions variable in between all values of the original array */
                for (i=0; i<((insertions + 1)  * (inputStructure.length - 1) + 1); ++i) {
                        
                        /* if it is time to do so, pull a true audio bar value from the original string into the new string */
                        if (counter = 0) {
                                array[i] = inputStructure[inputPlace];
                                
                        /* if it is not time to pull a value from the original array, insert the specified number of evenly spaced, calculated bar values into the new array */
                        } else {        
                                /* space the insertion between the nearest two true values from the original input array */
                                array[i] = inputStructure[inputPlace] + ((inputStructure[inputPlace + 1] - inputStructure[inputPlace]) * counter / (insertions + 1));
                        }
                        
                        counter = counter + 1;

                        /* reset the counter after each complete iteration of insertions is compelte so that an original array value can be pulled into the new array */
                        if (counter > insertions) {
                                counter = 0;
                                inputPlace = inputPlace + 1;
                        }
                        
                        counter = counter + 1;
                        
                }       
        }
        
        result.outputStructure = array;
        return result;
}

In this particular code, I do not have any inputs manually declared, as they will all be pulled in from Quartz Composer. I have had no problems with this part. I assume for testing purposes, you could just hard code in values for the inputStructure, insertion, and time.

Problem: The problem that I have identified seems to be with the if statement inside of my for loop. When I run the code, it sets every value in the new array to 0.

Question: I guess my question is what am I doing wrong with the if statement in the for loop that is causing it to overwrite all of my other iteration values? I was able to get the counter working such that each iteration would return the counter value "0 1 2 0 1 2" in the case of the insertion being set to 2, but I was unable to say okay, now check during each iteration, and if the counter is 0, pull a value from the original array. Otherwise, do something else, in this case, calculate and add the fake bar value into the new array.

Aucun commentaire:

Enregistrer un commentaire