Chris@654: /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */ Chris@654: Chris@654: /* Chris@654: Sonic Visualiser Chris@654: An audio file viewer and annotation editor. Chris@654: Centre for Digital Music, Queen Mary, University of London. Chris@654: Chris@654: This program is free software; you can redistribute it and/or Chris@654: modify it under the terms of the GNU General Public License as Chris@654: published by the Free Software Foundation; either version 2 of the Chris@654: License, or (at your option) any later version. See the file Chris@654: COPYING included with this distribution for more information. Chris@654: */ Chris@654: Chris@654: #ifndef SV_OSC_SCRIPT_H Chris@654: #define SV_OSC_SCRIPT_H Chris@654: Chris@654: #include Chris@654: #include Chris@654: #include Chris@654: Chris@654: #include "base/Debug.h" Chris@654: #include "base/StringBits.h" Chris@654: #include "data/osc/OSCQueue.h" Chris@654: #include "data/osc/OSCMessage.h" Chris@654: Chris@654: #include Chris@654: Chris@654: class OSCScript : public QThread Chris@654: { Chris@654: Q_OBJECT Chris@654: Chris@654: public: Chris@654: OSCScript(QString filename, OSCQueue *queue) : Chris@654: m_filename(filename), Chris@654: m_queue(queue), Chris@654: m_abandoning(false) { Chris@654: } Chris@654: Chris@654: void run() override { Chris@658: Chris@654: if (!m_queue) { Chris@654: SVCERR << "OSCScript: No OSC queue available" << endl; Chris@654: throw std::runtime_error("OSC queue not running"); Chris@654: } Chris@658: Chris@658: QFile f; Chris@658: QString reportedFilename; Chris@658: Chris@658: if (m_filename == "-") { Chris@658: f.open(stdin, QFile::ReadOnly | QFile::Text); Chris@658: reportedFilename = ""; Chris@658: } else { Chris@658: f.setFileName(m_filename); Chris@658: if (!f.open(QFile::ReadOnly | QFile::Text)) { Chris@658: SVCERR << "OSCScript: Failed to open script file \"" Chris@658: << m_filename << "\" for reading" << endl; Chris@658: throw std::runtime_error("OSC script file not found"); Chris@658: } Chris@658: reportedFilename = m_filename; Chris@658: } Chris@654: Chris@658: QTextStream str(&f); Chris@654: int lineno = 0; Chris@658: Chris@654: while (!str.atEnd() && !m_abandoning) { Chris@654: Chris@654: ++lineno; Chris@654: Chris@654: QString line = str.readLine().trimmed(); Chris@654: if (line == QString()) continue; Chris@654: Chris@654: if (line[0] == '#') { Chris@654: continue; Chris@654: Chris@654: } else if (line[0].isDigit()) { Chris@654: bool ok = false; Chris@654: float pause = line.toFloat(&ok); Chris@654: if (ok) { Chris@658: SVCERR << "OSCScript: " Chris@658: << reportedFilename << ":" << lineno Chris@654: << ": pausing for " << pause << " sec" << endl; Chris@654: msleep(unsigned(round(pause * 1000.0f))); Chris@654: continue; Chris@654: } else { Chris@658: SVCERR << "OSCScript: " Chris@658: << reportedFilename << ":" << lineno Chris@658: << ": warning: failed to parse sleep time, ignoring" Chris@654: << endl; Chris@658: continue; Chris@654: } Chris@654: Chris@654: } else if (line[0] == '/' && line.size() > 1) { Chris@654: QStringList parts = StringBits::splitQuoted(line, ' '); Chris@654: if (parts.empty()) { Chris@658: SVCERR << "OSCScript: " Chris@658: << reportedFilename << ":" << lineno Chris@654: << ": warning: empty command spec, ignoring" Chris@654: << endl; Chris@654: continue; Chris@654: } Chris@654: OSCMessage message; Chris@654: message.setMethod(parts[0].mid(1)); Chris@654: for (int i = 1; i < parts.size(); ++i) { Chris@654: message.addArg(parts[i]); Chris@654: } Chris@658: SVCERR << "OSCScript: " << reportedFilename << ":" << lineno Chris@654: << ": invoking: \"" << parts[0] << "\"" << endl; Chris@654: m_queue->postMessage(message); Chris@654: Chris@654: } else { Chris@658: SVCERR << "OSCScript: " << reportedFilename << ":" << lineno Chris@658: << ": warning: message expected, ignoring" << endl; Chris@654: } Chris@654: } Chris@654: Chris@658: SVCERR << "OSCScript: " << reportedFilename << ": finished" << endl; Chris@654: } Chris@654: Chris@654: void abandon() { Chris@654: m_abandoning = true; Chris@654: } Chris@654: Chris@654: private: Chris@654: QString m_filename; Chris@654: OSCQueue *m_queue; // I do not own this Chris@654: bool m_abandoning; Chris@654: }; Chris@654: Chris@654: #endif Chris@654: