# Sound Code in void draw makes me problem

**URL:** <https://discourse.processing.org/t/sound-code-in-void-draw-makes-me-problem/3538>\
**Category:** Libraries\
**Created:** [September 14, 2018, 6:12pm UTC](https://discourse.processing.org/t/sound-code-in-void-draw-makes-me-problem/3538 "2018-09-14T18:12:22Z")\
**Posts on this page:** 19\
**Page:** 1

<div class="post-metadata">

**Author:** ![TheKillerHD](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/thekillerhd/32/1527_2.png) [@TheKillerHD](https://discourse.processing.org/u/TheKillerHD)\
**Post date:** [September 14, 2018, 6:12pm UTC](https://discourse.processing.org/t/sound-code-in-void-draw-makes-me-problem/3538/1 "2018-09-14T18:12:22Z")

</div>

I want the sound to be played when the ball touches the player. At the moment I have the sound in void draw and that makes me problems because messages appear in the console and then the game blocks.  
Can I somehow write it in void setup so that the sound plays when the player touches the ball?

```auto
void draw(){
  background(bg);
  rect(spieler_x, spieler_y, 20, 100);
  rect(ball_x, ball_y, 10 ,10);
  
  //Blur-Effekt
  for(int i = (blur_anzahl - 1); i > 0; i--){
    fill(255, 0, 0, 255/(i*2));
    positions_array[i][0] = positions_array[i-1][0];
    positions_array[i][1] = positions_array[i-1][1];
    rect(positions_array[i][0], positions_array[i][1], 10 ,10);
  }
  positions_array[0][0] = ball_x;
  positions_array[0][1] = ball_y;
  fill(200, 0, 0, 255);
 
  
  if(keyPressed){
    if(keyCode == DOWN){
      if(spieler_y < 650){
        spieler_y = spieler_y + 5;
      }
    }
    if(keyCode == UP){
      if(spieler_y > 50){  
        spieler_y = spieler_y - 5;
      }
    }
  }
  ball_x = ball_x + ball_geschwindigkeit_x;
  ball_y = ball_y + ball_geschwindigkeit_y;
  
  if(ball_x < 30){
    if(ball_y < (spieler_y + 55) && ball_y > (spieler_y - 55)){
      file = new SoundFile(this, "plop.mp3"); //Sound
      file.play();
      ball_geschwindigkeit_x = (-ball_geschwindigkeit_x) + 1;
      ball_geschwindigkeit_y = ball_geschwindigkeit_y - (spieler_y - ball_y) * 0.1;
      runde = runde + 1;
    }else{
       file = new SoundFile(this, "game_over.mp3"); //Sound
       file.play();

```

---

<div class="post-metadata">

**Author:** ![jb4x](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jb4x/32/789_2.png) [@jb4x](https://discourse.processing.org/u/jb4x)\
**Post date:** [September 14, 2018, 6:49pm UTC](https://discourse.processing.org/t/sound-code-in-void-draw-makes-me-problem/3538/2 "2018-09-14T18:49:55Z")

</div>

Hi TheKillerHD,

Please avoid posting picture of your code.

Instead copy/paste your code here, select it and hit the code format button `</>`

Thanks 🙂

---

<div class="post-metadata">

**Author:** ![TheKillerHD](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/thekillerhd/32/1527_2.png) [@TheKillerHD](https://discourse.processing.org/u/TheKillerHD)\
**Post date:** [September 14, 2018, 7:06pm UTC](https://discourse.processing.org/t/sound-code-in-void-draw-makes-me-problem/3538/3 "2018-09-14T19:06:48Z")

</div>

Okay I changed it now. But can you help me with this problem?

---

<div class="post-metadata">

**Author:** ![jb4x](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jb4x/32/789_2.png) [@jb4x](https://discourse.processing.org/u/jb4x)\
**Post date:** [September 14, 2018, 7:09pm UTC](https://discourse.processing.org/t/sound-code-in-void-draw-makes-me-problem/3538/4 "2018-09-14T19:09:28Z")

</div>

You want to load your sound in the `setup()` function: that’s made for it.

What you are currently doing right now is loading your song over and over every time you want to play it.

Instead create a global variable, load it in setup and play it when needed.

---

<div class="post-metadata">

**Author:** ![TheKillerHD](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/thekillerhd/32/1527_2.png) [@TheKillerHD](https://discourse.processing.org/u/TheKillerHD)\
**Post date:** [September 14, 2018, 7:25pm UTC](https://discourse.processing.org/t/sound-code-in-void-draw-makes-me-problem/3538/5 "2018-09-14T19:25:08Z")

</div>

I don’t understand how I can do this. Can you give me pls an example how I can do it? I have reuploaded my post with the command because I have forgot that I have two sounds.

---

<div class="post-metadata">

**Author:** ![jb4x](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jb4x/32/789_2.png) [@jb4x](https://discourse.processing.org/u/jb4x)\
**Post date:** [September 14, 2018, 7:35pm UTC](https://discourse.processing.org/t/sound-code-in-void-draw-makes-me-problem/3538/6 "2018-09-14T19:35:30Z")

</div>

You can open the following example in processing:

![ProcSoundEx](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/1/199878f19ecf37d7ed85dd84dbb4bb9ae5bd2053.png)

It will give you an idea on how it works.

Then you can replace the example code with the following:

```auto
import processing.sound.*;

SoundFile soundfile;

void setup() {
  size(640, 360);
  background(255);

  //Load a soundfile
  soundfile = new SoundFile(this, "vibraphon.aiff");
}

void draw() {
}

void mousePressed() {
  if (soundfile.isPlaying() == 1) {
    soundfile.stop();
  } else {
    soundfile.play();
  }
}

```

What it will do is play and stop the sound alternatively every time you hit the mouse.

---

<div class="post-metadata">

**Author:** ![TheKillerHD](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/thekillerhd/32/1527_2.png) [@TheKillerHD](https://discourse.processing.org/u/TheKillerHD)\
**Post date:** [September 14, 2018, 7:41pm UTC](https://discourse.processing.org/t/sound-code-in-void-draw-makes-me-problem/3538/7 "2018-09-14T19:41:46Z")

</div>

And how can I do this with two sounds?

---

<div class="post-metadata">

**Author:** ![jb4x](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jb4x/32/789_2.png) [@jb4x](https://discourse.processing.org/u/jb4x)\
**Post date:** [September 14, 2018, 7:43pm UTC](https://discourse.processing.org/t/sound-code-in-void-draw-makes-me-problem/3538/8 "2018-09-14T19:43:46Z")

</div>

Just declare 2 variables like you would do for any other kind of variables:

```auto
import processing.sound.*;

SoundFile soundfile1;
SoundFile soundfile2;

void setup() {
  ...
  soundfile1 = new SoundFile(this, "file1.aiff");
  soundfile2 = new SoundFile(this, "file2.aiff");
}

```

---

<div class="post-metadata">

**Author:** ![TheKillerHD](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/thekillerhd/32/1527_2.png) [@TheKillerHD](https://discourse.processing.org/u/TheKillerHD)\
**Post date:** [September 14, 2018, 7:56pm UTC](https://discourse.processing.org/t/sound-code-in-void-draw-makes-me-problem/3538/9 "2018-09-14T19:56:22Z")

</div>

Now the game don’t block, but I became the same message in the console and I don’t know how I can’t put the soundfile1.play(); and the soundfile2.play(); in the void draw.

```auto
void setup(){
  positions_array = new float [blur_anzahl][2];
  spieler_x = 20;
  spieler_y = 353;
  ball_x = 450;
  ball_y = 350;
  ball_geschwindigkeit_x = - 4;
  ball_geschwindigkeit_y = 0;
  runde = 0;
  size (900, 700);
  rectMode(CENTER);
  bg = loadImage("pong_bg.png"); //Hintergrundbild
  noStroke();
  schrift = loadFont("burbank30.vlw");
  textFont(schrift);
  
  //Sound
  soundfile1 = new SoundFile(this, "plop.mp3");
  soundfile2 = new SoundFile(this, "game_over.mp3");
}

void draw(){
  background(bg);
  rect(spieler_x, spieler_y, 20, 100);
  rect(ball_x, ball_y, 10 ,10);
  
  //Blur-Effekt
  for(int i = (blur_anzahl - 1); i > 0; i--){
    fill(255, 0, 0, 255/(i*2));
    positions_array[i][0] = positions_array[i-1][0];
    positions_array[i][1] = positions_array[i-1][1];
    rect(positions_array[i][0], positions_array[i][1], 10 ,10);
  }
  positions_array[0][0] = ball_x;
  positions_array[0][1] = ball_y;
  fill(200, 0, 0, 255);
 
  
  if(keyPressed){
    if(keyCode == DOWN){
      if(spieler_y < 650){
        spieler_y = spieler_y + 5;
      }
    }
    if(keyCode == UP){
      if(spieler_y > 50){  
        spieler_y = spieler_y - 5;
      }
    }
  }
  ball_x = ball_x + ball_geschwindigkeit_x;
  ball_y = ball_y + ball_geschwindigkeit_y;
  
  //This is when then ball touches the player
  if(ball_x < 30){
    if(ball_y < (spieler_y + 55) && ball_y > (spieler_y - 55)){
      soundfile1.play(); //Sound
      ball_geschwindigkeit_x = (-ball_geschwindigkeit_x) + 1;
      ball_geschwindigkeit_y = ball_geschwindigkeit_y - (spieler_y - ball_y) * 0.1;
      runde = runde + 1;
    }else{
       soundfile2.play(); //Sound
       ball_x = 450;
       ball_y = 350;
       ball_geschwindigkeit_x = - 4;
       ball_geschwindigkeit_y = 0;
       runde = 0;
       spieler_y = 353;

```

---

<div class="post-metadata">

**Author:** ![jb4x](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jb4x/32/789_2.png) [@jb4x](https://discourse.processing.org/u/jb4x)\
**Post date:** [September 14, 2018, 7:59pm UTC](https://discourse.processing.org/t/sound-code-in-void-draw-makes-me-problem/3538/10 "2018-09-14T19:59:06Z")

</div>

Can you tell us what is the error message?

---

<div class="post-metadata">

**Author:** ![TheKillerHD](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/thekillerhd/32/1527_2.png) [@TheKillerHD](https://discourse.processing.org/u/TheKillerHD)\
**Post date:** [September 14, 2018, 8:00pm UTC](https://discourse.processing.org/t/sound-code-in-void-draw-makes-me-problem/3538/11 "2018-09-14T20:00:24Z")

</div>

This is the message: Sep 14, 2018 9:50:38 PM com.jsyn.devices.javasound.JavaSoundAudioDevice   
INFORMATION: JSyn: default output latency set to 80 msec for Windows 10  
Sep 14, 2018 9:50:38 PM com.jsyn.engine.SynthesisEngine start  
INFORMATION: Pure Java JSyn from [www.softsynth.com](http://www.softsynth.com), rate = 44100, RT, V16.8.0 (build 463, 2017-10-16)  
Sep 14, 2018 9:50:50 PM com.jsyn.engine.SynthesisEngine$EngineThread run  
INFORMATION: JSyn synthesis thread in finally code.

---

<div class="post-metadata">

**Author:** ![EnhancedLoop7](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/enhancedloop7/32/1040_2.png) [@EnhancedLoop7](https://discourse.processing.org/u/EnhancedLoop7)\
**Post date:** [September 14, 2018, 10:35pm UTC](https://discourse.processing.org/t/sound-code-in-void-draw-makes-me-problem/3538/12 "2018-09-14T22:35:08Z")

</div>

Is there still a problem with the Processing Sound library? If so, I would recommend using the old minim library. Should work well 😃

EnhancedLoop7

---

<div class="post-metadata">

**Author:** ![TheKillerHD](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/thekillerhd/32/1527_2.png) [@TheKillerHD](https://discourse.processing.org/u/TheKillerHD)\
**Post date:** [September 15, 2018, 9:13am UTC](https://discourse.processing.org/t/sound-code-in-void-draw-makes-me-problem/3538/13 "2018-09-15T09:13:42Z")

</div>

Okay I try, let’s hope it works.

---

<div class="post-metadata">

**Author:** ![TheKillerHD](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/thekillerhd/32/1527_2.png) [@TheKillerHD](https://discourse.processing.org/u/TheKillerHD)\
**Post date:** [September 15, 2018, 9:20am UTC](https://discourse.processing.org/t/sound-code-in-void-draw-makes-me-problem/3538/14 "2018-09-15T09:20:05Z")

</div>

Minim doesn’t work because I haven’t the old processing verison. I have the 3.4 processing version and that’s the problem why I can’t use the minim library.

**The message in the console comes now only if I click on the playbutton.**

---

<div class="post-metadata">

**Author:** ![jb4x](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jb4x/32/789_2.png) [@jb4x](https://discourse.processing.org/u/jb4x)\
**Post date:** [September 15, 2018, 10:25am UTC](https://discourse.processing.org/t/sound-code-in-void-draw-makes-me-problem/3538/15 "2018-09-15T10:25:27Z")

</div>

Maybe @kevinstadler can help, he is updating the sound library.

---

<div class="post-metadata">

**Author:** ![kevinstadler](https://avatars.discourse-cdn.com/v4/letter/k/bc79bd/32.png) [@kevinstadler](https://discourse.processing.org/u/kevinstadler)\
**Post date:** [September 16, 2018, 4:50am UTC](https://discourse.processing.org/t/sound-code-in-void-draw-makes-me-problem/3538/16 "2018-09-16T04:50:04Z")

</div>

@TheKillerHD

> [@](#):
>
> This is the message: Sep 14, 2018 9:50:38 PM com.jsyn.devices.javasound.JavaSoundAudioDevice  
> INFORMATION: JSyn: default output latency set to 80 msec for Windows 10  
> Sep 14, 2018 9:50:38 PM com.jsyn.engine.SynthesisEngine start  
> INFORMATION: Pure Java JSyn from [www.softsynth.com](http://www.softsynth.com), rate = 44100, RT, V16.8.0 (build 463, 2017-10-16)  
> Sep 14, 2018 9:50:50 PM com.jsyn.engine.SynthesisEngine$EngineThread run  
> INFORMATION: JSyn synthesis thread in finally code.

This is not an error message, just information about the sound library loading, you can ignore it (I’ll try to suppress the message in later versions of the library).

Is there actually anything that’s not working about the sound playback, except for the fact that it prints this message?

---

<div class="post-metadata">

**Author:** ![kevinstadler](https://avatars.discourse-cdn.com/v4/letter/k/bc79bd/32.png) [@kevinstadler](https://discourse.processing.org/u/kevinstadler)\
**Post date:** [September 16, 2018, 4:52am UTC](https://discourse.processing.org/t/sound-code-in-void-draw-makes-me-problem/3538/17 "2018-09-16T04:52:12Z")

</div>

> [@jb4x](#):
>
> What you are currently doing right now is loading your song over and over every time you want to play it.

@jb4x people repeatedly loading the same sound file in `draw()` shouldn’t actually be a problem, the new Sound library first checks if it has already loaded a file from the same path and simply reuses the same one from memory!

---

<div class="post-metadata">

**Author:** ![jb4x](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jb4x/32/789_2.png) [@jb4x](https://discourse.processing.org/u/jb4x)\
**Post date:** [September 16, 2018, 5:39am UTC](https://discourse.processing.org/t/sound-code-in-void-draw-makes-me-problem/3538/18 "2018-09-16T05:39:04Z")

</div>

Hoo, I didn’t know about that one! Thanks for clarifying 🙂

Still, I prefer to load everything once for clarity in the code.

What is strange is that doing so apparently changed the behavior of his sketch:

> [@TheKillerHD](#):
>
> Now the game don’t block, but I became the same message in the console and I don’t know how I can’t put the soundfile1.play(); and the soundfile2.play(); in the void draw.

---

<div class="post-metadata">

**Author:** ![TheKillerHD](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/thekillerhd/32/1527_2.png) [@TheKillerHD](https://discourse.processing.org/u/TheKillerHD)\
**Post date:** [September 16, 2018, 8:23am UTC](https://discourse.processing.org/t/sound-code-in-void-draw-makes-me-problem/3538/19 "2018-09-16T08:23:58Z")

</div>

No, everything works and I’m very grateful that you helped me so well with the problem.
