Problems with G4P and Minim
in
Contributed Library Questions
•
1 month ago
Hi, I have some problems, I have this code, the idea is that the mic of my mac sense the note and compare this note with the midi note in the program, I think that I have some problems with detect correctly of the microphone, I don´t know if I need some code more like something to calibrate.
Also the idea is that when the note sensing with the microphone is not the same with the midi note appears an image like match to indicate that the note sensing is not the same that the midi note, the problem is that the image always appears.
Does anyone know how can I do?
thanks :D
import g4p_controls.*;
import ddf.minim.spi.*;
import ddf.minim.signals.*;
import ddf.minim.*;
import ddf.minim.analysis.*;
import ddf.minim.ugens.*;
import ddf.minim.effects.*;
import arb.soundcipher.*;
import arb.soundcipher.constants.*;
import processing.serial.*;
boolean visible;
String[] files;
Serial mipuerto;
Minim minim;
AudioInput in;
FFT fft;
GImageButton btnError;
SoundCipher part1 = new SoundCipher(this);
int mode = 1;
float[] pitches = {69, 69, 76, 76, 78, 78, 76, 74, 74, 73, 73, 71, 71, 69,
76, 76, 74, 74, 73, 73, 71, 76, 76, 74, 74, 73, 73, 71,
69, 69, 76, 76, 78, 78, 76, 74, 74, 73, 73, 71, 71, 69,
69, 69, 76, 76, 78, 78, 76, 74, 74, 73, 73, 71, 71, 69,
76, 76, 74, 74, 73, 73, 71, 76, 76, 74, 74, 73, 73, 71,
69, 69, 76, 76, 78, 78, 76, 74, 74, 73, 73, 71, 71, 69};
float[] dynamics = new float[pitches.length];
float[] durations = {1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 2,
1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 2,
1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 2,
1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 2,
1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 2,
1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 2};
int a = 0;
int n = 0;
int c = 0;
float xLoc = 0;
float xLoc2 = 0;
float xLoc3 = 0;
float newyLoc = 438; // if I put this don't works
float yLoc2 = -150;
float lineDistY = 100;
color blancas = color(255);
color negras = color(0);
color nota = color(204,102,150);
//Del siguiente arreglo las unidades indican dedo, decenas indican cuerda
int[] pitch_to_LED = {
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // octava 0, pitch 0 a 11
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // octava 1, pitch 12 a 23
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // octava 2, pitch 24 a 35
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // octava 3, pitch 36 a 47
0, 0, 0, 0, 0, 0, 0, 40, 0, 41, 0, 42, // octava 4, pitch 48 a 59
43, 0, 44, 0, 31, 0, 32, 33, 0, 34, 0, 21, // octava 5, pitch 60 a 71
0, 22, 23, 0, 24, 0, 11, 0, 12, 13, 0, 14, // octava 6, pitch 72 a 83
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // octava 7, pitch 84 a 95
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // octava 8, pitch 96 a 107
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // octava 9, pitch 108 a 119
0, 0, 0, 0, 0, 0, 0 //, 0, 0, 0, 0, 0, // octava 10, pitch 120 a 127
};
float freq;
float pitch;
float spectrumScale = 1;
int Imax = 0;
float Amax = 0;
float fmax = 0;
int highest = 0;
void setup(){
println(Serial.list());
String Puerto = Serial.list()[1];
mipuerto = new Serial(this, Puerto, 9600);
for(int i=0; i<pitches.length; i++){
dynamics[i] = random(40) + 70;
}
size(1024,768);
background(255);
stroke(0);
frameRate(2);
mic();
make_score_screen();
settings_score_visible(false);
}
void draw(){
if (a >= pitches.length-1){
a = 0;
}
if (75+xLoc3 >= width){
xLoc3 = 0;
yLoc2 = yLoc2 + lineDistY;
}
ellipse(xLoc3+75, height-3*pitches[a]+yLoc2-288, 8, 8);
fill(nota);
part1.playNote(pitches[a], dynamics[a], durations[a]);
a += 1;
xLoc3 += 20.0;
int newNotas = int(pitches[a]);
mipuerto.write(pitch_to_LED[newNotas]);
fft.forward(in.left);
highest = 0;
for(int i = 0; i < fft.specSize()-1; i++)
{
if(fft.getBand(i) > fft.getBand(highest))
highest = i;
fmax = fft.indexToFreq(highest);
pitch = round(69 + 12*(log(fmax/440.0)/log(2.0)));
}
score_screen();
println("pitch" + pitch + "pitches" + pitches[a] + "dedos" + pitch_to_LED[newNotas]);
}
void score_screen(){
switch(mode){
case 1:
settings_score_visible(true);
break;
case 2:
settings_score_visible(false);
}
if( pitch-2 < pitches[n]){
mode = 1;
}
if(pitch == pitches[a]){
mode = 2;
}
if(pitch+2 > pitches[n]){
mode = 1;
}
}
void make_score_screen(){
files = new String[]{
"helpOff.png"
};
btnError = new GImageButton(this, 400, 400, files);
}
void mic(){
minim = new Minim(this);
in = minim.getLineIn(Minim.STEREO);
fft = new FFT(in.bufferSize(), in.sampleRate());
}
void settings_score_visible(boolean visible){
btnError.setVisible(visible);
}
public void handleButtonEvents(GImageButton button, GEvent event){
}
1