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 › clipping problem and so on...
Page Index Toggle Pages: 1
clipping problem and so on... (Read 448 times)
clipping problem and so on...
Dec 29th, 2006, 8:11pm
 
hello, this program made keypad keyboard.
type z->440hz, x->495hz, c->550hz...., then rings sine wave.  
(requires controlP5 )

it can record the value and playback.
but sounds always clip...

in addition, i want to know better way to playback(without using audiofile).

best,
yosuke hayashi

Code:

import krister.Ess.*;
import controlP5.*;

AudioChannel myChannel;
SoundData[] sdata;

//gui
ControlP5 controlP5;

//src
SineWave mySine;

//filter
Envelope myEnvelope;
HighPass myHigh;
Mix myMix;
Normalize myNormalize;

//recording
boolean recFlag = false;

int freq;
color toneColor;
private int cnt;

boolean doFadeIn = false;

void setup(){
size(520, 175);
background(255);
int cnt = 0;

sdata = new SoundData[24];

controlP5 = new ControlP5(this);
controlP5.setColorBackground(color(0,0,0));
controlP5.setColorForeground(color(40,40,50));
controlP5.setColorValue(color(255,255,255));
controlP5.setColorLabel(color(10, 10, 10));
controlP5.setColorActive(color(245,80,125));

controlP5.addBang("play", 450, 10, 50, 20).setLabel("play");
controlP5.addToggle("recValue", false, 380, 10, 50, 20).setLabel("rec");

Ess.start(this);

myChannel = new AudioChannel();

//oscs
mySine = new SineWave(130, .5);

//env
EPoint[] env = new EPoint[4];
env[0] = new EPoint(0, 0);
env[1] = new EPoint(.1, .4);
env[2] = new EPoint(.16, .2);
env[3] = new EPoint(.21, 0);
myEnvelope = new Envelope(env);

myNormalize = new Normalize();

myChannel.initChannel(myChannel.frames(300));

for(int i=0;i<sdata.length;i++) {
sdata[i] = new SoundData(0, 0, i);
sdata[i].drawData();
}

frameRate(30);
}

void draw(){
background(255);

for(int i=0;i<sdata.length;i++){
sdata[i].drawData();
}

}

void keyPressed() {
switch(key) {
case 122: //z
freq = 440;
toneColor = color(60);
break;

case 120: //x
freq = 495;
toneColor = color(80);
break;

case 99: //c
freq = 550;
toneColor = color(100);
break;

case 118: //v
freq = 587;
toneColor = color(120);
break;

case 98: //b
freq = 660;
toneColor = color(140);
break;

case 110: //n
freq = 733;
toneColor = color(160);
break;

case 109: //m
freq = 825;
toneColor = color(180);
break;

case 44: //,
freq = 880;
toneColor = color(200);
break;
}

if(cnt >= sdata.length) {
cnt = 0;
}
sdata[cnt] = new SoundData(freq, toneColor, cnt);
sdata[cnt].playSound();
if(recFlag == true) {
cnt++;
}
}

void recValue(boolean theValue) {
recFlag = theValue;
}

void play() {
for(int i=0; i<cnt; i++) {
sdata[i].playSound();
try{
Thread.sleep(500);
}catch(InterruptedException e){}
}
}

public void stop(){
Ess.stop();
super.stop();
}

class SoundData {
int f, index, col;
color tone;

public SoundData(int _f, color _tone, int _index) {
f = _f;
tone = _tone;
index = _index;
}

public void drawData() {
if(8 <= index && index < 16) {
col = 130;
}
else if(16 <= index && index < 24) {
col = 250;
}
else {
col = 10;
}

fill(tone);
rect(col, (index % 8) * 20 + 10, 110, 15);
}

public void playSound() {
mySine.frequency = f;
mySine.generate(myChannel);
myEnvelope.filter(myChannel);
myNormalize.filter(myChannel);
myChannel.play();
}
}
Page Index Toggle Pages: 1