Different soundtrack for different modes

Hello!

I have this game, where I want a different soundtracks to play in the different modes.

int mode = 0;
int INTRO = 0;
int KASSE = 1;
int BUTIK = 2;
int STUDY = 3;

import processing.sound.*;
SoundFile file;

String musik = "musik.wav";
String path;

void setup() {
 path = sketchPath(musik);
  file = new SoundFile(this, path);
  file.play();
}

void draw (){
  
  if (mode== INTRO){  // introskærm
  doIntroMode();
  }
  else if (mode==KASSE){       // kassespil
  doKasseMode();
  }
  else if (mode ==BUTIK){      // i butikken
  doButikMode();
  }
  else if (mode ==STUDY){      // læse op
  doStudyMode();
  }

}

right now, the same song plays along no matter what

I have tried:

void setup(){
 if (mode == INTRO){
  path = sketchPath(musik);
  file = new SoundFile(this, path);
  file.play();
  }
  
  if (mode == KASSE){
  
  path = sketchPath(kasselyd);
  fil = new SoundFile(this, path);
  fil.play();
  }
  
  else{
    
  }
  
}

but it doesn’t work

hi @anna,
you can load 4 sound files at setup already,
and when change the mode stop all but the ONE
and start the ONE if not running.

import processing.sound.*;
SoundFile file0,file1,file2,file3;

String sfile0 = "data/0.aif"; //"data/musik.wav";
String sfile1 = "data/1.aif";
String sfile2 = "data/2.aif";
String sfile3 = "data/3.aif";

int mode = 0, INTRO = 0, KASSE = 1, BUTIK = 2, STUDY = 3;

void setup() {                          // load all 4 files first
  file0 = new SoundFile(this, sfile0);
  file1 = new SoundFile(this, sfile1);
  file2 = new SoundFile(this, sfile2);
  file3 = new SoundFile(this, sfile3);  
}

void draw () {
  if      (mode == INTRO)   doIntroMode();   // introskærm
  else if (mode == KASSE)   doKasseMode();   // kassespil
  else if (mode == BUTIK)   doButikMode();   // i butikken
  else if (mode == STUDY)   doStudyMode();   // læse op
}

void doIntroMode() {
  reset_others(0);
  if ( !file0.isPlaying() ) file0.loop();
}
void doKasseMode() {
  reset_others(1);
  if ( !file1.isPlaying() ) file1.loop();
}
void doButikMode() {
  reset_others(2);
  if ( !file2.isPlaying() ) file2.loop();
}
void doStudyMode() {
  reset_others(3);
  if ( !file3.isPlaying() ) file3.loop();
}

void reset_others(int nOT) {
  if ( nOT != 0 && file0.isPlaying() ) file0.stop();
  if ( nOT != 1 && file1.isPlaying() ) file1.stop();
  if ( nOT != 2 && file2.isPlaying() ) file2.stop();
  if ( nOT != 3 && file3.isPlaying() ) file3.stop();  
}

void keyPressed() {
  if ( key == '0' ) mode = 0; 
  if ( key == '1' ) mode = 1; 
  if ( key == '2' ) mode = 2; 
  if ( key == '3' ) mode = 3; 
  println("mode "+mode);
}

if more modes needed better use arrays
and also might go back and load the song files
while draw…

1 Like