Amp in Sound library not working

/**

  • This is a simple sound file player. Use the mouse position to control playback
  • speed, amplitude and stereo panning.
    */
    // Map mouseY from 0.2 to 1.0 for amplitude
    float amplitude = map(mouseY, 0, width, 0.2, 1.0);
    soundfile.amp(amplitude);

The file.amp(desired_volume) doesn’t seem to be working at all. We tried in both macOS and Windows setups. Not even the code example within the library, called “SimplePlayback” works. It simply doesn’t affect the amplitude of the sound being played.

It used to work until recently. Any clues as to what to do next? Is there other way to handle the volume or something else to try out?

Hello @todocono,

Initially it did not seem to work for me as well.
Too much going and a cacophony of sound and I slimmed it down to volume only!

There was an error as well in mapping.

I then went in to the various Windows sound settings and looked around and then it seemed to work! I did not change anything and initially did not see “Simple Playback” in there initially.

Try this modified version:

/**
 * This is a simple sound file player. Use the mouse position to control playback
 * speed, amplitude and stereo panning.
 */

// GLV modified code below for amplitude only:

import processing.sound.*;

SoundFile soundfile;

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

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

  // Play the file in a loop
  soundfile.loop();
  
  mouseY = height/2; // Set initial value before using mouse in sketch window
}      

void draw() {
  background(255);
  
  // Map mouseY from 0.0 to 1.0 for amplitude
  //float amplitude = map(mouseY, 0, width, 0.0, 1.0);
  float amplitude = map(mouseY, 0, height, 0.0, 1.0); //< Corrected to height!
  
  println(mouseY, amplitude);
  
  circle(width/2, mouseY, 20);
  
  soundfile.amp(amplitude);
}

My Volume mixer:

The GitHub source:

:)

This is great! Thank you for your answer. We tried the example in several setups, both for instructors and students, and none of them worked.

BUT, after your explanation, we tried commenting out the “confusing” parts, and it’s working fine.

It appears that there is a conflict due to the incorrect mapping you mentioned. I didn’t investigate any further.

In any case, what needs to be updated is the code example. Thanks again!

Oh, wait… I got a bit confused. Your example uses .loop() . If, instead, I use .play(), it doesn’t work anymore.

The original code I was trying to get working is an example we use in class:

import processing.sound.*;

// declare a SoundFile object
SoundFile sound;

void setup() {
  size(640, 480);
  // create the instance and load the file
  sound = new SoundFile(this, "cello-f2.aif");
}

void draw() {
  background(0);
}

void mouseClicked() {

  float pitch = map(mouseX, 0, width, 0.1, 5); // use mouseY to determine the volume
  float vol = map(mouseY, 0, height, 1, 0); // set the playback rate for the soundfile 
  
  sound.rate(pitch);
  sound.amp(vol);
  sound.play();  //this starts using the amplitude if I use loop()
}

Thanks for any help!

1 Like

Hello,

Try setting the volume after it starts playing.

  if(sound.isPlaying())  // I was not patient so stopped it while testing!
    {
    sound.stop();
    }
    
  sound.play();
  sound.amp(vol);

If there is interest I have some much longer code that I wrote to thoroughly test this behavior.

:)

Thanks for flagging this, and welcome!

I saw that you already opened an issue on GitHub. Thanks for that :blue_heart: I went ahead and tagged @kevinstadler who maintains the sound library.

If you’re up for it, contributing that small fix would be very welcome. And if relevant in your context, it could be a nice activity to do with students, as a way to show them how open source contributions work in practice. Just an idea! The example you mentioned lives here on GitHub.

Thanks again for taking the time to report it!

Raphaël
Processing Community Lead @ Processing Foundation

2 Likes

Thanks for all the support and suggestions. @glv, I’ll reach out via DM. What I prepared for my class is the following:

/**
* This is a simple sound file player. Use the mouse position to control playback
* speed, amplitude and stereo panning.
* Click on the canvas to play sounds, with amplitude and frequency changing on mouse coordinates.
*/

import processing.sound.*;

SoundFile soundfile;

void setup() {
size(640, 360);
strokeWeight(5);

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

// These methods return useful infos about the file
println("SFSampleRate= " + soundfile.sampleRate() + " Hz");
println("SFSamples= " + soundfile.frames() + " samples");
println("SFDuration= " + soundfile.duration() + " seconds");
}

void draw() {
float b = map(mouseX, 0, width, 0, 255);
background(b);
float f = map(mouseY, 0, height, 0, 255);
fill(f);

circle(mouseX, mouseY, 50);
}

void mousePressed() {
[soundfile.play](http://soundfile.play)();// Play the file, and only then affect it

// Map mouseX from 0.25 to 4.0 for amplitude
float playbackSpeed = map(mouseX, 0, width, 0.25, 4.0);
soundfile.rate(playbackSpeed);

// Map mouseY from 0 to 1.0 for amplitude
float amplitude = map(mouseY, 0, height, 0, 1.0);
soundfile.amp(amplitude);
}

FYI, if what you say is true (my own testing indicates so), then the processing reference should also be rewritten (https://processing.org/reference/libraries/sound/SoundFile_amp_.html)

1 Like