Created
November 1, 2012 21:40
-
-
Save OmerShapira/3996777 to your computer and use it in GitHub Desktop.
PitchWheel
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//import ddf.minim.spi.*; | |
import ddf.minim.signals.*; | |
import ddf.minim.*; | |
import ddf.minim.analysis.*; | |
//import ddf.minim.ugens.*; | |
//import ddf.minim.effects.*; | |
//final int[] noteFreqs = {31,33,35,37,39,41,44,46,49,52,55,58,62,65,69,73,78,82,87,93,98,104,110,117,123,131,139,147,156,165,175,185,196,208,220,233,247,262,277,294,311,330,349,370,392,415,440,466,494,523,554,587,622,659,698,740,784,831,880,932,988,1047,1109,1175,1245,1319,1397,1480,1568,1661,1760,1865,1976,2093,2217,2349,2489,2637,2794,2960,3136,3322,3520,3729,3951,4186,4435,4699,4978}; | |
//final String[] noteNames = {"NOTE_B0","NOTE_C1","NOTE_CS1","NOTE_D1","NOTE_DS1","NOTE_E1","NOTE_F1","NOTE_FS1","NOTE_G1","NOTE_GS1","NOTE_A1","NOTE_AS1","NOTE_B1","NOTE_C2","NOTE_CS2","NOTE_D2","NOTE_DS2","NOTE_E2","NOTE_F2","NOTE_FS2","NOTE_G2","NOTE_GS2","NOTE_A2","NOTE_AS2","NOTE_B2","NOTE_C3","NOTE_CS3","NOTE_D3","NOTE_DS3","NOTE_E3","NOTE_F3","NOTE_FS3","NOTE_G3","NOTE_GS3","NOTE_A3","NOTE_AS3","NOTE_B3","NOTE_C4","NOTE_CS4","NOTE_D4","NOTE_DS4","NOTE_E4","NOTE_F4","NOTE_FS4","NOTE_G4","NOTE_GS4","NOTE_A4","NOTE_AS4","NOTE_B4","NOTE_C5","NOTE_CS5","NOTE_D5","NOTE_DS5","NOTE_E5","NOTE_F5","NOTE_FS5","NOTE_G5","NOTE_GS5","NOTE_A5","NOTE_AS5","NOTE_B5","NOTE_C6","NOTE_CS6","NOTE_D6","NOTE_DS6","NOTE_E6","NOTE_F6","NOTE_FS6","NOTE_G6","NOTE_GS6","NOTE_A6","NOTE_AS6","NOTE_B6","NOTE_C7","NOTE_CS7","NOTE_D7","NOTE_DS7","NOTE_E7","NOTE_F7","NOTE_FS7","NOTE_G7","NOTE_GS7","NOTE_A7","NOTE_AS7","NOTE_B7","NOTE_C8","NOTE_CS8","NOTE_D8","NOTE_DS8"}; | |
final int[] noteFreqs = {31,33,37,41,44,49,55,62,65,73,82,87,98,110,123,131,147,165,175,196,220,247,262,294,330,349,392,440,494,523,587,659,698,784,880,988,1047,1175,1319,1397,1568,1760,1976,2093,2349,2637,2794,3136,3520,3951,4186,4699}; | |
final String[] noteNames = {"B0","C1","D1","E1","F1","G1","A1","B1","C2","D2","E2","F2","G2","A2","B2","C3","D3","E3","F3","G3","A3","B3","C4","D4","E4","F4","G4","A4","B4","C5","D5","E5","F5","G5","A5","B5","C6","D6","E6","F6","G6","A6","B6","C7","D7","E7","F7","G7","A7","B7","C8","D8"}; | |
final String[] majorScale = {"C4","D","E","F","G","A","B","C5"}; | |
Minim minim; | |
AudioInput input; | |
FFT fft; | |
pitchDetector pd; | |
boolean switched = false; | |
void setup() { | |
size(300, 200); | |
frameRate(18); | |
minim = new Minim(this); | |
input = minim.getLineIn(Minim.STEREO, 512); | |
fft = new FFT(input.bufferSize(), input.sampleRate()); | |
pd = new pitchDetector(); | |
input.addEffect(pd); | |
} | |
void draw () { | |
background(0); | |
fft.forward(input.mix); | |
stroke(255, 0, 0, 128); | |
for (int i = 0; i < fft.specSize(); i++) | |
{ | |
line(i, height, i, height - fft.getBand(i)*4); | |
} | |
float p = pd.getPitch(); | |
float m = getMaxFFT(); | |
text("Freq: "+p+" "+getNote(p)+ | |
"\nMax: "+m+ " "+getNote(m), width/3, height/3); | |
} | |
void stop () { | |
// the AudioInput you got from Minim.getLineIn() | |
input.close(); | |
minim.stop(); | |
// check that this works: "You might also close an audio class during execution of your program in order to free the resources it has." | |
super.stop(); | |
} | |
float getMaxFFT() { | |
int m = 0; | |
float maxVal = 0; | |
for (int i = 0; i < fft.specSize(); i++) { | |
float f = fft.getBand(i); | |
if (f > maxVal) { | |
maxVal=f; | |
m=i; | |
} | |
} | |
return fft.indexToFreq(m); | |
} | |
String getNote (float f){ | |
for (int i = 0; i< noteFreqs.length -1 ; i++ ){ | |
if (f > noteFreqs[i] && f < noteFreqs[i+1]){ | |
if ( f > (noteFreqs[i] + noteFreqs[i+1])/2f) { //TODO Change to nonlinear according to B*2^(n/12) | |
return noteNames[i+1]; | |
} else{ | |
return noteNames[i]; | |
} | |
} | |
} | |
return ("B0"); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment