Mercurial > hg > svapp
comparison framework/OSCScript.h @ 673:d62fd61082a1
Merge from branch tuning-difference
author | Chris Cannam |
---|---|
date | Fri, 17 May 2019 09:46:22 +0100 |
parents | 94f518af106c |
children | 2915cc467ebf |
comparison
equal
deleted
inserted
replaced
665:e19c609a7bec | 673:d62fd61082a1 |
---|---|
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 if (!m_queue) { | |
43 SVCERR << "OSCScript: No OSC queue available" << endl; | |
44 throw std::runtime_error("OSC queue not running"); | |
45 } | |
46 | |
47 QFile f; | |
48 QString reportedFilename; | |
49 | |
50 if (m_filename == "-") { | |
51 f.open(stdin, QFile::ReadOnly | QFile::Text); | |
52 reportedFilename = "<stdin>"; | |
53 } else { | |
54 f.setFileName(m_filename); | |
55 if (!f.open(QFile::ReadOnly | QFile::Text)) { | |
56 SVCERR << "OSCScript: Failed to open script file \"" | |
57 << m_filename << "\" for reading" << endl; | |
58 throw std::runtime_error("OSC script file not found"); | |
59 } | |
60 reportedFilename = m_filename; | |
61 } | |
62 | |
63 QTextStream str(&f); | |
64 int lineno = 0; | |
65 | |
66 while (!str.atEnd() && !m_abandoning) { | |
67 | |
68 ++lineno; | |
69 | |
70 QString line = str.readLine().trimmed(); | |
71 if (line == QString()) continue; | |
72 | |
73 if (line[0] == '#') { | |
74 continue; | |
75 | |
76 } else if (line[0].isDigit()) { | |
77 bool ok = false; | |
78 float pause = line.toFloat(&ok); | |
79 if (ok) { | |
80 SVCERR << "OSCScript: " | |
81 << reportedFilename << ":" << lineno | |
82 << ": pausing for " << pause << " sec" << endl; | |
83 msleep(unsigned(round(pause * 1000.0f))); | |
84 continue; | |
85 } else { | |
86 SVCERR << "OSCScript: " | |
87 << reportedFilename << ":" << lineno | |
88 << ": warning: failed to parse sleep time, ignoring" | |
89 << endl; | |
90 continue; | |
91 } | |
92 | |
93 } else if (line[0] == '/' && line.size() > 1) { | |
94 QStringList parts = StringBits::splitQuoted(line, ' '); | |
95 if (parts.empty()) { | |
96 SVCERR << "OSCScript: " | |
97 << reportedFilename << ":" << lineno | |
98 << ": warning: empty command spec, ignoring" | |
99 << endl; | |
100 continue; | |
101 } | |
102 OSCMessage message; | |
103 message.setMethod(parts[0].mid(1)); | |
104 for (int i = 1; i < parts.size(); ++i) { | |
105 message.addArg(parts[i]); | |
106 } | |
107 SVCERR << "OSCScript: " << reportedFilename << ":" << lineno | |
108 << ": invoking: \"" << parts[0] << "\"" << endl; | |
109 m_queue->postMessage(message); | |
110 | |
111 } else { | |
112 SVCERR << "OSCScript: " << reportedFilename << ":" << lineno | |
113 << ": warning: message expected, ignoring" << endl; | |
114 } | |
115 } | |
116 | |
117 SVCERR << "OSCScript: " << reportedFilename << ": finished" << endl; | |
118 } | |
119 | |
120 void abandon() { | |
121 m_abandoning = true; | |
122 } | |
123 | |
124 private: | |
125 QString m_filename; | |
126 OSCQueue *m_queue; // I do not own this | |
127 bool m_abandoning; | |
128 }; | |
129 | |
130 #endif | |
131 |