| 1 | /* |
| 2 | Copyright (C) 2008-2016 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 o ScoreEvent *event = 0; |
| 18 | while (!csound_event_queue.try_dequeue(event)) { |
| 19 | delete event; |
| 20 | } |
| 21 | char *score_text = 0; |
| 22 | while (!csound_score_queue.try_dequeue(score_text)) { |
| 23 | free(score_text); |
| 24 | } |
| 25 | f the GNU Lesser General Public |
| 26 | License along with Csound; if not, write to the Free Software |
| 27 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA |
| 28 | 02111-1307 USA |
| 29 | */ |
| 30 | |
| 31 | |
| 32 | #include "CsoundHtmlOnlyWrapper.h" |
| 33 | #include "csoundhtmlview.h" |
| 34 | #include "console.h" |
| 35 | #include <QApplication> |
| 36 | #include <QDebug> |
| 37 | |
| 38 | CsoundHtmlOnlyWrapper::CsoundHtmlOnlyWrapper(QObject *parent) : |
| 39 | QObject(parent), |
| 40 | message_callback(nullptr), |
| 41 | m_options(nullptr), |
| 42 | csoundHtmlView(nullptr), |
| 43 | console(nullptr) |
| 44 | { |
| 45 | csound.SetHostData(this); |
| 46 | // TEST: move SetMessageCallback to Start() and set it to 0 in stop |
| 47 | //csound.SetMessageCallback(CsoundHtmlOnlyWrapper::csoundMessageCallback_); |
| 48 | } |
| 49 | |
| 50 | CsoundHtmlOnlyWrapper::~CsoundHtmlOnlyWrapper() { |
| 51 | stop(); |
| 52 | } |
| 53 | |
| 54 | void CsoundHtmlOnlyWrapper::setCsoundHtmlView(CsoundHtmlView *csoundHtmlView_) { |
| 55 | csoundHtmlView = csoundHtmlView_; |
| 56 | } |
| 57 | |
| 58 | void CsoundHtmlOnlyWrapper::registerConsole(ConsoleWidget *console_){ |
| 59 | console = console_; |
| 60 | if (console != nullptr) { |
| 61 | connect(this, SIGNAL(passMessages(QString)), console, SLOT(appendMessage(QString)), Qt::UniqueConnection); |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | int CsoundHtmlOnlyWrapper::compileCsd(const QString &filename) { |
| 66 | return csound.CompileCsd(filename.toLocal8Bit()); |
| 67 | } |
| 68 | |
| 69 | int CsoundHtmlOnlyWrapper::compileCsdText(const QString &text) { |
| 70 | return csound.CompileCsdText(text.toLocal8Bit()); |
| 71 | } |
| 72 | |
| 73 | int CsoundHtmlOnlyWrapper::compileOrc(const QString &text) { |
| 74 | return csound.CompileOrc(text.toLocal8Bit()); |
| 75 | } |
| 76 | |
| 77 | double CsoundHtmlOnlyWrapper::evalCode(const QString &text) { |
| 78 | return csound.EvalCode(text.toLocal8Bit()); |
| 79 | } |
| 80 | |
| 81 | |
| 82 | double CsoundHtmlOnlyWrapper::get0dBFS() { |
| 83 | return csound.Get0dBFS(); //cs->Get0dBFS(); |
| 84 | } |
| 85 | |
| 86 | int CsoundHtmlOnlyWrapper::getApiVersion() { |
| 87 | return csound.GetAPIVersion(); |
| 88 | } |
| 89 | |
| 90 | double CsoundHtmlOnlyWrapper::getControlChannel(const QString &name) { |
| 91 | int result = 0; |
| 92 | double value = csound.GetControlChannel(name.toLocal8Bit(), &result); |
| 93 | return value; |
| 94 | } |
| 95 | |
| 96 | qint64 CsoundHtmlOnlyWrapper::getCurrentTimeSamples() { // FIXME: unknown type int64_t qint64 |
| 97 | return csound.GetCurrentTimeSamples(); |
| 98 | } |
| 99 | |
| 100 | QString CsoundHtmlOnlyWrapper::getEnv(const QString &name) { // not sure, if it works... test with setGlobalEnv |
| 101 | return csound.GetEnv(name.toLocal8Bit()); |
| 102 | } |
| 103 | |
| 104 | int CsoundHtmlOnlyWrapper::getKsmps() { |
| 105 | return csound.GetKsmps(); |
| 106 | } |
| 107 | |
| 108 | int CsoundHtmlOnlyWrapper::getNchnls() { |
| 109 | return csound.GetNchnls(); |
| 110 | } |
| 111 | |
| 112 | int CsoundHtmlOnlyWrapper::getNchnlsInput() { |
| 113 | return csound.GetNchnlsInput(); |
| 114 | } |
| 115 | |
| 116 | QString CsoundHtmlOnlyWrapper::getOutputName() { |
| 117 | return QString(csound.GetOutputName()); |
| 118 | } |
| 119 | |
| 120 | double CsoundHtmlOnlyWrapper::getScoreOffsetSeconds() { |
| 121 | return csound.GetScoreOffsetSeconds(); |
| 122 | } |
| 123 | |
| 124 | double CsoundHtmlOnlyWrapper::getScoreTime() { |
| 125 | return csound.GetScoreTime(); |
| 126 | } |
| 127 | |
| 128 | int CsoundHtmlOnlyWrapper::getSr() { |
| 129 | return csound.GetSr(); |
| 130 | } |
| 131 | |
| 132 | QString CsoundHtmlOnlyWrapper::getStringChannel(const QString &name) { |
| 133 | char buffer[0x100]; |
| 134 | csound.GetStringChannel(name.toLocal8Bit(), buffer); |
| 135 | return QString(buffer); |
| 136 | } |
| 137 | |
| 138 | int CsoundHtmlOnlyWrapper::getVersion() { |
| 139 | return csound.GetVersion(); |
| 140 | } |
| 141 | |
| 142 | bool CsoundHtmlOnlyWrapper::isPlaying() { |
| 143 | return csound.IsPlaying(); |
| 144 | } |
| 145 | |
| 146 | int CsoundHtmlOnlyWrapper::isScorePending() { |
| 147 | return csound.IsScorePending(); |
| 148 | } |
| 149 | |
| 150 | void CsoundHtmlOnlyWrapper::message(const QString &text) { |
| 151 | csound.Message("%s", text.toLocal8Bit().constData()); |
| 152 | } |
| 153 | |
| 154 | int CsoundHtmlOnlyWrapper::perform() { |
| 155 | // Perform in a separate thread of execution. |
| 156 | return csound.Perform(); |
| 157 | } |
| 158 | |
| 159 | int CsoundHtmlOnlyWrapper::readScore(const QString &text) { |
| 160 | csound.ReadScore(text.toLocal8Bit()); |
| 161 | return 0; |
| 162 | } |
| 163 | |
| 164 | void CsoundHtmlOnlyWrapper::reset() { |
| 165 | csound.Reset(); |
| 166 | } |
| 167 | |
| 168 | void CsoundHtmlOnlyWrapper::rewindScore() { |
| 169 | csound.RewindScore(); |
| 170 | } |
| 171 | |
| 172 | int CsoundHtmlOnlyWrapper::runUtility(const QString &command, int argc, char **argv) { |
| 173 | return csound.RunUtility(command.toLocal8Bit(), argc, argv); // probably does not work from JS due char ** |
| 174 | } |
| 175 | |
| 176 | int CsoundHtmlOnlyWrapper::scoreEvent(char opcode, const double *pfields, long field_count) { |
| 177 | csound.ScoreEvent(opcode, pfields, field_count); |
| 178 | return 0; |
| 179 | } |
| 180 | |
| 181 | void CsoundHtmlOnlyWrapper::setControlChannel(const QString &name, double value) { |
| 182 | csound.SetControlChannel(name.toLocal8Bit(), value); |
| 183 | } |
| 184 | |
| 185 | int CsoundHtmlOnlyWrapper::setGlobalEnv(const QString &name, const QString &value) { |
| 186 | return csound.SetGlobalEnv(name.toLocal8Bit(), value.toLocal8Bit()); |
| 187 | } |
| 188 | |
| 189 | void CsoundHtmlOnlyWrapper::setInput(const QString &name){ |
| 190 | csound.SetInput(name.toLocal8Bit()); |
| 191 | } |
| 192 | |
| 193 | void CsoundHtmlOnlyWrapper::setMessageCallback(QObject *callback){ |
| 194 | qDebug() << "CsoundHtmlOnlyWrapper::setMessageCallback: " << callback; |
| 195 | message_callback = callback; |
| 196 | message_callback->dumpObjectInfo(); |
| 197 | } |
| 198 | |
| 199 | int CsoundHtmlOnlyWrapper::setOption(const QString &name){ |
| 200 | return csound.SetOption(name.toLocal8Bit().data()); |
| 201 | } |
| 202 | |
| 203 | void CsoundHtmlOnlyWrapper::setOutput(const QString &name, const QString &type, const QString &format){ |
| 204 | csound.SetOutput(name.toLocal8Bit(), type.toLocal8Bit(), format.toLocal8Bit()); |
| 205 | } |
| 206 | |
| 207 | void CsoundHtmlOnlyWrapper::setScoreOffsetSeconds(double value){ |
| 208 | csound.SetScoreOffsetSeconds(value); |
| 209 | } |
| 210 | |
| 211 | void CsoundHtmlOnlyWrapper::setScorePending(bool value){ |
| 212 | csound.SetScorePending((int) value); |
| 213 | } |
| 214 | |
| 215 | void CsoundHtmlOnlyWrapper::setStringChannel(const QString &name, const QString &value){ |
| 216 | csound.SetStringChannel(name.toLocal8Bit(), value.toLocal8Bit().data()); |
| 217 | } |
| 218 | |
| 219 | int CsoundHtmlOnlyWrapper::start(){ |
| 220 | int result = 0; |
| 221 | // set options from CsoundQt |
| 222 | if (m_options) { |
| 223 | foreach (QString option, m_options->generateCmdLineFlagsList() ) { |
| 224 | setOption(option); |
| 225 | } |
| 226 | } |
| 227 | //TEST: set message callback here, not by init of the class |
| 228 | csound.SetMessageCallback(CsoundHtmlOnlyWrapper::csoundMessageCallback_); |
| 229 | result = csound.Start(); |
| 230 | return result; |
| 231 | } |
| 232 | |
| 233 | int CsoundHtmlOnlyWrapper::Start() { |
| 234 | if ( isPlaying() ) { |
| 235 | csound.Stop(); |
| 236 | csound.Join(); |
| 237 | // using stop() does not work since Cleanup and Reset are not needed |
| 238 | } |
| 239 | |
| 240 | start(); |
| 241 | return perform(); |
| 242 | } |
| 243 | |
| 244 | void CsoundHtmlOnlyWrapper::stop(){ |
| 245 | csound.Stop(); |
| 246 | csound.Join(); |
| 247 | csound.Cleanup(); |
| 248 | csound.Reset(); // DOES THIS PERHAPS changes options, so that jack is not any more used as audio driver |
| 249 | } |
| 250 | |
| 251 | double CsoundHtmlOnlyWrapper::tableGet(int table_number, int index){ |
| 252 | return csound.TableGet(table_number, index); |
| 253 | } |
| 254 | |
| 255 | int CsoundHtmlOnlyWrapper::tableLength(int table_number){ |
| 256 | return csound.TableLength(table_number); |
| 257 | } |
| 258 | |
| 259 | void CsoundHtmlOnlyWrapper::tableSet(int table_number, int index, double value){ |
| 260 | csound.TableSet(table_number, index, value); |
| 261 | } |
| 262 | |
| 263 | void CsoundHtmlOnlyWrapper::setOptions(CsoundOptions *options) |
| 264 | { |
| 265 | m_options = options; |
| 266 | } |
| 267 | |
| 268 | void CsoundHtmlOnlyWrapper::csoundMessageCallback_(CSOUND *csound, |
| 269 | int attributes, |
| 270 | const char *format, |
| 271 | va_list args) { |
| 272 | return reinterpret_cast<CsoundHtmlOnlyWrapper *>(csoundGetHostData(csound))->csoundMessageCallback(attributes, format, args); |
| 273 | } |
| 274 | |
| 275 | void CsoundHtmlOnlyWrapper::csoundMessageCallback(int attributes, |
| 276 | const char *format, |
| 277 | va_list args) |
| 278 | { |
| 279 | (void) attributes; |
| 280 | #ifdef USE_QT_GT_54 |
| 281 | QString message = QString::vasprintf(format, args); |
| 282 | #else |
| 283 | QString message; |
| 284 | message.sprintf(format, args); // NB! Should pass but not tested! |
| 285 | #endif |
| 286 | qDebug() << message; |
| 287 | if (console) { // protext against console==NULL |
| 288 | if (!console->isHidden()) { // otherwise crash on exit |
| 289 | passMessages(message); |
| 290 | } |
| 291 | } |
| 292 | for (int i = 0, n = message.length(); i < n; i++) { |
| 293 | auto c = message[i]; |
| 294 | if (c == '\n') { |
| 295 | QString code = "console.log(\"" + csoundMessageBuffer + "\\n\");"; |
| 296 | if (csoundHtmlView != nullptr) { |
| 297 | #ifdef USE_WEBKIT |
| 298 | //csoundHtmlView->webView->page()->mainFrame()->evaluateJavaScript(code); // this crashes, no messages to JS console from webkit |
| 299 | #else |
| 300 | |
| 301 | // csoundHtmlView->webView->page()->runJavaScript(code); // this crashes too on exit - FIXME! |
| 302 | #endif |
| 303 | } |
| 304 | csoundMessageBuffer.clear(); |
| 305 | } else { |
| 306 | csoundMessageBuffer.append(c); |
| 307 | } |
| 308 | } |
| 309 | } |
| 310 | |
| 311 |