我正在尝试制作一个处理应用程序,它现在将根据按下的键来播放音符——将来我将用 Arduino 整数输入替换键。当我只弹奏一个音符(正弦波)时,听起来不错,但当我弹奏多个音符时,声音变得非常不连贯,根本不像正在演奏的音符的组合。我将如何解决这个问题?此外,我发现当我松开按键时,正弦波的中心位置与其原始位置不同。为什么会这样?
import ddf.minim.*;
import ddf.minim.signals.*;
import ddf.minim.ugens.*;
import processing.serial.*;
Minim minim;
AudioOutput out;
SineWave sinea;
SineWave sineb;
SineWave sinec;
void setup()
{
size(displayWidth, displayHeight, P3D);
minim = new Minim(this);
out = minim.getLineOut(Minim.MONO);
sinea = new SineWave(440.00,1,out.sampleRate());
sineb = new SineWave(493.88,1,out.sampleRate());
sinec = new SineWave(523.25,1,out.sampleRate());
sinea.portamento(200);
sineb.portamento(200);
sinec.portamento(200);
out.addSignal(sinea);
out.addSignal(sineb);
out.addSignal(sinec);
sinea.setFreq(0);
sineb.setFreq(0);
sinec.setFreq(0);
smooth();
stroke(255);
strokeWeight(3);
}
void draw() {
background(0);
for(int i = 0; i < out.bufferSize() - 1; i++)
{
float x1 = map(i, 0, out.bufferSize(), 0, width);
float x2 = map(i+1, 0, out.bufferSize(), 0, width);
line(x1, height/2 + out.right.get(i)*100, x2, height/2 + out.mix.get(i+1)*100);
}
}
void keyPressed() {
switch (key) {
case 'a':
sinea.setFreq(440);
break;
case 'b':
sineb.setFreq(493.88);
break;
case'c':
sinec.setFreq(523.25);
break;
}
}
void keyReleased() {
switch (key) {
case 'a':
sinea.setFreq(0);
break;
case 'b':
sineb.setFreq(0);
break;
case'c':
sinec.setFreq(0);
break;
}
}
void stop() {
out.close();
minim.stop();
super.stop();
}