Hi Brett, Long time no speak!

I have come across a problem with looping samples in memory whereby the sample data becomes corrupted on subsequent loop positioning. I have written a short program compiled using Microsoft Visual C++ 6.0 to prove that the bug exists. Just copy and paste the code at the bottom of ths post into your compiler to see for yourself.

The reason I actually found it is because I was viewing the waveform data from different samples and setting different loop points within them. Each time I set a new loop point the data around the area of the previous loop points became corrupted. I think that the sample data at the end position becomes overwritten with the sample data from the start position but I cant be sure.

The program below does the following things:

First it loads a sample into memory then it performs two passes/parses:

PASS 1. Calculate a CRC (Cyclic Redundancy Check) value for the sample then place random loop points and play the sample.

PASS 2. Recalculate the CRC and compare it with the previous one.

Finally exit with the results clearly showing that the sample data has changed somehow!

You can prove that the FSOUND_Sample_SetLoopPoints funtion is the culprit by commenting it out and then re-running the code. The CRC’s show that no corruption has occurred when loop points are not set.

I am now closer than ever to requiring one of your commercial license and look forward eagerly to your reply.

Regards, TS.


include “stdafx.h”

include “fmod.h”

include <stdlib.h>

include <time.h>

// Name of a sample to load

define SAMPLE_NAME “C:\Mp3\New\03-Track.MP3”

// NOTE: Assumption that sample is 16Bit stereo and MMX mixer is being
// used!

define SAMPLE_LEN 2 * 2 // 4 bytes

int main(int argc, char* argv[])
// Variables
void *ptr1, *ptr2;
unsigned int len1, len2;
int startPos, endPos;
int iChannel = -1;
unsigned CRC, prevCRC;
unsigned char *ptr, c;

// Seed random number generator with current time
srand((unsigned) time(NULL));

// Initialise FMOD
if(!FSOUND_Init(44100, 64,
    return FSOUND_GetError();

// Open sample (tried both Unmanaged and Free but bug still occurs)
printf("Loading Sample: %s ...\r\n", SAMPLE_NAME);
FSOUND_SAMPLE* pSample = FSOUND_Sample_Load(
printf("Sample Loaded\r\n");

// Two passes to prove bug
for(int iParse = 0; iParse &lt; 2 &amp;&amp; pSample; iParse++)
    // Keep getting two 'random' loop points until the start pos
    // is before the end pos and they are both within sample
    // length
        startPos = rand();
        endPos = rand();
    } while(endPos &lt;= startPos &amp;&amp;
        (unsigned int) endPos &lt;= FSOUND_Sample_GetLength(pSample));

    // Lock sample buffer from start to end
        0, FSOUND_Sample_GetLength(pSample) * SAMPLE_LEN,
        &amp;ptr1, &amp;ptr2, &amp;len1, &amp;len2))

    // Get cylclic redundancy check value to compare data changes
    CRC = 0;
    printf("Parse %d: Getting CRC\r\n", iParse);

    // Get buffer in form we can use
    ptr = (unsigned char *) ptr1;

    // Scan whole sample calculating CRC
    unsigned lengthBytes = FSOUND_Sample_GetLength(pSample) * SAMPLE_LEN;
    for(unsigned u = 0; u &lt; lengthBytes; u++)
        c = ptr[u];
        CRC = CRC ^ (c &lt;&lt; 8);
        for(int i = 0; i++ &lt; 8;)
            if(CRC &amp; 0x8000)
                CRC = (CRC &lt;&lt; 1) ^ 0x1021;
                CRC &lt;&lt;= 1;

        // Show % finished every one hundred thousand bytes
        if(!(u % 100000))
                (int) ((double) u / (double) lengthBytes * 100.0));

    // Unlock sample buffer
    FSOUND_Sample_Unlock(pSample, ptr1, ptr2, len1, len2); 

    printf("Parse %d: CRC(%d) Loop from %d to %d\r\n",
        iParse, CRC, startPos, endPos);

    // If not first parse then compare crc to previous parse's crc
        if(CRC != prevCRC)
            printf("Sample data has changed!!!\r\n");
            printf("CRC changed from %d to %d\r\n", prevCRC, CRC);

    // Remember CRC value for next parse
    prevCRC = CRC;

    // Stop playing sample
        FSOUND_SetPaused(iChannel, true);

    // Set up loop points and mode
    FSOUND_Sample_SetLoopMode(pSample, FSOUND_LOOP_NORMAL);
    FSOUND_Sample_SetLoopPoints(pSample, startPos, endPos);

    // Start sample playing
    iChannel = FSOUND_PlaySound(FSOUND_FREE, pSample);
    if(iChannel == -1)

// Finish with sample
    // Stop playing sample
    FSOUND_SetPaused(iChannel, true);

    // Free sample

// Finish with FMOD

printf("Press Enter to finish");

return 0;


———–CUT/PASTE END——————–

  • You must to post comments

Thanks for your speedily response. I have tried the new dll but there seems to be another problem in that when I draw the sample data in a view window and lock and unlock the sample each time. The second time it tries to lock the sample the FSOUND_Sample_Lock returns successfully but the buffer ptr and len are both null values.

Anyhow, the information you provided me with means that I can save and restore the data myself and still use the release dll which I know works.

I have two other problems with fmod regarding loading samples into memory but I will post a new topic on this shortly.

Thanks again Brett.


  • You must to post comments

Sorry Brett, I was a bit too hasty there! I was incidently getting a null ptr returned from FSOUND_Sample_Lock with a successful return from the function but it seems to have gone away now?!?! I believe it might have been something to do with my own code.

The new dll works great now at keeping the sample integrity during lock/unlock while looping, but there is still a problem in that if I set the FSOUND_ENABLEFX flag in the FSOUND_Sample_Load function, I cant get the looping to work any more!

Any ideas! Sorry for wasting your time before :oops:.


  • You must to post comments

Another quick question.

Can you nest FSOUND_Sample_Lock calls or can there only be one lock at a time?

  • You must to post comments

Any ideas on the FSOUND_ENABLEFX question Brett?

  • You must to post comments

Thank you Brett. 😀 I will be speaking to you privately about a commercial license in the next two weeks. Take care and keep up the good work. 8)


  • You must to post comments
Showing 5 results
Your Answer

Please first to submit.