From: Larry on
Hi,

I was reading something about RIFF and wave format file here:
http://www.codeguru.com/cpp/g-m/multimedia/audio/article.php/c8935/
http://www.codeguru.com/cpp/g-m/multimedia/audio/article.php/c8935__2/

"...a WAVE file is a collection of a number of different types of chunks.
But, there are three chunks that are required to be present in a valid wave
file, RIFF, FMT and DATA"

Now my question is: are they sort of tags like in mp3 (idv2 tags) or they
are part of each chunk?

I mean, If I do some capturing with the waveForm API and I'd like to save
the buffers (4096 bytes each) should I put those 3 tags at the very
beginning fo the file or each every chunk of raw data? (RIFF,FMT,DATA+4096
bytes...again RIFF,FMT,DATA+4096...) ????

thanks



From: ScottMcP [MVP] on
On Dec 26, 7:51 pm, "Larry" <dontmewit...(a)got.it> wrote:
> Hi,
>
>    I was reading something about RIFF and wave format file here:http://www.codeguru.com/cpp/g-m/multimedia/audio/article.php/c8935/http://www.codeguru.com/cpp/g-m/multimedia/audio/article.php/c8935__2/
>
> "...a WAVE file is a collection of a number of different types of chunks.
> But, there are three chunks that are required to be present in a valid wave
> file, RIFF, FMT and DATA"
>
> Now my question is: are they sort of tags like in mp3 (idv2 tags) or they
> are part of each chunk?
>
> I mean, If I do some capturing with the waveForm API and I'd like to save
> the buffers (4096 bytes each) should I put those 3 tags at the very
> beginning fo the file or each every chunk of raw data? (RIFF,FMT,DATA+4096
> bytes...again RIFF,FMT,DATA+4096...) ????
>
> thanks

Writing a WAV file using ordinary file APIs is difficult. But Windows
has functions that handle most of it for you. Download these SDK
audio samples. The one named LOWPASS is a good example of writing a
WAV file.

<http://www.microsoft.com/downloads/details.aspx?
displaylang=en&FamilyID=7aae2273-0320-4419-a0c3-a8c063df9d36>
From: Larry on

"ScottMcP [MVP]" <scottmcp(a)mvps.org> ha scritto nel messaggio
news:1195d8f9-eddd-48ca-b13d-f05bbe6e1938(a)q16g2000vbc.googlegroups.com...

> Writing a WAV file using ordinary file APIs is difficult. But Windows
> has functions that handle most of it for you. Download these SDK
> audio samples. The one named LOWPASS is a good example of writing a
> WAV file.

Ok. By now, I have taken the difficult path so I cannot get out till I get
it sorted out!

Right now I am codign what should be put ahead of any chunk of raw data, the
thing is, I have to get a binary string out of all of this (is there anyway
to pack several structs in a single string?) thanks

#include <windows.h>
#include <stdlib.h>

const unsigned int STANDARD_WAVEFORMAT_SIZE = 16;

struct WAVE_FORMAT
{
WORD wFormatTag;
WORD wChannels;
DWORD dwSamplesPerSec;
DWORD dwAvgBytesPerSec;
WORD wBlockAlign;
WORD wBitsPerSample;
};

struct RIFF_HEADER
{
TCHAR szRiffID[4]; // 'R','I','F','F'
DWORD dwRiffSize;
TCHAR szRiffFormat[4]; // 'W','A','V','E'
};

struct FMT_BLOCK
{
TCHAR szFmtID[4]; // 'f','m','t',' '
DWORD dwFmtSize;
WAVE_FORMAT wavFormat;
};

struct DATA_BLOCK
{
TCHAR szDataID[4]; // 'd','a','t','a'
DWORD dwDataSize;
};

void GetARiffChunk(RIFF_HEADER& rh);
void GetADataBlock(DATA_BLOCK& db);
void GetAWaveFormat(WAVE_FORMAT& wf);
void GetAFmtBlock(FMT_BLOCK& fb);

void main()
{
RIFF_HEADER rh;
FMT_BLOCK fb;
DATA_BLOCK db;

GetARiffChunk(rh);
GetAFmtBlock(fb);
GetADataBlock(db);

system("pause");
}

void GetARiffChunk(RIFF_HEADER& rh)
{
rh.dwRiffSize = sizeof(rh);
rh.szRiffID[0] = 'R';
rh.szRiffID[1] = 'I';
rh.szRiffID[2] = 'F';
rh.szRiffID[3] = 'F';
// Lets add the wave tag id.
rh.szRiffFormat[0] = 'W';
rh.szRiffFormat[1] = 'A';
rh.szRiffFormat[2] = 'V';
rh.szRiffFormat[3] = 'E';
rh.dwRiffSize = sizeof(rh);
};

