Wav files

BlitzPlus Forums/BlitzPlus Programming/Wav files

Coops(Posted 2004) [#1]
Hi there.

I wonder if anybody can help...

Using Blitz+, I want to be able to write a .WAV file from scratch, with all relevant headers and chunks etc.. Which can be read by any Windows program that plays Wav files. (i.e. Soundforge)

But what I need to know is...

What format should I write the file? I mean, should I write the file as 1's and 0's entirely? Or should there be Hex and ASCI?

You see, I am programming a physical model of a string, which is easy enough, but being able to compile the data into a listenable form seems much more difficult. (if not, impossible in Blitz+)

Could anybody help me please? I found this site which explains how the structure of a WAV file is organised...
http://www.sonicspot.com/guide/wavefiles.html

Please Help!!
Chris


CS_TBL(Posted 2004) [#2]
www.wotsit.org

For my own softsynth-stuff I do in blitz, what I usually do is just write it all raw, and alt-tab to soundforge where I load the file as raw.

Once it's loaded, (and closed), you can load the file again with alt-f 1 (the first file in the filehistory).

this alt-tabbing works for me.. prolly not much slower than loading the wave file again in blitz.


Coops(Posted 2004) [#3]
what do you mean by 'RAW'?


CS_TBL(Posted 2004) [#4]
a raw waveform is uh .. just 'raw' :) without a header..

Like .txt is raw, because there's no header (like .doc).

So, just write your sample to a file, and use the raw-converter from soundforge to specify what is what.

However, I doubt you want to do heavyweight DSP's such as reverb in Blitz, so I assume you write a mono sound to a file. In that case, if your waveform is 16 bit unsigned mono (values between 0..65535), simply choose 16 bit unsigned mono in the raw converter from soundforge (which will show up when you select 'raw' in the filetypes pulldown menu)


Coops(Posted 2004) [#5]
That sounds ok, and I'm sure you know what you are talking about!

But is there a way to create a free-standing program in Blitz capable of physical models sounding as they might in virtual reality... ?

It's important to have the whole program play the sounds
cheers


wedoe(Posted 2004) [#6]
I did some stuff on this a while back.
I post some here when I get home tomorrow (At work now).

To write a wav-file you just have to write the
wav-headers to the file (stereo/mono/hz/etc) and
in my case (mono) i just made the wav out of bytes
varying from 0-255. Worked a charm.

Since BB doesn't offer wave-modelling the sound must be
written to a file and loaded back with the loadsound command.
I'm sure there are DLL's or userlibs out there that can sculpt a sound on the fly tho.


wedoe(Posted 2004) [#7]
The wav-header writer was written by someone else I think, doesn't look like my style,
well here it is, set up like a function. Play around with it and see what you get:

Function WriteWavHeader(File%, Channels%, Frequency%, Bits%, Samples%)

	Local SampleSize% = Channels% * (Bits% / 8)
	Local WavSize%    = (Samples% * SampleSize%) + 52 ; Size of the wave file - include room for headers
	
	; "RIFF" header text
	WriteByte File%, 82
	WriteByte File%, 73
	WriteByte File%, 70
	WriteByte File%, 70
	
	; Size of the wav file not including the "RIFF" header and this 4-byte file size
	WriteInt File%, WavSize% - 8
	
	; "WAVE" header text
	WriteByte File%, 87
	WriteByte File%, 65
	WriteByte File%, 86
	WriteByte File%, 69
	
	; "fmt " header text
	WriteByte File%, 102
	WriteByte File%, 109
	WriteByte File%, 116
	WriteByte File%, 32
	
	 ; Size of the "fmt " section
	WriteInt File%, 18
	
	; Format - PCM
	WriteByte File%, $01
	WriteByte File%, $00
	
	; Channels
	WriteByte File%,  Channels% And $FF
	WriteByte File%, (Channels% And $FF00) Shr 8
	
	; Samples per second
	WriteInt File%, Frequency%
	
	; Average bytes per second
	WriteInt File%, Frequency% * SampleSize%
	
	; Block align
	WriteByte File%,  SampleSize% And $FF
	WriteByte File%, (SampleSize% And $FF00) Shr 8
	
	; Bits per sample
	WriteByte File%,  Bits% And $FF
	WriteByte File%, (Bits% And $FF00) Shr 8
	
	; Amount of extra information bytes
	WriteByte File%, $00
	WriteByte File%, $00
	
	; "data" header text
	WriteByte File%, 100
	WriteByte File%, 97
	WriteByte File%, 116
	WriteByte File%, 97
	
	; Size of the data section
	WriteInt File%, WavSize% - 52

End Function


After you written the wav-header just fill inn the wav-data.
Use some sine-curves to get you started.


Coops(Posted 2004) [#8]
You are an absolute star!

Thankyou so much.

Is there any tips you can give as to what numbers to use as the data?

Coops


wedoe(Posted 2004) [#9]
Not really, just play around with it, short sine-curves are a good start.

Like: 0,1,3,10,20,30,40,50,60,70,80,90,100,107,109,110,109,107,100,90,80,70,60,50,40,30,20,10,3,1,0
(Not actually a sine, just from the top of my head as a start.)
The curve should vary from -127 to + 127 IFAI can remember.


sswift(Posted 2004) [#10]
Look in that "cool demos in blitz" thread in the general forum. My pong example near the bottom of the thread uses this function to write a wave file header and then fills the file with a 16 bit 44khz sound effect which I generate dynamically.

The file shows how to make a ping noise with a sine wave, and static, both of which fade out over time. The code is kinda messy though. And I'm not entirely positive I wrote the sine wave out to the file properly. Sounds kinda harsh. But it's a start. :-)

Oh and 8 bit sounds vary from -127 to 128. 16 bit goes from -32767 to 32768.