Jump to content

output to a text file from java sound API

- - - - -

  • Please log in to reply
No replies to this topic

#1
bondage

bondage

    Newbie

  • Members
  • Pip
  • 1 posts
Hi,

I am experimenting with the java sound API.

I came across a nice program that has been made to record a stream of audio in computer memory and which is stored as an array of type byte.

The capture portion of the program captures audio data from the microphone and stores it in a ByteArrayOutputStream object.

I am not familiar however with outputing text files in java.

I was wondering how to go about outputing the captured audio samples are stored in a text file once the playback button is pressed.

The source code of the program is at
Code:

Java Sound, Getting Started, Part 1, Playback — Developer.com

I would like to add some simple code that adds the recorded sampled audio data consists of a sequence of bytes to a simple text file.

I would then like to use the outputed data to analyse them in excel.

Thus basically i want to have the software to output the sampled data to a text file, preferebly with a fresh like after each output so that i can load them individually in excel.

Input data from a microphone is captured and saved in a ByteArrayOutputStream object when the user clicks the Capture button.

Data capture stops when the user clicks the Stop button.

Playback begins when the user clicks the Playback button.

The most relevant code is this

 //This method captures audio input

  // from a microphone and saves it in

  // a ByteArrayOutputStream object.

  private void captureAudio(){

    try{

      //Get everything set up for

      // capture

      audioFormat = getAudioFormat();

      DataLine.Info dataLineInfo =

                new DataLine.Info(

                  TargetDataLine.class,

                   audioFormat);

      targetDataLine = (TargetDataLine)

                   AudioSystem.getLine(

                         dataLineInfo);

      targetDataLine.open(audioFormat);

      targetDataLine.start();


      //Create a thread to capture the

      // microphone data and start it

      // running.  It will run until

      // the Stop button is clicked.

      Thread captureThread =

                new Thread(

                  new CaptureThread());

      captureThread.start();

    } catch (Exception e) {

      System.out.println(e);

      System.exit(0);

    }//end catch

  }//end captureAudio method



From research i have done, the best thing is to use filewriter to output as integer to a text file (maybe using a for next loop) that cathes the audio data bytes in the ByteArrayOutputStream object.

The output will be a stream of integers which that i can use to work with in MATLAB/Excel/Octave.

Thanks




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users