Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > reading wav files

Reply
Thread Tools

reading wav files

 
 
Sarchiapone
Guest
Posts: n/a
 
      01-30-2010
Has anyone experieces in reading wav files ? After some efforts I was
able to read the header, but now I need to read the data. I really don't
understand if they are byte, short in or int. I only get negative
numbers, if I execute this code

while(fread(&c,sizeof(short int),1,fp)>0)
{
printf("%d\n",c);
}

 
Reply With Quote
 
 
 
 
Antoninus Twink
Guest
Posts: n/a
 
      01-30-2010
On 30 Jan 2010 at 15:18, Sarchiapone wrote:
> Has anyone experieces in reading wav files ?


No, but here's where I'd start: http://www.mega-nerd.com/libsndfile/

 
Reply With Quote
 
 
 
 
Sjouke Burry
Guest
Posts: n/a
 
      01-30-2010
Sarchiapone wrote:
> Has anyone experieces in reading wav files ? After some efforts I was
> able to read the header, but now I need to read the data. I really don't
> understand if they are byte, short in or int. I only get negative
> numbers, if I execute this code
>
> while(fread(&c,sizeof(short int),1,fp)>0)
> {
> printf("%d\n",c);
> }
>

I have a piece of dos 16 bit software, reading, testing
and then sounding a wav file on an SB PRO compatible
soundcard.
If you want it, tell me where to mail it, source code and exe.
 
Reply With Quote
 
Beej Jorgensen
Guest
Posts: n/a
 
      01-30-2010
On 01/30/2010 07:18 AM, Sarchiapone wrote:
> Has anyone experieces in reading wav files ? After some efforts I was
> able to read the header, but now I need to read the data. I really don't
> understand if they are byte, short in or int. I only get negative
> numbers, if I execute this code


Here's another web page about it:

http://www.dragonwins.com/wav/

Each sample type is dictated by the "sample depth" you got out of the
"fmt " subchunk. If it's 8 bits or less, it's unsigned, else it's
signed. The number of bytes taken by the sample is the minimum number
of whole bytes that can store it.

> while(fread(&c,sizeof(short int),1,fp)>0)
> {
> printf("%d\n",c);
> }


It's weird, to me, that every number should print as negative, though.
It seems like /some/ of them should come out as positive even if you're
reading data of the wrong size or your alignment is off. Maybe it's
just extremely bad luck?

-Beej

 
Reply With Quote
 
Sarchiapone
Guest
Posts: n/a
 
      01-30-2010
Il Sat, 30 Jan 2010 18:03:12 +0100, Sjouke Burry ha scritto:

> Sarchiapone wrote:
>> Has anyone experieces in reading wav files ? After some efforts I was
>> able to read the header, but now I need to read the data. I really
>> don't understand if they are byte, short in or int. I only get negative
>> numbers, if I execute this code
>>
>> while(fread(&c,sizeof(short int),1,fp)>0) {
>> printf("%d\n",c);
>> }
>>

> I have a piece of dos 16 bit software, reading, testing and then
> sounding a wav file on an SB PRO compatible soundcard.
> If you want it, tell me where to mail it, source code and exe.


Well, I need source code, for I am compiling under Linux OS. If you could
send me the code, it would be great. My email address is francesco dot
difusco at gmail dot com

Francesco
 
Reply With Quote
 
Sarchiapone
Guest
Posts: n/a
 
      01-30-2010
Il Sat, 30 Jan 2010 10:07:51 -0800, Beej Jorgensen ha scritto:

> On 01/30/2010 07:18 AM, Sarchiapone wrote:
>> Has anyone experieces in reading wav files ? After some efforts I was
>> able to read the header, but now I need to read the data. I really
>> don't understand if they are byte, short in or int. I only get negative
>> numbers, if I execute this code

>
> Here's another web page about it:
>
> http://www.dragonwins.com/wav/
>
> Each sample type is dictated by the "sample depth" you got out of the
> "fmt " subchunk. If it's 8 bits or less, it's unsigned, else it's
> signed. The number of bytes taken by the sample is the minimum number
> of whole bytes that can store it.
>
>> while(fread(&c,sizeof(short int),1,fp)>0) {
>> printf("%d\n",c);
>> }

>
> It's weird, to me, that every number should print as negative, though.
> It seems like /some/ of them should come out as positive even if you're
> reading data of the wrong size or your alignment is off. Maybe it's
> just extremely bad luck?
>
> -Beej


Maybe it is extremely bad luck
 
Reply With Quote
 
Moi
Guest
Posts: n/a
 
      01-30-2010
On Sat, 30 Jan 2010 15:18:25 +0000, Sarchiapone wrote:

