I have here a working way of shifting a 24 bit audio file to a 32 bit buffer. The whole (!) file should be read to this big buffer. For every 3 bytes a fread is used in the loop. That is too slow. Any idea how to make this fast?
Thanks a lot!
Regards
Thomas
1 2 3 4 5 6 7 8 9
unsignedchar *buf = newunsignedchar[3]; // 3 bytes - 24 bit
unsignedint *buffer32 = newunsignedint[size32]; // big buffer with the whole file
for (i=0; i < size32/4; i++)
{
fread(buf, 3, 1, fd); // 3 bytes - 24 bit , fread for every 3 bytes is too slow !!!
//24-bit data being packed into the lower 3 bytes of a 32-bit word:
buffer32[i] = null[0] | buf[2] << 24 | buf[1] << 16 | buf[0] << 8 ;
}
delete[] buf;
Use a memory mapped file, letting operating system handle reading from the file behind the scenes. You will "see" a contiguous chunk of memory as a result.
I prob. wouldn't use a memory mapped file here. But it looks like you could read the i/p file in one go into a big enough buffer and then walk through it 3 bytes at a time.
You are already allocating a single buffer for the result, so I presume the file you're dealing with is not that big?
An inbetween solution would be to use two loops. The outer loop reads the file in big chunks, and the inner one does the byte-wise stuff. But as you are already creating a single buffer for the result...
By the way, I see you're using new and delete, so this is "C++ code". So why not use std::ifstream and std::vector<>?
And in your code above, I think buf is too tiny to allocate on the heap.
1 2 3 4 5 6 7 8 9
unsignedchar buf[3]; // 3 bytes - 24 bit - on the stack
unsignedint *buffer32 = newunsignedint[size32]; // big buffer with the whole file
for (i=0; i < size32/4; i++)
{
fread(buf, 3, 1, fd); // 3 bytes - 24 bit , fread for every 3 bytes is too slow !!!
//24-bit data being packed into the lower 3 bytes of a 32-bit word:
buffer32[i] = null[0] | buf[2] << 24 | buf[1] << 16 | buf[0] << 8 ;
}
// no need to delete buf! :-)
Andy
PS I would use a memory mapped file when dealing with a big enough file. Or if I needed random access. But it would be to solve an overly ore complicated problem.
The file could be very big! So using a separate big 24 bit buffer would implicate two very big buffers. This is very fast, but too big... May be a bigger "buf" could be the solution?