Go Back   Cockos Incorporated Forums > REAPER Forums > ReaScript, JSFX, REAPER Plug-in Extensions, Developer Forum

Reply
 
Thread Tools Display Modes
Old 06-23-2019, 04:13 AM   #1
GLD
Human being with feelings
 
Join Date: Jun 2019
Posts: 21
Default Please help me translate/migrate from JS/GAS to EEL2 for my JSFX plugin

Hello!

This is my first post here so apologies in advance if I am in the wrong place etc.

I have written a moving average algorithm that I would like to implement in JSFX.

I had it working in Google scripts (my coding abilities are pretty limited so I needed a more familiar platform to test and debug).

I tried to translate or migrate it to EEL2 using what I could glean from the online guides but unsurprisingly, it does not work.

I will post below both sets of code and I would really appreciate if someone could point me in the right direction!

Many thanks in advance,

GLD

GAS Code (working):
Code:
 function WorkingAve(){
  var ss = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet1"); //get sheet
  var values = ss.getDataRange().getValues(); //get data range
  var buffcount = 0; // should stay at samplesize when buffer is full
  var poscount = 0; // iterates through to samplesize and then resets
  var samplesize = 3; // sample size will be drop down menu
  var n = (samplesize-1)/2;//how much is either side of the middle
  var buff = 0;// buffer
  var pos = new Array(samplesize);// position array
  var out = 0; // output
  var prev = 0;// previous is the position of a sample to be subtracted from the buffer
  var Output = new Array(0); // output array
  var zeros = samplesize ;// counts consecutive zeros
  var sample = 0 ; // counts consecutive non-zero samples
  var logz = 0; //logger builder
  var minusn = 0;// gets the sample 'N' turns ago position - N
  for (i in values){//start loop through incoming values
  
    if (values[i] == 0){//if incoming value is a zero
      //incoming value is zero
      // increment zeros counter up to samplesize
      zeros = Math.min(((1*zeros) + 1),samplesize) ;
      // nested if to test if the buffer is empty
      // if the buffer is empty, reset the sample counter
      if ((zeros*1) == (samplesize)){
        //buffer is empty, reset sample counter
        sample = 0;
        //buffer is empty, reset pos counter
        poscount = 0;
      }else{
        //if incoming value is zero but buffer is not yet empty
        // increment sample counter up to sample size
        sample = Math.min(((1*sample) +1),samplesize) 
        if (poscount*1 == samplesize*1){
          // pos counter at maximum
          // reset pos counter to first position
          poscount = 1;
        } else {
          //pos counter not at maximum
          //increment pos counter
          poscount = +poscount+1;
        } // end pos counter increment
      } // end if buffer is empty
      
    }else{
      //incoming value is NOT zero
      //reset zeros counter
      zeros = 0 ;
      // increment sample counter up to sample size
      sample = Math.min(((1*sample) +1),samplesize) 
      //increment position counter for pos array
      // test if pos counter needs to be reset
      if (poscount*1 == samplesize*1){
      // pos counter at maximum
      // reset pos counter to  first position
      poscount = 1;
      } else {
      //pos counter not at maximum
      //increment pos counter
        poscount = +poscount+1;
      } // end pos counter increment
    }//end if incoming value is zero
    
    // Previous counter for finding previous values to take away from the buffer
  // if sample = samplesize then run the counter
    if (sample*1 == samplesize*1){
      //buffer is full
    // if position counter is at the end then previous to take is the first sample in the position array
   if (poscount*1 < samplesize*1){
    // position counter is less than sample size and not zero
    // set previous counter to the next position in the buffer
    prev = (poscount*1)+1; 
   }else{
     // position counter is not less than samplesize (and buffer is full)
     // set prev to first position
     prev = 1;
             }//end if previous counter vs position
    } else{
      //Buffer is not full
      // set previous to zero (no subtraction needed)
      prev = 0;
    }//end if for previous counter buffer test
    
    
    // minus n counter for finding the minus N sample for beginning of calculations
    // if sample is less than N+1
    if (sample*1 < (n*1)+1){
      //sample is less than N+1 middle value
      //reset counter
      minusn = 0;
    }else if (sample*1 < samplesize*1){ 
      // sample is less than sample size but bigger equal to or bigger than the middle value
      minusn = sample*1 - n*1;
    }else{
        minusn = 0;    
    }//End if for minus n counter
    
    
       
    //1. add current value to buffer or reset it to zero if sample is zero
    if (sample == 0){
      // sample counter is zero
      //buffer and position array should be empty
      //empty buffer
      buff = 0 ;
      //empty position array
      pos = new Array(samplesize);// position array reset
    }else { 
      // sample is NOT zero
      //4. if pos not zero then add to pos buffer ready for next loop
      // add incoming value to buffer
     buff = (buff*1) +(values[i]*1);
      // add incoming value to position array
     pos[1*poscount] = + values[i]*1;
      
    }// 1. end buffer add if sample not zero
     
    
    
      
    
    
    //3. depending on sample number, find the moving average
    // if sample number < N+1 (middle) output zero
    //if sample number < SS output pos - n
    // sample number = SS output moving average
    if (1*sample < (1*n)+1){
      // sample number is less than middle of sample size N+1
      // output zero
      out = 0;
    }else if (1*sample < (1*samplesize)){
    // sample number < SS 
    //output pos - n
      out = pos[minusn];
      }else { // sample size is sample size
        out = (1*buff)/(samplesize*1);
       // sample number = sample size
       // output moving average
      }//end if sample number < N+1
    
    //2. if previous not zero then subtract from buffer
    if (prev > 0){
      //previous counter is greater than zero
      //subtract previous position array value from buffer
    buff = + (1*buff) - (1*pos[prev*1]);
    }//end if previous not zero
        
       ss.getRange("firstcell").offset(i,0).setValue(1*out);   
    
    
  //LOGGER
    // clear log
    logz = "";
    // add incoming to log
  //logz =  "Incoming = "+parseInt(1*values[i]);  
    // add sample counter to log
  logz = logz +  "  Sample counter = "+sample; 
    // add zeros counter to log
  //logz = logz + "  Zeros Counter = "+zeros; 
    // add position counter to log
  logz = logz + "  Pos Counter = "+poscount; 
     // add previous counter to log
  logz = logz + "  Prev Counter = "+prev; 
         // add minus n counter to log
  logz = logz + "  Minus N Counter = "+minusn; 
  
    
     // add buffer value to log
  logz = logz + "  Buff = "+buff; 
      // add output value to log
  logz = logz + "  output = "+out; 
    
    // print to log
  Logger.log(logz) ;
  }//end values loop
  
}// end function Working Average ???????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????
GLD is offline   Reply With Quote
Old 06-23-2019, 04:13 AM   #2
GLD
Human being with feelings
 
