1 /*
2     SuperCollider real time audio synthesis system
3     Copyright (c) 2002 James McCartney. All rights reserved.
4     http://www.audiosynth.com
5 
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.
10 
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.
15 
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
19 */
20 
21 
22 #pragma once
23 
24 #include <ctype.h> // for size_t
25 #include <cstdlib>
26 #include <cstring>
27 
28 #include "SC_Types.h"
29 #include "scsynthsend.h"
30 
31 ////////////////////////////////////////////////////////////////////////
32 
33 // replacement for calloc.
34 // calloc lazily zeroes memory on first touch. This is good for most purposes, but bad for realtime audio.
35 void* zalloc(size_t n, size_t size);
36 void zfree(void* ptr);
37 
38 ////////////////////////////////////////////////////////////////////////
39 
40 void World_Run(struct World* inWorld);
41 void World_Start(World* inWorld);
42 void World_SetSampleRate(struct World* inWorld, double inSampleRate);
43 
44 extern "C" {
45 void* World_Alloc(struct World* inWorld, size_t inByteSize);
46 void* World_Realloc(struct World* inWorld, void* inPtr, size_t inByteSize);
47 void World_Free(struct World* inWorld, void* inPtr);
48 void World_NRTLock(World* world);
49 void World_NRTUnlock(World* world);
50 }
51 
52 size_t World_TotalFree(struct World* inWorld);
53 size_t World_LargestFreeChunk(struct World* inWorld);
54 
55 
56 int32 GetKey(struct Node* inNode);
57 int32 GetHash(struct Node* inNode);
58 bool World_AddNode(struct World* inWorld, struct Node* inNode);
59 bool World_RemoveNode(struct World* inWorld, struct Node* inNode);
60 
61 extern "C" {
62 struct Node* World_GetNode(struct World* inWorld, int32 inID);
63 struct Graph* World_GetGraph(struct World* inWorld, int32 inID);
64 }
65 
66 struct Group* World_GetGroup(struct World* inWorld, int32 inID);
67 
68 int32* GetKey(struct UnitDef* inUnitDef);
69 int32 GetHash(struct UnitDef* inUnitDef);
70 bool AddUnitDef(struct UnitDef* inUnitDef);
71 bool RemoveUnitDef(struct UnitDef* inUnitDef);
72 struct UnitDef* GetUnitDef(int32* inKey);
73 
74 int32* GetKey(struct BufGen* inBufGen);
75 int32 GetHash(struct BufGen* inBufGen);
76 bool AddBufGen(struct BufGen* inBufGen);
77 bool RemoveBufGen(struct BufGen* inBufGen);
78 struct BufGen* GetBufGen(int32* inKey);
79 
80 int32* GetKey(struct PlugInCmd* inPlugInCmd);
81 int32 GetHash(struct PlugInCmd* inPlugInCmd);
82 bool AddPlugInCmd(struct PlugInCmd* inPlugInCmd);
83 bool RemovePlugInCmd(struct PlugInCmd* inPlugInCmd);
84 struct PlugInCmd* GetPlugInCmd(int32* inKey);
85 int PlugIn_DoCmd(struct World* inWorld, int inSize, char* inArgs, struct ReplyAddress* inReply);
86 
87 int32* GetKey(struct GraphDef* inGraphDef);
88 int32 GetHash(struct GraphDef* inGraphDef);
89 void World_AddGraphDef(struct World* inWorld, struct GraphDef* inGraphDef);
90 void World_RemoveGraphDef(struct World* inWorld, struct GraphDef* inGraphDef);
91 struct GraphDef* World_GetGraphDef(struct World* inWorld, int32* inKey);
92 void World_FreeAllGraphDefs(World* inWorld);
93 void GraphDef_Free(GraphDef* inGraphDef);
94 void GraphDef_Define(World* inWorld, GraphDef* inList);
95 void GraphDef_FreeOverwritten(World* inWorld);
96 
97 SCErr bufAlloc(struct SndBuf* buf, int numChannels, int numFrames, double sampleRate);
98 
99 ////////////////////////////////////////////////////////////////////////
100 
101 void Rate_Init(struct Rate* inRate, double inSampleRate, int inBufLength);
102 
103 ////////////////////////////////////////////////////////////////////////
104 
105 #define GRAPHDEF(inGraph) ((GraphDef*)((inGraph)->mNode.mDef))
106 #define GRAPH_PARAM_TABLE(inGraph) (GRAPHDEF(inGraph)->mParamSpecTable)
107 
108 int Graph_New(struct World* inWorld, struct GraphDef* def, int32 inID, struct sc_msg_iter* args,
109               struct Graph** outGraph, bool argtype = true);
110 void Graph_Ctor(struct World* inWorld, struct GraphDef* inGraphDef, struct Graph* graph, struct sc_msg_iter* msg,
111                 bool argtype);
112 void Graph_Dtor(struct Graph* inGraph);
113 int Graph_GetControl(struct Graph* inGraph, uint32 inIndex, float& outValue);
114 int Graph_GetControl(struct Graph* inGraph, int32 inHash, int32* inName, uint32 inIndex, float& outValue);
115 void Graph_SetControl(struct Graph* inGraph, uint32 inIndex, float inValue);
116 void Graph_SetControl(struct Graph* inGraph, int32 inHash, int32* inName, uint32 inIndex, float inValue);
117 void Graph_MapControl(Graph* inGraph, uint32 inIndex, uint32 inBus);
118 void Graph_MapControl(Graph* inGraph, int32 inHash, int32* inName, uint32 inIndex, uint32 inBus);
119 void Graph_MapAudioControl(Graph* inGraph, uint32 inIndex, uint32 inBus);
120 void Graph_MapAudioControl(Graph* inGraph, int32 inHash, int32* inName, uint32 inIndex, uint32 inBus);
121 void Graph_Trace(Graph* inGraph);
122 
123 ////////////////////////////////////////////////////////////////////////
124 
125 int Node_New(struct World* inWorld, struct NodeDef* def, int32 inID, struct Node** outNode);
126 void Node_Dtor(struct Node* inNode);
127 void Node_Remove(struct Node* s);
128 void Node_RemoveID(Node* inNode);
129 void Node_Delete(struct Node* inNode);
130 void Node_AddAfter(struct Node* s, struct Node* afterThisOne);
131 void Node_AddBefore(struct Node* s, struct Node* beforeThisOne);
132 void Node_Replace(struct Node* s, struct Node* replaceThisOne);
133 void Node_SetControl(Node* inNode, int inIndex, float inValue);
134 void Node_SetControl(Node* inNode, int32 inHash, int32* inName, int inIndex, float inValue);
135 void Node_MapControl(Node* inNode, int inIndex, int inBus);
136 void Node_MapControl(Node* inNode, int32 inHash, int32* inName, int inIndex, int inBus);
137 void Node_MapAudioControl(Node* inNode, int inIndex, int inBus);
138 void Node_MapAudioControl(Node* inNode, int32 inHash, int32* inName, int inIndex, int inBus);
139 void Node_StateMsg(Node* inNode, int inState);
140 void Node_Trace(Node* inNode);
141 void Node_SendReply(Node* inNode, int replyID, const char* cmdName, int numArgs, const float* values);
142 void Node_SendReply(Node* inNode, int replyID, const char* cmdName, float value);
143 
144 extern "C" {
145 void Node_SetRun(Node* inNode, int inRun);
146 void Node_SendTrigger(Node* inNode, int triggerID, float value);
147 void Node_End(struct Node* inNode);
148 void Node_NullCalc(struct Node* inNode);
149 void Unit_DoneAction(int doneAction, struct Unit* unit);
150 }
151 
152 ////////////////////////////////////////////////////////////////////////
153 
154 extern "C" {
155 void Group_Calc(Group* inGroup);
156 void Graph_Calc(struct Graph* inGraph);
157 }
158 
159 int Group_New(World* inWorld, int32 inID, Group** outGroup);
160 void Group_Dtor(Group* inGroup);
161 void Group_DeleteAll(Group* inGroup);
162 void Group_DeepFreeGraphs(Group* inGroup);
163 void Group_AddHead(Group* s, Node* child);
164 void Group_AddTail(Group* s, Node* child);
165 void Group_Insert(Group* s, Node* child, int inIndex);
166 void Group_SetControl(struct Group* inGroup, uint32 inIndex, float inValue);
167 void Group_SetControl(struct Group* inGroup, int32 inHash, int32* inName, uint32 inIndex, float inValue);
168 void Group_MapControl(Group* inGroup, uint32 inIndex, uint32 inBus);
169 void Group_MapControl(Group* inGroup, int32 inHash, int32* inName, uint32 inIndex, uint32 inBus);
170 void Group_MapAudioControl(Group* inGroup, uint32 inIndex, uint32 inBus);
171 void Group_MapAudioControl(Group* inGroup, int32 inHash, int32* inName, uint32 inIndex, uint32 inBus);
172 void Group_Trace(Group* inGroup);
173 void Group_DumpTree(Group* inGroup);
174 void Group_DumpTreeAndControls(Group* inGroup);
175 void Group_CountNodeTags(Group* inGroup, int* count);
176 void Group_CountNodeAndControlTags(Group* inGroup, int* count, int* controlAndDefCount);
177 void Group_QueryTree(Group* inGroup, big_scpacket* packet);
178 void Group_QueryTreeAndControls(Group* inGroup, big_scpacket* packet);
179 
180 ////////////////////////////////////////////////////////////////////////
181 
182 struct Unit* Unit_New(struct World* inWorld, struct UnitSpec* inUnitSpec, char*& memory);
183 void Unit_EndCalc(struct Unit* inUnit, int inNumSamples);
184 void Unit_End(struct Unit* inUnit);
185 
186 void Unit_Dtor(struct Unit* inUnit);
187 
188 extern "C" {
189 void Unit_ZeroOutputs(struct Unit* inUnit, int inNumSamples);
190 }
191 
192 ////////////////////////////////////////////////////////////////////////
193 
194 void SendDone(struct ReplyAddress* inReply, const char* inCommandName);
195 void SendDoneWithIntValue(struct ReplyAddress* inReply, const char* inCommandName, int value);
196 void SendDoneWithVarArgs(struct ReplyAddress* inReply, const char* inCommandName, const char* oscFormat, ...);
197 void SendFailure(struct ReplyAddress* inReply, const char* inCommandName, const char* errString);
198 void SendFailureWithIntValue(struct ReplyAddress* inReply, const char* inCommandName, const char* errString,
199                              uint32 index);
200 void SendFailureWithVarArgs(struct ReplyAddress* inReply, const char* inCommandName, const char* errString,
201                             const char* oscFormat, ...);
202 void ReportLateness(struct ReplyAddress* inReply, float32 seconds);
203 int32 Hash(struct ReplyAddress* inReplyAddress);
204 
205 ////////////////////////////////////////////////////////////////////////
206 
207 extern "C" {
208 int32 server_timeseed();
209 }
210 
211 ////////////////////////////////////////////////////////////////////////
212 
213 typedef bool (*AsyncStageFn)(World* inWorld, void* cmdData);
214 typedef void (*AsyncFreeFn)(World* inWorld, void* cmdData);
215 
216 int PerformAsynchronousCommand(
217     World* inWorld, void* replyAddr, const char* cmdName, void* cmdData,
218     AsyncStageFn stage2, // stage2 is non real time
219     AsyncStageFn stage3, // stage3 is real time - completion msg performed if stage3 returns true
220     AsyncStageFn stage4, // stage4 is non real time - sends done if stage4 returns true
221     AsyncFreeFn cleanup, int completionMsgSize, void* completionMsgData);
222 
223 ////////////////////////////////////////////////////////////////////////
224