Merge pull request #506 from andrewcsmith/patch-2
[supercollider.git] / include / plugin_interface / SC_InterfaceTable.h
blobf8c05e5c85725695d851fc3cb8377ece0822a5b7
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 static const int sc_api_version = 2;
26 #include "SC_Types.h"
27 #include "SC_SndBuf.h"
28 #include "SC_Unit.h"
29 #include "SC_BufGen.h"
30 #include "SC_FifoMsg.h"
31 #include "SC_fftlib.h"
32 #include "SC_Export.h"
34 #ifndef NO_LIBSNDFILE
35 #include <sndfile.h>
36 #endif
38 struct World;
40 typedef bool (*AsyncStageFn)(World *inWorld, void* cmdData);
41 typedef void (*AsyncFreeFn)(World *inWorld, void* cmdData);
43 struct ScopeBufferHnd
45 void *internalData;
46 float *data;
47 uint32 channels;
48 uint32 maxFrames;
50 float *channel_data( uint32 channel ) {
51 return data + (channel * maxFrames);
54 operator bool ()
56 return internalData != 0;
60 struct InterfaceTable
62 unsigned int mSineSize;
63 float32 *mSineWavetable;
64 float32 *mSine;
65 float32 *mCosecant;
67 // call printf for debugging. should not use in finished code.
68 int (*fPrint)(const char *fmt, ...);
70 // get a seed for a random number generator
71 int32 (*fRanSeed)();
73 // define a unit def
74 bool (*fDefineUnit)(const char *inUnitClassName, size_t inAllocSize,
75 UnitCtorFunc inCtor, UnitDtorFunc inDtor, uint32 inFlags);
77 // define a command /cmd
78 bool (*fDefinePlugInCmd)(const char *inCmdName, PlugInCmdFunc inFunc, void* inUserData);
80 // define a command for a unit generator /u_cmd
81 bool (*fDefineUnitCmd)(const char *inUnitClassName, const char *inCmdName, UnitCmdFunc inFunc);
83 // define a buf gen
84 bool (*fDefineBufGen)(const char *inName, BufGenFunc inFunc);
86 // clear all of the unit's outputs.
87 void (*fClearUnitOutputs)(Unit *inUnit, int inNumSamples);
89 // non real time memory allocation
90 void* (*fNRTAlloc)(size_t inSize);
91 void* (*fNRTRealloc)(void *inPtr, size_t inSize);
92 void (*fNRTFree)(void *inPtr);
94 // real time memory allocation
95 void* (*fRTAlloc)(World *inWorld, size_t inSize);
96 void* (*fRTRealloc)(World *inWorld, void *inPtr, size_t inSize);
97 void (*fRTFree)(World *inWorld, void *inPtr);
99 // call to set a Node to run or not.
100 void (*fNodeRun)(struct Node* node, int run);
102 // call to stop a Graph after the next buffer.
103 void (*fNodeEnd)(struct Node* graph);
105 // send a trigger from a Node to clients
106 void (*fSendTrigger)(struct Node* inNode, int triggerID, float value);
108 // send a reply message from a Node to clients
109 void (*fSendNodeReply)(struct Node* inNode, int replyID, const char* cmdName, int numArgs, const float* values);
111 // sending messages between real time and non real time levels.
112 bool (*fSendMsgFromRT)(World *inWorld, struct FifoMsg& inMsg);
113 bool (*fSendMsgToRT)(World *inWorld, struct FifoMsg& inMsg);
115 // libsndfile support
116 #ifdef NO_LIBSNDFILE
117 int (*fSndFileFormatInfoFromStrings)(void *info,
118 const char *headerFormatString, const char *sampleFormatString);
119 #else
120 int (*fSndFileFormatInfoFromStrings)(SF_INFO *info,
121 const char *headerFormatString, const char *sampleFormatString);
122 #endif
124 // get nodes by id
125 struct Node* (*fGetNode)(World *inWorld, int inID);
126 struct Graph* (*fGetGraph)(World *inWorld, int inID);
128 void (*fNRTLock)(World *inWorld);
129 void (*fNRTUnlock)(World *inWorld);
131 bool mAltivecAvailable;
133 void (*fGroup_DeleteAll)(struct Group* group);
134 void (*fDoneAction)(int doneAction, struct Unit *unit);
136 int (*fDoAsynchronousCommand)
138 World *inWorld,
139 void* replyAddr,
140 const char* cmdName,
141 void *cmdData,
142 AsyncStageFn stage2, // stage2 is non real time
143 AsyncStageFn stage3, // stage3 is real time - completion msg performed if stage3 returns true
144 AsyncStageFn stage4, // stage4 is non real time - sends done if stage4 returns true
145 AsyncFreeFn cleanup,
146 int completionMsgSize,
147 void* completionMsgData
151 // fBufAlloc should only be called within a BufGenFunc
152 int (*fBufAlloc)(SndBuf *inBuf, int inChannels, int inFrames, double inSampleRate);
154 // To initialise a specific FFT, ensure your input and output buffers exist. Internal data structures
155 // will be allocated using the alloc object,
156 // Both "fullsize" and "winsize" should be powers of two (this is not checked internally).
157 struct scfft * (*fSCfftCreate)(size_t fullsize, size_t winsize, SCFFT_WindowFunction wintype,
158 float *indata, float *outdata, SCFFT_Direction forward, SCFFT_Allocator & alloc);
160 void (*fSCfftDoFFT)(scfft *f);
161 void (*fSCfftDoIFFT)(scfft *f);
163 // destroy any resources held internally.
164 void (*fSCfftDestroy)(scfft *f, SCFFT_Allocator & alloc);
166 // Get scope buffer. Returns the maximum number of possile frames.
167 bool (*fGetScopeBuffer)(World *inWorld, int index, int channels, int maxFrames, ScopeBufferHnd &);
168 void (*fPushScopeBuffer)(World *inWorld, ScopeBufferHnd &, int frames);
169 void (*fReleaseScopeBuffer)(World *inWorld, ScopeBufferHnd &);
172 typedef struct InterfaceTable InterfaceTable;
174 #define Print (*ft->fPrint)
175 #define RanSeed (*ft->fRanSeed)
176 #define NodeEnd (*ft->fNodeEnd)
177 #define NodeRun (*ft->fNodeRun)
178 #define DefineUnit (*ft->fDefineUnit)
179 #define DefinePlugInCmd (*ft->fDefinePlugInCmd)
180 #define DefineUnitCmd (*ft->fDefineUnitCmd)
181 #define DefineBufGen (*ft->fDefineBufGen)
182 #define ClearUnitOutputs (*ft->fClearUnitOutputs)
183 #define SendTrigger (*ft->fSendTrigger)
184 #define SendNodeReply (*ft->fSendNodeReply)
185 #define SendMsgFromRT (*ft->fSendMsgFromRT)
186 #define SendMsgToRT (*ft->fSendMsgToRT)
187 #define DoneAction (*ft->fDoneAction)
189 #define NRTAlloc (*ft->fNRTAlloc)
190 #define NRTRealloc (*ft->fNRTRealloc)
191 #define NRTFree (*ft->fNRTFree)
193 #define RTAlloc (*ft->fRTAlloc)
194 #define RTRealloc (*ft->fRTRealloc)
195 #define RTFree (*ft->fRTFree)
197 #define SC_GetNode (*ft->fGetNode)
198 #define SC_GetGraph (*ft->fGetGraph)
200 #define NRTLock (*ft->fNRTLock)
201 #define NRTUnlock (*ft->fNRTUnlock)
203 #define BufAlloc (*ft->fBufAlloc)
205 #define GroupDeleteAll (*ft->fGroup_DeleteAll)
207 #define SndFileFormatInfoFromStrings (*ft->fSndFileFormatInfoFromStrings)
209 #define DoAsynchronousCommand (*ft->fDoAsynchronousCommand)
211 #define DefineSimpleUnit(name) \
212 (*ft->fDefineUnit)(#name, sizeof(name), (UnitCtorFunc)&name##_Ctor, 0, 0);
214 #define DefineDtorUnit(name) \
215 (*ft->fDefineUnit)(#name, sizeof(name), (UnitCtorFunc)&name##_Ctor, \
216 (UnitDtorFunc)&name##_Dtor, 0);
218 #define DefineSimpleCantAliasUnit(name) \
219 (*ft->fDefineUnit)(#name, sizeof(name), (UnitCtorFunc)&name##_Ctor, 0, kUnitDef_CantAliasInputsToOutputs);
221 #define DefineDtorCantAliasUnit(name) \
222 (*ft->fDefineUnit)(#name, sizeof(name), (UnitCtorFunc)&name##_Ctor, \
223 (UnitDtorFunc)&name##_Dtor, kUnitDef_CantAliasInputsToOutputs);
225 #ifdef STATIC_PLUGINS
226 #define PluginLoad(name) void name##_Load(InterfaceTable *inTable)
227 #else
228 #define PluginLoad(name) \
229 C_LINKAGE SC_API_EXPORT int api_version(void) { return sc_api_version; } \
230 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