Jump to content

  • Log In with Google      Sign In   
  • Create Account


Creating sounds from scratch - need help understanding


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
6 replies to this topic

#1 Jossos   Members   -  Reputation: 200

Like
0Likes
Like

Posted 17 May 2013 - 05:24 AM

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[i] = 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!

 

 



Sponsor:

#2 Kylotan   Moderators   -  Reputation: 3333

Like
2Likes
Like

Posted 17 May 2013 - 11:46 AM

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.

 

It's because the volume is set to 10000. That's basically the maximum amplitude, and every sample is a fraction of that.

It looks like an arbitrary number in this case. Each sample is a short, which means a signed 16 bit value. This is the same as a compact disc or a typical WAV file. So you could increase volume as high as 32767, which is the largest value that will fit in a short.

This range won't have any effect on whether you can produce a sine wave 'nicely' or not, because it's all relative. If your samples were 24 bit then you might use values that were much larger, and if your values were floats you'd probably stick to -1 to +1.

 

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.

 

That's a programming issue completely unrelated to audio. Basically when you open a wave file it's a binary series of bytes, not human readable decimals. When you print them, some of those bytes are represented by letters, some by numbers, some by symbols, and some are not visible at all. To display the values from a wave file, you need to do something more sophisticated and parse the file according to its specific format. How to do that is a bit tricky but it can be done with a couple of pages of Windows API code.

 

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?

 

Going back to my answer to question 1, these values are arbitrary, and relate to the sample format being used. Digital sound can be described along 2 axes: sample rate (measured in Hertz) and the sample format (eg. 16bit signed integer, 24bit signed integer, 32 bit float). It's the sample format that is of interest here. Basically that specifies how each sample is to be stored. A maximum amplitude sine wave stored in a 16bit format would vary between -32768 to +32767. One in 24bit would vary from -8388608 to 8388607. A 32 bit floating point is slightly different - it's conventional to vary the maximum from -1.0 to +1.0, but unlike the integer formats, you can go outside that range if you like. (With the result being distortion when the time comes to output the value.)

 

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?

 

Hertz is a measure of frequency, not a specific thing in the file. I think you're talking about individual samples. You could certainly make a series of samples that never went below zero and if it still followed the standard sine wave pattern then it would sound pretty much the same. Zero is just an arbitrary point that lies in the middle of the range and has no special meaning.


Edited by Kylotan, 17 May 2013 - 11:47 AM.
crap editor


#3 Jossos   Members   -  Reputation: 200

Like
0Likes
Like

Posted 17 May 2013 - 05:31 PM

Question 1.

 

It's because the volume is set to 10000. That's basically the maximum amplitude, and every sample is a fraction of that.

It looks like an arbitrary number in this case. Each sample is a short, which means a signed 16 bit value. This is the same as a compact disc or a typical WAV file. So you could increase volume as high as 32767, which is the largest value that will fit in a short.

This range won't have any effect on whether you can produce a sine wave 'nicely' or not, because it's all relative. If your samples were 24 bit then you might use values that were much larger, and if your values were floats you'd probably stick to -1 to +1.

 

Thankyou so much. Really helpful stuff!

 

Just I'm having a little trouble understanding your answer to question 1. So, setting the "volume" variable to 32767 (the max of a short) makes the sound louder(as expected), so then why using the -1 to +1 range with a float give the same sound? When you say it's relative, does the sound just depend on the highest and lowest numbers and... uh something happens to "stretch" the numbers or what happens?



#4 bschmidt1962   Crossbones+   -  Reputation: 1781

Like
3Likes
Like

Posted 18 May 2013 - 11:47 AM

Just I'm having a little trouble understanding your answer to question 1. So, setting the "volume" variable to 32767 (the max of a short) makes the sound louder(as expected), so then why using the -1 to +1 range with a float give the same sound?

 

