PIC and processing sound recording
in
Integration and Hardware
•
2 years ago
Hi,
For a project I am working on I am trying to get a PICAXE chip to work with processing. So far I have managed to get the pic interfacing and sending serial inputs to the processing on my laptop. I also have another code which records, stops and plays back but this only works on my laptop as a result of it being based off only key presses. My question is how would I implement/or simply change some of the code of the recording code to be able to do all those inputs such as record/stop/play but through serial inputs from the pic(eg pressing a switch rather than a key on my laptop)
All help is much appreciated thanks.
The code I am using to send The serial input from the switches is as follows;
// Example by Tom Igoe
import processing.serial.*;
Serial myPort; // The serial port:
void setup() {
// List all the available serial ports:
println(Serial.list());
// List all the available serial ports:
println(Serial.list());
// I know that the first port in the serial list on my mac
// is always my Keyspan adaptor, so I open Serial.list()[0].
// Open whatever port is the one you're using.
myPort = new Serial(this, Serial.list()[1], 4800);
// is always my Keyspan adaptor, so I open Serial.list()[0].
// Open whatever port is the one you're using.
myPort = new Serial(this, Serial.list()[1], 4800);
}
void draw() {
while (myPort.available() > 0) {
int inByte = myPort.read();
println(inByte);
}
}
while (myPort.available() > 0) {
int inByte = myPort.read();
println(inByte);
}
}
And the code for the recording is as follows;
import ddf.minim.*;
Minim minim;
AudioInput input;
AudioRecorder recorder;
AudioPlayer player;
AudioInput input;
AudioRecorder recorder;
AudioPlayer player;
void setup()
{
// set size
size(400, 400);
background(0);
{
// set size
size(400, 400);
background(0);
// make the Minim sound object
minim = new Minim(this);
minim = new Minim(this);
// get a stereo line-in, e.g. from microphone
input = minim.getLineIn(Minim.STEREO);
input = minim.getLineIn(Minim.STEREO);
}
void draw()
{
{
// draw the recording waveforms
if ( recorder != null ) {
if ( recorder.isRecording() ) {
background(255,0,0);
stroke(255);
for(int i = 0; i < input.bufferSize() - 1; i++)
{
line(i, 50 + input.left.get(i)*50, i+1, 50 + input.left.get(i+1)*50);
line(i, 150 + input.right.get(i)*50, i+1, 150 + input.right.get(i+1)*50);
}
}
}
if ( recorder != null ) {
if ( recorder.isRecording() ) {
background(255,0,0);
stroke(255);
for(int i = 0; i < input.bufferSize() - 1; i++)
{
line(i, 50 + input.left.get(i)*50, i+1, 50 + input.left.get(i+1)*50);
line(i, 150 + input.right.get(i)*50, i+1, 150 + input.right.get(i+1)*50);
}
}
}
// draw the playing waveforms
if( player != null ) {
if (player.isPlaying() ) {
background(0);
stroke(255);
for(int i = 0; i < player.left.size()-1; i++)
{
line(i, 50 + player.left.get(i)*50, i+1, 50 + player.left.get(i+1)*50);
line(i, 150 + player.right.get(i)*50, i+1, 150 + player.right.get(i+1)*50);
}
}
}
}
if( player != null ) {
if (player.isPlaying() ) {
background(0);
stroke(255);
for(int i = 0; i < player.left.size()-1; i++)
{
line(i, 50 + player.left.get(i)*50, i+1, 50 + player.left.get(i+1)*50);
line(i, 150 + player.right.get(i)*50, i+1, 150 + player.right.get(i+1)*50);
}
}
}
}
void keyPressed() {
// check which key was pressed
switch(key){
// check which key was pressed
switch(key){
case 'r': // RECORD
if (player != null) {
player.close();
player = null;
}
if (recorder == null){
// create recorder and set a filename to save to
recorder = minim.createRecorder(input, "sound_02.wav", true);
}
recorder.beginRecord();
background(255,0,0);
break;
if (player != null) {
player.close();
player = null;
}
if (recorder == null){
// create recorder and set a filename to save to
recorder = minim.createRecorder(input, "sound_02.wav", true);
}
recorder.beginRecord();
background(255,0,0);
break;
case 's': // STOP RECORD / STOP PLAY
if ( recorder != null) {
if ( recorder.isRecording() ) {
recorder.endRecord();
recorder.save();
background(0);
recorder = null;
}
}
else if ( player != null) {
if ( player.isPlaying() ) {
player.pause();
}
}
break;
if ( recorder != null) {
if ( recorder.isRecording() ) {
recorder.endRecord();
recorder.save();
background(0);
recorder = null;
}
}
else if ( player != null) {
if ( player.isPlaying() ) {
player.pause();
}
}
break;
case 'p': // PLAY
if ( recorder != null) {
if (recorder.isRecording() ) {
recorder.endRecord();
recorder.save();
background(0);
recorder = null;
}
}
if (player == null){
// create a player and load the file
player = minim.loadFile("sound_02.wav");
}
player.rewind();
player.play();
break;
if ( recorder != null) {
if (recorder.isRecording() ) {
recorder.endRecord();
recorder.save();
background(0);
recorder = null;
}
}
if (player == null){
// create a player and load the file
player = minim.loadFile("sound_02.wav");
}
player.rewind();
player.play();
break;
}
}
void stop()
{
// always close the Minim object when closing program
if (player != null) {
player.close();
}
input.close();
minim.stop();
{
// always close the Minim object when closing program
if (player != null) {
player.close();
}
input.close();
minim.stop();
super.stop();
}
}
The program I am using to send serial inputs to the processing from the PICAXE programming editor is
main:
If pin1 = 1 then
sertxd (43)
pause 1000
endif
sertxd (43)
pause 1000
endif
If input0 = 1 then
sertxd (77)
pause 1000
endif
sertxd (77)
pause 1000
endif
If input7 = 1 then
sertxd (55)
pause 1000
endif
goto main
sertxd (55)
pause 1000
endif
goto main
1