annotate framework/OSCScript.h @ 715:ec8f21a20fa7

Use updated snap api
author Chris Cannam
date Thu, 17 Oct 2019 11:13:11 +0100
parents 94f518af106c
children 2915cc467ebf
rev   line source
Chris@654 1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
Chris@654 2
Chris@654 3 /*
Chris@654 4 Sonic Visualiser
Chris@654 5 An audio file viewer and annotation editor.
Chris@654 6 Centre for Digital Music, Queen Mary, University of London.
Chris@654 7
Chris@654 8 This program is free software; you can redistribute it and/or
Chris@654 9 modify it under the terms of the GNU General Public License as
Chris@654 10 published by the Free Software Foundation; either version 2 of the
Chris@654 11 License, or (at your option) any later version. See the file
Chris@654 12 COPYING included with this distribution for more information.
Chris@654 13 */
Chris@654 14
Chris@654 15 #ifndef SV_OSC_SCRIPT_H
Chris@654 16 #define SV_OSC_SCRIPT_H
Chris@654 17
Chris@654 18 #include <QThread>
Chris@654 19 #include <QFile>
Chris@654 20 #include <QTextStream>
Chris@654 21
Chris@654 22 #include "base/Debug.h"
Chris@654 23 #include "base/StringBits.h"
Chris@654 24 #include "data/osc/OSCQueue.h"
Chris@654 25 #include "data/osc/OSCMessage.h"
Chris@654 26
Chris@654 27 #include <stdexcept>
Chris@654 28
Chris@654 29 class OSCScript : public QThread
Chris@654 30 {
Chris@654 31 Q_OBJECT
Chris@654 32
Chris@654 33 public:
Chris@654 34 OSCScript(QString filename, OSCQueue *queue) :
Chris@654 35 m_filename(filename),
Chris@654 36 m_queue(queue),
Chris@654 37 m_abandoning(false) {
Chris@654 38 }
Chris@654 39
Chris@654 40 void run() override {
Chris@658 41
Chris@654 42 if (!m_queue) {
Chris@654 43 SVCERR << "OSCScript: No OSC queue available" << endl;
Chris@654 44 throw std::runtime_error("OSC queue not running");
Chris@654 45 }
Chris@658 46
Chris@658 47 QFile f;
Chris@658 48 QString reportedFilename;
Chris@658 49
Chris@658 50 if (m_filename == "-") {
Chris@658 51 f.open(stdin, QFile::ReadOnly | QFile::Text);
Chris@658 52 reportedFilename = "<stdin>";
Chris@658 53 } else {
Chris@658 54 f.setFileName(m_filename);
Chris@658 55 if (!f.open(QFile::ReadOnly | QFile::Text)) {
Chris@658 56 SVCERR << "OSCScript: Failed to open script file \""
Chris@658 57 << m_filename << "\" for reading" << endl;
Chris@658 58 throw std::runtime_error("OSC script file not found");
Chris@658 59 }
Chris@658 60 reportedFilename = m_filename;
Chris@658 61 }
Chris@654 62
Chris@658 63 QTextStream str(&f);
Chris@654 64 int lineno = 0;
Chris@658 65
Chris@654 66 while (!str.atEnd() && !m_abandoning) {
Chris@654 67
Chris@654 68 ++lineno;
Chris@654 69
Chris@654 70 QString line = str.readLine().trimmed();
Chris@654 71 if (line == QString()) continue;
Chris@654 72
Chris@654 73 if (line[0] == '#') {
Chris@654 74 continue;
Chris@654 75
Chris@654 76 } else if (line[0].isDigit()) {
Chris@654 77 bool ok = false;
Chris@654 78 float pause = line.toFloat(&ok);
Chris@654 79 if (ok) {
Chris@658 80 SVCERR << "OSCScript: "
Chris@658 81 << reportedFilename << ":" << lineno
Chris@654 82 << ": pausing for " << pause << " sec" << endl;
Chris@654 83 msleep(unsigned(round(pause * 1000.0f)));
Chris@654 84 continue;
Chris@654 85 } else {
Chris@658 86 SVCERR << "OSCScript: "
Chris@658 87 << reportedFilename << ":" << lineno
Chris@658 88 << ": warning: failed to parse sleep time, ignoring"
Chris@654 89 << endl;
Chris@658 90 continue;
Chris@654 91 }
Chris@654 92
Chris@654 93 } else if (line[0] == '/' && line.size() > 1) {
Chris@654 94 QStringList parts = StringBits::splitQuoted(line, ' ');
Chris@654 95 if (parts.empty()) {
Chris@658 96 SVCERR << "OSCScript: "
Chris@658 97 << reportedFilename << ":" << lineno
Chris@654 98 << ": warning: empty command spec, ignoring"
Chris@654 99 << endl;
Chris@654 100 continue;
Chris@654 101 }
Chris@654 102 OSCMessage message;
Chris@654 103 message.setMethod(parts[0].mid(1));
Chris@654 104 for (int i = 1; i < parts.size(); ++i) {
Chris@654 105 message.addArg(parts[i]);
Chris@654 106 }
Chris@658 107 SVCERR << "OSCScript: " << reportedFilename << ":" << lineno
Chris@654 108 << ": invoking: \"" << parts[0] << "\"" << endl;
Chris@654 109 m_queue->postMessage(message);
Chris@654 110
Chris@654 111 } else {
Chris@658 112 SVCERR << "OSCScript: " << reportedFilename << ":" << lineno
Chris@658 113 << ": warning: message expected, ignoring" << endl;
Chris@654 114 }
Chris@654 115 }
Chris@654 116
Chris@658 117 SVCERR << "OSCScript: " << reportedFilename << ": finished" << endl;
Chris@654 118 }
Chris@654 119
Chris@654 120 void abandon() {
Chris@654 121 m_abandoning = true;
Chris@654 122 }
Chris@654 123
Chris@654 124 private:
Chris@654 125 QString m_filename;
Chris@654 126 OSCQueue *m_queue; // I do not own this
Chris@654 127 bool m_abandoning;
Chris@654 128 };
Chris@654 129
Chris@654 130 #endif
Chris@654 131