DSPRelated.com
Forums

DSP newbie:FIFO buffers in C++

Started by Chris Sperry September 26, 2003
I've come to realise that in order to get a resampling component that I
have developed to integrate properly in my application, I will need to
implement a buffer so that I can look ahead of the current buffer (at
the expense of introducing a bit of latency). I assume that a FIFO would
be the best way to implement this buffering. 

The question is: how efficient would the STL <queue> implementation be
for implementing the FIFO? I believe that 2 choices of containment are
available for a queue... the STL list (linked list... massive storage
overhead) or the deque... or would it be better to implement something
myself based on arrays/vectors... perhaps a circular buffer (DXSound
style)? It really needs to be efficient, as I'm coding on a rather
underspec'ed machine (by today's standards)...

Anyone got any pointers? There's a bit of stuff at
http:\\www.musicdsp.org but it all looks a bit incomplete...

Cheers all,

Chris
Chris Sperry wrote:

> I've come to realise that in order to get a resampling component > that I have developed to integrate properly in my application, I > will need to implement a buffer so that I can look ahead of the > current buffer (at the expense of introducing a bit of latency). I > assume that a FIFO would be the best way to implement this > buffering. > > The question is: how efficient would the STL <queue> > implementation be for implementing the FIFO? I believe that 2 > choices of containment are available for a queue... the STL list > (linked list... massive storage overhead) or the deque... or would > it be better to implement something myself based on > arrays/vectors... perhaps a circular buffer (DXSound style)? It > really needs to be efficient, as I'm coding on a rather > underspec'ed machine (by today's standards)... > > Anyone got any pointers? There's a bit of stuff at > http:\\www.musicdsp.org but it all looks a bit incomplete... > > Cheers all, > > Chris
Hi Chris, if you don't use STL already, I'd probably not use it only for a FIFO. If your code uses a lot of STL already, why not. I'd certainly implement it by myself. At first, it's not too difficult. Secondly, there's a good chance, that my specialized application doesn't require everything which is contained in such a generalized library block. My own might perform better therefore. And, if you're a newbie: don't underestimate the effect of learning by doing... Bernhard
Bernhard Holzmayer wrote:

> Chris Sperry wrote: > >> I've come to realise that in order to get a resampling component >> that I have developed to integrate properly in my application, I >> will need to implement a buffer so that I can look ahead of the >> current buffer (at the expense of introducing a bit of latency). >> I assume that a FIFO would be the best way to implement this >> buffering. >>... >> >> Cheers all, >> Chris >
If you decide to do it by yourself, here's a starting point: build a class, which contains the buffer. the constructor should flush/empty it. an input method, which ripples down the contents, and then puts the input at the end of the queue. another method for the output side, which just takes the oldest item. Differences are how you do the ripple (moving contents from one array element to the next and so on - or just changing pointers). I'd probably use a circular buffer for it. In any case you have to solve the situations of empty or full buffers. But if everything is hidden in the FIFO object, you can easily start with a quick'n'dirty approach and then refine and/or optimize later without side effects outside the class. Bernhard
Thanks for the hints Bernhard. I'm losing my laptop tomorrow, so it's
going to be a while before I can have a go at coding, but I'll be back
with more questions before long!

Bernhard Holzmayer wrote:
> > Bernhard Holzmayer wrote: > > > Chris Sperry wrote: > > > >> I've come to realise that in order to get a resampling component > >> that I have developed to integrate properly in my application, I > >> will need to implement a buffer so that I can look ahead of the > >> current buffer (at the expense of introducing a bit of latency). > >> I assume that a FIFO would be the best way to implement this > >> buffering. > >>... > >> > >> Cheers all, > >> Chris > > > If you decide to do it by yourself, here's a starting point: > > build a class, which contains the buffer. > the constructor should flush/empty it. > an input method, which ripples down the contents, > and then puts the input at the end of the queue. > another method for the output side, which just takes > the oldest item. > Differences are how you do the ripple (moving contents from one > array element to the next and so on - or just changing pointers). > I'd probably use a circular buffer for it. > > In any case you have to solve the situations of empty or full > buffers. > But if everything is hidden in the FIFO object, you can easily start > with a quick'n'dirty approach and then refine and/or optimize later > without side effects outside the class. > > Bernhard