Join Date: Jun 2019
Posts: 21
Default JSFX Code (Not Working)

EEL2 JSFX version:

Code:
  samplesize = slider1
    
	spl0 == 0?(//if incoming value is a zero
      //incoming value is zero
      // increment zeros counter up to samplesize
      zeros = min(((1*zeros) + 1),samplesize) ;
      // nested if to test if the buffer is empty
      // if the buffer is empty, reset the sample counter
      (zeros*1) == (samplesize)?(
        //buffer is empty, reset sample counter
        sample = 0;
        //buffer is empty, reset pos counter
        poscount = 0;
      ):(
        //if incoming value is zero but buffer is not yet empty
        // increment sample counter up to sample size
        sample = min(((1*sample) +1),samplesize) ;
        poscount*1 == samplesize*1?(
          // pos counter at maximum
          // reset pos counter to first position
          poscount = 1;
        ):(
          //pos counter not at maximum
          //increment pos counter
          poscount = +poscount+1;
         );// end pos counter increment
       ); // end if buffer is empty
      
    ):(
      //incoming value is NOT zero
      //reset zeros counter
      zeros = 0 ;
      // increment sample counter up to sample size
      sample = min(((1*sample) +1),samplesize) 
      //increment position counter for pos array
      // test if pos counter needs to be reset
      poscount*1 == samplesize*1?(
      // pos counter at maximum
      // reset pos counter to  first position
      poscount = 1;
      ):(
      //pos counter not at maximum
      //increment pos counter
        poscount = +poscount+1;
       );// end pos counter increment
    ); // end if incoming value is zero
    
    // Previous counter for finding previous values to take away from the buffer
  // if sample = samplesize then run the counter
    sample*1 == samplesize*1?(
      //buffer is full
    // if position counter is at the end then previous to take is the first sample in the position array
   poscount*1 < samplesize*1?(
    // position counter is less than sample size and not zero
    // set previous counter to the next position in the buffer
    prev = (poscount*1)+1; 
   ):(
     // position counter is not less than samplesize (and buffer is full)
     // set prev to first position
     prev = 1;
             ); // end if previous counter vs position
    ):(
      //Buffer is not full
      // set previous to zero (no subtraction needed)
      prev = 0;
    ); // end if for previous counter buffer test
    
    
    // minus n counter for finding the minus N sample for beginning of calculations
    // if sample is less than N+1
    sample*1 < (n*1)+1?(
      //sample is less than N+1 middle value
      //reset counter
      minusn = 0;
    ):( sample*1 < samplesize*1?( 
      // sample is less than sample size but bigger equal to or bigger than the middle value
      minusn = sample*1 - n*1;
    ):(
        minusn = 0;    
    ); // end if for minus n counter
    
    
       
    //1. add current value to buffer or reset it to zero if sample is zero
    sample == 0?(
      // sample counter is zero
      //buffer and position array should be empty
      //empty buffer
      buff = 0 ;
      //empty position array
      pos = new Array(samplesize);// position array reset
    ):( 
      // sample is NOT zero
      //4. if pos not zero then add to pos buffer ready for next loop
      // add incoming value to buffer
     buff = (buff*1) +(spl0*1);
      // add incoming value to position array
     pos[1*poscount] = + spl0*1;
      
    );// 1. end buffer add if sample not zero
     
    
    
      
    
    
    //3. depending on sample number, find the moving average
    // if sample number < N+1 (middle) spl0put zero
    //if sample number < SS spl0put pos - n
    // sample number = SS spl0put moving average
    1*sample < (1*n)+1?(
      // sample number is less than middle of sample size N+1
      // spl0put zero
      spl0 = 0;
    ):( 1*sample < (1*samplesize)?(
    // sample number < SS 
    //spl0put pos - n
      spl0 = pos[minusn];
      ):( // sample size is sample size
        spl0 = (1*buff)/(samplesize*1);
       // sample number = sample size
       // spl0put moving average
      ); // end if sample number < N+1
    
    //2. if previous not zero then subtract from buffer
    prev > 0?(
      //previous counter is greater than zero
      //subtract previous position array value from buffer
    buff = + (1*buff) - (1*pos[prev*1]);
    ); // end if previous not zero
        
       ss.getRange("firstcell").offset(i,0).setValue(1*spl0);
BTW - I put the slider info in '@init' and the rest in '@sample'

Could anyone please help me figure out where I am going wrong? Thank you!

Last edited by GLD; 06-23-2019 at 04:16 AM. Reason: Missing info
GLD is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -7. The time now is 06:53 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.