Look back again at Kylotan's answer to your #3.  There are 2 ways to represent numbers for digital audio. You can store them as integers (i.e. counting numbers 1,2,3,etc , with no decimal point) or floating point numbers (that have a decimal point,such as 0.335, -0.89, etc).

if you store a number as a 16-bit integer, the range for that number is -32768 to 32767.  (do a search on "2's complement representation).  So if you have a wave file that uses 16-bit integers, the range will be that, where -32768 corresponds to "most negative voltage" and 32768 will correspond to "most positive voltage" when it's time to turn your digital audio into a signal to send to a speaker.

 

But you can also store numbers as floating point.  In this case, -1.0 is the "most negative voltage" and +1.0 is the "most positive voltage".

 

Since both 1.0 (float) and 32767 (int) represent "maximum possible voltage" they will create the same volume of sound when played.

 

Very often programs like audacity will always show the range as -1 to 1, just to make things easier to read, when in reality,the sample values are -32768 to 32767. In fact, there is a well defined way to translate from floating point numbers to integers, and have the sound be exactly the same:

  float_value = (float) IntValue/32768.0;

of

  int_value = (int) floatValue * 32767;

 

(a bit more advanced answer would be that there is a standard for interpreting 16-bit integer numbers as fractions with a range from -1.0 to 0.99998-- if you're curious about that, do a search for "Q format numbers")


Brian Schmidt

Executive Director, GameSoundCon:

GameSoundCon 2014:October 7-8, Los Angeles, CA

 

Founder, EarGames

Founder, Brian Schmidt Studios, LLC

Music Composition & Sound Design

Audio Technology Consultant


#5 Jossos   Members   -  Reputation: 200

Like
0Likes
Like

Posted 18 May 2013 - 04:09 PM

@bschmidt1962

 

Thanks that clears things up. Would using either float/short be ok? I've decided to use floats now because they seem nicer to use.

 

Also, how would one disect a char array extracted from a wav file (or just the wav file) and get the sample numbers from that? I tried converting the chars to numbers, but I get a lot of random nonsense numbers (-128 to 128) that don't follow the pattern, so that's obviously not the way to interpret it. It would be nice if the numbers of samples could be displayed in audacity, but i dont think there's a way to do that.



#6 bschmidt1962   Crossbones+   -  Reputation: 1781

Like
1Likes
Like

Posted 18 May 2013 - 06:26 PM

Using floats will be much easier, yes, so it's smart to use them.  There are some other advantages; a 32-bit floating point number will give you better dynamic range than 16-bit ints.

 

A wave file consists of 2 main parts: the header and the audio samples themselves

In order to read samples from a wavefile, you have to read and parse the wavefile header.  That has information like the format format of the samples (16bit int, float, etc.) as well as things like sampling rate, number of samples.  http://en.wikipedia.org/wiki/WAV

You can probably search for simple source code on-line that does that.


Brian Schmidt

Executive Director, GameSoundCon:

GameSoundCon 2014:October 7-8, Los Angeles, CA

 

Founder, EarGames

Founder, Brian Schmidt Studios, LLC

Music Composition & Sound Design

Audio Technology Consultant


#7 Kylotan   Moderators   -  Reputation: 3333

Like
0Likes
Like

Posted 19 May 2013 - 03:48 PM

You can use floats if you like, but remember that whatever plays back the audio has to understand the format you've chosen. So if the code you currently have expects shorts (ie. signed 16 bit), you need to keep using that or change whatever part of the code dictates the format when either writing to a file or playing the sound back on the sound card.

 

Also, when you read chars, they are 8 bit values so, by definition, they range from -128 to 127. 2 chars in a row are basically 16 bits of data and can be interpreted as a short, ie. -32768 to 32767. Any file is just a list of bytes on disk, so you won't know whether to read them as chars or shorts or floats unless you know what the format is - which, as Brian said, is stored in the header according to the WAV format. (Other audio types may have a more complex representation, however.)






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS