# Help with sound analysis code

**URL:** <https://discourse.processing.org/t/help-with-sound-analysis-code/34113>\
**Category:** Libraries\
**Tags:** homework\
**Created:** [December 14, 2021, 5:49pm UTC](https://discourse.processing.org/t/help-with-sound-analysis-code/34113 "2021-12-14T17:49:08Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![nata4596](https://avatars.discourse-cdn.com/v4/letter/n/3da27b/32.png) [@nata4596](https://discourse.processing.org/u/nata4596)\
**Post date:** [December 14, 2021, 5:49pm UTC](https://discourse.processing.org/t/help-with-sound-analysis-code/34113/1 "2021-12-14T17:49:09Z")

</div>

I need help with this code. This is a program that measures rythm, it works fine. But i need that people lose or win when they reach the bpms of the song.  
In the last if i tried to do that but it only ends up in people losing. I don´t know what im doing wrong. Pls help

import processing.sound.\*;  
import javax.swing.JOptionPane;

AudioIn sample;  
BeatDetector beatDetector;  
SoundFile soundfile;  
int c=0;  
boolean interruptor = true;  
int tiempoAnterior=0;  
int periodo = 0;  
float bpm, contadorbpm =0,totalbpm=0, duracion=0;  
String[] canciones = {“blackbox.wav”, “tohipto.wav”, “pumpup.wav”, “babyo.mp3”, “ingobernable.mp3”, “ithinki.mp3”, “onemoretime.mp3”, “woman.mp3”, “sexonfire.mp3”, “elcondor.mp3”, “cenizas.mp3”, “unaaventura.mp3”};  
int[] bpms ={118, 155, 125, 103, 105, 128, 122, 108, 153, 88, 77, 93};  
int cual =7;

PImage fondo;  
PImage gana;  
PImage pierde;

void setup() {  
size(940, 788);  
background(255);  
fondo = loadImage (“fondo.png”);  
gana= loadImage (“gana.png”);  
pierde = loadImage (“pierde.png”);

sample = new AudioIn(this, 0);  
sample.start();  
soundfile = new SoundFile(this, canciones[cual]);  
soundfile.play();  
duracion = soundfile.duration();

beatDetector = new BeatDetector(this);  
beatDetector.input(sample);

// DETERMINA LA SENSIBILIDAD ANTES DE ESCUCHAR EL SIGUIENTE BEAT  
beatDetector.sensitivity(160);  
}

void draw() {

background(fondo);

// SI SE DETECTA EL BEAT ENCENDER A¡LA INDICACIÓN  
if (beatDetector.isBeat()) {

```
if (interruptor == true) {
  c++;
  // println(c);
  periodo = millis() - tiempoAnterior;
  tiempoAnterior = millis();
  println(60000/periodo);
  bpm = 60000/periodo;
  contadorbpm= bpm+contadorbpm;
  float playbackSpeed= map(bpm, 0, bpms[cual], 0, 1.0);
  soundfile.rate(playbackSpeed);
  fill(255);
}
interruptor = false;

```

}

else {  
interruptor = true;  
fill(0);  
}  
totalbpm=contadorbpm/c;  
if (!soundfile.isPlaying()) {  
if(c \<= 108){  
image(gana,200,300);  
} else {  
image(pierde,150,300);  
}  
}

stroke(0);  
strokeWeight(8);  
rect(40, 40, 80, 80);

fill(0);  
textAlign(LEFT, TOP);  
text(bpm, 40, 20);  
}

I need help with this code. This is a program that measures rythm, it works fine. But i need that people lose or win when they reach the bpms of the song.  
In the last if i tried to do that but it only ends up in people losing. I don´t know what im doing wrong. Pls help
