I have the following struct which I call using the Read_Header(FILE * InFile)
, do structs in C++ save data just like classes. For instance, can I only call the function Read_Header
once to set all the variable names in the struct from main.cpp, then get the values of the variable names just by calling them directly.
typedef struct RIFF_CHUNCK_DISCRIPTOR {
char RIFF[4]; // RIFF Header Magic header
int32_t ChunkSize; // RIFF Chunk Size
int32_t WAVE[4]; // WAVE Header
};
typedef struct CRIF_CHUNCK {
char Crif[4];
int32_t Length;
int32_t CrifCheckSum;
};
typedef struct FMT_CHUNCK_DISCRIPTOR {
char fmt[4]; // FMT header
int32_t Subchunk1Size; // Size of the fmt chunk
int16_t EncodingTag;
int16_t NumOfChan; // Number of channels
int32_t SamplesPerSec; // Sampling Frequency in Hz
int32_t bytesPerSec; // bytes per second
int16_t blockAlign; // 2=16-bit mono, 4=16-bit stereo
int16_t bitsPerSample; // Number of bits per sample
int16_t AudioFormat; // PCM = 0 , ADPCM = 2
int16_t SmplesPerChan;
};
typedef struct FACT_CHUNCK {
char fact[4];
int32_t FactSize;
int32_t dwSampleLength;
};
typedef struct META_DATA {
char meta[4];
uint32_t HeadData;// <length of the head data - 8>
uint8_t Version;
uint8_t Model;
uint32_t Serial;
uint32_t RecordingNumber;
uint16_t ChunkNumber;
uint32_t TimeStamp;
uint32_t MetadataChecksum;
};
typedef struct DATA_SUB_CHUNCK {
char Subchunk2ID[4]; // "data" string
int32_t Subchunk2Size; // Sampled data length
};
typedef struct CDAT {
char cdat[4]; // "data" string
int32_t CdatCheckSum; // Sampled data length
};
typedef struct FOOTER {
char foot[4]; // "data" string
int16_t PrevChunckNumb; // Sampled data length
int16_t NextChunckNumb;
int32_t FooterChunckSum;
};
WORD CWav::Read_Header(FILE* WAVF) {
int HeaderArraySize = 46;
int* HeaderData = new int[HeaderArraySize];
int A = 1;
while (1) {
HeaderIDSize = fread(&RCD, 1, sizeof(RIFF_CHUNCK_DISCRIPTOR), WAVF);
if (RCD.RIFF[0] == 'R' && RCD.RIFF[1] == 'I' && RCD.RIFF[2] == 'F' && RCD.RIFF[3] == 'F') {
break;
}
else {
fseek(WAVF, A++, SEEK_CUR);
}
}
int C = 1;
int Cpcm = 1;
while (1) {
FmtSize = fread(&FCD, 1, sizeof(FMT_CHUNCK_DISCRIPTOR), WAVF);
if (FCD.fmt[0] == 'f' && FCD.fmt[1] == 'm' && FCD.fmt[2] == 't') {
NumOfChanells = FCD.NumOfChan;
BitsPerSample = FCD.bitsPerSample;
SampleRate = FCD.SamplesPerSec;
if (FCD.EncodingTag == 0x0001) {
PCM = true;
}
break;
}
else {
fseek(WAVF, C++, SEEK_SET);
}
}
int T = 1;
if (PCM == false) {
while (1) {
FactSize = fread(&FactC, 1, sizeof(FACT_CHUNCK), WAVF);
if (FactC.fact[0] == 'f' && FactC.fact[1] == 'a' && FactC.fact[2] == 'c' && FactC.fact[3] == 't') {
break;
}
else {
fseek(WAVF, T++, SEEK_SET);
}
}
}
int F = 1;
while (1) {
MetaSize = fread(&MetaD, 1, sizeof(FACT_CHUNCK), WAVF);
if (MetaD.meta[0] == 'm' && MetaD.meta[1] == 'e' && MetaD.meta[2] == 't' && MetaD.meta[3] == 'a') {
SerialNum = MetaD.Serial;
RecordingNum = MetaD.RecordingNumber;
break;
}
else {
fseek(WAVF, F++, SEEK_SET);
}
}
int H = 1;
while (1) {
DataChunckSize = fread(&DSC, 1, sizeof(DATA_SUB_CHUNCK), WAVF);
if (DSC.Subchunk2ID[0] == 'd' && DSC.Subchunk2ID[1] == 'a' && DSC.Subchunk2ID[2] == 't' && DSC.Subchunk2ID[3] == 'a') {
break;
}
else {
fseek(WAVF, H++, SEEK_SET);
}
}
Cdata_Postion = 1;
while (1) {
CdatSize = fread(&CDA, 1, sizeof(DATA_SUB_CHUNCK), WAVF);
if (CDA.cdat[0] == 'c' && CDA.cdat[1] == 'd' && CDA.cdat[2] == 'a' && CDA.cdat[3] == 't') {
break;
}
else {
fseek(WAVF, Cdata_Postion++, SEEK_SET);
}
}
int f = 1;
while (1) {
FootSize = fread(&Footer, 1, sizeof(DATA_SUB_CHUNCK), WAVF);
if (Footer.foot[0] == 'f' && Footer.foot[1] == 'o' && Footer.foot[2] == 'o' && Footer.foot[3] == 't') {
break;
}
else {
fseek(WAVF, f++, SEEK_SET);
}
}
BUFFER_SIZE = DSC.Subchunk2Size / (FCD.NumOfChan * (FCD.bitsPerSample / 8));
return BUFFER_SIZE;
}