0

So, my teacher wants me to code a program that creates WAV files, and I really don't know how I should write all the data in bytes, create a file and give it the .WAV extension by only using the imports java.io.File and java.lang I'm not asking for code to just copy and paste, I just want to know how I am supposed to create all the data (which i think i understand that, in a WAV file is a header followed by a sin-like function), then store it in a File and give it the .wav I have already tried creating a .txt file with

File file = new File(path);
if(file.exists()==false) {
    try {
        file.createNewFile();
    } catch(IOException e) {
    }
}

But the file doesn't appear and i don't know how am i supposed to write all the data, like if it should be just one line of hex stuff, or binary, or idk

FSDford
  • 448
  • 5
  • 10
  • Start by printing `System.out.println( e.getMessage() );` in your `catch` to see if something is breaking. – stdunbar Oct 14 '20 at 23:01
  • 1
    That seems like a pretty hard assignment for a beginning programmer. In general, the WAV format is pretty complicated; if you assume things like length and encoding *maybe* you could generate a fixed header that would work. But are you sure you understood the assignment? It seems unreasonable unless they expect you to use the old `javax.sound` packages. – erickson Oct 14 '20 at 23:03
  • I'm sure i get the WAV format, and why does it works as a sound file. What i don't get is how to actually create it – JOSE RAMSES DELGADILLO SANCHEZ Oct 14 '20 at 23:33
  • Also, there's no exception, i tried adding a print, and it does not print anything – JOSE RAMSES DELGADILLO SANCHEZ Oct 14 '20 at 23:35
  • 3
    @stdunbar `e.printStackTrace();` is always the better choice. An exception message is of little use by itself. – VGR Oct 14 '20 at 23:49
  • @VGR - what extra information will the OP use in the full stack trace? "No such file or directory" or "Permission denied" will provide all that a beginner needs. – stdunbar Oct 15 '20 at 00:10
  • According to this page http://soundfile.sapp.org/doc/WaveFormat/ you would have to do a lot of bitwise operations to flip values from BigEndian to LittleEndian and so on. Creating a 'soundwave' by sampling a function is relatively easy. Putting it to the sound card is also relatively simple, but to encode it without a library is almost insane. Who gives such an assignment??? – TreffnonX Oct 15 '20 at 05:24
  • It's super hard to tell what you are asking. How to encode sound? How to package it in a WAV format? How to create a file? How to write to a file? All those together make your question too broad. But, amidst those questions, you seem to be indicating some confidence in the hard parts, and just aren't sure how to write the data to a file. The answers to the duplicated question cover that. If that doesn't address your question, revise your question to much, much more specific. – erickson Oct 15 '20 at 15:48
  • See also: https://stackoverflow.com/questions/3297749 – erickson Oct 15 '20 at 18:46

0 Answers0