Hi! I have create a sketch in which two sound run simultaneously and I can switch between them… the problem is that when I switch between them (using mouse pressed and release) I hear a click. How can I solve the problem? I suppose I need an envelope… but how to implement it?
Not well versed in the sound library but perhaps this sketch can help
http://learningprocessing.com/examples/chp20/example-20-07-envelope
import processing.sound.*;
SinOsc osc;
Env envelope;
int[] scale = {
60, 62, 64, 65, 67, 69, 71, 72
};
int note = 0;
void setup() {
size(200, 200);
osc = new SinOsc(this);
envelope = new Env(this);
}
void draw() {
background(255);
PVector v = new PVector();
if (frameCount % 60 == 0) {
osc.play(translateMIDI(scale[note]), 1);
envelope.play(osc, 0.01, 0.5, 1, 0.5);
note = (note + 1) % scale.length;
}
}
float translateMIDI(int note) {
return pow(2, ((note-69)/12.0))*440;
}
I tried in this way but it doesn’t work! When I switch between one sample and the other I hear a click!
import ddf.minim.;
import ddf.minim.spi.; // for AudioRecordingStream
import ddf.minim.ugens.*;
// declare everything we need to play our file
Minim minim;
FilePlayer filePlayer1, filePlayer2;
Gain gain1, gain2;
AudioOutput out;
// you can use your own file by putting it in the data directory of this sketch
// and changing the value assigned to fileName here.
String fileName1 = “groove.mp3”;
String fileName2 = “marcus_kellis_theme.mp3”;
void setup()
{
// setup the size of the app
size(640, 240);
// create our Minim object for loading audio
minim = new Minim(this);
// a FilePlayer reads from an AudioRecordingStream, which we
// can easily get from Minim using loadFileStream
filePlayer1 = new FilePlayer( minim.loadFileStream(fileName1) );
// and then we’ll tell the file player to loop indefinitely
filePlayer1.loop();
filePlayer2 = new FilePlayer( minim.loadFileStream(fileName2) );
// and then we’ll tell the file player to loop indefinitely
filePlayer2.loop();
gain1 = new Gain(0.0f);
gain2 = new Gain(0.0f);
// get a line out from Minim. It’s important that the file is the same audio format
// as our output (i.e. same sample rate, number of channels, etc).
out = minim.getLineOut();
// patch the file player to the output
filePlayer2.patch(gain1).patch(out);
filePlayer1.patch(gain2).patch(out);
}
// keyPressed is called whenever a key on the keyboard is pressed
void keyPressed()
{
// you can query whether the file is playing or not
// playing simply means that it is generating sound
// this will be true if you tell it to play() or loop()
if ( filePlayer1.isPlaying() )
{
// pauses playback of the file
filePlayer1.pause();
}
else
{
// starts the file looping again, picking up where we left off
filePlayer1.loop();
}
}
void mousePressed()
{
gain1.setValue(0.0);
gain2.setValue(-90.0);
println(“gain1=1 - gain2=0”);
/*
float pos = map(mouseX, 0, width, 0, filePlayer1.length());
filePlayer1.cue((int)pos);
*/
}
void mouseReleased() {
gain1.setValue(-90.0);
gain2.setValue(0.0);
println(“gain1=0 - gain2=1”);
}
// draw is run many times
void draw()
{
// erase the window to black
background( 0 );
// draw using a white stroke
stroke( 255 );
// draw the waveforms
for( int i = 0; i < out.bufferSize() - 1; i++ )
{
// find the x position of each buffer value
float x1 = map( i, 0, out.bufferSize(), 0, width );
float x2 = map( i+1, 0, out.bufferSize(), 0, width );
// draw a line from one buffer position to the next for both channels
line( x1, 50 + out.left.get(i)*50, x2, 50 + out.left.get(i+1)*50);
line( x1, 150 + out.right.get(i)*50, x2, 150 + out.right.get(i+1)*50);
}
float songPos = map( filePlayer1.position(), 0, filePlayer1.length(), 0, width );
stroke( 255, 0, 0 );
line( songPos, 0, songPos, height );
text( "loopCount: " + filePlayer1.loopCount(), 15, 15 );
}
please comment your code using ctrl + shift + c.
I’m honestly not sure I can hear the click, I loaded two files and if I make sure to stop it offbeat there is no click, suggesting the sound you might be hearing is simply the sound of an instrument or percussion, as its being played before the cutoff.
Again not a music man myself and not versed with this particular library so my feedback is going to be very limited.
You can hear a click, let’s say a glitch-click, when you switch between one sample audio file and the other!