We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now

x

Byte stream array

lacroix_al
lacroix_al asked
on
Medium Priority
395 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

Comment
Watch Question

Siva Prasanna KumarPrincipal Solutions Architect
CERTIFIED EXPERT
Top Expert 2006

Commented:
can you please make it a little clear actually what you want is that .wav files raw content in a .txt file????

Author

Commented:
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

Author

Commented:
Does this make sence?
Al
Principal Solutions Architect
CERTIFIED EXPERT
Top Expert 2006
Commented:
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.

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
Mayank SPrincipal Technologist
CERTIFIED EXPERT

Commented:
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.

Author

Commented:
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

Author

Commented:
Do you understand my question?
Thank
Al

Author

Commented:
Can some PLEASE help me with this.
Al
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.