# Minim: sound cracks on Pan and Gain

**URL:** <https://discourse.processing.org/t/minim-sound-cracks-on-pan-and-gain/11102>\
**Category:** Libraries\
**Created:** [May 10, 2019, 9:55am UTC](https://discourse.processing.org/t/minim-sound-cracks-on-pan-and-gain/11102 "2019-05-10T09:55:41Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![morisil](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/morisil/32/6166_2.png) [@morisil](https://discourse.processing.org/u/morisil)\
**Post date:** [May 10, 2019, 9:55am UTC](https://discourse.processing.org/t/minim-sound-cracks-on-pan-and-gain/11102/1 "2019-05-10T09:55:41Z")

</div>

I am having problem with controlling the playback of my audio files. The sound is somehow slightly chopped when pan and gain values are adjusted, especially if it is happening rapidly. Here is the code to reproduce the issue:

```auto
/*
 * Single file is played in the loop, when mouse pressed:
 * - pan control: horizontally mouse movement
 * - gain control: vertical mouse movement
 *
 * On Ubuntu Linux I can hear cracking sound when these values are changed rapidly.
 */

import ddf.minim.*;
import ddf.minim.ugens.*;

static final String SOUND_LOOP = "https://sampleswap.org/samples-ghost/LOOPING%20AMBIENCE/1648[kb]etheral-chord.wav.mp3";
static final float NON_AUDIBLE_GAIN = -100;

Minim minim;
AudioOutput out;
FilePlayer loopSound;
Pan pan;
Gain gain;

float panValue;
float gainValue;

void setup() {
  size(800, 600);
  minim = new Minim(this);
  pan = new Pan(0);
  gain = new Gain(0);
  out = minim.getLineOut(Minim.STEREO);
  loopSound = new FilePlayer(minim.loadFileStream(SOUND_LOOP));  
  loopSound.patch(gain).patch(pan).patch(out);
  loopSound.loop();
}

void draw() {}

void mouseDragged() {
  panValue = map(mouseX, 0, width, -1, 1);
  gainValue = map(mouseY, 0, height, 0, NON_AUDIBLE_GAIN);
  pan.setPan(panValue);
  gain.setValue(gainValue);
}

```

I also created a github project with challenges like this, which contains this exact code:

> **[morisil/processing-challenges](https://github.com/morisil/processing-challenges)**
>
> Trying things out in Processing, especially if they don't play well together. Maybe someone will have a fix - morisil/processing-challenges

Could it be due to the fact that on Linux Processing is running with Oracle JDK, which means it is using ALSA directly, not pulse audio?

I was also thinking about the difference in the sample frequency and the output frequency. Any ideas how to improve it?

---

<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 10, 2019, 10:11am UTC](https://discourse.processing.org/t/minim-sound-cracks-on-pan-and-gain/11102/2 "2019-05-10T10:11:38Z")

</div>

i have here ( win7 ) no problem, but use like:

```auto
float vol=-34.4; // 6 .. -48 / 100 .. 0 %
//... in setup
  out = minim.getLineOut();
  out.setGain(vol);
//... in draw..
  vol = map(mouseX, 0, width, -48, 6);
  out.setGain(vol); // master vol slider

```

where you see that

```auto
sound.patch(gain);

```

---

<div class="post-metadata">

**Author:** ![morisil](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/morisil/32/6166_2.png) [@morisil](https://discourse.processing.org/u/morisil)\
**Post date:** [May 10, 2019, 10:18am UTC](https://discourse.processing.org/t/minim-sound-cracks-on-pan-and-gain/11102/3 "2019-05-10T10:18:18Z")

</div>

Thanks, but it didn’t help, on Linux I can hear even more cracks. Especially when I move the mouse rapidly. ☹ I think it might be some ALSA problem. I will try to switch to pulseaudio. In my original code I have to control pan and gain of multiple samples separately, therefore master volume wouldn’t work for me.

---

<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 10, 2019, 10:33am UTC](https://discourse.processing.org/t/minim-sound-cracks-on-pan-and-gain/11102/4 "2019-05-10T10:33:32Z")

</div>

for individual sounds i use sliders and

```auto
  wave1.setAmplitude(amp1);  

```

range 0…1.0 // float

you want just test my code?  
[download link](http://kll.engineering-news.org/kllfusion01/downloads/minim_natural_frequencies.zip)

---

<div class="post-metadata">

**Author:** ![hamoid](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/hamoid/32/58_2.png) [@hamoid](https://discourse.processing.org/u/hamoid)\
**Post date:** [May 10, 2019, 12:24pm UTC](https://discourse.processing.org/t/minim-sound-cracks-on-pan-and-gain/11102/5 "2019-05-10T12:24:44Z")

</div>

What happens if you use the Processing Sound library and run the examples at Libraries \> Sound \> Oscillators? In my case those are unbearably glitchy, but the Minim example you posted sounds ok. Just wondering if it is like when one works, then the other doesn’t.

I tried on ArchLinux. I can try on Ubuntu later.

---

<div class="post-metadata">

**Author:** ![neilcsmith](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/neilcsmith/32/144_2.png) [@neilcsmith](https://discourse.processing.org/u/neilcsmith)\
**Post date:** [May 10, 2019, 8:00pm UTC](https://discourse.processing.org/t/minim-sound-cracks-on-pan-and-gain/11102/6 "2019-05-10T20:00:23Z")

</div>

> [@morisil](#):
>
> Could it be due to the fact that on Linux Processing is running with Oracle JDK, which means it is using ALSA directly, not pulse audio?

Most likely. You can delete the Java directory inside Processing and replace with a symlink to a system installed OpenJDK 8.

---

<div class="post-metadata">

**Author:** ![morisil](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/morisil/32/6166_2.png) [@morisil](https://discourse.processing.org/u/morisil)\
**Post date:** [May 11, 2019, 10:52am UTC](https://discourse.processing.org/t/minim-sound-cracks-on-pan-and-gain/11102/7 "2019-05-11T10:52:05Z")

</div>

I tried, without much improvement. So I checked with `pavucontrol` what is providing sound from the java process, and it says `alsa plugin`. It seems that even openjdk is falling back to ALSA ☹ I am giving up for now. It will be used on mac and I hope the sound won’t be an issue there.