> Has anyone experieces in reading wav files ? After some efforts I was
> able to read the header, but now I need to read the data. I really don't
> understand if they are byte, short in or int. I only get negative
> numbers, if I execute this code
>
> while(fread(&c,sizeof(short int),1,fp)>0) {
> printf("%d\n",c);
> }


What is the definition of c ?

If c is a signed short, why do you use %d as a format (%d expects an int).
use %hd if c is a short.
Also, I believe the sample frequency for .wav files is 44KHz,
which means that a typical music frequency takes in the order of 100
cycles for a full cycle. If the first sample is negative, I'd expect
the next ~~ 50 cycles to be negative, too.

BTW: post some complete code.
BTW2: use hexdump or octdump to verify your program's logic.
Just visually skip the file header.

HTH,
AvK
 
Reply With Quote
 
Sarchiapone
Guest
Posts: n/a
 
      01-30-2010
Il Sat, 30 Jan 2010 19:50:48 +0100, Moi ha scritto:

> On Sat, 30 Jan 2010 15:18:25 +0000, Sarchiapone wrote:
>
>> Has anyone experieces in reading wav files ? After some efforts I was
>> able to read the header, but now I need to read the data. I really
>> don't understand if they are byte, short in or int. I only get negative
>> numbers, if I execute this code
>>
>> while(fread(&c,sizeof(short int),1,fp)>0) { printf("%d\n",c);
>> }

>
> What is the definition of c ?


short int c;
>
> If c is a signed short, why do you use %d as a format (%d expects an
> int). use %hd if c is a short.
> Also, I believe the sample frequency for .wav files is 44KHz, which
> means that a typical music frequency takes in the order of 100
> cycles for a full cycle. If the first sample is negative, I'd expect
> the next ~~ 50 cycles to be negative, too.


I counted negative and positive values, and they were all negatives.
>
> BTW: post some complete code.
> BTW2: use hexdump or octdump to verify your program's logic. Just
> visually skip the file header.



This is the code

