1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411
|
/************************************************************************
************************************************************************
Copyright (C) 2003-2017 GRAME, Centre National de Creation Musicale
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
************************************************************************
************************************************************************/
#ifndef DSP_AUX_H
#define DSP_AUX_H
#include <string.h>
#include <cassert>
#include <list>
#include <map>
#include <string>
#include <vector>
#ifdef WIN32
#pragma warning(disable : 4800)
#endif
#include "faust/dsp/dsp.h"
#include "faust/export.h"
#include "exception.hh"
/*!
\brief the base class for smart pointers implementation
Any object that want to support smart pointers should
inherit from the smartable class which provides reference counting
and automatic delete when the reference count drops to zero.
*/
class faust_smartable {
private:
unsigned refCount;
public:
//! gives the reference count of the object
unsigned refs() const { return refCount; }
//! addReference increments the ref count and checks for refCount overflow
void addReference()
{
refCount++;
faustassert(refCount != 0);
}
//! removeReference delete the object when refCount is zero
void removeReference()
{
if (--refCount == 0) {
delete this;
}
}
protected:
faust_smartable() : refCount(0) {}
faust_smartable(const faust_smartable&) : refCount(0) {}
//! destructor checks for non-zero refCount
virtual ~faust_smartable() { faustassert(refCount == 0); }
faust_smartable& operator=(const faust_smartable&) { return *this; }
};
/*!
\brief the smart pointer implementation
A smart pointer is in charge of maintaining the objects reference count
by the way of pointers operators overloading. It supports class
inheritance and conversion whenever possible.
\n Instances of the SMARTP class are supposed to use \e smartable types (or at least
objects that implements the \e addReference and \e removeReference
methods in a consistent way).
*/
template <class T>
class faust_smartptr {
private:
//! the actual pointer to the class
T* fSmartPtr;
public:
//! an empty constructor - points to null
faust_smartptr() : fSmartPtr(0) {}
//! build a smart pointer from a class pointer
faust_smartptr(T* rawptr) : fSmartPtr(rawptr)
{
if (fSmartPtr) {
fSmartPtr->addReference();
}
}
//! build a smart pointer from an convertible class reference
template <class T2>
faust_smartptr(const faust_smartptr<T2>& ptr) : fSmartPtr((T*)ptr)
{
if (fSmartPtr) {
fSmartPtr->addReference();
}
}
//! build a smart pointer from another smart pointer reference
faust_smartptr(const faust_smartptr& ptr) : fSmartPtr((T*)ptr)
{
if (fSmartPtr) {
fSmartPtr->addReference();
}
}
//! the smart pointer destructor: simply removes one reference count
~faust_smartptr()
{
if (fSmartPtr) {
fSmartPtr->removeReference();
}
}
//! cast operator to retrieve the actual class pointer
operator T*() const { return fSmartPtr; }
//! '*' operator to access the actual class pointer
T& operator*() const
{
// checks for null dereference
faustassert(fSmartPtr != 0);
return *fSmartPtr;
}
//! operator -> overloading to access the actual class pointer
T* operator->() const
{
// checks for null dereference
faustassert(fSmartPtr != 0);
return fSmartPtr;
}
//! operator = that moves the actual class pointer
template <class T2>
faust_smartptr& operator=(T2 p1_)
{
*this = (T*)p1_;
return *this;
}
//! operator = that moves the actual class pointer
faust_smartptr& operator=(T* p_)
{
// check first that pointers differ
if (fSmartPtr != p_) {
// increments the ref count of the new pointer if not null
if (p_ != 0) {
p_->addReference();
}
// decrements the ref count of the old pointer if not null
if (fSmartPtr != 0) {
fSmartPtr->removeReference();
}
// and finally stores the new actual pointer
fSmartPtr = p_;
}
return *this;
}
//! operator < to support faust_smartptr map with Visual C++
bool operator<(const faust_smartptr<T>& p_) const { return fSmartPtr < ((T*)p_); }
//! operator = to support inherited class reference
faust_smartptr& operator=(const faust_smartptr<T>& p_) { return operator=((T*)p_); }
//! dynamic cast support
template <class T2>
faust_smartptr& cast(T2* p_)
{
return operator=(dynamic_cast<T*>(p_));
}
//! dynamic cast support
template <class T2>
faust_smartptr& cast(const faust_smartptr<T2>& p_)
{
return operator=(dynamic_cast<T*>(p_));
}
};
//----------------------------------------------------------------
// Smart DSP factory table
//----------------------------------------------------------------
template <class T>
struct dsp_factory_table : public std::map<T, std::list<dsp*> > {
typedef typename std::map<T, std::list<dsp*> >::iterator factory_iterator;
dsp_factory_table() {}
virtual ~dsp_factory_table() {}
bool getFactory(const std::string& sha_key, factory_iterator& res)
{
for (factory_iterator it = this->begin(); it != this->end(); it++) {
if ((*it).first->getSHAKey() == sha_key) {
res = it;
return true;
}
}
return false;
}
void setFactory(T factory)
{
this->insert(std::pair<T, std::list<dsp*> >(factory, std::list<dsp*>()));
}
bool addDSP(T factory, dsp* dsp)
{
// Add 'dsp' in its factory
factory_iterator it = this->find(factory);
if (it != this->end()) {
(*it).second.push_back(dsp);
return true;
} else {
std::cerr << "WARNING : addDSP factory not found!" << std::endl;
return false;
}
}
bool removeDSP(T factory, dsp* dsp)
{
// Remove 'dsp' from its factory
factory_iterator it = this->find(factory);
faustassert(it != this->end());
if (it != this->end()) {
(*it).second.remove(dsp);
return true;
} else {
std::cerr << "WARNING : removeDSP factory not found!" << std::endl;
return false;
}
}
std::vector<std::string> getAllDSPFactories()
{
factory_iterator it;
std::vector<std::string> sha_key_list;
for (it = this->begin(); it != this->end(); it++) {
sha_key_list.push_back((*it).first->getSHAKey());
}
return sha_key_list;
}
dsp_factory* getDSPFactoryFromSHAKey(const std::string& sha_key)
{
factory_iterator it;
if (getFactory(sha_key, it)) {
T sfactory = (*it).first;
sfactory->addReference();
return sfactory;
} else {
std::cerr << "WARNING : getDSPFactoryFromSHAKey factory not found!" << std::endl;
return 0;
}
}
bool deleteDSPFactory(T factory)
{
factory_iterator it;
if ((it = this->find(factory)) != this->end()) {
std::list<dsp*> dsp_list = (*it).second;
if (factory->refs() == 2) { // Function argument + the one in table...
// Possibly delete remaining DSP
for (const auto& it1 : dsp_list) {
delete it1;
}
// Last use, remove from the global table, pointer will be deleted
this->erase(factory);
return true;
} else {
factory->removeReference();
return false;
}
} else {
std::cerr << "WARNING : deleteDSPFactory factory not found!" << std::endl;
return false;
}
}
void deleteAllDSPFactories()
{
factory_iterator it;
for (it = this->begin(); it != this->end(); it++) {
// Decrement counter up to one...
while (((*it).first)->refs() > 1) {
((*it).first)->removeReference();
}
}
// Then clear the table thus finally deleting all ref = 1 smart pointers
this->clear();
}
};
// Compute SHA1 key from name_app, dsp_content and compilations arguments, and returns the
// dsp_content
std::string sha1FromDSP(const std::string& name_app, const std::string& dsp_content, int argc,
const char* argv[], std::string& sha_key);
class CTree;
typedef CTree* Tree;
typedef std::vector<Tree> tvec;
tvec boxesToSignalsAux(Tree box);
#ifdef __cplusplus
extern "C" {
#endif
LIBFAUST_API const char* expandCDSPFromFile(const char* filename, int argc, const char* argv[],
char* sha_key, char* error_msg);
LIBFAUST_API const char* expandCDSPFromString(const char* name_app, const char* dsp_content,
int argc, const char* argv[], char* sha_key,
char* error_msg);
LIBFAUST_API bool generateCAuxFilesFromFile(const char* filename, int argc, const char* argv[],
char* error_msg);
LIBFAUST_API bool generateCAuxFilesFromString(const char* name_app, const char* dsp_content,
int argc, const char* argv[], char* error_msg);
#ifdef __cplusplus
}
#endif
#define BUFFER_SIZE 1024
#define SAMPLE_RATE 44100
#define MAX_CHAN 64
#define MAX_SOUNDFILE_PARTS 256
#ifdef _MSC_VER
#define PRE_PACKED_STRUCTURE __pragma(pack(push, 1))
#define POST_PACKED_STRUCTURE \
; \
__pragma(pack(pop))
#else
#define PRE_PACKED_STRUCTURE
#define POST_PACKED_STRUCTURE __attribute__((__packed__))
#endif
PRE_PACKED_STRUCTURE
struct Soundfile {
enum { kBuffers, kLength, kSR, kOffset };
double** fBuffers; // use the largest size to cover 'float' and 'double' cases
int* fLength; // length of each part
int* fSR; // sample rate of each part
int* fOffset; // offset of each part in the global buffer
int fChannels; // max number of channels of all concatenated files
int fParts; // the total number of loaded parts
bool fIsDouble; // keep the sample format (float or double)
Soundfile(int max_chan)
{
fBuffers = new double*[max_chan];
fLength = new int[MAX_SOUNDFILE_PARTS];
fSR = new int[MAX_SOUNDFILE_PARTS];
fOffset = new int[MAX_SOUNDFILE_PARTS];
for (int part = 0; part < MAX_SOUNDFILE_PARTS; part++) {
fLength[part] = BUFFER_SIZE;
fSR[part] = SAMPLE_RATE;
fOffset[part] = 0;
}
// Allocate 1 channel
fChannels = 1;
fParts = 0;
fBuffers[0] = new double[BUFFER_SIZE];
faustassert(fBuffers[0]);
fIsDouble = true;
memset(fBuffers[0], 0, BUFFER_SIZE * sizeof(double));
// Share the same buffer for all other channels so that we have max_chan channels available
for (int chan = fChannels; chan < max_chan; chan++) {
fBuffers[chan] = fBuffers[0];
}
}
~Soundfile()
{
// Free the real channels only
for (int chan = 0; chan < fChannels; chan++) {
delete[] fBuffers[chan];
}
delete[] fBuffers;
delete[] fLength;
delete[] fSR;
delete[] fOffset;
}
} POST_PACKED_STRUCTURE;
#endif
|