Okay, I'd appreciate anyones help on this. I've got this code from an article on Gamedev.net written in C for opening a wave file and doing your stuff.
I am going to convert it to PHP.
There are a few issues. Here's the C;
void Load_Wave_File(char fname)
{
FILE fp;
fp = fopen(fname,"rb);
if (fp)
{
BYTE id[4], *sound_buffer; //four bytes to hold 'RIFF'
DWORD size; //32 bit value to hold file size
short format_tag, channels, block_align, bits_per_sample; //our 16 values
DWORD format_length, sample_rate, avg_bytes_sec, data_size, i; //our 32 bit values
fread(id, sizeof(BYTE), 4, fp); //read in first four bytes
if (!strcmp(id, "RIFF"))
{ //we had 'RIFF' let's continue
fread(size, sizeof(DWORD), 1, fp); //read in 32bit size value
fread(id, sizeof(BYTE), 4, fp); //read in 4 byte string now
if (!strcmp(id,"WAVE"))
{ //this is probably a wave file since it contained "WAVE"
fread(id, sizeof(BYTE), 4, fp); //read in 4 bytes "fmt ";
fread(&format_length, sizeof(DWORD),1,fp);
fread(&format_tag, sizeof(short), 1, fp); //check mmreg.h (i think?) for other
// possible format tags like ADPCM
fread(&channels, sizeof(short),1,fp); //1 mono, 2 stereo
fread(&sample_rate, sizeof(DWORD), 1, fp); //like 44100, 22050, etc...
fread(&avg_bytes_sec, sizeof(short), 1, fp); //probably won't need this
fread(&block_align, sizeof(short), 1, fp); //probably won't need this
fread(&bits_per_sample, sizeof(short), 1, fp); //8 bit or 16 bit file?
fread(id, sizeof(BYTE), 4, fp); //read in 'data'
fread(&data_size, sizeof(DWORD), 1, fp); //how many bytes of sound data we have
sound_buffer = (BYTE *) malloc (sizeof(BYTE) * data_size); //set aside sound buffer space
fread(sound_buffer, sizeof(BYTE), data_size, fp); //read in our whole sound data chunk
}
else
printf("Error: RIFF file but not a wave file\n");
}
else
printf("Error: not a RIFF file\n");
}
}
Now I know in PHP you don't declare your variable types, so wuth BYTE, DWORD, short I'm going to have to just declare them as generic variables and assign them the right sized values.
So there won't be a sizeof() equivalent either but I can just type the size as a number. No big deal.
But I've got the following code snippet from here from ages ago for reading 4-bytes of a wave file, and there is some issue with little endian numbers or something:
if( $Data == 'data'){
print "Yes!<BR>";
$i+=3;
fseek($fp, $i);
// there are not 4 byte of checkID here...
$bing1=ord(fread($fp,1)); // now we have to convert the byte in a long,
$bing2=ord(fread($fp,1)); // the php do not this. Be care, the number is reversed
$bing3=ord(fread($fp,1)); // on intel machine xxyyzzww -> wwzzyyxx...
$bing4=ord(fread($fp,1));
$ChunkSize = ($bing4*256+$bing3)*256+$bing2*256+$bing1;
return array($ChunkSize,$i);
}
This bottom bit looks alittle more complicated than it seems.
I don't quite understand the brackets around the $ChunkSize = ($bing4256+ - etc line. Shouldn't there be more?
This is only for four bytes. DWORD and short look much larger, they may need special (256 --- ) etc functions of their own just to extract correctly.
These are the issues that crop up at a first attempt. Anyone good at this?
I'll post the results if I get any.
Thanks
Gregg