Trouble with sampleRate() in Processing

Hi!

I just got started with programming and I’m having some trouble with an assignment. As far as I understand this, I’ve followed the instructions of the documentation correctly, but my code still doesn’t work.

I’m trying to program a sound and a square that edits amplitude on the X axis and pitch on the Y axis when you move your mouse over it.

This is my code so far:

import processing.sound.*;

SinOsc osc;
Sound s;

float freq=400.0;

int controlpanelx = 0;
int controlpanely = 0;
int controlpanelwidth = 700;
int controlpanelheight = 400;

void setup(){
  
  size(700, 400);
  background(255);
  frameRate(10);
  
  osc = new SinOsc(this);
  osc.play();

  s = new Sound(this);

}


  void draw() {

  float amplitude = map(mouseX, 0, width, 0.0, 0.9);

  s.volume(amplitude);
  
  float sampleRate = map(mouseY, 0, height, 1000, 200);
  
  s.sampleRate(600);
}

It opens the square window. Does nothing else.

If I remove the last line (s.sampleRate(600)), then there IS sound and it’s amplitude can be manipulated with by moving the mouse along its X axis, as expected, but the sampleRate doesn’t work.

In fact, if I click on it, Processing tells me that the value of the local variable “sampleRate” is not used , which I imagine is where the problem lies.

Any tips on how to get sampleRate working?

This is where I got the instructions from:
https://processing.org/reference/libraries/sound/Sound_sampleRate_.html

(I see that it says the line sampleRate(600) should be in setup and not in draw, but placing it there doesn’t fix the problem. As far as I can see, it makes no difference).

So far I’ve been unable to find any other related thread, example code, guide, etc. anywhere, so ANY help will be greatly appreciated.

Thanks!

I suggest you use the minim library. You can start by checking the provided examples.

Kf

You should use osc.amp() and osc.freq() of the SinOsc to set the amplitude and frequency.

https://processing.org/reference/libraries/sound/SinOsc.html

I would also remove Sound s. You need Sound only if you have multiple oscillators and want to adjust the global output volume of the whole sketch.

You probably shouldn’t set the sample rate yourself. This sets how many samples will the audio system produce per second (usually 48000, 44100 or 22050), and has nothing to do with volume or pitch.