SCDoc: Use proper static string constants instead of comparing string literals.
[supercollider.git] / include / server / SC_SequencedCommand.h
blobf962ce2402741d5927788d9ffc010f35c8c2a82e
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
22 * Having SequencedCommands allows performing actions that might otherwise require
23 * taking a mutex, which is undesirable in a real time thread.
24 * Some commands require several stages of processing at both the real time
25 * and non real time levels. This class does the messaging between levels for you
26 * so that you only need to write the functions.
29 #ifndef _SC_SequencedCommand_
30 #define _SC_SequencedCommand_
32 #include "OSC_Packet.h"
33 #include "SC_World.h"
34 #include "SC_BufGen.h"
35 #include "sc_msg_iter.h"
36 #ifndef NO_LIBSNDFILE
37 #ifdef _WIN32
38 #include <sndfile-win.h>
39 #else
40 #include <sndfile.h>
41 #endif
42 #endif
43 #include <new>
45 #define CallSequencedCommand(T, inWorld, inSize, inData, inReply) \
46 void* space = World_Alloc(inWorld, sizeof(T)); \
47 T *cmd = new (space) T(inWorld, inReply); \
48 if (!cmd) return kSCErr_Failed; \
49 int err = cmd->Init(inData, inSize); \
50 if (err) { \
51 cmd->~T(); \
52 World_Free(inWorld, space); \
53 return err; \
54 } \
55 if (inWorld->mRealTime) cmd->CallNextStage(); \
56 else cmd->CallEveryStage();
59 class SC_SequencedCommand
61 public:
62 SC_SequencedCommand(World *inWorld, ReplyAddress *inReplyAddress);
63 virtual ~SC_SequencedCommand();
65 void Delete();
67 void CallEveryStage();
68 void CallNextStage();
70 virtual int Init(char *inData, int inSize);
72 virtual bool Stage1(); // real time
73 virtual bool Stage2(); // non real time
74 virtual bool Stage3(); // real time
75 virtual void Stage4(); // non real time
77 void SendDone(const char *inCommandName);
78 void SendDoneWithIntValue(const char *inCommandName, int value);
81 protected:
82 int mNextStage;
83 ReplyAddress mReplyAddress;
84 World *mWorld;
86 int mMsgSize;
87 char *mMsgData;
89 virtual void CallDestructor()=0;
92 ///////////////////////////////////////////////////////////////////////////
94 class SyncCmd : public SC_SequencedCommand
96 public:
97 SyncCmd(World *inWorld, ReplyAddress *inReplyAddress);
99 virtual int Init(char *inData, int inSize);
101 virtual bool Stage2(); // non real time
102 virtual bool Stage3(); // real time
103 virtual void Stage4(); // non real time
105 protected:
106 virtual void CallDestructor();
107 int mID;
110 ///////////////////////////////////////////////////////////////////////////
112 class BufGenCmd : public SC_SequencedCommand
114 public:
115 BufGenCmd(World *inWorld, ReplyAddress *inReplyAddress);
116 virtual ~BufGenCmd();
118 virtual int Init(char *inData, int inSize);
120 virtual bool Stage2(); // non real time
121 virtual bool Stage3(); // real time
122 virtual void Stage4(); // non real time
124 protected:
125 int mBufIndex;
126 BufGen *mBufGen;
127 sc_msg_iter mMsg;
128 char *mData;
129 int mSize;
130 SndBuf mSndBuf;
131 float *mFreeData;
133 virtual void CallDestructor();
137 ///////////////////////////////////////////////////////////////////////////
139 class BufAllocCmd : public SC_SequencedCommand
141 public:
142 BufAllocCmd(World *inWorld, ReplyAddress *inReplyAddress);
144 virtual int Init(char *inData, int inSize);
146 virtual bool Stage2(); // non real time
147 virtual bool Stage3(); // real time
148 virtual void Stage4(); // non real time
150 protected:
151 int mBufIndex;
152 SndBuf mSndBuf;
153 int mNumChannels, mNumFrames;
154 float *mFreeData;
156 virtual void CallDestructor();
160 ///////////////////////////////////////////////////////////////////////////
163 class BufFreeCmd : public SC_SequencedCommand
165 public:
166 BufFreeCmd(World *inWorld, ReplyAddress *inReplyAddress);
168 virtual int Init(char *inData, int inSize);
170 virtual bool Stage2(); // non real time
171 virtual bool Stage3(); // real time
172 virtual void Stage4(); // non real time
174 protected:
175 int mBufIndex;
176 float *mFreeData;
178 virtual void CallDestructor();
182 ///////////////////////////////////////////////////////////////////////////
185 class BufCloseCmd : public SC_SequencedCommand
187 public:
188 BufCloseCmd(World *inWorld, ReplyAddress *inReplyAddress);
190 virtual int Init(char *inData, int inSize);
192 virtual bool Stage2(); // non real time
193 virtual bool Stage3(); // real time
194 virtual void Stage4(); // non real time
196 protected:
197 int mBufIndex;
199 virtual void CallDestructor();
203 ///////////////////////////////////////////////////////////////////////////
206 class BufZeroCmd : public SC_SequencedCommand
208 public:
209 BufZeroCmd(World *inWorld, ReplyAddress *inReplyAddress);
211 virtual int Init(char *inData, int inSize);
213 virtual bool Stage2(); // non real time
214 virtual bool Stage3(); // real time
215 virtual void Stage4(); // non real time
217 protected:
218 int mBufIndex;
220 virtual void CallDestructor();
223 ///////////////////////////////////////////////////////////////////////////
225 class BufAllocReadCmd : public SC_SequencedCommand
227 public:
228 BufAllocReadCmd(World *inWorld, ReplyAddress *inReplyAddress);
229 virtual ~BufAllocReadCmd();
231 virtual int Init(char *inData, int inSize);
233 virtual bool Stage2(); // non real time
234 virtual bool Stage3(); // real time
235 virtual void Stage4(); // non real time
237 protected:
238 int mBufIndex;
239 float *mFreeData;
240 SndBuf mSndBuf;
241 char *mFilename;
242 int mFileOffset, mNumFrames;
244 virtual void CallDestructor();
247 ///////////////////////////////////////////////////////////////////////////
249 class BufReadCmd : public SC_SequencedCommand
251 public:
252 BufReadCmd(World *inWorld, ReplyAddress *inReplyAddress);
253 virtual ~BufReadCmd();
255 virtual int Init(char *inData, int inSize);
257 virtual bool Stage2(); // non real time
258 virtual bool Stage3(); // real time
259 virtual void Stage4(); // non real time
261 protected:
262 int mBufIndex;
263 char *mFilename;
264 int mFileOffset, mNumFrames, mBufOffset;
265 bool mLeaveFileOpen;
266 double mSampleRate;
267 virtual void CallDestructor();
270 ///////////////////////////////////////////////////////////////////////////
272 class SC_BufReadCommand : public SC_SequencedCommand
274 public:
275 enum {
276 kMaxNumChannels = 32
279 SC_BufReadCommand(World* inWorld, ReplyAddress* inReplyAddress);
280 virtual ~SC_BufReadCommand();
282 protected:
283 void InitChannels(sc_msg_iter& msg);
284 bool CheckChannels(int inNumChannels);
285 void CopyChannels(float* dst, float* src, size_t srcChannels, size_t numFrames);
287 protected:
288 int mNumChannels;
289 int mChannels[kMaxNumChannels];
292 ///////////////////////////////////////////////////////////////////////////
294 class BufAllocReadChannelCmd : public SC_BufReadCommand
296 public:
297 BufAllocReadChannelCmd(World *inWorld, ReplyAddress *inReplyAddress);
298 virtual ~BufAllocReadChannelCmd();
300 virtual int Init(char *inData, int inSize);
302 virtual bool Stage2(); // non real time
303 virtual bool Stage3(); // real time
304 virtual void Stage4(); // non real time
306 protected:
307 int mBufIndex;
308 float *mFreeData;
309 SndBuf mSndBuf;
310 char *mFilename;
311 int mFileOffset, mNumFrames;
312 virtual void CallDestructor();
315 ///////////////////////////////////////////////////////////////////////////
317 class BufReadChannelCmd : public SC_BufReadCommand
319 public:
320 BufReadChannelCmd(World *inWorld, ReplyAddress *inReplyAddress);
321 virtual ~BufReadChannelCmd();
323 virtual int Init(char *inData, int inSize);
325 virtual bool Stage2(); // non real time
326 virtual bool Stage3(); // real time
327 virtual void Stage4(); // non real time
329 protected:
330 int mBufIndex;
331 char *mFilename;
332 int mFileOffset, mNumFrames, mBufOffset;
333 bool mLeaveFileOpen;
334 double mSampleRate;
335 virtual void CallDestructor();
338 ///////////////////////////////////////////////////////////////////////////
340 class BufWriteCmd : public SC_SequencedCommand
342 public:
343 BufWriteCmd(World *inWorld, ReplyAddress *inReplyAddress);
344 virtual ~BufWriteCmd();
346 virtual int Init(char *inData, int inSize);
348 virtual bool Stage2(); // non real time
349 virtual bool Stage3(); // real time
350 virtual void Stage4(); // non real time
352 protected:
353 int mBufIndex;
354 char *mFilename;
355 #ifndef NO_LIBSNDFILE
356 SF_INFO mFileInfo;
357 #endif
358 int mNumFrames, mBufOffset;
359 bool mLeaveFileOpen;
361 virtual void CallDestructor();
364 ///////////////////////////////////////////////////////////////////////////
366 class AudioQuitCmd : public SC_SequencedCommand
368 public:
369 AudioQuitCmd(World *inWorld, ReplyAddress *inReplyAddress);
371 virtual bool Stage2(); // non real time
372 virtual bool Stage3(); // real time
373 virtual void Stage4(); // non real time
375 protected:
377 virtual void CallDestructor();
380 ///////////////////////////////////////////////////////////////////////////
382 class AudioStatusCmd : public SC_SequencedCommand
384 public:
385 AudioStatusCmd(World *inWorld, ReplyAddress *inReplyAddress);
387 virtual bool Stage2(); // non real time
389 protected:
391 virtual void CallDestructor();
394 ///////////////////////////////////////////////////////////////////////////
396 class NotifyCmd : public SC_SequencedCommand
398 public:
399 NotifyCmd(World *inWorld, ReplyAddress *inReplyAddress);
401 virtual int Init(char *inData, int inSize);
403 virtual bool Stage2(); // non real time
405 protected:
407 virtual void CallDestructor();
409 int mOnOff;
410 int mID;
414 ///////////////////////////////////////////////////////////////////////////
416 #define CallSendFailureCommand(inWorld, inCmdName, inErrString, inReply) \
417 void* space = World_Alloc(inWorld, sizeof(SendFailureCmd)); \
418 SendFailureCmd *cmd = new (space) SendFailureCmd(inWorld, inReply); \
419 if (!cmd) return kSCErr_Failed; \
420 cmd->InitSendFailureCmd(inCmdName, inErrString); \
421 if (inWorld->mRealTime) cmd->CallNextStage(); \
422 else cmd->CallEveryStage(); \
424 class SendFailureCmd : public SC_SequencedCommand
426 public:
427 SendFailureCmd(World *inWorld, ReplyAddress *inReplyAddress);
428 virtual ~SendFailureCmd();
430 virtual void InitSendFailureCmd(const char *inCmdName, const char* inErrString);
432 virtual bool Stage2(); // non real time
434 protected:
435 char *mCmdName, *mErrString;
437 virtual void CallDestructor();
440 ///////////////////////////////////////////////////////////////////////////
442 #include "SC_GraphDef.h"
444 class LoadSynthDefCmd : public SC_SequencedCommand
446 public:
447 LoadSynthDefCmd(World *inWorld, ReplyAddress *inReplyAddress);
448 virtual ~LoadSynthDefCmd();
450 virtual int Init(char *inData, int inSize);
452 virtual bool Stage2(); // non real time
453 virtual bool Stage3(); // real time
454 virtual void Stage4(); // non real time
456 protected:
457 char *mFilename;
458 GraphDef *mDefs;
460 virtual void CallDestructor();
463 ///////////////////////////////////////////////////////////////////////////
465 #include "SC_GraphDef.h"
467 class RecvSynthDefCmd : public SC_SequencedCommand
469 public:
470 RecvSynthDefCmd(World *inWorld, ReplyAddress *inReplyAddress);
471 virtual ~RecvSynthDefCmd();
473 virtual int Init(char *inData, int inSize);
475 virtual bool Stage2(); // non real time
476 virtual bool Stage3(); // real time
477 virtual void Stage4(); // non real time
479 protected:
480 char *mBuffer;
481 GraphDef *mDefs;
483 virtual void CallDestructor();
486 ///////////////////////////////////////////////////////////////////////////
488 class LoadSynthDefDirCmd : public SC_SequencedCommand
490 public:
491 LoadSynthDefDirCmd(World *inWorld, ReplyAddress *inReplyAddress);
492 virtual ~LoadSynthDefDirCmd();
494 virtual int Init(char *inData, int inSize);
496 virtual bool Stage2(); // non real time
497 virtual bool Stage3(); // real time
498 virtual void Stage4(); // non real time
500 protected:
501 char *mFilename;
502 GraphDef *mDefs;
504 virtual void CallDestructor();
507 ///////////////////////////////////////////////////////////////////////////
509 class SendReplyCmd : public SC_SequencedCommand
511 public:
512 SendReplyCmd(World *inWorld, ReplyAddress *inReplyAddress);
514 virtual int Init(char *inData, int inSize);
516 virtual bool Stage2(); // non real time
518 protected:
520 virtual void CallDestructor();
523 ///////////////////////////////////////////////////////////////////////////
526 typedef bool (*AsyncStageFn)(World *inWorld, void* cmdData);
527 typedef void (*AsyncFreeFn)(World *inWorld, void* cmdData);
529 class AsyncPlugInCmd : public SC_SequencedCommand
531 public:
532 AsyncPlugInCmd(World *inWorld, ReplyAddress *inReplyAddress,
533 const char* cmdName,
534 void *cmdData,
535 AsyncStageFn stage2, // stage2 is non real time
536 AsyncStageFn stage3, // stage3 is real time - completion msg performed if stage3 returns true
537 AsyncStageFn stage4, // stage4 is non real time - sends done if stage4 returns true
538 AsyncFreeFn cleanup,
539 int completionMsgSize,
540 void* completionMsgData);
542 virtual ~AsyncPlugInCmd();
544 virtual bool Stage2(); // non real time
545 virtual bool Stage3(); // real time
546 virtual void Stage4(); // non real time
548 protected:
549 const char *mCmdName;
550 void *mCmdData;
551 AsyncStageFn mStage2, mStage3, mStage4;
552 AsyncFreeFn mCleanup;
554 virtual void CallDestructor();
557 ///////////////////////////////////////////////////////////////////////////
559 #endif