WAVE

Discuss anything programming related.
Post Reply
User avatar
Zaid
Posts: 250
Joined: Sun Jan 09, 2011 2:07 am

WAVE

Post by Zaid »

I have been learning the WAV file format and I kinda get how it's put together I have been trying to make a small app that loads all the chunks in the selected file but I can't seem to read it correctly.
I have tried making a WAV file from scratch using a tutorial I found and it works I just can't seem to add the instrument Chunk.
Does anyone know about this?

Some tutorials:
http://blogs.msdn.com/dawate/archive/20 ... ented.aspx
http://blogs.msdn.com/dawate/archive/20 ... ormat.aspx
http://blogs.msdn.com/dawate/archive/20 ... ing-c.aspx
http://blogs.msdn.com/dawate/archive/20 ... -in-c.aspx

WAV File Format:
http://www.sonicspot.com/guide/wavefiles.html
User avatar
troymac1ure
Keeper of Entity
Posts: 1282
Joined: Sat Aug 09, 2008 4:16 am
Location: British Columbia, Canada, eh
Contact:

Re: WAVE

Post by troymac1ure »

Haven't touched the wav format in years, but the setup is pretty basic.

The instrument playback may be a limitation in the software you are playing back with, not sure.

What is wrong with the reading portion? Try stepping through your code and add watches in so you can see the data being read. That way you can tell where you're going wrong as well. I assume you can read the main header no problem and the issue lies within one of the chunks.
As stated, strings have a preceding byte stating the length, so make sure on (eg. Label) chunk that you read this byte and then read the string by the given length. Also, don't rely upon a chunk being correct. Assume that the data inside is screwed and when you are done dealing with that chunk set the pointer to the next section of data by the length given in the header. This should help eliminate incorrectly coded chunks and your program will not error if it encounters an unknown chunk.

Make some general procedures, such as: (this is from memory and I haven't programmed in months, so it probably has errors)

Code: Select all

string readStringFromStream(stream s)
{
 byte length = s.ReadByte();
 return (new string(s.ReadChars(length)));
}
Anyways, step-by-step debugging is probably you're first step to finding out where your wrong. If you need help, post back.
User avatar
Zaid
Posts: 250
Joined: Sun Jan 09, 2011 2:07 am

Re: WAVE

Post by Zaid »

Okay, well how do I go about changing the frequency of of a file?

Do I edit the samples directly or what?

Right now I have a wave generator from the tutorials and It only uses the chunks (Header,format,data) and would I just read the samples and change those or would I add a different chunk to it?
User avatar
troymac1ure
Keeper of Entity
Posts: 1282
Joined: Sat Aug 09, 2008 4:16 am
Location: British Columbia, Canada, eh
Contact:

Re: WAVE

Post by troymac1ure »

The easiest way to change the frequency is to change the sample rate (but this also changes the length of the sound)
I never edited sound files, just played back mainly.

In a basic sense, the way that the raw data is stored is just how far in or out the speaker will move.
For example, in a 16-bit file at 0, the speaker is at rest. At 32,767 the speaker is fully out and at -32,768 the speaker is fully in. If you send 32,767, the speaker will just sit at full out position and (of course) no sound will be created as the speaker is not moving. If you send and alternating signal of 32,767 and -32,768 the speaker will jump back and forth between full in & full out and you will get a high pitched whine.

So to create different frequencies, you need to send out varying signals at different rates (slower rates for lower frequencies, faster for higher).
Take the following for example:

Code: Select all

          ----                  ----                ----                   ----         
      ---     ---            ---    ---          ---    ---            ---    ---      
   ---           ---      ---          ---    ---          ---      ---          ---   
---                  ----                 ----                 ----                 ---
Assuming this is a 440hz signal, you can change the frequency higher by playing these values at a faster speed (shortening the length of the sample) or calculate where the frequency step should be and loop that frequency for the time duration

Code: Select all

      --           --           --            --          --           --           --
    --  --       --  --       --  --       --  --       --  --       --  --       --
  --      --   --      --   --      --   --      --   --      --   --      --   --    
--          ---          ---          ---          ---          ---          ---        
Notice that you now have 6.5 wave cycles instead of 4 to keep the same duration, but with a higher pitch. This requires some math (never tried this, but it seems like it would be a fairly basic formula for a single frequency file). The problem is determining the frequencies (as you will most likely have frequencies overlapping each other in the majority of wave files)

You could modify your generator to have different buttons that represent each key frequency (440hz = A http://www.google.ca/url?sa=t&rct=j&q=k ... KgkHZQhbAQ)
and have a timer that records how long you press each button for. Then you can write these frequencies to the file for the specified duration and you will have a very basic recordable keyboard.

Hope this helps a tad.
Post Reply