Save content of a short-array as (uncompressed) audio file

All your problems with Audio, Video and Images.

Save content of a short-array as (uncompressed) audio file

Postby sowatt » Mon May 23, 2011 5:59 pm

I have recorded a 16 bit audios stream into a short-arry('buffer') via 'AudioRecord'. Every value in the array represents one sample value in the range [-32.768 ; +32.767]. The string representations of the array values are saved in a text file ( 'saveToSDCard()' ).

How can I save the the array values in a uncompressed audio file?

Thanks!

Here are the relevant code fragments. The complete eclipse project is attached to the posting.
Code: Select all
int AUDIO_ENCODING = AudioFormat.ENCODING_PCM_16BIT;
int FREQUENCY = 22050; //Herz
double REC_DURATION = 5.0; // in seconds
int bufferSize = -1;   
AudioRecord audioRecord = null;
short [] buffer = null; // data sink for recording the audio stream from mic
   

void initAudio() {
       
    int minBufferSize = AudioTrack.getMinBufferSize(FREQUENCY, AudioFormat.CHANNEL_OUT_MONO, AUDIO_ENCODING);   
       
    bufferSize = (int)(REC_DURATION * 10 * minBufferSize);
       
    buffer = new short[bufferSize];
       
    // Create new AudioRecord object to record the audio.
    audioRecord = new AudioRecord(
        MediaRecorder.AudioSource.MIC,
        FREQUENCY, AudioFormat.CHANNEL_IN_MONO,
        AUDIO_ENCODING, bufferSize);   
}


public void record() {
       
    try {
        audioRecord.startRecording();
        audioRecord.read(buffer, 0, bufferSize);
        audioRecord.stop();
        } catch (Throwable t) {
           Log.e(ACTIVITY_NAME,"record(): Recording Failed");
    } 
}
   

private void saveToSDCard(){

    java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("yyyy.MM.dd HH.mm.ss");
    String fileName = sdf.format(new java.util.Date());
       
    try {
           
        String rootS = Environment.getExternalStorageDirectory().toString();
        File root = Environment.getExternalStorageDirectory();
           
        if (root.canWrite()){
               
               File f = new File(rootS + "/AudioClassifier", fileName + ".txt");
               FileWriter fw = new FileWriter(f);
               BufferedWriter out = new BufferedWriter(fw);
               
                for(int i = 0; i < bufferSize / 2; i++){
                    out.write("" + buffer[i] + " ");
                 }
                 out.close();
                 fw.close();
        }
    } catch (IOException e) {
            e.printStackTrace();
    }
}
Attachments
AudioClassifier.zip
(54.69 KiB) Downloaded 30 times
sowatt
Once Poster
Once Poster
 
Posts: 1
Joined: Mon May 23, 2011 5:51 pm

Top

Re: Save content of a short-array as (uncompressed) audio fi

Postby www.rzr.online.fr » Thu Aug 11, 2011 1:24 pm

Under which licence is that code :

AudioClassifier.zip

resources/file/2977


can you publish that archive file again shipping a COPYING file
look at osi.org for some licences ...
User avatar
www.rzr.online.fr
Junior Developer
Junior Developer
 
Posts: 20
Joined: Tue Feb 03, 2009 11:39 am
Location: fr/35/rennes

Top

Return to Multimedia Problems

Who is online

Users browsing this forum: Google [Bot] and 15 guests