comparison framework/OSCScript.h @ 654:4f53620962d9 osc-script

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