开发者

1's and 0's to an audio file in java

开发者 https://www.devze.com 2023-03-27 05:54 出处:网络
I currently have a String that contains 1\'s and 0\'s. I want to make an audio file out of this string, preferably in .WAV format by using Java. How do i go about it ? Are there 开发者_运维知识库any A

I currently have a String that contains 1's and 0's. I want to make an audio file out of this string, preferably in .WAV format by using Java. How do i go about it ? Are there 开发者_运维知识库any API's to write bytes to make a .WAV ? any tutorials/blogs ?

Thanks in advance ! cheers !


This is the Java third party Sound API, it has many examples that can be usefull for you. And Sun Java API, you can explore here.

Here is Stack Question about java sound API.


I think your best bet is to look at the TargetDataLine interface. I assume you are already capable of converting your strings into a byte[] array. This byte array can be treated as the equivalent of raw audio data, but I have no idea if it will sound like anything at all interesting. With .wav format, you can choose to use 16-bit encoding (two bytes per sample) or 8-bit, too, I suppose, depending upon the size of your chars.

But given that 44100 samples per second is one of the most common rates, I would point out, that that this would require a LOT of text for very little time, if each sample is a letter!

The "key" functions in the TargetDataLine are an index into this array, and a read() method that makes use of this index as the read() method is repeatedly called. The read() method should return up to a buffer's worth of bytes with every call.

One of the methods you will need to implement, as well, will be getFormat(). I am using the following code in a simple "Theremin" synthesizer, to output bytes from a table floats (using a "wave table" of around 1000 floats for a single iteration of a sound wave) to a stereo, 16-bit, 44100 fps, little-endian .wav format to a SourceDataLine for playback.

@Override
public AudioFormat getFormat() 
{
    AudioFormat af = new AudioFormat(
        AudioFormat.Encoding.PCM_SIGNED, 
        44100, 16, 2, 4, 44100, false);
    return af;
}

You can learn more from the Java Tutorials. I would in particular look at the sections "Setting up a TargetDataLine" and "Reading from a TargetDataLine" in the Capturing Audio tutorial.

But also, maybe the very start of the sound tutorials where the nature of sound data is discussed, would be very much worth your time.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号