Hello
I tried this last night.
On a mac G5 and a mini-mac intel there are problems if the buffer is less then 4096.
Sorry for the not very clean code.
If it is not working, kill the code parts with font ...
Code:
import krister.Ess.*;
AudioStream myStream;
AudioChannel myChannel;
FFT myFFT;
int bufferSize;
int bufferDuration;
// ----Einstellungen---
float zoomY=100;
double ampScale = 0.5;
double valAlt=0;
double dTime=0;
double dTimeInc=0;
double tonTimeInc=0;
double tonTime=0;
float oneCycle =0;
double freq;
Handle hFreq;
Handle hAmp;
Handle hSpez;
float sampleRate = 0;
PFont font1;
void draw(){
int i;
int x=10;
background(153);
drawRegler();
drawSpectrum(10,300,100);
stroke(0);
fill(0);
text("Time:"+dTime, 15, 30);
text("Frequenz:"+freq, 15, 50);
fill(200,50,50,50);
rect(10, 200, 512, 200);
line (10,300,512+10,300);
float firstVal= myStream.buffer2[0];
for( i = 0; i < myStream.buffer2.length-1; i++){
if( (myStream.buffer2[i]>=0)&(myStream.buffer2[i+1]<=0)){
break;
}
}
int startI=i;
int stepX=myStream.buffer2.length/512;
stroke(255);
for( i = startI; i < myStream.buffer2.length-stepX; i=i+stepX){
line(x,300 - myStream.buffer2[i]*zoomY,x+1,300 - myStream.buffer2[i+stepX]*zoomY);
x++;
}
}
void setup(){
size(600,440);
smooth();
font1 = loadFont("Geneva-16.vlw");
textFont(font1, 14);
background(102);
setupRegler(3,10,80,200,540);
hFreq=handles[0];
hAmp=handles[1];
hSpez=handles[2];
hAmp.length=200;
hFreq.length=100;
hSpez.length=0;
sampleRate =44100;
Ess.start(this);
myStream=new AudioStream(8192/2,sampleRate); //2048 knackst schon
sampleRate=myStream.sampleRate;
bufferSize=myStream.buffer.length;
println("BufferSize="+bufferSize+"\nSampleRate="+sampleRate);
dTimeInc=1./sampleRate;
bufferSize=myStream.buffer.length;
bufferDuration=myStream.ms(bufferSize);
myFFT=new FFT(2048);
myStream.start();
// noSmooth();
drawRegler();
}
void audioStreamWrite(AudioStream theStream) {
double vL = 0;
ampScale=hAmp.val/800.;
double spez=hSpez.val*0.00005;
for (int i=0;i<theStream.size;i++) {
freq=hFreq.val*2.;
tonTimeInc =freq/sampleRate;
tonTime+=tonTimeInc;
vL=Math.sin(TWO_PI*tonTime*(1+(valAlt*valAlt)));
valAlt=vL*spez;
theStream.buffer[i]=(float)(vL*ampScale);
dTime+=dTimeInc;
}
}
//----REGLER----
//===========================//
Handle[] handles;
int reglerAnz;
int reglerBreite;
int reglerHoehe;
int reglerX;
int reglerY;
int reglerBoxSize=12;
void setupRegler(int vreglerAnz,int vX,int vY,int vH,int vB )
{
reglerAnz=vreglerAnz;
reglerBreite=vB;
println("breite="+reglerBreite);
reglerHoehe=vH;
reglerX=50;//vX;
reglerY=vY;
handles = new Handle[reglerAnz];
int hsize =1;
for(int i=0; i<reglerAnz; i++) {
handles[i] = new Handle(reglerX, reglerY+10+i*15, 0, reglerBoxSize, handles);
}
}
void drawRegler()
{
background(153);
for(int i=0; i<reglerAnz; i++) {
handles[i].update();
handles[i].display();
}
fill(0);
}
void drawSpectrum(int x,int y,int h) {
noStroke();
myFFT.getSpectrum(myStream);
for (int i=0; i<512; i++) {
float temp=myFFT.spectrum[i]*175;
rect(x+i,y+h,1,-temp);
}
}
void mouseReleased()
{
for(int i=0; i<reglerAnz; i++) {
handles[i].release();
}
}
class Handle
{
int x, y;
int boxx, boxy;
int length;
int val;
int size;
boolean over;
boolean press;
boolean locked = false;
boolean otherslocked = false;
Handle[] others;
Handle(int ix, int iy, int il, int is, Handle[] o)
{
x = ix;
y = iy;
length = il;
size = is;
boxx = x+length - size/2;
boxy = y - size/2;
others = o;
}
void update()
{
boxx = x+length;
boxy = y - size/2;
for(int i=0; i<others.length; i++) {
if(others[i].locked == true) {
otherslocked = true;
break;
}
else {
otherslocked = false;
}
}
if(otherslocked == false) {
over();
press();
}
if(press) {
length = lock(mouseX-reglerX-size/2, 0, reglerBreite-size-1);
}
if(val!=length){
val=length ;
}
}
void over()
{
if(overRect(boxx, boxy, size, size)) {
over = true;
}
else {
over = false;
}
}
void press()
{
if(over && mousePressed || locked) {
press = true;
locked = true;
}
else {
press = false;
}
}
void release()
{
locked = false;
}
void display()
{
line(x, y, x+length, y);
fill(255);
stroke(0);
rect(boxx, boxy, size, size);
if(over || press) {
line(boxx, boxy, boxx+size, boxy+size);
line(boxx, boxy+size, boxx+size, boxy);
}
}
}
boolean overRect(int x, int y, int width, int height)
{
if (mouseX >= x && mouseX <= x+width &&
mouseY >= y && mouseY <= y+height) {
return true;
}
else {
return false;
}
}
int lock(int val, int minv, int maxv)
{
return min(max(val, minv), maxv);
}
public void stop() {
Ess.stop();
super.stop();
}