samedi 24 janvier 2015

C++ Read file in the background

I am implementing an audio player application that pre-buffers a small part of the audio data and reads the rest of the data when it is required to do so, for example when the play command arrives. It's a real time application, to it's really important that theres near zero latency between the play command and the start of the playback.


Example: my audio stream is 10 Mb, I read part of it when the file is selected and start creating a buffer like this:



// Stuff to do as soon as the file is selected
// Allocate new memory for the current sample

// contains sample length in number of samples
sampleSize = SampleLib.smp.len;

// assume it's a 16-bit audio file, each sample is 2 bytes long
byteSize = sampleSize * sizeof(short);

// Allow 10 extra samples and fill with zeroes
SampleData = new short[sampleSize + 10]();

// PRELOAD_BYTESIZE is set to 65535 bytes
preloadByteSize = byteSize > PRELOAD_BYTESIZE ? PRELOAD_BYTESIZE : byteSize;

// Set pointer in file - WavePointer contains the exact location where the sample data starts in file
fseek(inFile, WavePointer, SEEK_SET);

// read preloadByteSize from inFile into SampleData
fread(SampleData, 1, preloadByteSize, inFile);


At this point my buffer SampleData contains only part of the audio data to start playing back as soon as the play command arrives. At the same time, the program should fill the rest of the buffer and continue playing up until the end of the audio sample with no interruption.



// Stuff to do as soon the playback starts
// Load the rest of the sample data

// If file is already in memory, avoid reading it again
if (preloadByteSize < ByteSize)
{
// Set pointer in file at stample start + preload size
fseek(fpFile, WavePointers + preloadByteSize, SEEK_SET);

// read the remaining bytes from inFile and fill the empty part of the buffer
fread(SampleData + preloadByteSize / sizeof(short), 1, ByteSize - preloadByteSize, inFile);

// remember the number of loaded bytes
preloadByteSize = ByteSize;
}


I expect that the second part of the code is executed in the background while the file is playing back, but actually it's all serial processing, so playback won't start until the rest of the buffer is loaded, thus retarding the playback. Ho can I have a background thread that loads the file data without interfering with the audio task? Can I do this with OpenMP?


Aucun commentaire:

Enregistrer un commentaire