60 #ifndef R8BBASE_INCLUDED 61 #define R8BBASE_INCLUDED 69 #if defined( R8B_WIN ) 87 #define R8B_VERSION "4.6" 94 #define M_PI 3.14159265358979324 103 #define M_2PI 6.28318530717958648 106 #if !defined( M_3PI ) 112 #define M_3PI 9.42477796076937972 115 #if !defined( M_4PI ) 121 #define M_4PI 12.56637061435917295 124 #if !defined( M_PId2 ) 130 #define M_PId2 1.57079632679489662 144 #define R8BNOCTOR( ClassName ) \ 146 ClassName( const ClassName& ) { } \ 147 ClassName& operator = ( const ClassName& ) { return( *this ); } 165 void*
operator new( size_t,
void* p )
175 void*
operator new(
size_t n )
177 return( :: malloc( n ));
185 void*
operator new[](
size_t n )
187 return( :: malloc( n ));
196 void operator delete(
void* p )
207 void operator delete[](
void* p )
231 return( :: malloc( Size ));
244 return( :: realloc( p, Size ));
298 R8BASSERT( Capacity > 0 || Capacity == 0 );
300 Data0 = allocmem( Capacity *
sizeof( T ) + Alignment );
301 Data = (T*) alignptr( Data0, Alignment );
303 R8BASSERT( Data0 != NULL || Capacity == 0 );
320 R8BASSERT( Capacity > 0 || Capacity == 0 );
323 Data0 = allocmem( Capacity *
sizeof( T ) + Alignment );
324 Data = (T*) alignptr( Data0, Alignment );
326 R8BASSERT( Data0 != NULL || Capacity == 0 );
338 void realloc(
const int PrevCapacity,
const int NewCapacity )
343 void*
const NewData0 = allocmem( NewCapacity *
sizeof( T ) +
346 T*
const NewData = (T*) alignptr( NewData0, Alignment );
347 const size_t CopySize = ( PrevCapacity > NewCapacity ?
348 NewCapacity : PrevCapacity ) *
sizeof( T );
352 memcpy( NewData, Data, CopySize );
359 R8BASSERT( Data0 != NULL || NewCapacity == 0 );
394 static const size_t Alignment = 32;
410 inline Tp alignptr(
const Tp ptr,
const uintptr_t align )
412 return( (Tp) ( (uintptr_t) ptr + align -
413 ( (uintptr_t) ptr & ( align - 1 ))) );
464 void operator = ( T2
const aObject )
474 T operator -> ()
const 506 T ResObject = Object;
533 #if defined( R8B_WIN ) 534 InitializeCriticalSectionAndSpinCount( &CritSec, 4000 );
536 pthread_mutexattr_t MutexAttrs;
537 pthread_mutexattr_init( &MutexAttrs );
538 pthread_mutexattr_settype( &MutexAttrs, PTHREAD_MUTEX_RECURSIVE );
539 pthread_mutex_init( &Mutex, &MutexAttrs );
540 pthread_mutexattr_destroy( &MutexAttrs );
546 #if defined( R8B_WIN ) 547 DeleteCriticalSection( &CritSec );
549 pthread_mutex_destroy( &Mutex );
560 #if defined( R8B_WIN ) 561 EnterCriticalSection( &CritSec );
563 pthread_mutex_lock( &Mutex );
574 #if defined( R8B_WIN ) 575 LeaveCriticalSection( &CritSec );
577 pthread_mutex_unlock( &Mutex );
582 #if defined( R8B_WIN ) 583 CRITICAL_SECTION CritSec;
587 pthread_mutex_t Mutex;
618 : SyncObj( aSyncObj )
620 if( SyncObj != NULL )
622 SyncObj -> acquire();
632 : SyncObj( &aSyncObj )
634 SyncObj -> acquire();
639 if( SyncObj != NULL )
641 SyncObj -> release();
664 #define R8BSYNC( SyncObject ) R8BSYNC_( SyncObject, __LINE__ ) 665 #define R8BSYNC_( SyncObject, id ) R8BSYNC__( SyncObject, id ) 666 #define R8BSYNC__( SyncObject, id ) CSyncKeeper SyncKeeper##id( SyncObject ) 690 : svalue1( sin( ph ))
691 , svalue2( sin( ph - si ))
692 , sincr( 2.0 * cos( si ))
706 CSineGen(
const double si,
const double ph,
const double g )
707 : svalue1( sin( ph ) * g )
708 , svalue2( sin( ph - si ) * g )
709 , sincr( 2.0 * cos( si ))
721 void init(
const double si,
const double ph )
724 svalue2 = sin( ph - si );
725 sincr = 2.0 * cos( si );
738 void init(
const double si,
const double ph,
const double g )
740 svalue1 = sin( ph ) * g;
741 svalue2 = sin( ph - si ) * g;
742 sincr = 2.0 * cos( si );
751 const double res = svalue1;
753 svalue1 = sincr * res - svalue2;
777 static const char OccupancyTable[] =
779 1, 1, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4,
780 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
781 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
782 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
783 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
784 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
785 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
786 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
787 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
788 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
789 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
790 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
791 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
792 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
793 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
794 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8
797 const int tt = v >> 16;
801 const int t = v >> 24;
802 return( t != 0 ? 24 + OccupancyTable[ t & 0xFF ] :
803 16 + OccupancyTable[ tt ]);
807 const int t = v >> 8;
808 return( t != 0 ? 8 + OccupancyTable[ t ] : OccupancyTable[ v ]);
825 const double th,
double& re0,
double& im0,
const int fltlat = 0 )
827 const double sincr = 2.0 * cos( th );
838 cvalue1 = cos( -fltlat * th );
839 svalue1 = sin( -fltlat * th );
842 double cvalue2 = cos( -( fltlat + 1 ) * th );
843 double svalue2 = sin( -( fltlat + 1 ) * th );
850 re += cvalue1 * flt[ 0 ];
851 im += svalue1 * flt[ 0 ];
855 double tmp = cvalue1;
856 cvalue1 = sincr * cvalue1 - cvalue2;
860 svalue1 = sincr * svalue1 - svalue2;
883 const int fltlen,
const double th,
double& re,
double& im,
double& gd )
892 const double thd2 = 1e-9;
893 double ths[ Count ] = { th - thd2, th + thd2 };
900 if( ths[ 1 ] >
M_PI )
908 for( i = 0; i < Count; i++ )
914 ph1[ i ] = atan2( im1, re1 );
917 if( fabs( ph1[ 1 ] - ph1[ 0 ]) >
M_PI )
919 if( ph1[ 1 ] > ph1[ 0 ])
929 const double thd = ths[ 1 ] - ths[ 0 ];
930 gd = ( ph1[ 1 ] - ph1[ 0 ]) / thd;
944 const double DCGain,
const int pstep = 1 )
988 const double xm2,
const double xm1,
const double x0,
const double x1,
989 const double x2,
const double x3,
const double x4 )
992 c[ 1 ] = ( 61.0 * ( x1 - xm1 ) + 16.0 * ( xm2 - x2 ) +
993 3.0 * ( x3 - xm3 )) / 76.0;
995 c[ 2 ] = ( 106.0 * ( xm1 + x1 ) + 10.0 * x3 + 6.0 * xm3 - 3.0 * x4 -
996 29.0 * ( xm2 + x2 ) - 167.0 * x0 ) / 76.0;
998 c[ 3 ] = ( 91.0 * ( x0 - x1 ) + 45.0 * ( x2 - xm1 ) +
999 13.0 * ( xm2 - x3 ) + 3.0 * ( x4 - xm3 )) / 76.0;
1020 const double xm2,
const double xm1,
const double x0,
const double x1,
1021 const double x2,
const double x3,
const double x4 )
1024 c[ 1 ] = ( 61.0 * ( x1 - xm1 ) + 16.0 * ( xm2 - x2 ) +
1025 3.0 * ( x3 - xm3 )) / 76.0;
1027 c[ 2 ] = ( 106.0 * ( xm1 + x1 ) + 10.0 * x3 + 6.0 * xm3 - 3.0 * x4 -
1028 29.0 * ( xm2 + x2 ) - 167.0 * x0 ) / 76.0;
1043 c[ 1 ] = 0.5 * ( y[ 2 ] - y[ 0 ]);
1044 c[ 2 ] = y[ 0 ] - 2.5 * y[ 1 ] + y[ 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
1120 return( v1 < v2 ? v1 : v2 );
1134 inline T
max(
const T& v1,
const T& v2 )
1136 return( v1 > v2 ? v1 : v2 );
1151 inline double clampr(
const double Value,
const double minv,
1174 inline double sqr(
const double x )
1185 inline double pows(
const double v,
const double p )
1187 return( v < 0.0 ? -pow( -v, p ) : pow( v, p ));
1197 return( exp( -( v * v )));
1207 return( log( v + sqrt( v * v + 1.0 )));
1218 const double ax = fabs( x );
1226 return( 1.0 + y * ( 3.5156229 + y * ( 3.0899424 + y * ( 1.2067492 +
1227 y * ( 0.2659732 + y * ( 0.360768e-1 + y * 0.45813e-2 ))))));
1232 return( exp( ax ) / sqrt( ax ) * ( 0.39894228 + y * ( 0.1328592e-1 +
1233 y * ( 0.225319e-2 + y * ( -0.157565e-2 + y * ( 0.916281e-2 +
1234 y * ( -0.2057706e-1 + y * ( 0.2635537e-1 + y * ( -0.1647633e-1 +
1235 y * 0.392377e-2 )))))))));
1240 #endif // R8BBASE_INCLUDED Sine signal generator class.
Definition: r8bbase.h:674
double asinh(const double v)
Definition: r8bbase.h:1205
double clampr(const double Value, const double minv, const double maxv)
Function "clamps" (clips) the specified value so that it is not lesser than "minv", and not greater than "maxv".
Definition: r8bbase.h:1151
int getBitOccupancy(const int v)
Definition: r8bbase.h:775
#define M_2PI
The M_2PI macro equals to "2 * pi" constant, fits 53-bit floating point mantissa. ...
Definition: r8bbase.h:103
CFixedBuffer(const int Capacity)
Constructor allocates memory so that the specified number of elements of type T can be stored in *thi...
Definition: r8bbase.h:296
void calcInterpCoeffs3p8(double *const c, const double *const y)
Function calculates coefficients used to calculate 3rd order segment interpolation polynomial on the ...
Definition: r8bbase.h:1077
void calcSpline2p8Coeffs(double *c, const double xm3, const double xm2, const double xm1, const double x0, const double x1, const double x2, const double x3, const double x4)
Function calculates coefficients used to calculate 2rd order spline (polynomial) on the equidistant l...
Definition: r8bbase.h:1019
#define R8BASSERT(e)
Assertion macro used to check for certain run-time conditions.
Definition: r8bconf.h:72
static void freemem(void *p)
Function frees a previously allocated memory block.
Definition: r8bbase.h:253
CPtrKeeper(T2 const aObject)
Constructor assigns a pointer to object to *this keeper.
Definition: r8bbase.h:446
void normalizeFIRFilter(double *const p, const int l, const double DCGain, const int pstep=1)
Function normalizes FIR filter so that its frequency response at DC is equal to DCGain.
Definition: r8bbase.h:943
void reset()
Function resets the keeped pointer and deletes the keeped object.
Definition: r8bbase.h:492
A "keeper" class for CSyncObject-based synchronization.
Definition: r8bbase.h:602
Pointer-to-object "keeper" class with automatic deletion.
Definition: r8bbase.h:429
void acquire()
Function "acquires" *this thread synchronizer object immediately or waits until another thread releas...
Definition: r8bbase.h:558
T unkeep()
Definition: r8bbase.h:504
The "configuration" inclusion file you can modify.
CSyncKeeper(CSyncObject *const aSyncObj)
Definition: r8bbase.h:617
void calcFIRFilterResponseAndGroupDelay(const double *const flt, const int fltlen, const double th, double &re, double &im, double &gd)
Function calculates frequency response and group delay of the specified FIR filter at the specified c...
Definition: r8bbase.h:882
#define R8B_MEMALLOCCLASS
Macro defines the name of the class that implements raw memory allocation functions, see the r8b::CStdMemAllocator class for details.
Definition: r8bconf.h:108
#define R8BNOCTOR(ClassName)
A special macro that defines empty copy-constructor and copy operator with the "private:" prefix...
Definition: r8bbase.h:144
void calcInterpCoeffs3p6(double *const c, const double *const y)
Function calculates coefficients used to calculate 3rd order segment interpolation polynomial on the ...
Definition: r8bbase.h:1057
Multi-threaded synchronization object class.
Definition: r8bbase.h:526
void calcFIRFilterResponse(const double *flt, int fltlen, const double th, double &re0, double &im0, const int fltlat=0)
Function calculates frequency response of the specified FIR filter at the specified circular frequenc...
Definition: r8bbase.h:824
CSyncObject * SyncObj
Sync object in use (can be NULL).
Definition: r8bbase.h:646
T max(const T &v1, const T &v2)
Definition: r8bbase.h:1134
T min(const T &v1, const T &v2)
Definition: r8bbase.h:1118
void calcInterpCoeffs2p8(double *const c, const double *const y)
Function calculates coefficients used to calculate 3rd order segment interpolation polynomial on the ...
Definition: r8bbase.h:1099
double gauss(const double v)
Definition: r8bbase.h:1195
double pows(const double v, const double p)
Definition: r8bbase.h:1185
void init(const double si, const double ph, const double g)
Function initializes *this sine signal generator.
Definition: r8bbase.h:738
double sqr(const double x)
Definition: r8bbase.h:1174
#define M_PI
The macro equals to "pi" constant, fits 53-bit floating point mantissa.
Definition: r8bbase.h:94
void calcInterpCoeffs3p4(double *const c, const double *const y)
Function calculates coefficients used to calculate 3rd order segment interpolation polynomial on the ...
Definition: r8bbase.h:1040
static void * allocmem(const size_t Size)
Function allocates memory block.
Definition: r8bbase.h:229
CSineGen(const double si, const double ph)
Constructor initializes *this sine signal generator.
Definition: r8bbase.h:689
CSyncKeeper(CSyncObject &aSyncObj)
Definition: r8bbase.h:631
void realloc(const int PrevCapacity, const int NewCapacity)
Function reallocates memory so that the specified number of elements of type T can be stored in *this...
Definition: r8bbase.h:338
double generate()
Definition: r8bbase.h:749
CSineGen(const double si, const double ph, const double g)
Constructor initializes *this sine signal generator.
Definition: r8bbase.h:706
double besselI0(const double x)
Definition: r8bbase.h:1216
void free()
Function deallocates a previously allocated buffer.
Definition: r8bbase.h:366
Templated memory buffer class for element buffers of fixed capacity.
Definition: r8bbase.h:278
void calcSpline3p8Coeffs(double *c, const double xm3, const double xm2, const double xm1, const double x0, const double x1, const double x2, const double x3, const double x4)
Function calculates coefficients used to calculate 3rd order spline (polynomial) on the equidistant l...
Definition: r8bbase.h:987
The default base class for objects that allocate blocks of memory.
Definition: r8bbase.h:219
T * getPtr() const
Definition: r8bbase.h:378
void release()
Function "releases" *this previously acquired thread synchronizer object.
Definition: r8bbase.h:572
The "r8brain-free-src" library namespace.
Definition: CDSPBlockConvolver.h:21
void init(const double si, const double ph)
Function initializes *this sine signal generator.
Definition: r8bbase.h:721
void alloc(const int Capacity)
Function allocates memory so that the specified number of elements of type T can be stored in *this b...
Definition: r8bbase.h:318
The default base class for objects created on heap.
Definition: r8bbase.h:156
static void * reallocmem(void *p, const size_t Size)
Function reallocates a previously allocated memory block.
Definition: r8bbase.h:242