Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are the steps to turn a string of notes into wav file java

I need to convert a string of text that represents a series notes into a wav file and am wondering what the process would be to do this? I don't need exact code, just the steps I would need to take. My text file would look something like this:

c 1
d 2
e 1

So that file would become a wav file that plays the note 'c' (frequency 16.35Hz) for one second, then a 'b' (18.35Hz) for 2 seconds, etc. This text is just an example - the actual text will contain more info, but I just want to start simple, once I have this down I'll implement more information to improve the quality.

I'm not against using an already created library - but was unable find a free or open source one.

The closest already answered question I've been able to find is Create an mp3 file from scratch C#. Everything I've found for java talk about playing wav or other audio files, but not creating them from scratch like I need.

Any help or direction would be great. Thanks in advance!

like image 266
MrMadsen Avatar asked Jul 04 '15 04:07

MrMadsen


People also ask

How do I make a WAV file from text?

Use File -> Export -> Export as WAV to convert the file to a WAV file. Audacity opens a Save File dialog. Save the WAV file where you'll remember it on your hard drive. Repeat the same process for all your MP3 files and you'll have a collection of WAV files suitable for use on microcontroller projects.


3 Answers

Use the Java SE MIDI package. Playing your song it what this is for, and you really don't want to reinvent that feature.

It isn't simple, but it is effective and isolates you from hardware differences. The Synthesizer interface will be particularly important to this task.

like image 78
msw Avatar answered Oct 14 '22 08:10

msw


WAV files are actually pretty simple to generate, and there's libraries that do it.

From there, you just read the file into a list of note-duration pairs, convert the notes to frequencies, then, for each entry, write the waveform. You can get a waveform with a loop and calls to Math.sin().

That is, is you really do want a WAV and not a MIDI.

like image 45
David Ehrmann Avatar answered Oct 14 '22 09:10

David Ehrmann


Check out the Java Tutorials Audio Trail. Most everything you need is there.

A sine function can be used generate pitched audio values. You will be iterating the function at very small increments (e.g., 44100 frames per second). Each value will have to be converted into a pair of bytes to accomplish 16-bit encoding, and then stored in a byte array. (There are stackoverflow threads that deal with the conversion.)

Java has the streams and formats you need that allow you to play back PCM byte data via a wav format, and the tutorial has a page/paragraph on how to save a wav file. The nice thing is that you don't have to worry about the wav header or any other details. You only have to specify the needed parameters in a format object and provide the raw PCM bytes.

The tutorial is not easy reading, though, especially if you are new to this.

like image 29
Phil Freihofner Avatar answered Oct 14 '22 09:10

Phil Freihofner