Problem with RMS Analysis [solved]

edited May 2017 in Library Questions

Hio. I'm currently working on an audio visualisation, i had the code running, but since i had time i wanted to implement a GUI (using G4P). Now with the GUI the analysis doesn't seem to work anymore. Here's the relevant code:

import g4p_controls.*;
import processing.sound.*;    

GButton buttonSong, buttonGo;
SoundFile soundfile;
Amplitude rms;

boolean playing, selected = false;
float sum;
int map, frames;

void analyzeSetup(){
    rms = new Amplitude(this);  
    }

void analyzeDraw(){
     if(frameCount%60 == 0 && playing == true){

      sum += (rms.analyze() - sum) * 0.75;
      map = (int) map(sum, 0, 0.5, 0, 180);

      frames = frames+1;
      println(sum," - ",frames," - ",map);
    }
}

void selectSong(){  
  buttonSong = new GButton(this, 180, 20, 25, 25);
  buttonSong.setText("...");
  buttonSong.addEventHandler(this, "openSong");
 }

public void openSong(GButton source, GEvent event) { 
  selectInput("Please select an .mp3", "pathToString");
}

void pathToString(File selection){
  song = selection.getPath(); 
  selected = true;
  soundfile = new SoundFile(this, song);
  rms.input(soundfile);
}

void startProgram(){
  buttonGo = new GButton(this, 210, 430, 80, 50);
  buttonGo.setText("Go!");
  buttonGo.addEventHandler(this, "Go");
}

public void Go(GButton source, GEvent event) { 
  if(selected == true){
    soundfile.play();
    playing = true;
  }
}

analyzeSetup(), selectSong() and startProgram() are in the main setup()-method, analyzeDraw() is in the main draw()-Method. The problem is that the song starts playing when clicking the Go-Button, but it doesn't seem to get analyzed, since sum and map constantly stay 0. My guess is to relocate the assignment of the soundfile and the rms.input. i've already played around a lot, but nothing worked. i hope someone can help me here.

thanks in advance. best regards, raiu.

Answers

  • Answer ✓

    found the issue myself. apparently the soundfile has to play before you assign the input to the rms. so i simply moved the rms.input(soundfile) to the "Go" function.

  • @raiu Great you solved it. Thanks for sharing your solution.

    Kf

Sign In or Register to comment.