I'm just making this for entertainment and educational purposes, I could end up doing something with it, but can anyone tell me why this isn't working?? It won't read correctly into the short datatype with the >> or it just fails for some other reason?
My output is only having the first 4 bytes set to "RIFF", after that is fail. You can get a good file to test this prog. with from a wikipedia page, or you can just look at the code, please see the function definition of ReadRiffHeader();
Oh, and can we do aggregate read operations on structures filled with only variables ??
--Edit--
Please take a look at:
https://ccrma.stanfo...cts/WaveFormat/
--Edit2--
Okay, so the >> probably doesn't work too great with binary file io??
I guess I thought it would read the sizeof one short, can anyone shed some light into this?
#define DEBUG
#include <iostream>
#include <fstream>
using namespace std;
struct RIFF_CHUNK_DESCRIPTOR
{
char RIFF[4]; // spells out 'R I F F'
int ChunkSize; //36 + SubChunk2Size
char WAVE[4]; // spells 'W A V E'
};
struct FORMAT_SUB_CHUNK
{
int SubChunk1Id;
int SubChunk1Size;
short AudioFormat; // 2 bytes, should equal 1 (PCM)
short NumChannels; // 2 bytes
int SampleRate;
int ByteRate;
short BlockAlign;
short BitsPerSample;
};
struct DATA_SUB_CHUNK
{
int SubChunk2Id;
int SubChunk2Size;
};
/* RIFF WAVE PCM */
struct WAVE
{
RIFF_CHUNK_DESCRIPTOR riffChunk;
FORMAT_SUB_CHUNK formatSubChunk;
DATA_SUB_CHUNK dataSubChunk;
//the data follows all of these
};
/* function prototypes */
int ReadRiffHeader(WAVE &,ifstream &);
int main()
{
WAVE soundFile;
memset(&soundFile,0,sizeof(WAVE));
ifstream inFile("file.wav",ios::in | ios::binary);
//populate the struct members, and display the SubChunk2Size-36
ReadRiffHeader(soundFile,inFile);
inFile.close();
return 0;
}
int ReadRiffHeader(WAVE & soundFile,ifstream& inFile)
{
/* populate the riffChunk structure */
inFile.read(soundFile.riffChunk.RIFF,4);
inFile >> soundFile.riffChunk.ChunkSize;
inFile.read(soundFile.riffChunk.WAVE,4);
/* output info about the riffChunk struct */
#ifdef DEBUG
cout << "Should spell RIFF: ";
for(int i=0;i<4;i++)
cout << soundFile.riffChunk.RIFF[i];
cout << endl;
cout << "Chunk Size: " << soundFile.riffChunk.ChunkSize << endl;
cout << "Should spell WAVE: ";
for(int i=0;i<4;i++)
cout << soundFile.riffChunk.WAVE[i];
cout << endl;
#endif
/* populate the format sub chunk structure */
inFile >> soundFile.formatSubChunk.SubChunk1Id;
inFile >> soundFile.formatSubChunk.SubChunk1Size;
inFile >> soundFile.formatSubChunk.AudioFormat; // 2 bytes
inFile >> soundFile.formatSubChunk.NumChannels; // 2 bytes
inFile >> soundFile.formatSubChunk.SampleRate;
inFile >> soundFile.formatSubChunk.ByteRate;
inFile >> soundFile.formatSubChunk.BlockAlign; // 2 bytes
inFile >> soundFile.formatSubChunk.BitsPerSample; // 2 bytes
/* output info about the formatSubChunk struct */
#ifdef DEBUG
cout << "SubChunk1Id: " << soundFile.formatSubChunk.SubChunk1Id << endl;
cout << "SubChunk1Size: " << soundFile.formatSubChunk.SubChunk1Size << endl;
cout << "Audio Format (should equal 1 for PCM): " << soundFile.formatSubChunk.AudioFormat << endl;
#endif
return 0;
}
This post has been edited by taylorc8: 04 March 2010 - 02:47 PM

New Topic/Question
Reply




MultiQuote




|