0

So my goal here is to store the y values from the Fourier Transform of a sound file into a byte array.

NOTE: I am trying to avoid importing javax

I did this by simply storing the bytes of the sound file into a byte array. But I'm not sure if this is the correct way to approach it.

Here is my code:

public static void main ( String[] args ) {

    try{
        File f = new File("C:/Users/Maxwell/Desktop/TestSoundFile.m4a");

        byte[] bytesFromFile = getBytes(f);
        System.out.println(Arrays.toString(bytesFromFile));
    }
    catch(Exception e) {
        System.out.println("Nope. Just nope.");
    }
}


public static byte[] getBytes(File f) throws FileNotFoundException, IOException {

    byte[] buffer = new byte[1024];
    ByteArrayOutputStream os = new ByteArrayOutputStream();
    FileInputStream fis = new FileInputStream(f);

    int read;

    while((read = fis.read(buffer)) != -1) {
        os.write(buffer, 0, read);
    }
    fis.close();
    os.close();

    return os.toByteArray();

}

The main function here is the getBytes() function.

What I did was simply store the bytes from the sound file into an array.

In the main() function, the print statement prints a large array of integers. It looks right to me, but I'm not entirely sure if it actually worked, or the values have nothing to do with the sound.

I'm kind of a newb when working with sound on Java.

Have I achieved my goal?

2
  • I wonder what are you trying to achieve if you aren't sure how to verify and test the results of your code! Commented Feb 26, 2018 at 20:01
  • @user2004685 - My apologies if it's not clear. I'm trying to find a better way to explain it. Commented Feb 26, 2018 at 20:10

1 Answer 1

1

According to wikipedia, .m4a files are usually encoded with AAC or ALAC formats, and this kind of file format can contain metadata. The bytes you're picking up could not translate directly to the actual audio data, or you could even be picking up metadata that's not related to the actual audio.

I don't know enough to tell you how to manage this kind of audio file but it seems you should probably research the file type. The only audio format I've worked with that I know can store audio in the way you expect is .wav files, so you might have an easier time with the actual programming if you can convert the audio beforehand.

Sign up to request clarification or add additional context in comments.

4 Comments

Thank you so much. I will try and convert to .wav format
Emphasis on 'can', as it can still contain metadata if you don't know the file's origin, but it will still be a lot easier to manipulate the actual audio data
The .wav file is coming from an audio recorder I made. The first 44 bytes are used for the header, after that is meant to be the data. Is there any other metadata that could be present I should be aware about? Does the audio name also get stored in the byte array? Sorry for the questions, I want to understand this.
Lesse, looking around I found an article on managing wav files in java you might want to look at which only uses java.io libraries. It includes a few links to more resources on the wav format. Since I have no experience with m4a I tried looking around and found an article on how to manage them but it seems you'll be depending on various external libraries to manage the AAC encoding.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.