void GetADataBlock(DATA_BLOCK& db)
{
db.szDataID[0] = 'd';
db.szDataID[1] = 'a';
db.szDataID[2] = 't';
db.szDataID[3] = 'a';
db.dwDataSize = 4096;
}

void GetAWaveFormat(WAVE_FORMAT& wf)
{
wf.dwAvgBytesPerSec = 0;
wf.dwSamplesPerSec = 0;
wf.wBitsPerSample = 0;
wf.wBlockAlign = 0;
wf.wChannels = 0;
wf.wFormatTag = 1;
}

void GetAFmtBlock(FMT_BLOCK& fb)
{
WAVE_FORMAT wf;
GetAWaveFormat(wf);
fb.szFmtID[0] = 'f';
fb.szFmtID[1] = 'm';
fb.szFmtID[2] = 't';
fb.szFmtID[3] = ' ';
fb.wavFormat = wf;
fb.dwFmtSize = sizeof(fb);
}


From: Larry on

"Larry" <dontmewithme(a)got.it> ha scritto nel messaggio
news:4b36bd1e$0$1121$4fafbaef(a)reader2.news.tin.it...
>
> "ScottMcP [MVP]" <scottmcp(a)mvps.org> ha scritto nel messaggio
> news:1195d8f9-eddd-48ca-b13d-f05bbe6e1938(a)q16g2000vbc.googlegroups.com...
>
>> Writing a WAV file using ordinary file APIs is difficult. But Windows
>> has functions that handle most of it for you. Download these SDK
>> audio samples. The one named LOWPASS is a good example of writing a
>> WAV file.

Actually I have just found this:
http://msdn.microsoft.com/en-us/library/dd757663%28VS.85%29.aspx

It basically uses the mmioOpen to create a riff chunk in a file! So I coded
this for the moment:

HMMIO hfile;
MMCKINFO mmckinfo;
mmckinfo.fccType = mmioFOURCC('W', 'A', 'V', 'E');
hfile = mmioOpen(file, NULL, MMIO_CREATE|MMIO_WRITE);
mmioCreateChunk(hfile, &mmckinfo, MMIO_CREATERIFF);

I wonder how I can add the "fmt" with the formatex so that I have a real
chuck!

thanks

From: Bob Masta on
On Sun, 27 Dec 2009 01:51:36 +0100, "Larry"
<dontmewithme(a)got.it> wrote:

>Hi,
>
> I was reading something about RIFF and wave format file here:
>http://www.codeguru.com/cpp/g-m/multimedia/audio/article.php/c8935/
>http://www.codeguru.com/cpp/g-m/multimedia/audio/article.php/c8935__2/
>
>"...a WAVE file is a collection of a number of different types of chunks.
>But, there are three chunks that are required to be present in a valid wave
>file, RIFF, FMT and DATA"
>
>Now my question is: are they sort of tags like in mp3 (idv2 tags) or they
>are part of each chunk?
>
>I mean, If I do some capturing with the waveForm API and I'd like to save
>the buffers (4096 bytes each) should I put those 3 tags at the very
>beginning fo the file or each every chunk of raw data? (RIFF,FMT,DATA+4096
>bytes...again RIFF,FMT,DATA+4096...) ????
>

Begging to differ with Scott, but writing WAV
files with standard file APIs need not be too
difficult. You need to create one RIFF chunk, one
WAVE chunk, and one DATA chunk for all the data,
no matter how much it is (up to 2 GB or so limit
of RIFF format.)

The DATA section is just what you are already
recording. The WAVE format header includes sample
rate plus a few arcane details that you can
usually boiler-plate if you are running at fixed
sample rate. The only thing that's at all tricky
is that the RIFF and DATA chunk size dwords are
written last, after the recording is done (when
you know what the final size is). So you have to
move the file pointer back and fill those in after
initially writing dummy placeholder values.

Best regards,


Bob Masta

DAQARTA v5.00
Data AcQuisition And Real-Time Analysis
www.daqarta.com
Scope, Spectrum, Spectrogram, Sound Level Meter
Frequency Counter, FREE Signal Generator
Pitch Track, Pitch-to-MIDI
DaqMusic - FREE MUSIC, Forever!
(Some assembly required)
Science (and fun!) with your sound card!