Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Byte stream array

Posted on 2006-03-22
8
Medium Priority
?
383 Views
Last Modified: 2009-12-16
Hi,
I have this code:

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;

import javax.sound.sampled.AudioFileFormat;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;


public class AudioDataBuffer
{

      /** The size of the temporary read buffer, in frames.
       */
      private static final int      BUFFER_LENGTH = 1024;

      public static void main(String[] args)
            throws Exception
      {
      
            File      sourceFile = new File("C:/Documents and Settings/s118608/Desktop/School/CIS416_PROGRAMMING/Project/dsclip.wav");
            File      targetFile = new File("C:/Documents and Settings/s118608/Desktop/School/CIS416_PROGRAMMING/Project/dsclip.txt");
                        
            /* Get the type of the source file. We need this information
               later to write the audio data to a file of the same type.
            */
            AudioFileFormat fileFormat = AudioSystem.getAudioFileFormat(sourceFile);
            AudioFileFormat.Type targetFileType = fileFormat.getType();
            AudioFormat audioFormat = fileFormat.getFormat();
            
            System.out.println("fileFormat "+fileFormat);
            System.out.println("targetFileType "+targetFileType);
            System.out.println("audioFormat "+audioFormat);

            /* Read the audio data into a memory buffer.
             */
            AudioInputStream inputAIS = AudioSystem.getAudioInputStream(sourceFile);
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            int nBufferSize = BUFFER_LENGTH * audioFormat.getFrameSize();
            byte[]      abBuffer = new byte[nBufferSize];


            /* Here's the byte array everybody wants.
             */
            byte[] abAudioData = baos.toByteArray();
            
            System.out.println("abAudioData "+abAudioData);

            /* And now, write it to a file again.
             */
            ByteArrayInputStream bais = new ByteArrayInputStream(abAudioData);
            AudioInputStream outputAIS = new AudioInputStream(bais, audioFormat,abAudioData.length / audioFormat.getFrameSize());
            int      nWrittenBytes = AudioSystem.write(outputAIS,targetFileType,targetFile);
            System.out.println("nWrittenBytes "+nWrittenBytes);
      }
}

When I run it, I get an output of :

--------------------Configuration: <Default>--------------------
fileFormat WAVE (.wav) file, byte length: 10051, data format: ULAW 8000.0 Hz, 8 bit, mono, 1 bytes/frame, , frame length: 10005
targetFileType WAVE
audioFormat ULAW 8000.0 Hz, 8 bit, mono, 1 bytes/frame,
abAudioData [B@e7b241
nWrittenBytes 46

Process completed.

The problem is this, I want the output of the targetFileType WAVE to be text.
What did I do wrong?

Thanks
Al

0
Comment
Question by:lacroix_al
  • 5
  • 2
8 Comments
 
LVL 23

Expert Comment

by:Siva Prasanna Kumar
ID: 16260482
can you please make it a little clear actually what you want is that .wav files raw content in a .txt file????
0
 

Author Comment

by:lacroix_al
ID: 16260601
What I am looking for is to read the wav file into a buffer.
and write all the data to a text file.
ex.
I need to know all to sample points and there hex addresses.
thanks
Al
0
 

Author Comment

by:lacroix_al
ID: 16263498
Does this make sence?
Al
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 23

Accepted Solution

by:
Siva Prasanna Kumar earned 1500 total points
ID: 16265958
hi lacroix_al ,

if you just want to read a WAV file & write its content to a TXT file

here is the code

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class convertwartotxt
{

    public static void main(String[] args)
    {      
        FileReader fr = null;
        FileWriter fw = null;
       
        try
        {
            fr = new FileReader("C:\\CHILD.wav");
                   
            fw = new FileWriter("C:\\out.txt");
           
            int ch = 0;
                 
            while((ch = fr.read()) != -1)
            {    
                System.out.println(Integer.toHexString(ch));
                fw.write(ch);
            }
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }  
        finally
        {
            try
            {
                fr.close();
                fw.close();
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }            
        }    
    }
}

just make sure you point to the file correctly.
I think this must be the answer else if you want to use ByteArrayStream you can use it in similar fashion.

0
 
LVL 30

Expert Comment

by:Mayank S
ID: 16266591
BTW, what is the need to convert the wav to a txt? It will not be readable anyway as all you are doing is creating a new file with the same data but a different extension.
0
 

Author Comment

by:lacroix_al
ID: 16273064
shivaspk

Ok this is good.
What I really want to do is match the data points to the hex address and write it to a file.
ex.
7F hex is located at address 1E7A hex.

Thanks
Al
0
 

Author Comment

by:lacroix_al
ID: 16283429
Do you understand my question?
Thank
Al
0
 

Author Comment

by:lacroix_al
ID: 16285459
Can some PLEASE help me with this.
Al
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
Suggested Courses
Course of the Month12 days, 6 hours left to enroll

564 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question