Debian Codegraph @

src/pyqcsobject.cpp

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 #include "pyqcsobject.h"
24 #include "qutecsound.h"
25 #include "qutesheet.h"
26 #include "opentryparser.h"
27 #include "csoundengine.h"
28
29 #include <QMessageBox>
30 #include <QDir>
31 #include "csound.h"
32
33 PyQcsObject::PyQcsObject():QObject(NULL)
34 {
35 m_qcs = 0;
36 m_tablePtr = (MYFLT **) malloc(sizeof(MYFLT *));
37 }
38
39 PyQcsObject::~PyQcsObject()
40 {
41 free(m_tablePtr);
42 }
43
44 void PyQcsObject::setCsoundQt(CsoundQt *qcs)
45 {
46 m_qcs = qcs;
47 }
48
49 QString PyQcsObject::getVersion()
50 {
51 return QString(PYQCSVERSION);
52 }
53
54 void PyQcsObject::refresh()
55 {
56 qApp->processEvents();
57 }
58
59 void PyQcsObject::play(int index, bool realtime)
60 {
61 qDebug() << "PyQcsObject::play " << index << realtime;
62 // Notice arguments are inverted...
63 m_qcs->play(realtime, index);
64 }
65
66 void PyQcsObject::pause(int index)
67 {
68 m_qcs->pause(index);
69 }
70
71 void PyQcsObject::stop(int index)
72 {
73 m_qcs->stop(index);
74 }
75
76 void PyQcsObject::stopAll()
77 {
78 m_qcs->stopAll();
79 }
80
81 void PyQcsObject::record(int index)
82 {
83 m_qcs->record(true, index); // recording must be stoppend via q.stop()
84 }
85
86 bool PyQcsObject::opcodeExists(QString opcodeName)
87 {
88 return m_qcs->m_opcodeTree->isOpcode(opcodeName);
89 }
90
91 void PyQcsObject::setDocument(int index)
92 {
93 QString name = m_qcs->setDocument(index);
94 PythonQtObjectPtr mainContext = PythonQt::self()->getMainModule();
95 QString path = name.left(name.lastIndexOf("/"));
96 mainContext.call("os.chdir", QVariantList() << path );
97 mainContext.evalScript("print('cd \"" + path + "\"')");
98 }
99
100 int PyQcsObject::loadDocument(QString name, bool runNow)
101 {
102 QDir d(name);
103 d.makeAbsolute();
104 qDebug() << d.absolutePath();
105 if (!QFile::exists(d.absolutePath())) {
106 PythonQtObjectPtr mainContext = PythonQt::self()->getMainModule();
107 mainContext.evalScript("print('File not found.')");
108 return -1;
109 } else {
110 return m_qcs->loadFile(d.absolutePath(), runNow);
111 }
112 }
113
114 void PyQcsObject::insertText(QString text, int index, int section)
115 {
116 return m_qcs->insertText(text, index,section);
117 }
118
119 void PyQcsObject::setCsd(QString text, int index)
120 {
121 return m_qcs->setCsd(text, index);
122 }
123
124 void PyQcsObject::setFullText(QString text, int index)
125 {
126 return m_qcs->setFullText(text, index);
127 }
128
129 void PyQcsObject::setOrc(QString text, int index)
130 {
131 return m_qcs->setOrc(text, index);
132 }
133
134 void PyQcsObject::setSco(QString text, int index)
135 {
136 return m_qcs->setSco(text, index);
137 }
138
139 void PyQcsObject::setWidgetsText(QString text, int index)
140 {
141 return m_qcs->setWidgetsText(text, index);
142 }
143
144 void PyQcsObject::setPresetsText(QString text, int index)
145 {
146 return m_qcs->setPresetsText(text, index);
147 }
148
149 void PyQcsObject::setOptionsText(QString text, int index)
150 {
151 return m_qcs->setOptionsText(text, index);
152 }
153
154 int PyQcsObject::getDocument(QString name)
155 {
156 return m_qcs->getDocument(name);
157 }
158
159 int PyQcsObject::newDocument(QString name)
160 {
161 PythonQtObjectPtr mainContext = PythonQt::self()->getMainModule();
162
163 if (name.isEmpty()) {
164 mainContext.evalScript("print('Please specify a filename')");
165 return -1;
166 }
167 QDir d(name);
168 qDebug() << d;
169 if (QFile::exists(d.absolutePath())) {
170 mainContext.evalScript("print('File already exists. Use loadDocument()')");
171 return -1;
172 }
173 m_qcs->newFile();
174 if (!m_qcs->saveFile(d.absolutePath())) {
175 mainContext.evalScript("print('Error saving file.')");
176 }
177 return m_qcs->getDocument(name);
178 }
179
180 QString PyQcsObject::getSelectedText(int index, int section)
181 {
182 return m_qcs->getSelectedText(index,section);
183 }
184
185 QString PyQcsObject::getCsd(int index)
186 {
187 return m_qcs->getCsd(index);
188 }
189
190 QString PyQcsObject::getFullText(int index)
191 {
192 return m_qcs->getFullText(index);
193 }
194
195 QString PyQcsObject::getOrc(int index)
196 {
197 return m_qcs->getOrc(index);
198 }
199
200 QString PyQcsObject::getSco(int index)
201 {
202 return m_qcs->getSco(index);
203 }
204
205 QString PyQcsObject::getWidgetsText(int index)
206 {
207 return m_qcs->getWidgetsText(index);
208 }
209
210 QString PyQcsObject::getSelectedWidgetsText(int index)
211 {
212 return m_qcs->getSelectedWidgetsText(index);
213 }
214
215 QString PyQcsObject::getPresetsText(int index)
216 {
217 return m_qcs->getPresetsText(index);
218 }
219
220 QString PyQcsObject::getOptionsText(int index)
221 {
222 return m_qcs->getOptionsText(index);
223 }
224
225 QString PyQcsObject::getFileName(int index)
226 {
227 return m_qcs->getFileName(index);
228 }
229
230 QString PyQcsObject::getFilePath(int index)
231 {
232 return m_qcs->getFilePath(index);
233 }
234
235 void PyQcsObject::setChannelValue(QString channel, double value, int index)
236 {
237 return m_qcs->setChannelValue(channel, value, index);
238 }
239
240
241 double PyQcsObject::getChannelValue(QString channel, int index)
242 {
243 return m_qcs->getChannelValue(channel, index);
244 }
245
246 void PyQcsObject::setChannelString(QString channel, QString value, int index)
247 {
248 return m_qcs->setChannelString(channel, value, index);
249 }
250
251 QString PyQcsObject::getChannelString(QString channel, int index)
252 {
253 return m_qcs->getChannelString(channel, index);
254 }
255
256 void PyQcsObject::setCsChannel(QString channel, double value, int index)
257 {
258 CsoundEngine *e = m_qcs->getEngine(index);
259 MYFLT *p;
260 if (e != NULL) {
261 CSOUND *cs = e->getCsound();
262
263 if (cs) {
264 controlChannelHints_t hints; // this does not work with csound5
265 int ret = csoundGetControlChannelHints(cs, channel.toLocal8Bit(), &hints);
266 if (ret == 0) {
267 csoundSetControlChannel(cs, channel.toLocal8Bit(), (MYFLT) value);
268 return;
269 }
270 }
271 }
272
273 QString message="Channel '" + channel + "' does not exist or is not exposed with chn_k.";
274 PythonQtObjectPtr mainContext = PythonQt::self()->getMainModule();
275 mainContext.evalScript("print(\'"+message+"\')");
276 }
277
278 void PyQcsObject::setCsChannel(QString channel, QString stringValue, int index)
279 {
280 CsoundEngine *e = m_qcs->getEngine(index);
281 MYFLT *p;
282 if (e != NULL) {
283 CSOUND *cs = e->getCsound();
284 if (cs != NULL && !(csoundGetChannelPtr(cs, &p, channel.toLocal8Bit().constData(), CSOUND_STRING_CHANNEL | CSOUND_INPUT_CHANNEL))) {
285 // FIXME not thread safe and not checking string bounds.
286 strcpy((char*) p,stringValue.toLocal8Bit().constData() );
287 return;
288 }
289 }
290 QString message="Could not set string into channel "+ channel;
291 PythonQtObjectPtr mainContext = PythonQt::self()->getMainModule();
292 mainContext.evalScript("print(\'"+message+"\')");
293 }
294
295
296 double PyQcsObject::getCsChannel(QString channel, int index)
297 {
298 CsoundEngine *e = m_qcs->getEngine(index);
299 MYFLT *value = new MYFLT;
300 //*value = 0;
301 if (e != NULL) {
302 CSOUND *cs = e->getCsound();
303 if (cs != NULL ) {
304 if ( !( csoundGetChannelPtr(cs,&value,channel.toLocal8Bit(),
305 CSOUND_CONTROL_CHANNEL | CSOUND_OUTPUT_CHANNEL)))
306 return (double) *value;
307 }
308 }
309
310 QString message="Could not read from channel "+channel;
311 PythonQtObjectPtr mainContext = PythonQt::self()->getMainModule();
312 mainContext.evalScript("print(\'"+message+"\')");
313 return 0;//m_qcs->getCsChannel(channel, index);
314 }
315
316
317
318 QString PyQcsObject::getCsStringChannel(QString channel, int index)
319 {
320 CsoundEngine *e = m_qcs->getEngine(index);
321
322 if (e != NULL) {
323 CSOUND *cs = e->getCsound();
324
325 if (cs != NULL) {
326 int maxlen = csoundGetChannelDatasize(cs, channel.toLocal8Bit());
327 char *value = new char[maxlen];
328 if ( !( csoundGetChannelPtr(cs,(MYFLT **) &value,channel.toLocal8Bit(),
329 CSOUND_STRING_CHANNEL | CSOUND_OUTPUT_CHANNEL))) {
330 return QString(value);
331 }
332 }
333 }
334 QString message="Could not read from channel "+channel;
335 PythonQtObjectPtr mainContext = PythonQt::self()->getMainModule();
336 mainContext.evalScript("print(\'"+message+"\')");
337 return QString();//m_qcs->getCsChannel(channel, index);
338
339 }
340
341
342 void PyQcsObject::setWidgetProperty(QString widgetid, QString property, QVariant value, int index)
343 {
344 return m_qcs->setWidgetProperty(widgetid, property, value, index);
345 }
346
347 QVariant PyQcsObject::getWidgetProperty(QString widgetid, QString property, int index)
348 {
349 QStringList properties = listWidgetProperties(widgetid,index);
350 if ( properties.contains(property) ) {
351 return m_qcs->getWidgetProperty(widgetid, property, index);
352
353 } else {
354
355 QString message="Widget "+widgetid+" does not have property "+property+" available properties are: "+properties.join(", ")+".";
356 PythonQtObjectPtr mainContext = PythonQt::self()->getMainModule();
357 mainContext.evalScript("print(\'"+message+"\')");
358 }
359 return (int) -1;
360 }
361
362 QStringList PyQcsObject::getWidgetUuids(int index)
363 {
364 return m_qcs->getWidgetUuids(index);
365 }
366
367 QStringList PyQcsObject::listWidgetProperties(QString widgetid, int index)
368 {
369
370 return m_qcs->listWidgetProperties(widgetid,index);
371 }
372
373
374 QString PyQcsObject::createNewLabel(int x, int y, QString channel, int index)
375 {
376 // FIXME all this interaction with widgets must be correctly placed on the widgets undo/redo stack. It's currently not!
377 return m_qcs->createNewLabel(x,y, channel, index);
378 }
379
380 QString PyQcsObject::createNewDisplay(int x, int y, QString channel, int index)
381 {
382 return m_qcs->createNewDisplay(x,y, channel, index);
383 }
384
385 QString PyQcsObject::createNewScrollNumber(int x, int y, QString channel, int index)
386 {
387 return m_qcs->createNewScrollNumber(x,y, channel, index);
388 }
389
390 QString PyQcsObject::createNewLineEdit(int x, int y, QString channel, int index)
391 {
392 return m_qcs->createNewLineEdit(x,y, channel, index);
393 }
394
395 QString PyQcsObject::createNewSpinBox(int x, int y, QString channel, int index)
396 {
397 return m_qcs->createNewSpinBox(x,y, channel, index);
398 }
399
400 QString PyQcsObject::createNewSlider(QString channel, int index)
401 {
402 return m_qcs->createNewSlider(0,0, channel, index);
403 }
404
405 QString PyQcsObject::createNewSlider(int x, int y, QString channel, int index)
406 {
407 return m_qcs->createNewSlider(x,y, channel, index);
408 }
409
410 QString PyQcsObject::createNewButton(int x, int y, QString channel, int index)
411 {
412 return m_qcs->createNewButton(x,y, channel, index);
413 }
414
415 QString PyQcsObject::createNewKnob(int x, int y, QString channel, int index)
416 {
417 return m_qcs->createNewKnob(x,y, channel, index);
418 }
419
420 QString PyQcsObject::createNewCheckBox(int x, int y, QString channel, int index)
421 {
422 return m_qcs->createNewCheckBox(x,y, channel, index);
423 }
424
425 QString PyQcsObject::createNewMenu(int x, int y, QString channel, int index)
426 {
427 return m_qcs->createNewMenu(x,y, channel, index);
428 }
429
430 QString PyQcsObject::createNewMeter(int x, int y, QString channel, int index)
431 {
432 return m_qcs->createNewMeter(x,y, channel, index);
433 }
434
435 QString PyQcsObject::createNewConsole(int x, int y, QString channel, int index)
436 {
437 return m_qcs->createNewConsole(x,y, channel, index);
438 }
439
440 QString PyQcsObject::createNewGraph(int x, int y, QString channel, int index)
441 {
442 return m_qcs->createNewGraph(x,y, channel, index);
443 }
444
445 QString PyQcsObject::createNewScope(int x, int y, QString channel, int index)
446 {
447 return m_qcs->createNewScope(x,y, channel, index);
448 }
449
450 bool PyQcsObject::destroyWidget(QString widgetid)
451 {
452 return m_qcs->destroyWidget(widgetid);
453 }
454
455 QuteSheet* PyQcsObject::getSheet(int index, int sheetIndex)
456 {
457 return new QuteSheet(this, m_qcs->getSheet(index,sheetIndex));
458 }
459
460 QuteSheet* PyQcsObject::getSheet(int index, QString sheetName)
461 {
462 return new QuteSheet(this, m_qcs->getSheet(index,sheetName));
463 }
464
465 void PyQcsObject::schedule(QVariant time, QVariant event)
466 {
467 qDebug() << "PyQcsObject::schedule" << event;
468 if (time.canConvert<double>()) { // a single event
469 QString eventLine = "i ";
470 if (event.canConvert<QVariantList>()) {
471 QVariantList fields = event.value<QVariantList>();
472 for (int f = 0; f < fields.size(); f++) {
473 if (strcmp(fields[f].typeName(), "double") || strcmp(fields[f].typeName(),"int")) {
474 eventLine.append(fields[f].toString() + " ");
475 }
476 else {
477 eventLine.append("\"" + fields[f].toString() + "\"");
478 }
479 }
480 }
481 qDebug() << "PyQcsObject::schedule: " << eventLine;
482 }
483 else if (time.canConvert<QVariantList>()) { // list of events
484
485 }
486 }
487
488 void PyQcsObject::sendEvent(QString events)
489 {
490 // qDebug() << "PyQcsObject::sendEvent" << events;
491 m_qcs->sendEvent(events);
492 // if (events.type() == QVariant::String ) { // a single event in a string
493 // m_qcs->sendEvent(events.toString());
494 // qDebug() << "PyQcsObject::sendEvent sent: " << events.toString();
495 // }
496 // else if (time.canConvert<QVariantList>()) { // list of events
497 //
498 // }
499 }
500
501 void PyQcsObject::sendEvent(int index, QString events)
502 {
503 m_qcs->sendEvent(index, events);
504 }
505
506 CSOUND* PyQcsObject::getCurrentCsound()
507 {
508 CSOUND *cs = NULL;
509 CsoundEngine *e = m_qcs->getEngine();
510 if (e != NULL) {
511 cs = e->getCsound();
512 }
513 return cs;
514 }
515
516
517 double PyQcsObject::getSampleRate(int index)
518 {
519 double sr = -1.0;
520 CsoundEngine *e = m_qcs->getEngine(index);
521 if (e != NULL) {
522 CSOUND *cs = e->getCsound();
523 if (cs != NULL) {
524 sr = (double) csoundGetSr (cs);
525 }
526 }
527 return sr;
528 }
529
530 int PyQcsObject::getKsmps(int index)
531 {
532 int ksmps = -1;
533 CsoundEngine *e = m_qcs->getEngine(index);
534 if (e != NULL) {
535 CSOUND *cs = e->getCsound();
536 if (cs != NULL) {
537 ksmps = csoundGetKsmps (cs);
538 }
539 }
540 return ksmps;
541 }
542
543 int PyQcsObject::getNumChannels(int index)
544 {
545 int nchnls = -1;
546 CsoundEngine *e = m_qcs->getEngine(index);
547 if (e != NULL) {
548 CSOUND *cs = e->getCsound();
549 if (cs != NULL) {
550 nchnls = csoundGetNchnls (cs);
551 }
552 }
553 return nchnls;
554 }
555
556 MYFLT *PyQcsObject::getTableArray(int ftable, int index)
557 {
558 CsoundEngine *e = m_qcs->getEngine(index);
559 if (e != NULL) {
560 CSOUND *cs = e->getCsound();
561 int ret = csoundGetTable(cs, m_tablePtr, ftable);
562 }
563 return *m_tablePtr;
564 }
565
566
567 void PyQcsObject::evaluateCsound(QString code)
568 {
569 m_qcs->evaluateCsound(code);
570 }
571
572
573 //void PyQcsObject::writeListToTable(int ftable, QVariantList values, int offset, int count)
574 //{
575 //
576 //}
577
578 //QVariantList PyQcsObject::readTableToList(int ftable, int offset, int count)
579 //{
580 // CSOUND *cs = m_qcs->getEngine()->getCsound();
581 // PythonQtObjectPtr mainContext = PythonQt::self()->getMainModule();
582 // QVariantList list;
583 // if (cs == 0) {
584 // mainContext.evalScript("print 'Csound Engine invalid.'");
585 // return list;
586 // }
587 // int tabLen = csoundTableLength(cs, ftable);
588 // if (tabLen < 0) {
589 // mainContext.evalScript("print '''readTableToList(): Invalid table " + QString::number(ftable) + "'''");
590 //// return QVariantList();
591 // }
592 // MYFLT **tablePtr = 0;
593 // int ret = csoundGetTable(cs, tablePtr, ftable);
594 //// qDebug() << "PyQcsObject::readTableToList " << tablePtr << "--" << tabLen;
595 //// while (offset < tabLen && count > 0) {
596 //// list << (double) *(tablePtr[offset]);
597 //// offset++;
598 //// count--;
599 //// }
600 // return list;
601 //}
602
603 //void PyQcsObject::writeArrayToTable(int ftable, QVariantList values, int offset, int count)
604 //{
605 // qDebug() << "Not implemented";
606 //}
607 //
608 //QVariantList PyQcsObject::readArrayToList(int ftable, int offset, int count)
609 //{
610 // qDebug() << "Not implemented";
611 // return QVariantList();
612 //}
613
614 void PyQcsObject::registerProcessCallback(QString func, int skipPeriods, int index)
615 {
616 m_qcs->getEngine(index)->registerProcessCallback(func, skipPeriods);
617 qDebug() << "PyQcsObject::registerProcessCallback" << func << skipPeriods;
618 // PythonQtObjectPtr mainContext = PythonQt::self()->getMainModule();
619 // mainContext.evalScript(func);
620 }
621
622 void PyQcsObject::loadPreset(int presetIndex,int index)
623 {
624 m_qcs->loadPreset(presetIndex, index);
625 }
626
627
628
629