I am a novice programmer having a problem with the Sound library. I am trying to use the SinOsc function to generate a smooth sine wave. I also want it to vary the pitch as it reads serial data (range 0 to 255) that is generated by my Arduino every 33 microseconds. The problem is that since the SinOsc function is in the “draw” loop, the audio output is VERY choppy to the point where all you hear is a buzzsaw effect and no sinewave. If I move the SIne wave code out of the “draw” loop, it will not pick up the new serial data that is being generated to vary the pitch. The code has no errors and compiles nicely, but the sound is terrible!
Are there any ideas as to how I can make this a smooth sounding sine wave that varies in pitch based on the values I am receiving from the serial data stream?
Thank you much for looking at the code I included below:
//******************************************************************************
// Sound example
//
// To be used with the Arduino connected and running program:
// "p13_TouchSensorLamp_with_scaled_Serial_Output"
// already loaded to create the serial data that
// varies from 0 to 255, read by this sketch
//
//******************************************************************************
import processing.sound.*;
import processing.serial.*;
Sound s;
Serial myPort;
// Global Constants
final int BAUD_RATE = 9600; // MUST be same as the Arduino
// Global variable(s)
int pitchinput = 0; // holds the "pitchinput" value
void setup() {
// print a list of available serial ports to the
// Processing status window (Just one available in my case)
println("Available serial ports:");
println(Serial.list());
// Tell the serial object the information it needs to communicate
myPort = new Serial(this, Serial.list()[0], BAUD_RATE);
size(200, 200);
// Create a Sound object for globally controlling the output volume.
s = new Sound(this);
}
void draw() {
// Map vertical mouse position to volume.
float amplitude = map(mouseY, 0, height, 0.4, 0.0);
// Instead of setting the volume for every oscillator individually, we
// control the overall output volume of the whole Sound library.
s.volume(amplitude);
if (myPort.available() > 0) {
// read and print for easy debugging
pitchinput = myPort.read();
println(pitchinput);
}
// Play sine oscillator
SinOsc sin = new SinOsc(this);
sin.play(500 + pitchinput * 4, 1);
}