| 1 | /* |
| 2 | Copyright (C) 2008, 2009 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 | |
| 24 | #include <QtConcurrent> |
| 25 | #include <QThread> |
| 26 | |
| 27 | #ifdef Q_OS_WIN |
| 28 | #include <ole2.h> // for OleInitialize() FLTK bug workaround |
| 29 | #endif |
| 30 | |
| 31 | #include "csound_standard_types.h" |
| 32 | |
| 33 | #include "csoundengine.h" |
| 34 | #include "widgetlayout.h" |
| 35 | #include "console.h" |
| 36 | #include "qutescope.h" // Needed for passing the ud to the scope for display data |
| 37 | #include "qutegraph.h" // Needed for passing the ud to the graph for display data |
| 38 | #include "midihandler.h" |
| 39 | |
| 40 | // #define QDEBUG qDebug() << __FUNCTION__ << ":" |
| 41 | |
| 42 | CsoundEngine::CsoundEngine(ConfigLists *configlists) : |
| 43 | m_options(configlists) |
| 44 | { |
| 45 | QMutexLocker locker(&m_playMutex); |
| 46 | ud = new CsoundUserData(); |
| 47 | ud->csEngine = this; |
| 48 | ud->csound = nullptr; |
| 49 | ud->perfThread = nullptr; |
| 50 | ud->flags = QCS_NO_FLAGS; |
| 51 | ud->mouseValues.resize(6); // For _MouseX _MouseY _MouseRelX _MouseRelY _MouseBut1 and _MouseBut2 channels |
| 52 | ud->wl = nullptr; |
| 53 | ud->midiBuffer = nullptr; |
| 54 | ud->virtualMidiBuffer = nullptr; |
| 55 | ud->playMutex = &m_playMutex; |
| 56 | #ifdef QCS_PYTHONQT |
| 57 | ud->m_pythonCallback = ""; |
| 58 | #endif |
| 59 | m_consoleBufferSize = 0; |
| 60 | m_recording = false; |
| 61 | #ifndef QCS_DESTROY_CSOUND |
| 62 | ud->csound=csoundCreate( (void *) ud); |
| 63 | ud->midiBuffer = csoundCreateCircularBuffer(ud->csound, 1024, sizeof(unsigned char)); |
| 64 | Q_ASSERT(ud->midiBuffer); |
| 65 | #endif |
| 66 | eventQueue.resize(QCS_MAX_EVENTS); |
| 67 | eventTimeStamps.resize(QCS_MAX_EVENTS); |
| 68 | eventQueueSize = 0; |
| 69 | m_refreshTime = QCS_QUEUETIMER_DEFAULT_TIME; // TODO Eventually allow this to be changed |
| 70 | ud->msgRefreshTime = m_refreshTime*1000; |
| 71 | ud->runDispatcher = true; |
| 72 | m_msgUpdateThread = QtConcurrent::run(messageListDispatcher, (void *) ud); |
| 73 | #ifdef QCS_DEBUGGER |
| 74 | m_debugging = false; |
| 75 | #endif |
| 76 | } |
| 77 | |
| 78 | CsoundEngine::~CsoundEngine() |
| 79 | { |
| 80 | disconnect(SIGNAL(passMessages(QString)),0,0); |
| 81 | disconnect(this, 0,0,0); |
| 82 | ud->runDispatcher = false; |
| 83 | m_msgUpdateThread.waitForFinished(); // Join the message thread |
| 84 | stop(); |
| 85 | #ifndef QCS_DESTROY_CSOUND |
| 86 | csoundDestroyCircularBuffer(ud->csound, ud->midiBuffer); |
| 87 | csoundDestroy(ud->csound); |
| 88 | #endif |
| 89 | delete ud; |
| 90 | } |
| 91 | |
| 92 | |
| 93 | void CsoundEngine::outputValueCallback (CSOUND *csound, |
| 94 | const char *channelName, |
| 95 | void *channelValuePtr, |
| 96 | const void *channelType) |
| 97 | { |
| 98 | // Called by the csound running engine when 'outvalue' opcode is used |
| 99 | // To pass data from Csound to CsoundQt |
| 100 | CsoundUserData *ud = (CsoundUserData *) csoundGetHostData(csound); |
| 101 | if (channelType == &CS_VAR_TYPE_S) { |
| 102 | ud->csEngine->passOutString(channelName, (const char *) channelValuePtr); |
| 103 | } |
| 104 | else if (channelType == &CS_VAR_TYPE_K){ |
| 105 | ud->csEngine->passOutValue(channelName, *((MYFLT *)channelValuePtr)); |
| 106 | } else { |
| 107 | QDEBUG << "Unsupported type"; |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | void CsoundEngine::inputValueCallback (CSOUND *csound, |
| 112 | const char *channelName, |
| 113 | void *channelValuePtr, |
| 114 | const void *channelType) |
| 115 | { |
| 116 | // Called by the csound running engine when 'invalue' opcode is used |
| 117 | // To pass data from CsoundQt to Csound |
| 118 | CsoundUserData *ud = (CsoundUserData *) csoundGetHostData(csound); |
| 119 | if (channelType == &CS_VAR_TYPE_S) { // channel is a string channel |
| 120 | char *string = (char *) channelValuePtr; |
| 121 | QString newValue = ud->wl->getStringForChannel(channelName); |
| 122 | int maxlen = csoundGetChannelDatasize(csound, channelName); |
| 123 | if (newValue.size() > 0) { |
| 124 | strncpy(string, newValue.toLocal8Bit(), maxlen - 1); |
| 125 | string[newValue.size()] = '\0'; |
| 126 | } else { |
| 127 | string[0] = '\0'; |
| 128 | } |
| 129 | } |
| 130 | else if (channelType == &CS_VAR_TYPE_K) { // Not a string channel |
| 131 | //FIXME check if mouse tracking is active, and move this from here |
| 132 | MYFLT *value = (MYFLT *) channelValuePtr; |
| 133 | if(!strcmp(channelName, "_Mouse")) { |
| 134 | const char *suffix = &channelName[6]; |
| 135 | if (!strcmp(suffix, "X")) { |
| 136 | *value = (MYFLT) ud->mouseValues[0]; |
| 137 | } |
| 138 | else if (!strcmp(suffix, "Y")) { |
| 139 | *value = (MYFLT) ud->mouseValues[1]; |
| 140 | } |
| 141 | else if(!strcmp(suffix, "RelX")) { |
| 142 | *value = (MYFLT) ud->mouseValues[2]; |
| 143 | } |
| 144 | else if(!strcmp(suffix, "RelY")) { |
| 145 | *value = (MYFLT) ud->mouseValues[3]; |
| 146 | } |
| 147 | else if(!strcmp(suffix, "But1")) { |
| 148 | *value = (MYFLT) ud->mouseValues[4]; |
| 149 | } |
| 150 | else if(!strcmp(suffix, "But2")) { |
| 151 | *value = (MYFLT) ud->mouseValues[5]; |
| 152 | } |
| 153 | } |
| 154 | else { |
| 155 | // QString name(channelName); |
| 156 | *value = (MYFLT) ud->wl->getValueForChannel(channelName); |
| 157 | } |
| 158 | } else { |
| 159 | QDEBUG << "Unsupported type"; |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | int CsoundEngine::midiInOpenCb(CSOUND *csound, void **ud, const char *devName) |
| 164 | { |
| 165 | CsoundUserData *userData = (CsoundUserData *) csoundGetHostData(csound); |
| 166 | Q_UNUSED(devName); |
| 167 | Q_ASSERT(userData->midiBuffer); |
| 168 | if (userData) { |
| 169 | *ud = userData; |
| 170 | } else { |
| 171 | qDebug() << "Error! userData not set for midiInOpenCb"; |
| 172 | return CSOUND_ERROR; |
| 173 | } |
| 174 | return CSOUND_SUCCESS; |
| 175 | } |
| 176 | |
| 177 | int CsoundEngine::midiReadCb(CSOUND *csound, void *ud_, unsigned char *buf, int nBytes) |
| 178 | { |
| 179 | CsoundUserData *ud = (CsoundUserData *) ud_; |
| 180 | Q_UNUSED(csound); |
| 181 | int count, countVirtual; |
| 182 | count = csoundReadCircularBuffer(ud->csound, ud->midiBuffer, buf, nBytes); |
| 183 | countVirtual = csoundReadCircularBuffer(ud->csound, ud->virtualMidiBuffer, buf + count, nBytes - count); |
| 184 | return count + countVirtual; |
| 185 | } |
| 186 | |
| 187 | int CsoundEngine::midiInCloseCb(CSOUND *csound, void *ud) |
| 188 | { |
| 189 | Q_UNUSED(csound); |
| 190 | Q_UNUSED(ud); |
| 191 | return CSOUND_SUCCESS; |
| 192 | } |
| 193 | |
| 194 | int CsoundEngine::midiOutOpenCb(CSOUND *csound, void **ud, const char *devName) |
| 195 | { |
| 196 | CsoundUserData *userData = (CsoundUserData *) csoundGetHostData(csound); |
| 197 | Q_UNUSED(devName); |
| 198 | Q_ASSERT(userData->midiBuffer); |
| 199 | if (userData) { |
| 200 | *ud = userData; |
| 201 | } else { |
| 202 | QDEBUG << "Error! userData not set for midiInOpenCb"; |
| 203 | return CSOUND_ERROR; |
| 204 | } |
| 205 | return CSOUND_SUCCESS; |
| 206 | |
| 207 | } |
| 208 | |
| 209 | int CsoundEngine::midiWriteCb(CSOUND *csound, void *ud_, const unsigned char *buf, int nBytes) |
| 210 | { |
| 211 | CsoundUserData *userData = (CsoundUserData *) ud_; |
| 212 | std::vector<unsigned char> message; |
| 213 | Q_UNUSED(csound); |
| 214 | message.push_back(*buf++); |
| 215 | nBytes--; |
| 216 | while (nBytes--) { |
| 217 | if (*buf & 0x80) { |
| 218 | userData->midiHandler->sendMidiOut(&message); |
| 219 | message.clear(); |
| 220 | } |
| 221 | message.push_back(*buf++); |
| 222 | } |
| 223 | if (message.size() > 0) { |
| 224 | userData->midiHandler->sendMidiOut(&message); |
| 225 | } |
| 226 | return 0; |
| 227 | } |
| 228 | |
| 229 | int CsoundEngine::midiOutCloseCb(CSOUND *csound, void *ud) |
| 230 | { |
| 231 | (void) csound; |
| 232 | (void) ud; |
| 233 | return 0; |
| 234 | } |
| 235 | |
| 236 | const char *CsoundEngine::midiErrorStringCb(int) |
| 237 | { |
| 238 | |
| 239 | return nullptr; |
| 240 | } |
| 241 | |
| 242 | void CsoundEngine::queueMidiIn(std::vector< unsigned char > *message) |
| 243 | { |
| 244 | if (ud->midiBuffer) { |
| 245 | csoundWriteCircularBuffer(ud->csound, ud->midiBuffer, message->data(), message->size()* sizeof(unsigned char)); |
| 246 | } |
| 247 | } |
| 248 | |
| 249 | void CsoundEngine::queueVirtualMidiIn(std::vector< unsigned char > &message) |
| 250 | { |
| 251 | if (ud->virtualMidiBuffer) { |
| 252 | csoundWriteCircularBuffer(ud->csound, ud->virtualMidiBuffer, message.data(), message.size()* sizeof(unsigned char)); |
| 253 | } |
| 254 | } |
| 255 | |
| 256 | void CsoundEngine::sendMidiOut(QVector<unsigned char> &message) |
| 257 | { |
| 258 | (void) message; |
| 259 | } |
| 260 | |
| 261 | |
| 262 | void CsoundEngine::makeGraphCallback(CSOUND *csound, WINDAT *windat, const char * /*name*/) |
| 263 | { |
| 264 | CsoundUserData *ud = (CsoundUserData *) csoundGetHostData(csound); |
| 265 | // Csound reuses windat, so it is not guaranteed to be unique |
| 266 | // name seems not to be used. |
| 267 | ud->wl->appendCurve(windat); |
| 268 | } |
| 269 | |
| 270 | void CsoundEngine::drawGraphCallback(CSOUND *csound, WINDAT *windat) |
| 271 | { |
| 272 | CsoundUserData *udata = (CsoundUserData *) csoundGetHostData(csound); |
| 273 | // This callback paints data on curves |
| 274 | udata->wl->updateCurve(windat); |
| 275 | } |
| 276 | |
| 277 | void CsoundEngine::killGraphCallback(CSOUND *csound, WINDAT *windat) |
| 278 | { |
| 279 | // When is this callback called?? |
| 280 | CsoundUserData *udata = (CsoundUserData *) csoundGetHostData(csound); |
| 281 | udata->wl->killCurve(windat); |
| 282 | } |
| 283 | |
| 284 | int CsoundEngine::exitGraphCallback(CSOUND *csound) |
| 285 | { |
| 286 | CsoundUserData *udata = (CsoundUserData *) csoundGetHostData(csound); |
| 287 | return udata->wl->killCurves(csound); |
| 288 | } |
| 289 | |
| 290 | int CsoundEngine::keyEventCallback(void *userData, |
| 291 | void *p, |
| 292 | unsigned int type) |
| 293 | { |
| 294 | if (type != CSOUND_CALLBACK_KBD_EVENT && type != CSOUND_CALLBACK_KBD_TEXT) { |
| 295 | return 1; |
| 296 | } |
| 297 | CsoundUserData *ud = (CsoundUserData *) userData; |
| 298 | // WidgetLayout *wl = (WidgetLayout *) ud->wl; |
| 299 | int *value = (int *) p; |
| 300 | int key = ud->csEngine->popKeyPressEvent(); |
| 301 | if (key >= 0) { |
| 302 | *value = key; |
| 303 | qDebug() << "Pressed: " << key; |
| 304 | } |
| 305 | else if (type & CSOUND_CALLBACK_KBD_EVENT) { |
| 306 | key = ud->csEngine->popKeyReleaseEvent(); |
| 307 | if (key >= 0) { |
| 308 | *value = key | 0x10000; |
| 309 | qDebug() << "Released: " << key; |
| 310 | } |
| 311 | } |
| 312 | return 0; |
| 313 | } |
| 314 | |
| 315 | void CsoundEngine::csThread(void *data) |
| 316 | { |
| 317 | CsoundUserData* udata = (CsoundUserData*)data; |
| 318 | if (!(udata->flags & QCS_NO_COPY_BUFFER)) { |
| 319 | MYFLT *outputBuffer = csoundGetSpout(udata->csound); |
| 320 | // outputBufferSize == ksmps |
| 321 | long numSamples = udata->outputBufferSize * udata->numChnls; |
| 322 | udata->audioOutputBuffer.putManyScaled(outputBuffer, numSamples, |
| 323 | 1.0/udata->zerodBFS); |
| 324 | // for (int i = 0; i < udata->outputBufferSize*udata->numChnls; i++) { |
| 325 | // udata->audioOutputBuffer.put(outputBuffer[i]/ udata->zerodBFS); |
| 326 | // } |
| 327 | } |
| 328 | // udata->wl->getValues(&udata->channelNames, |
| 329 | // &udata->values, |
| 330 | // &udata->stringValues); |
| 331 | if (udata->enableWidgets) { |
| 332 | // csoundDeleteChannelList(udata->csound, *channelList); |
| 333 | writeWidgetValues(udata); |
| 334 | readWidgetValues(udata); |
| 335 | } |
| 336 | if (!(udata->flags & QCS_NO_RT_EVENTS)) { |
| 337 | udata->csEngine->processEventQueue(); |
| 338 | } |
| 339 | #ifdef QCS_PYTHONQT |
| 340 | if (!(udata->flags & QCS_NO_PYTHON_CALLBACK)) { |
| 341 | if (!udata->m_pythonCallback.isEmpty()) { |
| 342 | if (udata->m_pythonCallbackCounter >= udata->m_pythonCallbackSkip) { |
| 343 | udata->m_pythonConsole->evaluate(udata->m_pythonCallback, false); |
| 344 | udata->m_pythonCallbackCounter = 0; |
| 345 | } |
| 346 | else { |
| 347 | udata->m_pythonCallbackCounter++; |
| 348 | } |
| 349 | } |
| 350 | } |
| 351 | #endif |
| 352 | } |
| 353 | |
| 354 | void CsoundEngine::readWidgetValues(CsoundUserData *ud) |
| 355 | { |
| 356 | MYFLT* pvalue; |
| 357 | |
| 358 | if (ud->wl->valueMutex.tryLock()) { |
| 359 | QHash<QString, double>::const_iterator i; |
| 360 | QHash<QString, double>::const_iterator end = ud->wl->newValues.constEnd(); |
| 361 | for (i = ud->wl->newValues.constBegin(); i != end; ++i) { |
| 362 | if(csoundGetChannelPtr(ud->csound, &pvalue, i.key().toLocal8Bit().constData(), |
| 363 | CSOUND_INPUT_CHANNEL | CSOUND_CONTROL_CHANNEL) == 0) { |
| 364 | // 0 == success |
| 365 | *pvalue = (MYFLT) i.value(); |
| 366 | } |
| 367 | } |
| 368 | ud->wl->newValues.clear(); |
| 369 | ud->wl->valueMutex.unlock(); |
| 370 | } |
| 371 | if (ud->wl->stringValueMutex.tryLock()) { |
| 372 | QHash<QString, QString>::const_iterator i; |
| 373 | QHash<QString, QString>::const_iterator end = ud->wl->newStringValues.constEnd(); |
| 374 | for (i = ud->wl->newStringValues.constBegin(); i != end; ++i) { |
| 375 | csoundSetStringChannel(ud->csound, i.key().toLocal8Bit().data(), |
| 376 | i.value().toLocal8Bit().data()); |
| 377 | } |
| 378 | ud->wl->newStringValues.clear(); |
| 379 | ud->wl->stringValueMutex.unlock(); |
| 380 | } |
| 381 | } |
| 382 | |
| 383 | void CsoundEngine::writeWidgetValues(CsoundUserData *ud) |
| 384 | { |
| 385 | MYFLT* pvalue; |
| 386 | for (int i = 0; i < ud->outputChannelNames.size(); i++) { |
| 387 | if (ud->outputChannelNames[i] != "" |
| 388 | && csoundGetChannelPtr(ud->csound, &pvalue, |
| 389 | ud->outputChannelNames[i].toLocal8Bit().constData(), |
| 390 | CSOUND_OUTPUT_CHANNEL | CSOUND_CONTROL_CHANNEL) == 0) { |
| 391 | if(ud->previousOutputValues[i] != *pvalue) { |
| 392 | ud->wl->setValue(ud->outputChannelNames[i],*pvalue); |
| 393 | ud->previousOutputValues[i] = *pvalue; |
| 394 | } |
| 395 | } |
| 396 | } |
| 397 | for (int i = 0; i < ud->outputStringChannelNames.size(); i++) { |
| 398 | if (ud->outputStringChannelNames[i] != "" |
| 399 | && csoundGetChannelPtr(ud->csound, &pvalue, |
| 400 | ud->outputStringChannelNames[i].toLocal8Bit().constData(), |
| 401 | CSOUND_OUTPUT_CHANNEL | CSOUND_STRING_CHANNEL) == 0) { |
| 402 | char chanString[2048]; // large enough for long strings in displays |
| 403 | csoundGetStringChannel(ud->csound, ud->outputStringChannelNames[i].toLocal8Bit().constData(), |
| 404 | chanString); |
| 405 | if(ud->previousStringOutputValues[i] != QString(chanString)) { |
| 406 | ud->wl->setValue(ud->outputStringChannelNames[i],QString(chanString)); |
| 407 | ud->previousStringOutputValues[i] = QString(chanString); |
| 408 | } |
| 409 | } |
| 410 | } |
| 411 | } |
| 412 | |
| 413 | void CsoundEngine::setWidgetLayout(WidgetLayout *wl) |
| 414 | { |
| 415 | ud->wl = wl; |
| 416 | // connect(wl, SIGNAL(destroyed()), this, SLOT(widgetLayoutDestroyed())); |
| 417 | // Key presses on widget layout and console are passed to the engine |
| 418 | connect(wl, SIGNAL(keyPressed(int)), |
| 419 | this, SLOT(keyPressForCsound(int))); |
| 420 | connect(wl, SIGNAL(keyReleased(int)), |
| 421 | this, SLOT(keyReleaseForCsound(int))); |
| 422 | |
| 423 | // Register scopes and graphs to pass them the engine's user data |
| 424 | connect(wl, SIGNAL(registerScope(QuteScope*)), |
| 425 | this,SLOT(registerScope(QuteScope*))); |
| 426 | connect(wl, SIGNAL(registerGraph(QuteGraph*)), |
| 427 | this,SLOT(registerGraph(QuteGraph*))); |
| 428 | connect(wl, SIGNAL(requestCsoundUserData(QuteWidget*)), |
| 429 | this, SLOT(requestCsoundUserData(QuteWidget*))); |
| 430 | connect(this, SIGNAL(passMessages(QString)), wl, SLOT(appendMessage(QString)), Qt::UniqueConnection); |
| 431 | } |
| 432 | |
| 433 | void CsoundEngine::setMidiHandler(MidiHandler *mh) |
| 434 | { |
| 435 | ud->midiHandler = mh; |
| 436 | } |
| 437 | |
| 438 | void CsoundEngine::enableWidgets(bool enable) |
| 439 | { |
| 440 | ud->enableWidgets = enable; |
| 441 | } |
| 442 | |
| 443 | void CsoundEngine::registerConsole(ConsoleWidget *c) |
| 444 | { |
| 445 | consoles.append(c); |
| 446 | connect(this,SIGNAL(passMessages(QString)), c, SLOT(appendMessage(QString)), Qt::UniqueConnection); |
| 447 | } |
| 448 | |
| 449 | QList<QPair<int, QString> > CsoundEngine::getErrorLines() |
| 450 | { |
| 451 | QList<QPair<int, QString> > list; |
| 452 | if (consoles.size() > 0) { |
| 453 | QList<int> lines = consoles[0]->errorLines; |
| 454 | QStringList texts = consoles[0]->errorTexts; |
| 455 | for (int i = 0; i < lines.size(); i++) { |
| 456 | list.append(QPair<int, QString>(lines[i], texts[i])); |
| 457 | } |
| 458 | } |
| 459 | return list; |
| 460 | } |
| 461 | |
| 462 | void CsoundEngine::setConsoleBufferSize(int size) |
| 463 | { |
| 464 | m_consoleBufferSize = size; |
| 465 | } |
| 466 | |
| 467 | QList <int> CsoundEngine::getAnsiKeySequence(int key) // convert sepcial keys (Qt::Key) like Esc, arrows, F1 etc to ANSI escape key sequence for Csound |
| 468 | { |
| 469 | QList <int> keyArray; |
| 470 | if (key < 0xff) { |
| 471 | keyArray << key; |
| 472 | } else switch (key) { |
| 473 | case Qt::Key_Escape: keyArray << 27; break; |
| 474 | case Qt::Key_Tab: keyArray << 9; break; |
| 475 | case Qt::Key_Backspace: keyArray << 127; break; |
| 476 | case Qt::Key_Up: keyArray << 27 << 91 << 65; break; |
| 477 | case Qt::Key_Down: keyArray << 27 << 91 << 66; break; |
| 478 | case Qt::Key_Right: keyArray << 27 << 91 << 67; break; |
| 479 | case Qt::Key_Left: keyArray << 27 << 91 << 68; break; |
| 480 | case Qt::Key_Home: keyArray << 27 << 91 << 72; break; |
| 481 | case Qt::Key_End: keyArray << 27 << 91 << 70; break; |
| 482 | case Qt::Key_PageUp: keyArray << 27 << 53 << 126; break; |
| 483 | case Qt::Key_PageDown: keyArray << 27 << 52 << 126; break; |
| 484 | case Qt::Key_F1: keyArray << 27 << 91 << 80; break; |
| 485 | case Qt::Key_F2: keyArray << 27 << 79 << 81; break; |
| 486 | case Qt::Key_F3: keyArray << 27 << 79 << 82; break; |
| 487 | case Qt::Key_F4: keyArray << 27 << 79 << 83; break; |
| 488 | case Qt::Key_F5: keyArray << 27 << 91 << 49 << 53 << 126; break; |
| 489 | case Qt::Key_F6: keyArray << 27 << 91 << 49 << 55 << 126; break; |
| 490 | case Qt::Key_F7: keyArray << 27 << 91 << 49 << 56 << 126; break; |
| 491 | case Qt::Key_F8: keyArray << 27 << 91 << 49 << 57 << 126; break; |
| 492 | case Qt::Key_F9: keyArray << 27 << 91 << 50 << 48 << 126; break; |
| 493 | case Qt::Key_F10: keyArray << 27 << 91 << 50 << 49 << 126; break; |
| 494 | case Qt::Key_F11: keyArray << 27 << 91 << 50 << 51 << 126; break; |
| 495 | case Qt::Key_F12: keyArray << 27 << 91 << 50 << 52 << 126; break; |
| 496 | case Qt::Key_Insert: keyArray << 27 << 91 << 50 << 126; break; |
| 497 | case Qt::Key_Delete: keyArray << 27 << 91 << 51 << 126; break; |
| 498 | |
| 499 | default: qDebug()<<"Key " << key << " ignored."; //keyArray << key; |
| 500 | } |
| 501 | return keyArray; |
| 502 | } |
| 503 | |
| 504 | void CsoundEngine::keyPressForCsound(int key) |
| 505 | { |
| 506 | keyMutex.lock(); |
| 507 | keyPressBuffer << getAnsiKeySequence(key); |
| 508 | keyMutex.unlock(); |
| 509 | } |
| 510 | |
| 511 | void CsoundEngine::keyReleaseForCsound(int key) // NB! I did not change this to int since seems Csound actually does not use it? |
| 512 | { |
| 513 | keyMutex.lock(); |
| 514 | keyReleaseBuffer << key; |
| 515 | keyMutex.unlock(); |
| 516 | } |
| 517 | |
| 518 | void CsoundEngine::requestCsoundUserData(QuteWidget *widget) { |
| 519 | widget->setCsoundUserData(ud); |
| 520 | } |
| 521 | |
| 522 | void CsoundEngine::registerScope(QuteScope *scope) |
| 523 | { |
| 524 | scope->setUd(ud); |
| 525 | } |
| 526 | |
| 527 | void CsoundEngine::registerGraph(QuteGraph *graph) |
| 528 | { |
| 529 | graph->setUd(ud); |
| 530 | } |
| 531 | |
| 532 | void CsoundEngine::evaluate(QString code) |
| 533 | { |
| 534 | CSOUND *csound = getCsound(); |
| 535 | if (csound) { |
| 536 | csoundCompileOrc(csound, code.toLatin1()); |
| 537 | queueMessage(tr("Csound code evaluated.\n")); |
| 538 | } else { |
| 539 | queueMessage(tr("Csound is not running. Code not evaluated.")); |
| 540 | } |
| 541 | } |
| 542 | |
| 543 | //void CsoundEngine::unregisterScope(QuteScope *scope) |
| 544 | //{ |
| 545 | // // TODO is it necessary to unregiter scopes? |
| 546 | // qDebug() << "Not implemented"; |
| 547 | //} |
| 548 | |
| 549 | int CsoundEngine::popKeyPressEvent() |
| 550 | { |
| 551 | int value = -1; |
| 552 | if (keyMutex.tryLock()) { |
| 553 | if (!keyPressBuffer.isEmpty()) { |
| 554 | value = keyPressBuffer.takeFirst(); |
| 555 | } |
| 556 | |
| 557 | keyMutex.unlock(); |
| 558 | } |
| 559 | return value; |
| 560 | } |
| 561 | |
| 562 | int CsoundEngine::popKeyReleaseEvent() |
| 563 | { |
| 564 | int value = -1; |
| 565 | if (keyMutex.tryLock()) { |
| 566 | if (!keyReleaseBuffer.isEmpty()) { |
| 567 | value = keyReleaseBuffer.takeFirst(); |
| 568 | } |
| 569 | keyMutex.unlock(); |
| 570 | } |
| 571 | return value; |
| 572 | } |
| 573 | |
| 574 | void CsoundEngine::processEventQueue() |
| 575 | { |
| 576 | // This function should only be called when Csound is running |
| 577 | eventMutex.lock(); |
| 578 | while (eventQueueSize > 0) { |
| 579 | eventQueueSize--; |
| 580 | m_playMutex.lock(); |
| 581 | #ifdef QCS_DESTROY_CSOUND |
| 582 | if (ud->perfThread) { |
| 583 | //ScoreEvent is not working |
| 584 | // ud->perfThread->ScoreEvent(0, type, eventElements.size(), pFields); |
| 585 | // qDebug() << eventQueue[eventQueueSize]; |
| 586 | ud->perfThread->InputMessage(eventQueue[eventQueueSize].toLatin1()); |
| 587 | } |
| 588 | else { |
| 589 | QDEBUG << "WARNING: ud->perfThread is NULL"; |
| 590 | } |
| 591 | #else |
| 592 | if (ud->perfThread) { |
| 593 | ud->perfThread->InputMessage(eventQueue[eventQueueSize].toLatin1()); |
| 594 | } |
| 595 | #endif |
| 596 | m_playMutex.unlock(); |
| 597 | } |
| 598 | eventMutex.unlock(); |
| 599 | } |
| 600 | |
| 601 | void CsoundEngine::passOutValue(QString channelName, double value) |
| 602 | { |
| 603 | ud->wl->newValue(QPair<QString, double>(channelName, value)); |
| 604 | } |
| 605 | |
| 606 | void CsoundEngine::passOutString(QString channelName, QString value) |
| 607 | { |
| 608 | // qDebug() ; |
| 609 | ud->wl->newValue(QPair<QString, QString>(channelName, value)); |
| 610 | } |
| 611 | |
| 612 | int CsoundEngine::play(CsoundOptions *options) |
| 613 | { |
| 614 | QMutexLocker locker(&m_playMutex); |
| 615 | if (ud->perfThread && (ud->perfThread->GetStatus() == 0)) { |
| 616 | // GetStatus == 0 means playing |
| 617 | // ud->perfThread->TogglePause(); // no need for that when there is Pause button |
| 618 | QDEBUG << "Already playing"; |
| 619 | return 0; |
| 620 | } |
| 621 | if (options) { |
| 622 | m_options = *options; |
| 623 | } |
| 624 | locker.unlock(); |
| 625 | if(options->checkSyntaxOnly) { |
| 626 | return checkSyntax(); |
| 627 | } |
| 628 | if(options->checkSyntaxBeforeRun) { |
| 629 | QDEBUG << "Checking Syntax"; |
| 630 | int ret = checkSyntax(); |
| 631 | if(ret != 0) { |
| 632 | QDEBUG << "Syntax check failed with return code:" << ret; |
| 633 | return ret; |
| 634 | } |
| 635 | QDEBUG << "Syntax check ok, running csound"; |
| 636 | } |
| 637 | return runCsound(); |
| 638 | } |
| 639 | |
| 640 | void CsoundEngine::stop() |
| 641 | { |
| 642 | stopRecording(); |
| 643 | stopCsound(); |
| 644 | } |
| 645 | |
| 646 | void CsoundEngine::pause() |
| 647 | { |
| 648 | QMutexLocker locker(&m_playMutex); |
| 649 | if (ud->perfThread && (ud->perfThread->GetStatus() == 0)) { |
| 650 | //ud->perfThread->Pause(); |
| 651 | ud->perfThread->TogglePause(); |
| 652 | m_paused = !m_paused; |
| 653 | qDebug() << "Paused: " << m_paused; |
| 654 | } |
| 655 | } |
| 656 | |
| 657 | int CsoundEngine::startRecording(int sampleformat, QString fileName) |
| 658 | { |
| 659 | qDebug("start recording (%i-bit samples): %s", |
| 660 | (sampleformat + 2) * 8, |
| 661 | fileName.toLocal8Bit().constData()); |
| 662 | m_paused = false; |
| 663 | // clip instead of wrap when converting floats to ints |
| 664 | #ifdef PERFTHREAD_RECORD |
| 665 | // perfthread record API is only available with csound >= 6.04 |
| 666 | if (ud->perfThread) { |
| 667 | ud->lastRecordingOutfile = fileName; |
| 668 | m_recording = true; |
| 669 | ud->perfThread->Record(fileName.toLocal8Bit().constData(), |
| 670 | (sampleformat + 2) * 8); |
| 671 | } |
| 672 | #endif |
| 673 | return 0; |
| 674 | } |
| 675 | |
| 676 | void CsoundEngine::stopRecording() |
| 677 | { |
| 678 | m_recording = false; |
| 679 | m_paused = false; |
| 680 | |
| 681 | #ifdef PERFTHREAD_RECORD |
| 682 | if (ud->perfThread) { |
| 683 | ud->perfThread->StopRecord(); |
| 684 | } |
| 685 | //qDebug("Recording stopped."); |
| 686 | #endif |
| 687 | } |
| 688 | |
| 689 | void CsoundEngine::queueEvent(QString eventLine, int delay) |
| 690 | { |
| 691 | // TODO: implement delayed events |
| 692 | (void) delay; |
| 693 | // qDebug("CsoundEngine::queueEvent %s", eventLine.toStdString().c_str()); |
| 694 | if (!isRunning()) { |
| 695 | QMutexLocker lock(&m_messageMutex); |
| 696 | messageQueue << tr("Csound is not running! Event ignored.\n"); |
| 697 | return; |
| 698 | } |
| 699 | if (eventQueueSize < QCS_MAX_EVENTS) { |
| 700 | eventMutex.lock(); |
| 701 | eventQueue[eventQueueSize] = eventLine; |
| 702 | eventQueueSize++; |
| 703 | eventMutex.unlock(); |
| 704 | } |
| 705 | else { |
| 706 | qDebug("Warning: event queue full, event not processed"); |
| 707 | } |
| 708 | } |
| 709 | |
| 710 | int CsoundEngine::checkSyntax() { |
| 711 | QDEBUG << "$$$ checkSyntax 0"; |
| 712 | QMutexLocker locker(&m_playMutex); |
| 713 | QDEBUG << "$$$ checkSyntax 1"; |
| 714 | |
| 715 | CsoundOptions options(m_options); |
| 716 | options.checkSyntaxOnly = true; |
| 717 | |
| 718 | ud->csound = csoundCreate((void *) ud); |
| 719 | QDEBUG << "$$$ checkSyntax 2"; |
| 720 | |
| 721 | eventQueueSize = 0; |
| 722 | ud->msgRefreshTime = m_refreshTime*1000; |
| 723 | QDir::setCurrent(m_options.fileName1); |
| 724 | for (int i = 0; i < consoles.size(); i++) { |
| 725 | consoles[i]->reset(); |
| 726 | } |
| 727 | csoundCreateMessageBuffer(ud->csound, 0); |
| 728 | |
| 729 | char const **argv;// since there was change in Csound API |
| 730 | argv = (const char **) calloc(33, sizeof(char*)); |
| 731 | |
| 732 | int argc = options.generateCmdLine((char **)argv); |
| 733 | |
| 734 | ud->result = csoundCompile(ud->csound, argc, argv); |
| 735 | |
| 736 | for (int i = 0; i < argc; i++) { |
| 737 | qDebug() << argv[i]; |
| 738 | free((char *) argv[i]); |
| 739 | } |
| 740 | free(argv); |
| 741 | int out; |
| 742 | if (ud->result != 256) { |
| 743 | qDebug() << "Csound syntax check failed! " << ud->result; |
| 744 | flushQueues(); |
| 745 | locker.unlock(); // otherwise csoundStop will freeze |
| 746 | stop(); |
| 747 | emit (errorLines(getErrorLines())); |
| 748 | |
| 749 | out = -3; // compilation error |
| 750 | } else { |
| 751 | // this might still have failed... |
| 752 | QDEBUG << "Syntax check ok, return code: " << ud->result; |
| 753 | out = 0; // OK |
| 754 | } |
| 755 | // csoundDestroyMessageBuffer(ud->csound); |
| 756 | // csoundCleanup(ud->csound); |
| 757 | // csoundReset(ud->csound); |
| 758 | return out; |
| 759 | } |
| 760 | |
| 761 | |
| 762 | int CsoundEngine::runCsound() |
| 763 | { |
| 764 | QMutexLocker locker(&m_playMutex); |
| 765 | #ifdef MACOSX_PRE_SNOW |
| 766 | // Remember menu bar to set it after FLTK grabs it |
| 767 | menuBarHandle = GetMenuBar(); |
| 768 | #endif |
| 769 | #ifdef Q_OS_WIN |
| 770 | // Call OleInitialize twice to keep the FLTK opcodes from reducing the COM |
| 771 | // reference count to zero. |
| 772 | // OleInitialize(NULL); // Do not initialize here but in CsoundQt onbject |
| 773 | // OleInitialize(NULL); |
| 774 | #endif |
| 775 | eventQueueSize = 0; |
| 776 | // Flush events gathered while idle. |
| 777 | ud->audioOutputBuffer.allZero(); |
| 778 | ud->msgRefreshTime = m_refreshTime*1000; |
| 779 | QDir::setCurrent(m_options.fileName1); |
| 780 | for (int i = 0; i < consoles.size(); i++) { |
| 781 | consoles[i]->reset(); |
| 782 | } |
| 783 | #ifdef QCS_DESTROY_CSOUND |
| 784 | ud->csound=csoundCreate((void *) ud); |
| 785 | ud->midiBuffer = csoundCreateCircularBuffer(ud->csound, 1024, sizeof(unsigned char)); |
| 786 | Q_ASSERT(ud->midiBuffer); |
| 787 | ud->virtualMidiBuffer = csoundCreateCircularBuffer(ud->csound, 1024, sizeof(unsigned char)); |
| 788 | Q_ASSERT(ud->virtualMidiBuffer); |
| 789 | // csoundFlushCircularBuffer(ud->csound, ud->midiBuffer); |
| 790 | #endif |
| 791 | #ifdef QCS_DEBUGGER |
| 792 | if(m_debugging) { |
| 793 | csoundDebuggerInit(ud->csound); |
| 794 | foreach(QVariantList bp, m_startBreakpoints) { |
| 795 | Q_ASSERT(bp.size() > 1); |
| 796 | if (bp[0].toString() == "instr") { |
| 797 | Q_ASSERT(bp.size() == 3); |
| 798 | csoundSetInstrumentBreakpoint(ud->csound, bp[1].toDouble(), bp[2].toInt()); |
| 799 | } else if (bp[0].toString() == "line") { |
| 800 | Q_ASSERT(bp.size() == 4); |
| 801 | csoundSetBreakpoint(ud->csound, bp[2].toInt(), bp[1].toInt(), bp[3].toInt()); |
| 802 | } else { |
| 803 | qDebug() << "Wrong breakpoint format"; |
| 804 | } |
| 805 | } |
| 806 | csoundSetBreakpointCallback(ud->csound, &CsoundEngine::breakpointCallback, (void *) this); |
| 807 | } |
| 808 | #endif |
| 809 | if(!m_options.useCsoundMidi) { |
| 810 | csoundSetHostImplementedMIDIIO(ud->csound, 1); |
| 811 | csoundSetExternalMidiInOpenCallback(ud->csound, &midiInOpenCb); |
| 812 | csoundSetExternalMidiReadCallback(ud->csound, &midiReadCb); |
| 813 | csoundSetExternalMidiInCloseCallback(ud->csound, &midiInCloseCb); |
| 814 | csoundSetExternalMidiOutOpenCallback(ud->csound, &midiOutOpenCb); |
| 815 | csoundSetExternalMidiWriteCallback(ud->csound, &midiWriteCb); |
| 816 | csoundSetExternalMidiOutCloseCallback(ud->csound, &midiOutCloseCb); |
| 817 | csoundSetExternalMidiErrorStringCallback(ud->csound, &midiErrorStringCb); |
| 818 | } |
| 819 | csoundCreateMessageBuffer(ud->csound, 0); |
| 820 | |
| 821 | if (m_options.enableFLTK) { |
| 822 | // Disable FLTK graphs, but allow FLTK widgets. |
| 823 | int *var = (int*) csoundQueryGlobalVariable(ud->csound, "FLTK_Flags"); |
| 824 | if (var) { |
| 825 | *var = 4; |
| 826 | } else { |
| 827 | if (csoundCreateGlobalVariable(ud->csound, "FLTK_Flags", sizeof(int)) != CSOUND_SUCCESS) { |
| 828 | qDebug() << "Error creating the FTLK_Flags variable"; |
| 829 | } else { |
| 830 | int *var = (int*) csoundQueryGlobalVariable(ud->csound, "FLTK_Flags"); |
| 831 | if (var) { |
| 832 | *var = 4; |
| 833 | } else { |
| 834 | qDebug() << "Error reading the FTLK_Flags variable"; |
| 835 | } |
| 836 | } |
| 837 | } |
| 838 | } |
| 839 | else { |
| 840 | csoundSetGlobalEnv("CS_OMIT_LIBS", "fluidOpcodes,virtual,widgets"); |
| 841 | int *var = (int*) csoundQueryGlobalVariable(ud->csound, "FLTK_Flags"); |
| 842 | if (var) { |
| 843 | *var = 3; |
| 844 | } else { |
| 845 | qDebug() << "Error reading the FTLK_Flags variable"; |
| 846 | } |
| 847 | } |
| 848 | csoundRegisterKeyboardCallback(ud->csound, |
| 849 | &CsoundEngine::keyEventCallback, |
| 850 | (void *) ud, CSOUND_CALLBACK_KBD_EVENT | CSOUND_CALLBACK_KBD_TEXT); |
| 851 | // necessary to put something into the buffer, otherwise sensekey complains when |
| 852 | // not started from terminal |
| 853 | csoundKeyPress(getCsound(),'\0'); |
| 854 | |
| 855 | #ifdef QCS_RTMIDI |
| 856 | if (!m_options.useCsoundMidi) { |
| 857 | csoundSetOption(ud->csound, const_cast<char *>("-+rtmidi=hostbased")); |
| 858 | csoundSetOption(ud->csound, const_cast<char *>("-M0")); |
| 859 | csoundSetOption(ud->csound, const_cast<char *>("-Q0")); |
| 860 | } |
| 861 | #endif |
| 862 | csoundSetIsGraphable(ud->csound, true); |
| 863 | csoundSetMakeGraphCallback(ud->csound, &CsoundEngine::makeGraphCallback); |
| 864 | csoundSetDrawGraphCallback(ud->csound, &CsoundEngine::drawGraphCallback); |
| 865 | csoundSetKillGraphCallback(ud->csound, &CsoundEngine::killGraphCallback); |
| 866 | csoundSetExitGraphCallback(ud->csound, &CsoundEngine::exitGraphCallback); |
| 867 | if (!m_options.fileName1.endsWith(".html", Qt::CaseInsensitive)) { |
| 868 | #if CS_APIVERSION>=4 |
| 869 | char const **argv;// since there was change in Csound API |
| 870 | argv = (const char **) calloc(33, sizeof(char*)); |
| 871 | #else |
| 872 | char **argv; |
| 873 | argv = (char **) calloc(33, sizeof(char*)); |
| 874 | #endif |
| 875 | |
| 876 | int argc = m_options.generateCmdLine((char **)argv); |
| 877 | |
| 878 | qDebug() << "------------ Compiling csd..."; |
| 879 | ud->result=csoundCompile(ud->csound,argc,argv); |
| 880 | for (int i = 0; i < argc; i++) { |
| 881 | qDebug() << argv[i]; |
| 882 | free((char *) argv[i]); |
| 883 | } |
| 884 | free(argv); |
| 885 | if (ud->result != CSOUND_SUCCESS) { |
| 886 | qDebug() << "Csound compile failed! " << ud->result; |
| 887 | // Commenting out flushQues fixes the crash. |
| 888 | // Investigate closer, if it must be here |
| 889 | // seems that messages are outputted into console anyway... |
| 890 | flushQueues(); // the line was here in some earlier version. Otherwise errormessaged won't be processed by Console::appendMessage() |
| 891 | locker.unlock(); // otherwise csoundStop will freeze |
| 892 | stop(); |
| 893 | emit (errorLines(getErrorLines())); |
| 894 | return -3; |
| 895 | } |
| 896 | } |
| 897 | |
| 898 | ud->zerodBFS = csoundGet0dBFS(ud->csound); |
| 899 | ud->sampleRate = csoundGetSr(ud->csound); |
| 900 | ud->numChnls = csoundGetNchnls(ud->csound); |
| 901 | ud->outputBufferSize = csoundGetKsmps(ud->csound); |
| 902 | if (ud->enableWidgets) { |
| 903 | setupChannels(); |
| 904 | } |
| 905 | // Do not run the performance thread if the piece is an HTML file, |
| 906 | // the HTML code must do that. |
| 907 | if (!m_options.fileName1.endsWith(".html", Qt::CaseInsensitive)) { |
| 908 | ud->perfThread = new CsoundPerformanceThread(ud->csound); |
| 909 | ud->perfThread->SetProcessCallback(CsoundEngine::csThread, (void*)ud); |
| 910 | ud->perfThread->Play(); |
| 911 | m_paused = false; |
| 912 | } |
| 913 | ud->audioOutputBuffer.resize(ud->numChnls * 2048); |
| 914 | return 0; |
| 915 | } |
| 916 | |
| 917 | void CsoundEngine::stopCsound() |
| 918 | { |
| 919 | // perfThread->ScoreEvent(0, 'e', 0, 0); |
| 920 | // m_playMutex.lock(); |
| 921 | QMutexLocker locker(&m_playMutex); |
| 922 | if(!ud->perfThread) |
| 923 | return; |
| 924 | |
| 925 | CsoundPerformanceThread *pt = ud->perfThread; |
| 926 | |
| 927 | pt->Stop(); |
| 928 | m_paused = false; |
| 929 | |
| 930 | unsigned int waitTime = 100; |
| 931 | |
| 932 | for(int msecs = 2000; msecs > 0; msecs -= waitTime) { |
| 933 | int status = pt->GetStatus(); |
| 934 | if(status > 0) { |
| 935 | QDEBUG << "Csound stopped OK"; |
| 936 | break; |
| 937 | } |
| 938 | if(status < 0) { |
| 939 | QDEBUG << "Error when stopping csound"; |
| 940 | break; |
| 941 | } |
| 942 | if(status == 0) { |
| 943 | QDEBUG << "Csound still playing, trying again in " << waitTime << "msecs"; |
| 944 | QThread::msleep(waitTime); |
| 945 | // pt->Stop(); |
| 946 | } |
| 947 | } |
| 948 | |
| 949 | |
| 950 | |
| 951 | if(pt->GetStatus() <= 0) { |
| 952 | QDEBUG << "Csound's performance thread failed to stop, stopping csound"; |
| 953 | locker.unlock(); |
| 954 | csoundMutex.lock(); |
| 955 | pt->SetProcessCallback(nullptr, nullptr); |
| 956 | QThread::msleep(200); |
| 957 | QDEBUG << "Destroying csound..."; |
| 958 | // delete pt; |
| 959 | csoundDestroy(ud->csound); |
| 960 | QDEBUG << "Destroyed ok"; |
| 961 | ud->perfThread = nullptr; |
| 962 | csoundMutex.unlock(); |
| 963 | emit stopSignal(); |
| 964 | QDEBUG << "Stopped OK..."; |
| 965 | return; |
| 966 | } |
| 967 | |
| 968 | QDEBUG << "Joining..."; |
| 969 | pt->Join(); |
| 970 | QDEBUG << "Joined."; |
| 971 | |
| 972 | ud->perfThread = NULL; |
| 973 | delete pt; |
| 974 | |
| 975 | QDEBUG << "Cleaning up csound..."; |
| 976 | |
| 977 | this->cleanupCsound(); |
| 978 | |
| 979 | QDEBUG << "Clean up OK"; |
| 980 | |
| 981 | |
| 982 | #ifdef QCS_DEBUGGER |
| 983 | stopDebug(); |
| 984 | #endif |
| 985 | |
| 986 | #ifdef MACOSX_PRE_SNOW |
| 987 | // Put menu bar back |
| 988 | SetMenuBar(menuBarHandle); |
| 989 | #endif |
| 990 | QDEBUG << "emitting stopSignal..."; |
| 991 | |
| 992 | locker.unlock(); |
| 993 | emit stopSignal(); |
| 994 | |
| 995 | QDEBUG << "Exiting stopCsound"; |
| 996 | } |
| 997 | |
| 998 | void CsoundEngine::cleanupCsound() |
| 999 | { |
| 1000 | if(ud->csound == nullptr) { |
| 1001 | QDEBUG << "csound is null"; |
| 1002 | return; |
| 1003 | } |
| 1004 | QMutexLocker locker(&csoundMutex); |
| 1005 | csoundSetIsGraphable(ud->csound, 0); |
| 1006 | csoundSetMakeGraphCallback(ud->csound, nullptr); |
| 1007 | csoundSetDrawGraphCallback(ud->csound, nullptr); |
| 1008 | csoundSetKillGraphCallback(ud->csound, nullptr); |
| 1009 | csoundSetExitGraphCallback(ud->csound, nullptr); |
| 1010 | csoundRemoveKeyboardCallback(ud->csound, &CsoundEngine::keyEventCallback); |
| 1011 | #ifdef QCS_DEBUGGER |
| 1012 | if(m_debugging) { |
| 1013 | csoundDebuggerClean(ud->csound); |
| 1014 | } |
| 1015 | #endif |
| 1016 | |
| 1017 | csoundCleanup(ud->csound); |
| 1018 | flushQueues(); |
| 1019 | csoundDestroyMessageBuffer(ud->csound); |
| 1020 | |
| 1021 | #ifdef QCS_DESTROY_CSOUND |
| 1022 | csoundDestroyCircularBuffer(ud->csound, ud->midiBuffer); |
| 1023 | ud->midiBuffer = nullptr; |
| 1024 | csoundDestroyCircularBuffer(ud->csound, ud->virtualMidiBuffer); |
| 1025 | ud->virtualMidiBuffer = nullptr; |
| 1026 | csoundDestroy(ud->csound); |
| 1027 | ud->csound = nullptr; |
| 1028 | #else |
| 1029 | csoundReset(ud->csound); |
| 1030 | #endif |
| 1031 | } |
| 1032 | |
| 1033 | void CsoundEngine::setupChannels() |
| 1034 | { |
| 1035 | ud->inputChannelNames.clear(); |
| 1036 | ud->outputChannelNames.clear(); |
| 1037 | ud->outputStringChannelNames.clear(); |
| 1038 | ud->previousOutputValues.clear(); |
| 1039 | ud->previousStringOutputValues.clear(); |
| 1040 | csoundSetInputChannelCallback(ud->csound, &CsoundEngine::inputValueCallback); |
| 1041 | csoundSetOutputChannelCallback(ud->csound, &CsoundEngine::outputValueCallback); |
| 1042 | // For chnget/chnset |
| 1043 | controlChannelInfo_t *channelList; |
| 1044 | int numChannels = csoundListChannels(ud->csound, &channelList); |
| 1045 | controlChannelInfo_t *entry = channelList; |
| 1046 | |
| 1047 | MYFLT *pvalue; |
| 1048 | QVector<QuteWidget *> widgets = ud->wl->getWidgets(); |
| 1049 | // Set channels values for existing channels (i.e. those declared with chn_* |
| 1050 | // in the csound header |
| 1051 | for (int i = 0; i < numChannels; i++) { |
| 1052 | // name type |
| 1053 | // if type is 0, no new channel is created if it does not exist, |
| 1054 | // the returned value is the channel type |
| 1055 | int chanType = csoundGetChannelPtr(ud->csound, &pvalue, entry->name, 0); |
| 1056 | if (chanType & CSOUND_INPUT_CHANNEL) { |
| 1057 | if ((chanType & CSOUND_CHANNEL_TYPE_MASK) == CSOUND_CONTROL_CHANNEL) { |
| 1058 | ud->wl->valueMutex.lock(); |
| 1059 | foreach (QuteWidget *w, widgets) { |
| 1060 | if (w->getChannelName() == QString(entry->name)) { |
| 1061 | ud->wl->newValues.insert(w->getChannelName(), w->getValue()); |
| 1062 | } |
| 1063 | if (w->getChannel2Name() == QString(entry->name)) { |
| 1064 | ud->wl->newValues.insert(w->getChannel2Name(), w->getValue2()); |
| 1065 | } |
| 1066 | } |
| 1067 | ud->wl->valueMutex.unlock(); |
| 1068 | } else if ((chanType & CSOUND_CHANNEL_TYPE_MASK) == CSOUND_STRING_CHANNEL) { |
| 1069 | ud->wl->stringValueMutex.lock(); |
| 1070 | foreach (QuteWidget *w, widgets) { |
| 1071 | if (w->getChannelName() == QString(entry->name)) { |
| 1072 | ud->wl->newStringValues.insert(w->getChannelName(), w->getStringValue()); |
| 1073 | } |
| 1074 | } |
| 1075 | ud->wl->stringValueMutex.unlock(); |
| 1076 | } |
| 1077 | } |
| 1078 | if (chanType & CSOUND_OUTPUT_CHANNEL) { // Channels can be input and output at the same time |
| 1079 | if ((chanType & CSOUND_CHANNEL_TYPE_MASK) == CSOUND_CONTROL_CHANNEL) { |
| 1080 | ud->outputChannelNames << QString(entry->name); |
| 1081 | ud->previousOutputValues << 0; |
| 1082 | foreach (QuteWidget *w, widgets) { |
| 1083 | if (w->getChannelName() == QString(entry->name)) { |
| 1084 | ud->previousOutputValues.last() = w->getValue(); |
| 1085 | continue; |
| 1086 | } |
| 1087 | if (w->getChannel2Name() == QString(entry->name)) { |
| 1088 | ud->previousOutputValues.last() = w->getValue2(); |
| 1089 | continue; |
| 1090 | } |
| 1091 | } |
| 1092 | } else if ((chanType & CSOUND_CHANNEL_TYPE_MASK) == CSOUND_STRING_CHANNEL) { |
| 1093 | ud->outputStringChannelNames << QString(entry->name); |
| 1094 | ud->previousStringOutputValues << ""; |
| 1095 | foreach (QuteWidget *w, widgets) { |
| 1096 | if (w->getChannelName() == QString(entry->name)) { |
| 1097 | ud->previousStringOutputValues.last() = w->getStringValue(); |
| 1098 | continue; |
| 1099 | } |
| 1100 | } |
| 1101 | } |
| 1102 | } |
| 1103 | entry++; |
| 1104 | } |
| 1105 | csoundDeleteChannelList(ud->csound, channelList); |
| 1106 | |
| 1107 | // Force creation of string channels for _Browse widgets |
| 1108 | foreach (QuteWidget *w, widgets) { |
| 1109 | if (w->getChannelName().startsWith("_Browse")) { |
| 1110 | csoundGetChannelPtr(ud->csound, &pvalue, w->getChannelName().toLocal8Bit(), |
| 1111 | CSOUND_INPUT_CHANNEL | CSOUND_OUTPUT_CHANNEL | CSOUND_STRING_CHANNEL); |
| 1112 | ud->wl->newStringValues.insert(w->getChannelName(), w->getStringValue()); |
| 1113 | } |
| 1114 | } |
| 1115 | } |
| 1116 | |
| 1117 | void CsoundEngine::messageListDispatcher(void *data) |
| 1118 | { |
| 1119 | CsoundUserData *ud_local = (CsoundUserData *) data; |
| 1120 | while (ud_local->runDispatcher) { |
| 1121 | ud_local->playMutex->lock(); |
| 1122 | if (ud_local->perfThread && (ud_local->perfThread->GetStatus() != 0)) { |
| 1123 | // In case score has ended |
| 1124 | ud_local->playMutex->unlock(); |
| 1125 | ud_local->csEngine->stop(); |
| 1126 | } else { |
| 1127 | ud_local->playMutex->unlock(); |
| 1128 | } |
| 1129 | CSOUND *csound = ud_local->csEngine->getCsound(); |
| 1130 | if (csound) { |
| 1131 | if (ud_local->wl) { |
| 1132 | ud_local->wl->getMouseValues(&ud_local->mouseValues); |
| 1133 | } |
| 1134 | int count = csoundGetMessageCnt(csound); |
| 1135 | ud_local->csEngine->m_messageMutex.lock(); |
| 1136 | for (int i = 0; i< count; i++) { |
| 1137 | ud_local->csEngine->messageQueue << csoundGetFirstMessage(csound); |
| 1138 | // FIXME: Is this thread safe? |
| 1139 | csoundPopFirstMessage(csound); |
| 1140 | } |
| 1141 | ud_local->csEngine->m_messageMutex.unlock(); |
| 1142 | } |
| 1143 | |
| 1144 | int counter = 0; |
| 1145 | ud_local->csEngine->m_messageMutex.lock(); |
| 1146 | while ((ud_local->csEngine->m_consoleBufferSize <= 0 || |
| 1147 | counter++ < ud_local->csEngine->m_consoleBufferSize)) { |
| 1148 | if (ud_local->csEngine->messageQueue.isEmpty()) { |
| 1149 | break; |
| 1150 | } |
| 1151 | QString msg = ud_local->csEngine->messageQueue.takeFirst(); |
| 1152 | // Must use signals to make things thread safe |
| 1153 | emit ud_local->csEngine->passMessages(msg); |
| 1154 | } |
| 1155 | if (!ud_local->csEngine->messageQueue.isEmpty() |
| 1156 | && ud_local->csEngine->m_consoleBufferSize > 0 |
| 1157 | && counter >= ud_local->csEngine->m_consoleBufferSize) { |
| 1158 | ud_local->csEngine->messageQueue.clear(); |
| 1159 | ud_local->csEngine->messageQueue << "\nCsoundQt: Message buffer overflow. Messages discarded!\n"; |
| 1160 | } |
| 1161 | ud_local->csEngine->m_messageMutex.unlock(); |
| 1162 | QThread::usleep(ud_local->msgRefreshTime); |
| 1163 | } |
| 1164 | } |
| 1165 | |
| 1166 | void CsoundEngine::flushQueues() |
| 1167 | { |
| 1168 | m_messageMutex.lock(); |
| 1169 | int count = csoundGetMessageCnt(ud->csound); |
| 1170 | for (int i = 0; i < count; i++) { |
| 1171 | messageQueue << csoundGetFirstMessage(ud->csound); |
| 1172 | csoundPopFirstMessage(ud->csound); |
| 1173 | } |
| 1174 | |
| 1175 | while (!messageQueue.isEmpty()) { |
| 1176 | QString msg = messageQueue.takeFirst(); |
| 1177 | ConsoleWidget *console = nullptr; |
| 1178 | for (int i = 0; i < consoles.size(); i++) { |
| 1179 | console = consoles[i]; |
| 1180 | console->appendMessage(msg); |
| 1181 | } |
| 1182 | ud->wl->appendMessage(msg); |
| 1183 | |
| 1184 | } |
| 1185 | m_messageMutex.unlock(); |
| 1186 | ud->wl->flushGraphBuffer(); |
| 1187 | } |
| 1188 | |
| 1189 | void CsoundEngine::queueMessage(QString message) |
| 1190 | { |
| 1191 | m_messageMutex.lock(); |
| 1192 | messageQueue << message; |
| 1193 | m_messageMutex.unlock(); |
| 1194 | } |
| 1195 | |
| 1196 | bool CsoundEngine::isRunning() |
| 1197 | { |
| 1198 | if(!m_playMutex.tryLock(1)) { |
| 1199 | qDebug() << "Could not acquire lock"; |
| 1200 | return false; |
| 1201 | } |
| 1202 | auto out = (ud->perfThread && (ud->perfThread->GetStatus() == 0)); |
| 1203 | m_playMutex.unlock(); |
| 1204 | return out; |
| 1205 | } |
| 1206 | |
| 1207 | bool CsoundEngine::isRecording() |
| 1208 | { |
| 1209 | return m_recording; |
| 1210 | } |
| 1211 | |
| 1212 | CSOUND *CsoundEngine::getCsound() |
| 1213 | { |
| 1214 | return ud->csound; |
| 1215 | } |
| 1216 | |
| 1217 | #ifdef QCS_PYTHONQT |
| 1218 | void CsoundEngine::registerProcessCallback(QString func, int skipPeriods) |
| 1219 | { |
| 1220 | ud->m_pythonCallback = func; |
| 1221 | ud->m_pythonCallbackSkip = skipPeriods; |
| 1222 | } |
| 1223 | |
| 1224 | void CsoundEngine::setPythonConsole(PythonConsole *pc) |
| 1225 | { |
| 1226 | ud->m_pythonConsole = pc; |
| 1227 | } |
| 1228 | #endif |
| 1229 | |
| 1230 | #ifdef QCS_DEBUGGER |
| 1231 | |
| 1232 | void CsoundEngine::breakpointCallback(CSOUND *csound, debug_bkpt_info_t *bkpt_info, void *udata) |
| 1233 | { |
| 1234 | debug_instr_t *debug_instr = bkpt_info->breakpointInstr; |
| 1235 | CsoundEngine *cs = (CsoundEngine *) udata; |
| 1236 | // Copy variable list |
| 1237 | debug_variable_t* vp = bkpt_info->instrVarList; |
| 1238 | cs->variableMutex.lock(); |
| 1239 | cs->m_varList.clear(); |
| 1240 | while (vp) { |
| 1241 | if (vp->name[0] != '#') { |
| 1242 | QVariantList varDetails; |
| 1243 | varDetails << vp->name; |
| 1244 | if (strcmp(vp->typeName, "i") == 0 |
| 1245 | || strcmp(vp->typeName, "k") == 0 |
| 1246 | || strcmp(vp->typeName, "r") == 0) { |
| 1247 | varDetails << *((MYFLT *) vp->data); |
| 1248 | } else if(strcmp(vp->typeName, "S") == 0) { |
| 1249 | varDetails << (char *) vp->data; |
| 1250 | } else if (strcmp(vp->typeName, "a") == 0) { |
| 1251 | MYFLT *data = (MYFLT *) vp->data; |
| 1252 | varDetails << *data << *(data + 1) |
| 1253 | << *(data + 2)<< *(data + 3); |
| 1254 | } else { |
| 1255 | varDetails << QVariant(); |
| 1256 | } |
| 1257 | varDetails << vp->typeName; |
| 1258 | cs->m_varList << varDetails; |
| 1259 | } |
| 1260 | vp = vp->next; |
| 1261 | } |
| 1262 | cs->variableMutex.unlock(); |
| 1263 | if (bkpt_info->currentOpcode) { |
| 1264 | cs->m_currentLine.store(bkpt_info->currentOpcode->line); |
| 1265 | } else { |
| 1266 | cs->m_currentLine.store(debug_instr->line); |
| 1267 | } |
| 1268 | |
| 1269 | debug_instr_t *debug_instr_list = bkpt_info->instrListHead; |
| 1270 | //Copy active instrument list |
| 1271 | cs->instrumentMutex.lock(); |
| 1272 | cs->m_instrumentList.clear(); |
| 1273 | while (debug_instr_list) { |
| 1274 | QVariantList instance; |
| 1275 | instance << debug_instr_list->p1; |
| 1276 | instance << QString("%1 %2").arg(debug_instr_list->p2).arg(debug_instr_list->p3); |
| 1277 | instance << (qulonglong)debug_instr_list->kcounter; |
| 1278 | cs->m_instrumentList << instance; |
| 1279 | debug_instr_list = debug_instr_list->next; |
| 1280 | } |
| 1281 | cs->instrumentMutex.unlock(); |
| 1282 | |
| 1283 | emit cs->breakpointReached(); |
| 1284 | } |
| 1285 | |
| 1286 | void CsoundEngine::setDebug() |
| 1287 | { |
| 1288 | if (isRunning()) { |
| 1289 | ud->perfThread->Pause(); |
| 1290 | csoundDebuggerInit(ud->csound); |
| 1291 | csoundSetBreakpointCallback(ud->csound, &CsoundEngine::breakpointCallback, (void *) this); |
| 1292 | // ud->perfThread->Play(); |
| 1293 | } |
| 1294 | m_debugging = true; |
| 1295 | } |
| 1296 | |
| 1297 | void CsoundEngine::pauseDebug() |
| 1298 | { |
| 1299 | if (isRunning() && m_debugging) { |
| 1300 | csoundDebugStop(ud->csound); |
| 1301 | } |
| 1302 | } |
| 1303 | |
| 1304 | void CsoundEngine::continueDebug() |
| 1305 | { |
| 1306 | if (isRunning() && m_debugging) { |
| 1307 | qDebug() ; |
| 1308 | csoundDebugContinue(ud->csound); |
| 1309 | } |
| 1310 | } |
| 1311 | |
| 1312 | void CsoundEngine::stopDebug() |
| 1313 | { |
| 1314 | if (isRunning() && m_debugging) { |
| 1315 | stop(); |
| 1316 | } |
| 1317 | m_debugging = false; |
| 1318 | } |
| 1319 | |
| 1320 | void CsoundEngine::addInstrumentBreakpoint(double instr, int skip) |
| 1321 | { |
| 1322 | if (isRunning() && m_debugging) { |
| 1323 | csoundSetInstrumentBreakpoint(ud->csound, instr, skip); |
| 1324 | } |
| 1325 | } |
| 1326 | |
| 1327 | void CsoundEngine::removeInstrumentBreakpoint(double instr) |
| 1328 | { |
| 1329 | if (isRunning() && m_debugging) { |
| 1330 | csoundRemoveInstrumentBreakpoint(ud->csound, instr); |
| 1331 | } |
| 1332 | } |
| 1333 | |
| 1334 | void CsoundEngine::addBreakpoint(int line, int instr, int skip) |
| 1335 | { |
| 1336 | if (isRunning() && m_debugging) { |
| 1337 | csoundSetBreakpoint(ud->csound, line, instr, skip); |
| 1338 | } |
| 1339 | } |
| 1340 | |
| 1341 | void CsoundEngine::removeBreakpoint(int line, int instr) |
| 1342 | { |
| 1343 | if (isRunning() && m_debugging) { |
| 1344 | csoundRemoveBreakpoint(ud->csound, line, instr); |
| 1345 | } |
| 1346 | } |
| 1347 | |
| 1348 | void CsoundEngine::setStartingBreakpoints(QVector<QVariantList> bps) |
| 1349 | { |
| 1350 | m_startBreakpoints = bps; |
| 1351 | } |
| 1352 | |
| 1353 | QVector<QVariantList> CsoundEngine::getVaribleList() |
| 1354 | { |
| 1355 | QVector<QVariantList> outList; |
| 1356 | variableMutex.lock(); |
| 1357 | outList = m_varList; |
| 1358 | variableMutex.unlock(); |
| 1359 | return outList; |
| 1360 | } |
| 1361 | |
| 1362 | QVector<QVariantList> CsoundEngine::getInstrumentList() |
| 1363 | { |
| 1364 | QVector<QVariantList> outList; |
| 1365 | instrumentMutex.lock(); |
| 1366 | outList = m_instrumentList; |
| 1367 | instrumentMutex.unlock(); |
| 1368 | return outList; |
| 1369 | } |
| 1370 | |
| 1371 | int CsoundEngine::getCurrentLine() |
| 1372 | { |
| 1373 | return m_currentLine.load(); |
| 1374 | } |
| 1375 | |
| 1376 | #endif |
| 1377 | |
| 1378 | CsoundUserData *CsoundEngine::getUserData() |
| 1379 | { |
| 1380 | return ud; |
| 1381 | } |
| 1382 | |
| 1383 | void CsoundEngine::clearConsoles(void) { |
| 1384 | for (int i = 1, n = consoles.size(); i < n; ++i) { |
| 1385 | consoles[i]->reset(); |
| 1386 | } |
| 1387 | } |