Re: pasing an array pointer and storing as a class member
stephen b <kernel@audiospillage.com> wrote in message
news:1192563109.115307.88440@i38g2000prf.googlegro ups.com...
> On 14 Oct, 19:56, "BobR" <removeBadB...@worldnet.att.net> wrote:
> > stephen b wrote in message...
> > > I am using arrays instead of vector<double> because I found the
> > > initialisation of many vector<double>s caused brief CPU spikes each
> > > time an object is created.
> >
> > I'm curious. How did you initialize the vectors?
> >
> > > Arrays seemed less troublesome which is
> > > important for real-time DSP stuff.
> >
> > I think it's the opposite. :-}
>
> really? right, I think I initialised my vectors like this:
> vector<double> vec (8192, 0.); so I guess maybe filling the array
> with zeros was causing some overhead. I also found arrays to be
> slightly more efficient in use although it's probably negligible even
> though I'll be using 20 or so per synth.
>
> > Could it be because you are dereferenceing un-initialized data (the
arrays)?
> > Let's test something. In your FilterSynth() constructor:
> >
> > FilterSynth(){
> > for( int i(0); i < 10; ++i ){
> > std::cout<< filtIn[ i ] <<'\n';
> > } // for(i)
> > } // Ctor
> >
> > What's your output from that? (I got garbage (UB)).
>
> well with the filter example I fill the array with zeros first but
> otherwise just garbage.
> Stephen.
>
By the time you initialize your raw arrays, there should be very little
difference with std::vectors. (you don't need the '0.0' in init, it is the
default.)
Try your tests with this and see how it goes:
This compiles on my system, but of course, you'll need to modify it to use
your base classes, etc.. Just for a suggestion.
class Filter{ public:
// Filter( double *inBlk, double *outBlk ){
// in = inBlk; out = outBlk; }
Filter( std::vector<double> const &inBlk,
std::vector<double> &outBlk )
: in( inBlk.begin() ), out( outBlk.begin() ){}
// void Process(UInt32 inNumFrames) {
void Process( int inNumFrames ){
double y0(0); // for test compile
for( int i(0); i < inNumFrames; ++i){
y0 = *(in)++;
*(out)++ = y0;
} // for(i)
} // Process(int)
private:
// double *in, *out;
std::vector<double>::const_iterator in;
std::vector<double>::iterator out;
};
// If 'in' and 'out' are only used in 'Process', you may want
// a normal constructor and use the following for 'Process':
// void Process( std::vector<double> const &inBlk,
// std::vector<double> &outBlk ){ .... }
// .... and change the call below. Then you wouldn't need the
// iterators in the class (just in the function).
class FilterSynth{ public:
FilterSynth() : filtIn(8192), filtOut(8192), filt( filtIn, filtOut ){}
virtual ~FilterSynth(){} // not needed if base Dtor is virtual.
// virtual UInt32 NextBlock( UInt32 inNumFrames ){
virtual int NextBlock( int inNumFrames ){
// this function is called by another class.
filt.Process( inNumFrames );
return 0;
}
private:
std::vector<double> filtIn;
std::vector<double> filtOut;
Filter filt;
};
This is a little 'loose', but I don't know exactly the problem you are
working.
Hope that s some.
--
Bob R
POVrookie
|