We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSound,  Music Libraries › Sinewave output sounds glitchy/jumpy
Page Index Toggle Pages: 1
Sinewave output sounds glitchy/jumpy (Read 1111 times)
Sinewave output sounds glitchy/jumpy
Dec 16th, 2009, 10:37am
 
I am working on a synthesizer-like program, basically when you press multiple keys together sine waves of different frequencies are added to an AudioOutput object.

This works well enough, except it sounds bad.

Basically, this is what it sounds like:

Code:

import ddf.minim.*;
import ddf.minim.signals.*;

Minim minim;
AudioOutput out;
SineWave sine;

void setup()
{
size(100,100);

minim = new Minim(this);
out = minim.getLineOut(Minim.STEREO);
sine = new SineWave(880, 1, out.sampleRate());
sine.portamento(30);
}


void draw()
{
out.addSignal(sine);
if(!keyPressed) out.clearSignals();
}

void stop()
{
out.close();
minim.stop();
super.stop();
}


Is there a better way to do this? I can get a nice consistent tone if I don't try and add signals, e.g. just having one sine signal and changing the frequency, but then I can't generate chords which is kinda the point of the program.

Any ideas?
Re: Sinewave output sounds glitchy/jumpy
Reply #1 - Dec 16th, 2009, 10:57am
 
Here is the full code if you want to have a look:
Code:

/*
Use 1-8 on your keyboard to play tones in the C major scale
*/

import ddf.minim.*;
import ddf.minim.signals.*;

Minim minim;
AudioOutput out;
SineWave sine;
SineWave nullSine;

float freq = 0;
float amp = 1;
int keysPressed = 0;
int port = 30;

//c5-c6 major scale in hz
float[] tones = {523.25, 587.33, 659.26, 698.46, 783.99, 880.00, 987.77, 1046.50};

char keys[] = {'1','2','3','4','5','6','7','8'};
boolean keyStates[];

void setup()
{
size(300,100);
keyStates = new boolean[keys.length];
minim = new Minim(this);

out = minim.getLineOut(Minim.STEREO);
nullSine= new SineWave(0, 0, out.sampleRate());
sine = new SineWave(0, amp, out.sampleRate());
sine.portamento(port);
}

//determine which keys have been pressed
void keyPressed()
{
for(int i=0;i<keys.length;i++)
{
if(key == keys[i])
keyStates[i] = true;
}
}

//determine which keys have been released
void keyReleased()
{
for(int i=0;i<keys.length;i++)
{
if(key == keys[i])
keyStates[i] = false;
}
}

void draw()
{
background(100);

out.clearSignals();
out.addSignal(nullSine);

//adjust amplitude to avoid distortion with chords
keysPressed=0;
for(int i=0; i<keyStates.length; i++)
{
if(keyStates[i]) keysPressed++;
}
if(keysPressed == 0) amp = 1.0;
else amp=1.0/keysPressed;


for(int i=0; i<keyStates.length; i++)
{
if(keyStates[i])
{
out.addSignal(new SineWave(tones[i], amp, out.sampleRate()));
}
}

/*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, 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);
}*/
}

void stop()
{
out.close();
minim.stop();
super.stop();
}
Re: Sinewave output sounds glitchy/jumpy
Reply #2 - Dec 27th, 2009, 11:43pm
 
At every frame, the output is destroyed and recreated.
Whatever the waveform looked like when the last frame ended, it will start over.
Edit: Note the effect if you add "frameRate(5);" in setup().

Generating the output only inside keyPressed() and keyReleased() can get rid of the constant crackling but the signal could still get 'chopped' whenever a tone is added or removed.
Page Index Toggle Pages: 1