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"
34 #include "SC_BufGen.h"
35 #include "sc_msg_iter.h"
38 #include <sndfile-win.h>
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); \
52 World_Free(inWorld, space); \
55 if (inWorld->mRealTime) cmd->CallNextStage(); \
56 else cmd->CallEveryStage();
59 class SC_SequencedCommand
62 SC_SequencedCommand(World
*inWorld
, ReplyAddress
*inReplyAddress
);
63 virtual ~SC_SequencedCommand();
67 void CallEveryStage();
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
);
83 ReplyAddress mReplyAddress
;
89 virtual void CallDestructor()=0;
92 ///////////////////////////////////////////////////////////////////////////
94 class SyncCmd
: public SC_SequencedCommand
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
106 virtual void CallDestructor();
110 ///////////////////////////////////////////////////////////////////////////
112 class BufGenCmd
: public SC_SequencedCommand
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
133 virtual void CallDestructor();
137 ///////////////////////////////////////////////////////////////////////////
139 class BufAllocCmd
: public SC_SequencedCommand
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
153 int mNumChannels
, mNumFrames
;
156 virtual void CallDestructor();
160 ///////////////////////////////////////////////////////////////////////////
163 class BufFreeCmd
: public SC_SequencedCommand
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
178 virtual void CallDestructor();
182 ///////////////////////////////////////////////////////////////////////////
185 class BufCloseCmd
: public SC_SequencedCommand
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
199 virtual void CallDestructor();
203 ///////////////////////////////////////////////////////////////////////////
206 class BufZeroCmd
: public SC_SequencedCommand
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
220 virtual void CallDestructor();
223 ///////////////////////////////////////////////////////////////////////////
225 class BufAllocReadCmd
: public SC_SequencedCommand
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
242 int mFileOffset
, mNumFrames
;
244 virtual void CallDestructor();
247 ///////////////////////////////////////////////////////////////////////////
249 class BufReadCmd
: public SC_SequencedCommand
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
264 int mFileOffset
, mNumFrames
, mBufOffset
;
267 virtual void CallDestructor();
270 ///////////////////////////////////////////////////////////////////////////
272 class SC_BufReadCommand
: public SC_SequencedCommand
279 SC_BufReadCommand(World
* inWorld
, ReplyAddress
* inReplyAddress
);
280 virtual ~SC_BufReadCommand();
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
);
289 int mChannels
[kMaxNumChannels
];
292 ///////////////////////////////////////////////////////////////////////////
294 class BufAllocReadChannelCmd
: public SC_BufReadCommand
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
311 int mFileOffset
, mNumFrames
;
312 virtual void CallDestructor();
315 ///////////////////////////////////////////////////////////////////////////
317 class BufReadChannelCmd
: public SC_BufReadCommand
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
332 int mFileOffset
, mNumFrames
, mBufOffset
;
335 virtual void CallDestructor();
338 ///////////////////////////////////////////////////////////////////////////
340 class BufWriteCmd
: public SC_SequencedCommand
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
355 #ifndef NO_LIBSNDFILE
358 int mNumFrames
, mBufOffset
;
361 virtual void CallDestructor();
364 ///////////////////////////////////////////////////////////////////////////
366 class AudioQuitCmd
: public SC_SequencedCommand
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
377 virtual void CallDestructor();
380 ///////////////////////////////////////////////////////////////////////////
382 class AudioStatusCmd
: public SC_SequencedCommand
385 AudioStatusCmd(World
*inWorld
, ReplyAddress
*inReplyAddress
);
387 virtual bool Stage2(); // non real time
391 virtual void CallDestructor();
394 ///////////////////////////////////////////////////////////////////////////
396 class NotifyCmd
: public SC_SequencedCommand
399 NotifyCmd(World
*inWorld
, ReplyAddress
*inReplyAddress
);
401 virtual int Init(char *inData
, int inSize
);
403 virtual bool Stage2(); // non real time
407 virtual void CallDestructor();
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
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
435 char *mCmdName
, *mErrString
;
437 virtual void CallDestructor();
440 ///////////////////////////////////////////////////////////////////////////
442 #include "SC_GraphDef.h"
444 class LoadSynthDefCmd
: public SC_SequencedCommand
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
460 virtual void CallDestructor();
463 ///////////////////////////////////////////////////////////////////////////
465 #include "SC_GraphDef.h"
467 class RecvSynthDefCmd
: public SC_SequencedCommand
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
483 virtual void CallDestructor();
486 ///////////////////////////////////////////////////////////////////////////
488 class LoadSynthDefDirCmd
: public SC_SequencedCommand
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
504 virtual void CallDestructor();
507 ///////////////////////////////////////////////////////////////////////////
509 class SendReplyCmd
: public SC_SequencedCommand
512 SendReplyCmd(World
*inWorld
, ReplyAddress
*inReplyAddress
);
514 virtual int Init(char *inData
, int inSize
);
516 virtual bool Stage2(); // non real time
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
532 AsyncPlugInCmd(World
*inWorld
, ReplyAddress
*inReplyAddress
,
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
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
549 const char *mCmdName
;
551 AsyncStageFn mStage2
, mStage3
, mStage4
;
552 AsyncFreeFn mCleanup
;
554 virtual void CallDestructor();
557 ///////////////////////////////////////////////////////////////////////////