#include "stdio.h"
#include "stdlib.h"
#include "string.h"
typedef unsigned char uint8;
typedef unsigned short uint16;
typedef unsigned long uint32;
typedef enum {false,true} boolean;
typedef struct
{
unsigned char chunkID[4]; // Contiene le lettere "RIFF" in
formato ASCII form (0x52494646 formato big-endian)
unsigned int ChunkSize; // 36 + SubChunk2Size, o più
precisamente:
// 4 + (8 + SubChunk1Size) + (8 + SubChunk2Size)
// Questa è la dimensione del resto del chunk
che segue questo numero.Questa è la dimensione dell'intero file
// in bytes meno 8 bytes per i due campi non
inclusi in queto conteggio
// ChunkID e ChunkSize.
unsigned char Format[4]; // Contiene le lettere
"WAVE"(0x57415645 formato big-endian).
unsigned char Subchunk1ID[4]; // Contiene le lettere "fmt
" (0x666d7420 formato big-endian).
unsigned int Subchunk1Size; // 16 for PCM. This is the size of the
rest of the Subchunk which follows this number
short int AudioFormat; // PCM = 1 (i.e. Linear quantization)
// Values other than 1 indicate someform of
compression.
short int NumChannels; // Mono = 1, Stereo += 2, etc.
int SampleRate; // 8000, 44100, etc.
int ByteRate; // = SampleRate * NumChannels * BitsPerSample/8
short int BlockAlign; // = NumChannels * BitsPerSample/8
// The number of bytes for one sample including
all channels
short int BitsPerSample;// 8 bits += 8, 16 bits += 16, etc.
short int Extra;
unsigned char Data[4]; // Contains the letters "data" (0x64617461 big-
endian form).
unsigned int DataSize ; // = NumSamples * NumChannels *
BitsPerSample/8 This is the number of bytes in the data.
} __attribute__ (( packed )) RIFF_HEADER;
boolean ispadded(RIFF_HEADER *header);
int main(int argc, char *argv[])
{
RIFF_HEADER *header;
FILE *fp;
int i;
int num_neg=0;
int num_pos=0;
short int c;
char *filename;
char *dati;
if(argc>1)
{
printf("Lunghezza argv[1]: %d\n",strlen(argv[1]));
filename=(char *)(malloc(strlen(argv[1])+1));
strcpy(filename,argv[1]);
printf("Filename:%s\n",filename);
fp=fopen(filename,"rb");
}
header=(RIFF_HEADER *)malloc(sizeof(RIFF_HEADER));
printf("Header Size=%d\n",sizeof(RIFF_HEADER));
fread(header,sizeof(RIFF_HEADER),1,fp);
printf("Is Padded %d\n",ispadded(header));
printf("ChunkID:%4.4s\n",header->chunkID);
printf("ChunkSize:%u\n",header->ChunkSize);
printf("Format:%4.4s\n",header->Format);
printf("Subchunk1ID:%4.4s\n", header->Subchunk1ID);
printf("Subchunk1Size:%u\n",header->Subchunk1Size);
printf("AudioFormat:%d\n",header->AudioFormat);
printf("NumChannels:%d\n",header->NumChannels);
printf("SampleRate:%d\n",header->SampleRate);
printf("ByteRate:%d\n",header->ByteRate);
printf("BlockAlign:%d\n",header->BlockAlign);
printf("BitsPerSample:%d\n",header->BitsPerSample);
printf("Extra:%d\n",header->Extra);
printf("Data:%4.4s\n",header->Data);
printf("DataSize:%u\n",(header->DataSize));
dati=(char *) malloc(header->DataSize+1);
if(dati==NULL) printf("Errore. memoria insufficiente!\n");
i=0;
//if (fread(dati,header->DataSize,1,fp)>0) printf("Lettura eseguita
correttamente\n");
//else
//printf("Lettura non eseguita correttamente\n");
//printf("strlen(dati)=%d\n",strlen(dati));
//dati[strlen(dati)+1]='\0';
//printf("strlen(dati)=%d\n",strlen(dati));
//fread(&c,sizeof(int),1,fp);

while(fread(&c,sizeof(short int),1,fp)>0)
{
if (c>0) num_pos++;
else
num_neg++;
printf("%hd\n",c);
}
printf("\n Neg:%d Pos:%d\n",num_neg, num_pos);
//while(*dati!='\0'){

//for (i=0;i<header->DataSize;i++)
//printf("\t i=%d dati=%d",i,dati[i]);
return 0;
}
boolean ispadded(RIFF_HEADER *header)
{
short int sizeofheaders=sizeof(RIFF_HEADER);
short int size_of_fields=0;
size_of_fields+=sizeof(header->chunkID);
size_of_fields+=sizeof(header->ChunkSize);
size_of_fields+=sizeof(header->Format);
size_of_fields+=sizeof(header->Subchunk1ID);
size_of_fields+=sizeof(header->Subchunk1Size);
size_of_fields+=sizeof(header->AudioFormat);
size_of_fields+=sizeof(header->NumChannels);
size_of_fields+=sizeof(header->SampleRate);
size_of_fields+=sizeof(header->ByteRate);
size_of_fields+=sizeof(header->BlockAlign);
size_of_fields+=sizeof(header->BitsPerSample);
size_of_fields+=sizeof(header->Data);
size_of_fields+=sizeof(header->DataSize);
size_of_fields+=sizeof(header->Extra);
printf("Header size=%d fields size=%d\n",sizeofheaders,size_of_fields);
return (sizeofheaders==size_of_fields);
}


 
Reply With Quote
 
Antoninus Twink
Guest
Posts: n/a
 
      01-30-2010
On 30 Jan 2010 at 18:50, Moi wrote:
> If c is a signed short, why do you use %d as a format (%d expects an int).
> use %hd if c is a short.


Why?

If c is a signed short then it will be promoted to signed int before the
function call anyway.

I think the main use for the h length modifier is in scanf. I guess it
could just conceivably be useful to use %hu in a printf when
sizeof(short) < sizeof(int): you could supply an unsigned int as an
argument, and have it reduced mod USHORT_MAX+1 without needing to write
an explicit cast.

 
Reply With Quote
 
Sarchiapone
Guest
Posts: n/a
 
      01-30-2010

> It's weird, to me, that every number should print as negative, though.
> It seems like /some/ of them should come out as positive even if you're
> reading data of the wrong size or your alignment is off. Maybe it's
> just extremely bad luck?
>
> -Beej


Well, I tried to play the wav file I was trying to read, and it was a
wrong file, so I tried with a different wav file, and the C program
worked properly.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
instead of depending on data = array('h') .. write samples 1 by 1 tow = wave.open("wav.wav", "w") '2+ Python 2 07-29-2009 06:26 PM
CD-DA to Wav to DVD as Wav files? makbertodelete@anothermessage.com DVD Video 10 09-27-2005 08:42 PM
Substituting AOL Wav files with MSN Wav files Thaqalain Computer Support 6 07-07-2005 08:17 PM
Req: Want To Record MID Files To WAV Files - HELP - Thanks George Computer Support 1 08-24-2003 06:09 AM
Wav files to MP3 files....? Rip Torin Computer Support 2 07-01-2003 09:26 AM



Advertisments