COCKOS
CONFEDERATED FORUMS
Cockos : REAPER : NINJAM : Forums
Forum Home : Register : FAQ : Members List : Search :
Prev Previous Post   Next Post Next
Old 06-30-2020, 12:41 PM   #1
SaschArt
Human being with feelings
 
SaschArt's Avatar
 
Join Date: Aug 2013
Posts: 236
Default Read wav files - wrong data

I want to make a plugin that can read audio files. For wav, I made this function:

H file:
Code:
void readWave(char *fname);
CPP file:
Code:
//Wav Header
struct wav_header_t {
    char chunkID[4]; 
    unsigned long chunkSize; 
    char format[4]; 
    char subchunk1ID[4]; 
    unsigned long subchunk1Size;
    unsigned short audioFormat;
    unsigned short numChannels;
    unsigned long sampleRate;
    unsigned long byteRate;
    unsigned short blockAlign;
    unsigned short bitsPerSample;
};

//Chunks
struct chunk_t {
    char ID[4];
    unsigned long size; 
};
void VSTCLASS::readWave(char *fname) {
    FILE *fin = fopen(fname, "rb");

    //Read WAV header
    wav_header_t header;
	fread(&header, sizeof(header), 1, fin);
	
    debugPrint("File Size:", header.chunkSize);
    debugPrint("Format Length:", header.subchunk1Size );
    debugPrint("Format Type:", header.audioFormat);
    debugPrint("Number of Channels:", header.numChannels);
    debugPrint("Sample Rate:", header.sampleRate);
    debugPrint("Sample Rate * Bits/Sample * Channels / 8:", header.byteRate);
    debugPrint("Bits per Sample * Channels / 8.1:", header.blockAlign);
	debugPrint("Bits per Sample:", header.bitsPerSample);

    chunk_t chunk;
    //go to data chunk
    while (true) {
        fread(&chunk, sizeof(chunk), 1, fin);
        debugPrint("chunk", chunk.ID[0], chunk.ID[1], chunk.ID[2], chunk.ID[3], chunk.size);
        if (*(unsigned int *)&chunk.ID == 0x61746164)
            break;
        //skip chunk data bytes
        fseek(fin, chunk.size, SEEK_CUR);
    }

    //Number of samples
    int sample_size = header.bitsPerSample / 8;
    int samples_count = chunk.size * 8 / header.bitsPerSample;
	debugPrint("Samples count:", samples_count);

    short int *value = new short int[samples_count];
	memset(value, 0, sizeof(short int) * samples_count);

    //Reading data
    for (int i = 0; i < samples_count; i++) {
        fread(&value[i]/, sample_size, 1, fin);
		debugPrint("sample:", i,  value[i]/32767.);		
	}
	
	fclose(fin);
}
Data from value[i] are completely wrong. Can you tell me where the mistake is please?
__________________
Audio plugins | BrainWaveProducer | EmRysRa
SaschArt is offline   Reply With Quote
 

Thread Tools
Display Modes

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

Forum Jump


All times are GMT -7. The time now is 05:11 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.