Debian Codegraph @

src/csoundengine.h

raw symbols SCIP
1 /*
2 Copyright (C) 2010 Andres Cabrera
3 [email protected]
4
5 This file is part of CsoundQt.
6
7 CsoundQt is free software; you can redistribute it
8 and/or modify it under the terms of the GNU Lesser General Public
9 License as published by the Free Software Foundation; either
10 version 2.1 of the License, or (at your option) any later version.
11
12 CsoundQt is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU Lesser General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public
18 License along with Csound; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
20 02111-1307 USA
21 */
22
23 #ifndef CSOUNDENGINE_H
24 #define CSOUNDENGINE_H
25
26 #include <QStringList>
27 #include <QTimer>
28 #include <QFuture>
29 #include <QAtomicInt>
30
31 #include <csound.hpp>
32 #include <csPerfThread.hpp>
33 #include <cwindow.h> // Necessary for WINDAT struct
34
35 #ifdef QCS_DEBUGGER
36 #include "csdebug.h"
37 #endif
38
39 #include "types.h"
40 #include "csoundoptions.h"
41 #ifdef QCS_PYTHONQT
42 #include "pythonconsole.h"
43 #endif
44
45
46 class ConsoleWidget;
47 class QuteScope;
48 class QuteGraph;
49 class Curve;
50 class CsoundEngine;
51 class WidgetLayout;
52 class MidiHandler;
53 class QuteWidget;
54
55 // Csound 5.10 needs to be destroyed for opcodes like ficlose to flush the output
56 // This still necessary for 5.12 and Csound6
57 #define QCS_DESTROY_CSOUND
58
59 typedef enum {
60 QCS_NO_FLAGS = 0,
61 QCS_NO_COPY_BUFFER = 1,
62 QCS_NO_PYTHON_CALLBACK = 2,
63 QCS_NO_CONSOLE_MESSAGES = 4,
64 QCS_NO_RT_EVENTS = 8
65 } PerfFlags;
66
67 struct CsoundUserData {
68 int result; //result of csoundCompile()
69 CSOUND *csound; // instance of csound
70 CsoundPerformanceThread *perfThread;
71 CsoundEngine *csEngine; // Pass engine
72 WidgetLayout *wl; // Pass widgets
73 MidiHandler *midiHandler; // For MIDI out
74 QMutex *playMutex; //perfThread access Mutex
75 /* performance */
76 bool runDispatcher;
77 QVector<double> mouseValues;
78 RingBuffer audioOutputBuffer;
79 bool enableWidgets; // Whether widget values are processed in the callback
80
81 /* current configuration */
82 // These should not be changed while Csound is running,
83 PerfFlags flags;
84 // Store some values to avoid function calls
85 MYFLT zerodBFS; //0dBFS value
86 int numChnls;
87 int sampleRate;
88 long outputBufferSize;
89 int msgRefreshTime; // In micro seconds
90
91 // Channels are only queried at the start of run, so only channels defined in instr 0 are available
92 QList<QString> inputChannelNames;
93 QList<QString> outputChannelNames;
94 QList<QString> outputStringChannelNames;
95 QList<QVariant> previousOutputValues;
96 QList<QVariant> previousStringOutputValues;
97 QString lastRecordingOutfile;
98
99 void *midiBuffer; //Csound Circular Buffer
100 void *virtualMidiBuffer; //Csound Circular Buffer
101
102 #ifdef QCS_PYTHONQT
103 PythonConsole *m_pythonConsole;
104 QString m_pythonCallback;
105 int m_pythonCallbackCounter;
106 int m_pythonCallbackSkip;
107 #endif
108 };
109
110
111 class CsoundEngine : public QObject
112 {
113 Q_OBJECT
114 public:
115 CsoundEngine(ConfigLists *configlists);
116 ~CsoundEngine();
117
118 static void outputValueCallback (CSOUND *csound,
119 const char *channelName,
120 void *channelValuePtr,
121 const void *channelType);
122 static void inputValueCallback (CSOUND *csound,
123 const char *channelName,
124 void *channelValuePtr,
125 const void *channelType);
126
127 static int midiInOpenCb(CSOUND *csound, void **ud, const char *devName);
128 static int midiReadCb(CSOUND *csound, void *ud_, unsigned char *buf, int nBytes);
129 static int midiInCloseCb(CSOUND *csound, void *ud);
130 static int midiOutOpenCb(CSOUND *csound, void **ud, const char *devName);
131 static int midiWriteCb(CSOUND *csound, void *ud_, const unsigned char *buf, int nBytes);
132 static int midiOutCloseCb(CSOUND *csound, void *ud);
133 static const char *midiErrorStringCb(int);
134 void queueMidiIn(std::vector<unsigned char> *message);
135 void queueVirtualMidiIn(std::vector<unsigned char> &message);
136 void sendMidiOut(QVector<unsigned char> &message);
137
138 static void makeGraphCallback(CSOUND *csound, WINDAT *windat, const char *name);
139 static void drawGraphCallback(CSOUND *csound, WINDAT *windat);
140 static void killGraphCallback(CSOUND *csound, WINDAT *windat);
141 static int exitGraphCallback(CSOUND *csound);
142 static int keyEventCallback(void *userData,
143 void *p,
144 unsigned int type);
145 static void csThread(void *data); //Thread function (called after each performance pass by the performance thread)
146
147 static void readWidgetValues(CsoundUserData *ud);
148 static void writeWidgetValues(CsoundUserData *ud);
149
150 // void setCsoundOptions(const CsoundOptions &options);
151 // Options unsafe to change while running
152 void setWidgetLayout(WidgetLayout *wl);
153 void setMidiHandler(MidiHandler *mh);
154 // Options safe to change while running
155 void enableWidgets(bool enable);
156
157 void registerConsole(ConsoleWidget *c); // Messages generated by Csound and CsoundQt are passed to the consoles registered here, and nowhere else
158 QList<QPair<int, QString> > getErrorLines();
159 void setConsoleBufferSize(int size);
160 int popKeyPressEvent();
161 int popKeyReleaseEvent();
162
163 void processEventQueue();
164 void passOutValue(QString channelName, double value);
165 void passOutString(QString channelName, QString value);
166 void flushQueues();
167 void queueMessage(QString message);
168
169 bool isRunning();
170 bool isRecording();
171 bool isPaused() {return m_paused; }
172
173 // To pass to parent document for access from python scripting
174 CSOUND * getCsound();
175 CsoundUserData *getUserData();
176 void clearConsoles(void);
177 #ifdef QCS_PYTHONQT
178 void registerProcessCallback(QString func, int skipPeriods);
179 void setPythonConsole(PythonConsole *pc);
180 #endif
181
182 #ifdef QCS_DEBUGGER
183 bool m_debugging;
184
185 static void breakpointCallback(CSOUND *csound, debug_bkpt_info_t *bkpt_info, void *udata);
186 void setDebug();
187 void pauseDebug();
188 void continueDebug();
189 void stopDebug();
190 void addInstrumentBreakpoint(double instr, int skip);
191 void removeInstrumentBreakpoint(double instr);
192 void addBreakpoint(int line, int instr, int skip);
193 void removeBreakpoint(int line, int instr);
194 void setStartingBreakpoints(QVector<QVariantList> bps);
195 QVector<QVariantList> getVaribleList();
196 QVector<QVariantList> getInstrumentList();
197 int getCurrentLine();
198 QVector<QVariantList> m_varList;
199 QVector<QVariantList> m_instrumentList;
200 QAtomicInt m_currentLine;
201 QMutex variableMutex;
202 QMutex instrumentMutex;
203 QVector<QVariantList> m_startBreakpoints;
204 #endif
205
206 public slots:
207 int play(CsoundOptions *options);
208 void stop();
209 void pause();
210 int startRecording(int format, QString filename);
211 void stopRecording();
212 void queueEvent(QString eventLine, int delay = 0);
213 void keyPressForCsound(int key); // For key press events from consoles and widget panel
214 void keyReleaseForCsound(int key);
215
216 void registerScope(QuteScope *scope);
217 // void unregisterScope(QuteScope *scope);
218 void registerGraph(QuteGraph *scope);
219 void requestCsoundUserData(QuteWidget *widget);
220 void setFlags(PerfFlags flags) {ud->flags = flags;}
221
222 void evaluate(QString code);
223
224 public:
225 QVector<ConsoleWidget *> consoles; // Consoles registered for message printing
226 int runCsound();
227 void stopCsound();
228 void cleanupCsound();
229 int checkSyntax();
230
231 private:
232 void setupChannels();
233 QList <int> getAnsiKeySequence(int key);
234
235 QFuture<void> m_msgUpdateThread;
236 static void messageListDispatcher(void *data); // Function run in updater thread
237
238 CsoundUserData *ud;
239
240 CsoundOptions m_options;
241
242 int m_consoleBufferSize;
243 QMutex m_messageMutex; // Protection for message queue
244 QStringList messageQueue; // Messages from Csound execution
245 QMutex keyMutex; // For keys pressed to pass to Csound from console and widget panel
246 QList <int> keyPressBuffer; // protected by keyMutex
247 QList <int> keyReleaseBuffer; // protected by keyMutex
248
249 bool m_recording;
250 bool m_paused;
251 // To prevent from starting a Csound instance while another is starting or closing
252 QMutex m_playMutex;
253 QMutex eventMutex;
254 QMutex csoundMutex;
255 QVector<QString> eventQueue;
256 int m_refreshTime; // time in milliseconds for widget value updates (both input and output)
257 QVector<unsigned long> eventTimeStamps;
258 int eventQueueSize;
259
260 private slots:
261
262 signals:
263 void errorLines(QList<QPair<int, QString> >);
264 void passMessages(QString msg);
265 void stopSignal(); // Sent when performance has stopped internally to inform others.playFromParent()
266 void breakpointReached();
267 };
268
269 #endif // CSOUNDENGINE_H