javaScript question

kataStatik's picture

I've been wrestling with this for a few days, and you guys seemed smart so I figured I'd ask. I have the audio input object going into the audio spectrum object, I have that structure split, I'm using the strrcuture index member objects to get the indexes I want. All of that is fine. Here's my problem: I'm trying to find out what value out of 10 frequency bands is the largest. I'm trying to use the javascript object for this. It's not working for a few reasons (could be I need to convert the floating point values to strings plus I'm not iterating properly to contstantly update as well as I'm not using if (!_testMode){} properly, but I'm not sure how to solve this problem. Here's my javascript. I don't get an error in te debugger, but I'm not getting a value other than zero returned either

function (__virtual outputNumber) main (__virtual inputNumber[10])
{
 
var result = new Object();
if (!_testMode){
function findmax(inputNumber){
var most = 0;
inputNumber.length = 10;
var a = inputNumber.length;
 
for (counter=0;counter<a;counter++)
   {
          if (inputNumber[counter] > most)
          {
              most = inputNumber[counter];
 
          }
   }
   result.outputNumber = most;   
   }
   return result;
   }
}

kataStatik's picture
Re: javaScript question

Anyone?

Achim Breidenbach's picture
Re: javaScript question

In your java Script you are defining a function "findmax()" but you never call it. My suggestion would be to remove the lines "function findmax(inputNumber){" and the corresponding closing bracket "}" because you don't need to define this function anyways. Also I would suggest to use the types for your inputs and outputs rather than "virtual", this gives QC a better clue what to connect to your JavaScript. Lastly you don't have to define the count of your array "inputNumber[10]" because this will give you 10 inputs rather than just one array.

This is my code suggestion:

function (__number outputNumber) main (__structure inputNumberArray)
{
   var result = new Object();
   var most = 0;
 
   if (!_testMode)
   {
      var a = inputNumberArray.length;
 
      for (counter=0; counter<a; counter++)
      {
         if (inputNumberArray[counter] > most)
         {
            most = inputNumberArray[counter];
         }
      }
   }
 
   result.outputNumber = most;   
   return result;
}

kataStatik's picture
Re: javaScript question

Achim - thank you for your response - this is definitely a step in the right direction. It is deeply appreciated. Here's a followup question:

The audio input object structure has 15 members. I'm only interested in a select 10 members out of the 15 (2,3,4,5,6,7,9,11,13,15) I was using the "structure index member" object to cherry pick those members - that's why I originally was looking at having 10 inputs to the javascript object.

Out of those 10 members, I want to know which of the 10 in an any instant has the most power. (I don't really need to know how much power the member has, just which member has it) Then I want to use that information to toggle (enable) an image on or off. I only want one image on at a time. (I have 10 images that are the same size occupying the same space. )

I've modified you're suggestion in the following way to allow me to know which index is being used - I'm starting the count from 1 instead of zero bc the zeron index was giving me a value of 1 when sending audio from MAX/MSP.

So, this is working more or less. Is there a smarter way to display a single image from a group of 10 images than using 10 images and 10 billboards and just "enabling" the billboard with the image I want based on the power of that audio index member? Thanks again!

function (__number outputNumber) main (__structure inputNumberArray) { var result = new Object(); var most = 0; var which = 0;

if (!_testMode) { var a = inputNumberArray.length;

  for (counter=1; counter<a; counter++)
  {
     if (inputNumberArray[counter] > most)
     {
        most = inputNumberArray[counter];
        which = counter;
     }
  }

}

result.outputNumber = which;
return result; }

kataStatik's picture
Re: javaScript question

I found directory scanner and image importer, so this is starting to get interesting!