• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
Jossos

Creating sounds from scratch - need help understanding

6 posts in this topic

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!

 

 

0

Share this post


Link to post
Share on other sites

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
crap editor
2

Share this post


Link to post
Share on other sites

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?

0

Share this post


Link to post
Share on other sites

@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.

0

Share this post


Link to post
Share on other sites

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.

1

Share this post


Link to post
Share on other sites

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.)

0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0