We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSound,  Music Libraries › record sound, save as mp3
Page Index Toggle Pages: 1
record sound, save as mp3? (Read 3005 times)
record sound, save as mp3?
May 10th, 2010, 8:39pm
 
I had do a recorder in Processing, it works fine,  but the file to save will be the wav format, I want it save as mp3 formal, anyone can help?
below is my code currently:


import ddf.minim.*;



Minim minim;
AudioInput in;
AudioRecorder recorder;

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

// change the file name
void newFile()
{      
 countname =( name + 1);
 recorder = minim.createRecorder(in, "file/A08May" + countname + ".wav", true);
 // println("file/" + countname + ".wav");
}
/*###############################*/


void setup()
{
 size(512, 200, P2D);
 textMode(SCREEN);
 
 minim = new Minim(this);

 // 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));
}

void draw()
{
 background(0);
 stroke(255);
 // 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);
 }
 
 if ( recorder.isRecording() )
 {
   text("Currently recording...", 5, 15);
 }
 else
 {
   text("Not recording.", 5, 15);
 }
}

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();
   }
   else
   {
     /*#######################################*/
     newFile();
     /*#######################################*/
     recorder.beginRecord();
   }
 }
 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();
}
Re: record sound, save as mp3?
Reply #1 - May 10th, 2010, 8:49pm
 
I had try to just change this line
recorder = minim.createRecorder(in, "file/A08May" + countname + ".wav", true);

to
recorder = minim.createRecorder(in, "file/A08May" + countname + ".mp3", true);

but it will got error...
Re: record sound, save as mp3?
Reply #2 - May 11th, 2010, 2:24am
 
A topic that belongs more to Sounds, Music section. I will move it.
[EDIT] Actually you posted there too. Avoid duplicate messages, you can delete your posts if needed.

I had a look at the Minim doc, it is not explicit on the file formats it supports, but the examples mention only WAV...
Actually, I am not even sure if we can record MP3 format in pure Java (I suppose it is possible by binding Lame DLL or similar).
You can convert it later...
Re: record sound, save as mp3?
Reply #3 - May 12th, 2010, 6:52pm
 
HOW?
Re: record sound, save as mp3?
Reply #4 - May 17th, 2010, 2:01am
 
You can convert to mp3 in iTunes or "Any Video Converter" or in QuickTime pro, lots of apps.  I found Any Video Converter on CNET.com

Anyway, I would be scared to use mp3 for recording real-time generated sounds because there's extra overhead encoding mp3, and your sketch could stutter or crash or whatever because of that.

I imagine that's the same reason recording a video results in a big fat uncompressed video even though that doesn't really run in real time...

So, I would normally record an uncompressed wav or aif and compress it later with another app.\ like the above.  for lots of files I would use Any Video Converter or iTunes because they work in batches
- hope that helps! Wink

Page Index Toggle Pages: 1