Experiment & Coding

Happy Coding Time

import ddf.minim.*;
import processing.serial.*;

import cc.arduino.*;
import java.util.Date;

Arduino arduino;
Arduino arduino1511;

Minim minim;
AudioInput in;
AudioRecorder recorder;
AudioPlayer player;
String[] files;

String path = “/Users/Yoogle/Desktop/Code/FMP/recorder4/sounds/“;

boolean hasFile = false;


int mosi = 0;
int p_mosi = 0;

int i = 0;
int v = 0;

int s = 0;
int brightness = 0;
/*##########################*/
int countname; //change the name
int name = 000000; //set the number in key’s’ function

// change the file name
void newFile()
{
i = 0;
countname =( name + 1);
recorder = minim.createRecorder(in, “/Users/Yoogle/Desktop/Code/FMP/recorder4/sounds/v_” + countname + “.wav”, true);
//println(“file/“ + countname + “.wav”);
}
/*###############################*/


void setup()
{
size(800, 1000, P2D);
//textMode(SCREEN);
rectMode(CENTER);
minim = new Minim(this);
println(Arduino.list());

// Modify this line, by changing the “0” to the index of the serial
// port corresponding to your Arduino board (as it appears in the list
// printed by the line above).
arduino = new Arduino(this, Arduino.list()[2], 57600);
arduino.pinMode(12,Arduino.OUTPUT);
arduino1511 = new Arduino(this, Arduino.list()[3], 9600);
arduino1511.pinMode(0, Arduino.INPUT);
// get a stereo line-in: sample buffer length of 2048
// default sample rate is 44100, default bit depth is 16
in = minim.getLineIn(Minim.STEREO, 2048);
// create a recorder that will record from the input to the filename specified, using buffered recording
// buffered recording means that all captured audio will be written into a sample buffer
// then when save() is called, the contents of the buffer will actually be written to a file
// the file will be located in the sketch’s root folder.

newFile();//go to change file name
textFont(createFont(“SanSerif”, 12));

println(“Listing all filenames in a directory: “);
String[] filenames = listFileNames(path);
printArray(filenames);
}

void draw()
{
files = listFileNames(path);
background(20);
stroke(230);
noFill();
rect(400, 500, 200, 800);
// draw the waveforms
// the values returned by left.get() and right.get() will be between -1 and 1,
// so we need to scale them up to see the waveform
//for(int i = 0; i < in.bufferSize() - 1; i++)
//{
// line(i, 50 + in.left.get(i)*50, i+1, 50 + in.left.get(i+1)*50);
// line(i, 150 + in.right.get(i)*50, i+1, 150 + in.right.get(i+1)*50);
//}
mosi = arduino1511.analogRead(0);
println(mosi);
if(mosi - p_mosi > 300){
player = minim.loadFile(path+files[files.length-1]);
player.play();
}
if ( recorder.isRecording() )
{
if (i <800) {
v = abs(int(in.left.get(i)*60));
print(v);
if(v > 0){

s = int(map(v,1,40,120,180));
brightness = int(map(v,1,50,100,255));
i = v+i;
delay(50);
}
fill(250);
rect(400, 900-i/2, 200, i);
if(s > 120){s -= 1;}
println(“and “ + brightness);
} else {
fill(250);
rect(400, 500, 200, 800);
s = 0;
}
text(“Currently recording...”, 5, 15);
arduino.analogWrite(10, s);
arduino.analogWrite(12, 255);
} else
{
fill(250);
rect(400, 900-i/2, 200, i);
text(“Not recording.”, 5, 15);
}
p_mosi = mosi;
}

void keyReleased()
{
if ( key == ‘r’ )
{
// to indicate that you want to start or stop capturing audio data, you must call
// beginRecord() and endRecord() on the AudioRecorder object. You can start and stop
// as many times as you like, the audio data will be appended to the end of the buffer
// (in the case of buffered recording) or to the end of the file (in the case of streamed recording).
if ( recorder.isRecording() )
{
recorder.endRecord();
arduino.analogWrite(10, 0);
name++; //change the file name, everytime +1
recorder.save();
println(“Done saving.”);
println(name);//check the name
} else
{
/*#######################################*/
newFile();
/*#######################################*/
recorder.beginRecord();
}
}
if(key == ‘p’){
player = minim.loadFile(path+files[files.length-1]);
player.play();
}
//if ( key == ‘s’ )
//{
// // we’ve filled the file out buffer,
// // now write it to the file we specified in createRecorder
// // in the case of buffered recording, if the buffer is large,
// // this will appear to freeze the sketch for sometime
// // in the case of streamed recording,
// // it will not freeze as the data is already in the file and all that is being done
// // is closing the file.
// // the method returns the recorded audio as an AudioRecording,
// // see the example AudioRecorder >> RecordAndPlayback for more about that

// name++; //change the file name, everytime +1
// recorder.save();
// println(“Done saving.”);
// println(name);//check the name
//}
}
void stop()
{
// always close Minim audio classes when you are done with them
in.close();
minim.stop();

super.stop();
}

String[] listFileNames(String dir) {
File file = new File(dir);
if (file.isDirectory()) {
String names[] = file.list();
return names;
} else {
// If it’s not a directory
return null;
}
}