# Different soundtrack for different modes

**URL:** <https://discourse.processing.org/t/different-soundtrack-for-different-modes/11172>\
**Category:** Libraries\
**Created:** [May 12, 2019, 2:54pm UTC](https://discourse.processing.org/t/different-soundtrack-for-different-modes/11172 "2019-05-12T14:54:11Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![anna](https://avatars.discourse-cdn.com/v4/letter/a/779978/32.png) [@anna](https://discourse.processing.org/u/anna)\
**Post date:** [May 12, 2019, 2:54pm UTC](https://discourse.processing.org/t/different-soundtrack-for-different-modes/11172/1 "2019-05-12T14:54:11Z")

</div>

Hello!

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

```auto
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

---

<div class="post-metadata">

**Author:** ![anna](https://avatars.discourse-cdn.com/v4/letter/a/779978/32.png) [@anna](https://discourse.processing.org/u/anna)\
**Post date:** [May 12, 2019, 8:33pm UTC](https://discourse.processing.org/t/different-soundtrack-for-different-modes/11172/2 "2019-05-12T20:33:39Z")

</div>

I have tried:

```auto
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

---

<div class="post-metadata">

**Author:** ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)\
**Post date:** [May 13, 2019, 5:08am UTC](https://discourse.processing.org/t/different-soundtrack-for-different-modes/11172/3 "2019-05-13T05:08:58Z")

</div>

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.

```auto
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…
