Board index » delphi » Writing Wave File Headers...

Writing Wave File Headers...

Hi,

I'm trying to write a program that can generate wave files from a set of
frequencies with a specificied start and stop times.

Now, I can generate the raw data for the files (which appears to be
correct), but I am having some difficulty with the header. Quite simply,
it isn't recognised, despite my best efforts to get it right.

The problem I have is that I don't seem to be able to copy the
information from memory to the file correctly - eg. the RIFF identifier
(just 4 bytes - one for each letter) ends up with another byte infront
of it.

Has anyone done this before? Or does anyone know a way of writing hex or
binary values to files? Or where I might find out more?

The code I'm currently using to write the header is below.

Thanks for any help you can give me,

Andy

{create the wave header}
  TopEnd.Id:='RIFF';                           {RIFF header}
  TopEnd.length:=(NoOfSamples*2)+36;           {total file length}

  TopEnd.waveid:='WAVE';                       {wave data}

  TopEnd.fmtid:='fmt ';                        {format chunk header}
  TopEnd.fmtlength:=13;                 {13 bytes in format chunk}
  TopEnd.wFormatTag:=$0000;                    {microsoft unknown
format}
  TopEnd.wChannels:=$0001;                     {mono channel}
  TopEnd.dwSamplesPerSec:=44100;               {44.1kHz playback}
  TopEnd.dwAvgBytesPerSec:=TopEnd.dwSamplesPerSec*2;      {44.1kHz
average}
  TopEnd.wBlockAlign:=$7e81;                            {block align}

  TopEnd.dataid:='DATA';                        {data chunk header}
  TopEnd.datalength:=(NoOfSamples*2);           {data length (mono
samples, but 16 bit values)}

  {write the file}
  assignfile(OUTPUTFILE,'c:\results\squeek2.wav');
  rewrite(OUTPUTFILE,2);
  blockwrite(OUTPUTFILE,topend,44);

 

Re:Writing Wave File Headers...


Quote
Andrew Burns wrote in message <360A58F4.7A548...@dial.pipex.com>...
>Has anyone done this before? Or does anyone know a way of writing hex or
>binary values to files? Or where I might find out more?

I've collected a few links about Delphi and wave files.  They're at
www.infomaster.net/external/efg/Library/Delphi/Graphics/Multimedia.htm

efg
_________________________________________
efg's Computer Lab:  http://infomaster.net/external/efg

Earl F. Glynn                 E-Mail:  EarlGl...@att.net
Overland Park, KS  USA

Re:Writing Wave File Headers...


On Thu, 24 Sep 1998 15:36:36 +0100, Andrew Burns

Quote
<mak...@dial.pipex.com> wrote:
>I'm trying to write a program that can generate wave files from a set of
>frequencies with a specificied start and stop times.

>Now, I can generate the raw data for the files (which appears to be
>correct), but I am having some difficulty with the header. Quite simply,
>it isn't recognised, despite my best efforts to get it right.

>The problem I have is that I don't seem to be able to copy the
>information from memory to the file correctly - eg. the RIFF identifier
>(just 4 bytes - one for each letter) ends up with another byte infront
>of it.

Hi Andy,
unfortunatley you haven't give a declaration of top end, so I can
guess only....
The chunk identifiers are 4 characters without ending zero. You can
use the MMSystem macro mmioFOURCC to create them:

Quote
>  TopEnd.Id:='RIFF';                           {RIFF header}

TopEnd.ID:=mmioFOURCC('R','I','F','F');

Quote
>  TopEnd.wFormatTag:=$0000;                    {microsoft unknown
>format}

Why do you use unknown format and not a common format like Windows
PCM?

For example the PCM header looks like this:

type
TWaveHeader = record
       rID             : array[0..3] of char;          { 'RIFF' }
       rLen            : longint;                          { Number of
data bytes + offset to data chunk = 36 }
       wID             : array[0..3] of char;        { 'WAVE' }
       fId             : array[0..3] of char;          { 'fmt ' }
       fLen            : longint;                         { length of
fmt chunk without chunk ID = $10 }
       wFormatTag      : word;                     { WAVE_FORMAT_PCM
= $0001; }
       nChannels       : word;                       { 1= Mono, 2 =
Stereo }
       nSamplesPerSec  : longint;                { e.g. 44100 }
       nAvgBytesPerSec : longint;              {
nChannels*(nBitsperSample div 8);
nSamplesperSec*nChannels*trunc(nBitsPerSample div 8);
       nBlockAlign     : word;                      {
       nBitsPerSample  : word;                   { 8 or 16 }
       dId             : array[0..3]of char;         { 'data' }
       dLen            : longint;                       { number of
following data bytes }
end;

Hope this helps.
- Ulli -

Re:Writing Wave File Headers...


Check out your header format at http://www.wotsit.org . It's a great place
describing hundred of file formats.
Very interesting.

Best regards from France,
Philippe.

Other Threads