SIMPLE *.Wav File Altercations

3eb49b762e76d2658c6b66127afee3ac
0
Cakey 101 Aug 15, 2008 at 03:22

Introduction - YOU DON’T HAVE TO READ THIS PART
Hi I’m Cakey, new to the forums. Not new to programming OR game development. I’ve written 3-D games in JAVA, VBC, darkbasic(a joke I know), and plenty of 2-D games in VB .NET, Flash, java-script/html/css etc. Here’s the scoop, poop(not being offensive but attempting to be funny?).

Background Information:
So I am writing this program in VB .net, and of course VB. net’s functionality won’t let me go where I need to go with this. So I decided to take on the task of learning C++. Not too complex as everyone complains about. I’m sadly currently running Vista on a lab-top(if this even should matter?).

OBJECTIVE:
In essence what I am trying to do is load a .wav file, add blank data to it for X seconds. So like I want to load BLEEP.wav then add say 0.5 seconds of blank sound to the end of the BLEEP.wav’s loaded data and save it to the hard-drive as another named *.wav(say booger.wav).

My Ideas:
I’m thinking DirectSound(Yes I have the SDK). I’m using DevCPP because VC++ is so faulty(opinion). Please don’t refer me to the SDK “sample” files that don’t work (except for the play-sound).

If you think DirectSound is a bad approach could you please suggest a different route(I’m trying to write a .dll so don’t recommend me to something unsupported)?

Your Part?
Are there any tutorials similiar to this(I can’t find ‘em)? Links, hints, tutorials, and example script would be impeccably appreciated, I need closure I’ve been trying every worm-hole to solve this for the past three - four weeks. Learning a new language, installing/reinstalling compilers&IDE’s, googling everything known to man. Dealing with thing’s like AFXRES.H not existing! I’m going crazy here!

6 Replies

Please log in or register to post a reply.

A8433b04cb41dd57113740b779f61acb
0
Reedbeta 167 Aug 15, 2008 at 03:40

Actually, there’s no need for DirectSound to do that. WAV files, like all files, are just a sequence of bytes - in order to modify one, you can load the file into memory, modify it there, and then save it back to disk. The WAV format is pretty simple and it’s not very hard at all to write your own routines to manipulate it, and it will be a good exercise to learn C++ better, and to understand what libraries like DirectSound are doing under the hood. Just google for the WAV format and you should find plenty of documentation. ;) To get you started, here is a short description of the basics of the format, and here is a more comprehensive description.

3eb49b762e76d2658c6b66127afee3ac
0
Cakey 101 Aug 15, 2008 at 06:14

Wow, that spells things out for me. Thanks reed-beta. So your saying no SDK “bull pucky” is neccessary. So would file write & read functions be a better method?

I knew about the “RIFF heading” thing. Not exactly sure how to write it. I guess I am just a little lost on how to add the whole silence for X seconds to a file part. I understand now that I’d have to modify the “data” portion of wave file.

I’m sorry like I stated before I am new to C++/Audio Programming. Thank you so much for the links and helping me better understand the audio format. You have brought me that much closer to my goal. However I am kind of unsure/clueless how I would read/write this data. I am not sober right now so I will have to revisit these links tommarrow after some college tests. If you/anyone else would happen to have some more clarification on this it’d be appreciated but I want to try to work from here- tomarrow.

6837d514b487de395be51432d9cdd078
0
TheNut 179 Aug 15, 2008 at 10:58

Probably it’s a good idea to understand digital/analog wave theory first. Have a look here: http://www.mediacollege.com/audio/01/. It’s quick and you should pick up on that fairly well. But, just to add onto it:

In the digital world
1) One sample = one frequency, although a sample is NOT the same thing as a frequency. It’s just common analog -> digital conversion to use the same number. If you have a 44100HZ audio file, you have 44100 samples per second in your wave.

2) Audio is represented in bytes (duh ;), meaning an audio sample is typically between -127 and 128. Or if it’s a 16bit audio sample, it’s between -32767 and 32768, and so on.

3) In order to generate sound, the samples need to have a sinusoidal-like waveform. Meaning if you analyze the bytes in a wave, it will look like a roller coaster ride. It should go up and then down, then back up and so on. As you manipulate those properties, you will generate different sounds. Search up on “synthesizers” to get a better understanding of how this works, including getting a glimpse of a DJ’s life ;)

So once you understand the basics, you should know that:
- The number of bytes per second of sound = (samples per second) * (number of channels) * (bytes per sample)
Where by now you should know that samples per second is the same number as frequency. The number of channels would be 1 for mono, 2 for stereo, etc…, and bytes per sample is typically 1, 2, or 3, although people commonly refer to it as bits such as 8, 16, or 24 (but real devs only work in bytes ;). If you’re importing audio from wav, mp3, ogg, etc… they have all this informaton in the headers.

Knowing the above equation should allow you to easily calculate how many bytes of zeros you would need to append to the wave form to add silence. Now you should ask yourself “why do zeros silence the wave? Here’s another question you should ask. Could I generate silence by filling it with any value? The answer is yes. You could do a memory copy with 0xFF, 0xA2, 0x24, etc… As long as there is no oscillation, you will not hear any sound. Zeroes are just easier to work with in editors because the 0’th line is in the center of the oscilloscope.

Once you grasp all of this, dealing with sounds (or wave theory in general) is actually simple and fun. Don’t think about wave, mp3, ogg, etc… They are just formats. Audio in the end is broken down into 3 simple factors: wavelength, amplitude, and frequency. Play with those and you’ll pick up on stuff very quickly. A while back I even wrote a program where the computer tries to make music. heh… funny stuff. Brought a whole new meaning to the word chip-tunes ;)

A8433b04cb41dd57113740b779f61acb
0
Reedbeta 167 Aug 15, 2008 at 16:33

@Cakey

So would file write & read functions be a better method?

Yes, you’ll want to look up how to do file I/O in C++. For starters you’ll probably want to look at the “stdio” functions, particularly: fopen, fread, fwrite, and fclose. Also see this article. Note that I’m showing you more of a C-style way to manipulate files than a C++-style way, but I personally prefer to use the C functions. You can also, if you like, use the C++ file I/O libraries, and here is a tutorial on those.

3eb49b762e76d2658c6b66127afee3ac
0
Cakey 101 Aug 15, 2008 at 20:17

Wow awesome responses here guys. You all obviously know what your talking about. I am gonna try to give this all a stab but as a fore-warning I might end up posting back here with some scripting help. I am using [extern “C”] I dunno if that means I am not using C++ or not but I think so far so good.

However for now I am going to try this, once again thanks a bunch!

A8433b04cb41dd57113740b779f61acb
0
Reedbeta 167 Aug 15, 2008 at 21:08

extern “C” means you’re using C++. ;)

(It’s necessary when linking some library or other code written in C into a program written in C++. It exists to smooth some of the slight incompatibilities between the two.)