WAV Files and Sound Creation/Recording

Started by
7 comments, last by HelloSkitty 12 years, 2 months ago
I've been playing with the MidiSynth java class for a while, for I want to find a way just to play a specific wavelength of sound and not have acoustic damping of simulated instruments.

I know how to play a .wav file using java, so my logical question is how do I make a .wav file? I've looked at the specification and understood not two words of it. The same thing happened with .png but I managed to find the BufferedImage class which solved my problems by automatically giving me an array to manipulate the image with. So my understanding of sound tells me that a wav file would be a 3-D array of integers (time x frequency x amplitude) Does anyone know of any java classes that can convert a 3D array into a wav and/or vice versa? (My understanding of a wav tells me that the file IS pretty much just a 3D array, so I am assuming this shouldn't be difficult.)



Alternatively, if the wav file format is not so simple as I make it out to be, is there a way to just play a note of a specified frequency and amplitude? I feel like there should be some System function that does so, but I haven't found any. And then is there any free software to record speaker output into a sound file?


My overall goal here is computational sound (While I could just use the MidiSynth piano, I'd rather use a lower level (for java this means minimal dependence on the javax package) approach to making sound) in java. Or C/C++ (but I prefer java). So if there is ANY other simpler way to do this than the two above methods, I would gladly take it.

Thanks in advance!

P.S. I would have put this in the creative sound subforum, but this is more about "making" sound rather than composing it.
A penny for my thoughts? Do you think I have only half a brain?

Not-so-proud owner of blog: http://agathokakologicalartolater.wordpress.com/
Advertisement
What about using fast [color=#000000]discrete Fourier transform (through packages like FFTW) to generate the sound in frequency domain and then transform it back in time domain? I've just used this api to write a software guitar tuner, but from what I recall, you should be able to generate pure frequency samples pretty easily. [color=#000000]Once you have your sample, just use a sound api to save it to disk...
Oops, sorry: from a second read I think I musunderstood your needs.

Then try Java Sound API for java or, for c++, just type "free c++ sound api" in google. SDL, FMOD (which, last time I used it, was notably easy to set up and use for such basic tasks, but check out the license), PortAudio, ....
For audio of a specific frequency and amplitude all you need is a sin wave. Something like:

for (int i=0; i < nNumSamples; i++)
{
sampleData = (short)(sin((float)i * frequencyMultiplier) * amplitude);
}

The wav file format is described at https://ccrma.stanford.edu/courses/422/projects/WaveFormat/
@cignox1: JAVA Sound API was helpful, thanks!

@Adam_42: I had looked at the wav file format before, but could not understand how parts of it worked. Specifically, a channel sample. I do not know the significance of any of the numbers in the channel. Also, when I dissected a .wav file, I found 20's where 00's should have been, and do not know why (though I suspect it is related to the space character being equal to 20). If anyone can explain what they mean by channels and samples and "the actual sound data" that would be helpful.
A penny for my thoughts? Do you think I have only half a brain?

Not-so-proud owner of blog: http://agathokakologicalartolater.wordpress.com/
Channels are simply separate streams of audio. Usually used to create stereo or surround sound. You'll only need one.

A sample is simply a measure of the amplitude at a point in time. It's usually stored as a 16-bit signed value, hence my use of the short data type in the example code. For full volume set the amplitude to 32767.
Okay, wait a minute... this overturns pretty much everything I thought I knew about sound.

So sound then is really not a 3D thing with time x amplitude x frequency and it's just 2D time x volume? This sort of makes sense, with soundcloud giving 2D representations of sound...

So the sample rate would be the sound equivalent of frames per second? So just getting this straight -- with a wavelength of 800 and a sample rate of 800 I'd have a sound note of 1 Hz, but if I changed the sample rate to 1600, I'd have 2 Hz and the entire .wav file would become higher pitched?

(Hypothetical) What happens if the computer is too slow and/or sample rate is too high? Does the sound play in real time, or does it just go through all of the "frames" as fast as the sound processing unit can go?
A penny for my thoughts? Do you think I have only half a brain?

Not-so-proud owner of blog: http://agathokakologicalartolater.wordpress.com/
Some reading for you:

http://en.wikipedia.org/wiki/Nyquist%E2%80%93Shannon_sampling_theorem
http://en.wikipedia.org/wiki/Fast_Fourier_transform (for converting between time domain and frequency domain representations)

If you play back at double the sample rate it will double the pitch (i.e. go up one octave). It will also halve the duration. This is commonly used to generate music, as well as implement effects like doppler shift.

If you had sample data at say 88.2 KHz but the sound card only supported 44.1KHz output then it could simply output every other sample in the source data. To improve quality it could also apply some pre filtering to cut out frequencies above 22.05 KHz before doing that as they can't be represented at the lower sampling rate and will cause some aliasing if not filtered out.
Alright, thanks!

I was able to get my program running with a sine wave of sound.

(And then I made white noise, and didn't hear anything, to my surprise)
A penny for my thoughts? Do you think I have only half a brain?

Not-so-proud owner of blog: http://agathokakologicalartolater.wordpress.com/

This topic is closed to new replies.

Advertisement