common: prevent buffer overflow
[supercollider.git] / include / plugin_interface / SC_InterfaceTable.h
blob311509210453574627ea704ebba5effc38243d3d
1 /*
2 SuperCollider real time audio synthesis system
3 Copyright (c) 2002 James McCartney. All rights reserved.
4 http://www.audiosynth.com
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 #ifndef _SC_SynthInterfaceTable_
22 #define _SC_SynthInterfaceTable_
24 #include "SC_Types.h"
25 #include "SC_SndBuf.h"
26 #include "SC_Unit.h"
27 #include "SC_BufGen.h"
28 #include "SC_FifoMsg.h"
29 #include "SC_fftlib.h"
30 #include "SC_Export.h"
32 #ifndef NO_LIBSNDFILE
33 #ifdef _WIN32
34 #include <sndfile-win.h>
35 #else
36 #include <sndfile.h>
37 #endif
38 #endif
40 struct World;
42 typedef bool (*AsyncStageFn)(World *inWorld, void* cmdData);
43 typedef void (*AsyncFreeFn)(World *inWorld, void* cmdData);
45 struct ScopeBufferHnd
47 void *internalData;
48 float *data;
49 uint32 channels;
50 uint32 maxFrames;
52 float *channel_data( uint32 channel ) {
53 return data + (channel * maxFrames);
56 operator bool ()
58 return internalData != 0;
62 struct InterfaceTable
64 unsigned int mSineSize;
65 float32 *mSineWavetable;
66 float32 *mSine;
67 float32 *mCosecant;
69 // call printf for debugging. should not use in finished code.
70 int (*fPrint)(const char *fmt, ...);
72 // get a seed for a random number generator
73 int32 (*fRanSeed)();
75 // define a unit def
76 bool (*fDefineUnit)(const char *inUnitClassName, size_t inAllocSize,
77 UnitCtorFunc inCtor, UnitDtorFunc inDtor, uint32 inFlags);
79 // define a command /cmd
80 bool (*fDefinePlugInCmd)(const char *inCmdName, PlugInCmdFunc inFunc, void* inUserData);
82 // define a command for a unit generator /u_cmd
83 bool (*fDefineUnitCmd)(const char *inUnitClassName, const char *inCmdName, UnitCmdFunc inFunc);
85 // define a buf gen
86 bool (*fDefineBufGen)(const char *inName, BufGenFunc inFunc);
88 // clear all of the unit's outputs.
89 void (*fClearUnitOutputs)(Unit *inUnit, int inNumSamples);
91 // non real time memory allocation
92 void* (*fNRTAlloc)(size_t inSize);
93 void* (*fNRTRealloc)(void *inPtr, size_t inSize);
94 void (*fNRTFree)(void *inPtr);
96 // real time memory allocation
97 void* (*fRTAlloc)(World *inWorld, size_t inSize);
98 void* (*fRTRealloc)(World *inWorld, void *inPtr, size_t inSize);
99 void (*fRTFree)(World *inWorld, void *inPtr);
101 // call to set a Node to run or not.
102 void (*fNodeRun)(struct Node* node, int run);
104 // call to stop a Graph after the next buffer.
105 void (*fNodeEnd)(struct Node* graph);
107 // send a trigger from a Node to clients
108 void (*fSendTrigger)(struct Node* inNode, int triggerID, float value);
110 // send a reply message from a Node to clients
111 void (*fSendNodeReply)(struct Node* inNode, int replyID, const char* cmdName, int numArgs, const float* values);
113 // sending messages between real time and non real time levels.
114 bool (*fSendMsgFromRT)(World *inWorld, struct FifoMsg& inMsg);
115 bool (*fSendMsgToRT)(World *inWorld, struct FifoMsg& inMsg);
117 // libsndfile support
118 #ifdef NO_LIBSNDFILE
119 int (*fSndFileFormatInfoFromStrings)(void *info,
120 const char *headerFormatString, const char *sampleFormatString);
121 #else
122 int (*fSndFileFormatInfoFromStrings)(SF_INFO *info,
123 const char *headerFormatString, const char *sampleFormatString);
124 #endif
126 // get nodes by id
127 struct Node* (*fGetNode)(World *inWorld, int inID);
128 struct Graph* (*fGetGraph)(World *inWorld, int inID);
130 void (*fNRTLock)(World *inWorld);
131 void (*fNRTUnlock)(World *inWorld);
133 bool mAltivecAvailable;
135 void (*fGroup_DeleteAll)(struct Group* group);
136 void (*fDoneAction)(int doneAction, struct Unit *unit);
138 int (*fDoAsynchronousCommand)
140 World *inWorld,
141 void* replyAddr,
142 const char* cmdName,
143 void *cmdData,
144 AsyncStageFn stage2, // stage2 is non real time
145 AsyncStageFn stage3, // stage3 is real time - completion msg performed if stage3 returns true
146 AsyncStageFn stage4, // stage4 is non real time - sends done if stage4 returns true
147 AsyncFreeFn cleanup,
148 int completionMsgSize,
149 void* completionMsgData
153 // fBufAlloc should only be called within a BufGenFunc
154 int (*fBufAlloc)(SndBuf *inBuf, int inChannels, int inFrames, double inSampleRate);
156 // To initialise a specific FFT, ensure your input and output buffers exist. Internal data structures
157 // will be allocated using the alloc object,
158 // Both "fullsize" and "winsize" should be powers of two (this is not checked internally).
159 struct scfft * (*fSCfftCreate)(size_t fullsize, size_t winsize, SCFFT_WindowFunction wintype,
160 float *indata, float *outdata, SCFFT_Direction forward, SCFFT_Allocator & alloc);
162 void (*fSCfftDoFFT)(scfft *f);
163 void (*fSCfftDoIFFT)(scfft *f);
165 // destroy any resources held internally.
166 void (*fSCfftDestroy)(scfft *f, SCFFT_Allocator & alloc);
168 // Get scope buffer. Returns the maximum number of possile frames.
169 bool (*fGetScopeBuffer)(World *inWorld, int index, int channels, int maxFrames, ScopeBufferHnd &);
170 void (*fPushScopeBuffer)(World *inWorld, ScopeBufferHnd &, int frames);
171 void (*fReleaseScopeBuffer)(World *inWorld, ScopeBufferHnd &);
174 typedef struct InterfaceTable InterfaceTable;
176 #define Print (*ft->fPrint)
177 #define RanSeed (*ft->fRanSeed)
178 #define NodeEnd (*ft->fNodeEnd)
179 #define NodeRun (*ft->fNodeRun)
180 #define DefineUnit (*ft->fDefineUnit)
181 #define DefinePlugInCmd (*ft->fDefinePlugInCmd)
182 #define DefineUnitCmd (*ft->fDefineUnitCmd)
183 #define DefineBufGen (*ft->fDefineBufGen)
184 #define ClearUnitOutputs (*ft->fClearUnitOutputs)
185 #define SendTrigger (*ft->fSendTrigger)
186 #define SendNodeReply (*ft->fSendNodeReply)
187 #define SendMsgFromRT (*ft->fSendMsgFromRT)
188 #define SendMsgToRT (*ft->fSendMsgToRT)
189 #define DoneAction (*ft->fDoneAction)
191 #define NRTAlloc (*ft->fNRTAlloc)
192 #define NRTRealloc (*ft->fNRTRealloc)
193 #define NRTFree (*ft->fNRTFree)
195 #define RTAlloc (*ft->fRTAlloc)
196 #define RTRealloc (*ft->fRTRealloc)
197 #define RTFree (*ft->fRTFree)
199 #define SC_GetNode (*ft->fGetNode)
200 #define SC_GetGraph (*ft->fGetGraph)
202 #define NRTLock (*ft->fNRTLock)
203 #define NRTUnlock (*ft->fNRTUnlock)
205 #define BufAlloc (*ft->fBufAlloc)
207 #define GroupDeleteAll (*ft->fGroup_DeleteAll)
209 #define SndFileFormatInfoFromStrings (*ft->fSndFileFormatInfoFromStrings)
211 #define DoAsynchronousCommand (*ft->fDoAsynchronousCommand)
213 #define DefineSimpleUnit(name) \
214 (*ft->fDefineUnit)(#name, sizeof(name), (UnitCtorFunc)&name##_Ctor, 0, 0);
216 #define DefineDtorUnit(name) \
217 (*ft->fDefineUnit)(#name, sizeof(name), (UnitCtorFunc)&name##_Ctor, \
218 (UnitDtorFunc)&name##_Dtor, 0);
220 #define DefineSimpleCantAliasUnit(name) \
221 (*ft->fDefineUnit)(#name, sizeof(name), (UnitCtorFunc)&name##_Ctor, 0, kUnitDef_CantAliasInputsToOutputs);
223 #define DefineDtorCantAliasUnit(name) \
224 (*ft->fDefineUnit)(#name, sizeof(name), (UnitCtorFunc)&name##_Ctor, \
225 (UnitDtorFunc)&name##_Dtor, kUnitDef_CantAliasInputsToOutputs);
227 #ifdef STATIC_PLUGINS
228 #define PluginLoad(name) void name##_Load(InterfaceTable *inTable)
229 #else
230 #define PluginLoad(name) C_LINKAGE SC_API_EXPORT void load(InterfaceTable *inTable)
231 #endif
233 #define scfft_create (*ft->fSCfftCreate)
234 #define scfft_dofft (*ft->fSCfftDoFFT)
235 #define scfft_doifft (*ft->fSCfftDoIFFT)
236 #define scfft_destroy (*ft->fSCfftDestroy)
239 class SCWorld_Allocator:
240 public SCFFT_Allocator
242 InterfaceTable * ft;
243 World * world;
245 public:
246 SCWorld_Allocator(InterfaceTable * ft, World * world):
247 ft(ft), world(world)
250 virtual void* alloc(size_t size)
252 return RTAlloc(world, size);
255 virtual void free(void* ptr)
257 RTFree(world, ptr);
262 #endif