Much of this code I copied and Don't fully understand, and I wish to understand it so I can do some cooler low-level sound effects. I also don't understand certain things about sound. Here's some code that I will then follow with questions:
float freq = 220.0f;
float seconds = 1;
float volume = 10000;
float phase = 0;
unsigned sample_rate = 44100;
size_t buf_size = seconds * sample_rate;
short *samples;
samples = new short[buf_size];
for (long i = 0; i < buf_size; i++)
{
samples = volume * sin((2.0f*float(M_PI)*freq + phase)/sample_rate * i);
}
(this is within a larger program with openAL processing and playing the resulting sound from the samples array)
It creates a nice sine wave(plays through the speakers correctly) that I can customize a few attributes of, such as hz(freq) the amplitude(volume) etc. Here are my questions:
Question 1.
I notice that the numbers in the array go from -9999 to 9999 when I print them out. Why is this? Is this the range that each hert(z?) can be within? This seems to produce the sinewave sound very nicely.
Question 2.
When I load a wave file and try to print the resulting array, I get some weird results. Like, instead of a number from -9999 to 9999 as with my sinewave, I get the Spade (from clubs,diamonds.hearts etc) among many other weird symbols. OpenAL doesn't seem to have a problem interpreting and playing this through the same code (replacing the new array loaded with the wave symbols, instead of the sinewave one). - Also, I'd like to dissect wave files into numbers so I can read/study them, so info on that would also be good, thanks.
Question 3.
I looked around online, and at Audacity. The numbers seem to go from -1.0 to 1.0 in a lot of examples. Having been using values from -9999 to 9999(generated from the sine and square formulas), I've no idea what's going on here. Somehow the OpenAL functions are interpreting all these different things into sound. Also If I were to experiment with these values(-1.0 to 1.0), putting them into an array, would I get sound as a result?
Question 4.
Lastly - a bit of theory stuff I'm not sure on (and can't quite find good google resources on) - If I were to create my own sound where every hertz never goes below 0, would the sound be audible? why/why not? Again I haven't tested this myself because of the above problems, but from every sound I've seen, there seems to be an equal amount of hertz thingies below and above 0 (I'm asserting this having looked at sound files in audacity, and the values from the sinewave function).
I'm sure you can tell I'm really beginner at this stuff, but I'm so keen to understand it because of the fun that could be had.
Help is greatly appreciated!