Rapid calls to fread crashes the application

Posted by Slynk on Stack Overflow See other posts from Stack Overflow or by Slynk
Published on 2012-09-13T03:31:18Z Indexed on 2012/09/13 3:38 UTC
Read the original article Hit count: 296

Filed under:
|
|
|

I'm writing a function to load a wave file and, in the process, split the data into 2 separate buffers if it's stereo. The program gets to i = 18 and crashes during the left channel fread pass. (You can ignore the couts, they are just there for debugging.) Maybe I should load the file in one pass and use memmove to fill the buffers?

if(params.channels == 2){
    params.leftChannelData = new unsigned char[params.dataSize/2];
    params.rightChannelData = new unsigned char[params.dataSize/2];

    bool isLeft = true;

    int offset = 0;
    const int stride = sizeof(BYTE) * (params.bitsPerSample/8);

    for(int i = 0; i < params.dataSize; i += stride)
    {
        std::cout << "i = " << i << " ";
        if(isLeft){
            std::cout << "Before Left Channel, ";
            fread(params.leftChannelData+offset, sizeof(BYTE), stride, file + i);
            std::cout << "After Left Channel, ";
        }
        else{
            std::cout << "Before Right Channel, ";
            fread(params.rightChannelData+offset, sizeof(BYTE), stride, file + i);
            std::cout << "After Right Channel, ";
            offset += stride;
            std::cout << "After offset incr.\n";
        }

        isLeft != isLeft;
    }

} else {
    params.leftChannelData = new unsigned char[params.dataSize];
    fread(params.leftChannelData, sizeof(BYTE), params.dataSize, file);
}

© Stack Overflow or respective owner

Related posts about c++

Related posts about audio