annotate osc/OSCQueue.cpp @ 185:f3191ab6d564

* Add MIDI file export (closes FR#1643721)
author Chris Cannam
date Thu, 04 Oct 2007 11:52:38 +0000
parents 4a42776c34cd
children
rev   line source
Chris@94 1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
Chris@94 2
Chris@94 3 /*
Chris@94 4 Sonic Visualiser
Chris@94 5 An audio file viewer and annotation editor.
Chris@94 6 Centre for Digital Music, Queen Mary, University of London.
Chris@94 7
Chris@94 8 This program is free software; you can redistribute it and/or
Chris@94 9 modify it under the terms of the GNU General Public License as
Chris@94 10 published by the Free Software Foundation; either version 2 of the
Chris@94 11 License, or (at your option) any later version. See the file
Chris@94 12 COPYING included with this distribution for more information.
Chris@94 13 */
Chris@94 14
Chris@94 15 /*
Chris@94 16 This is a modified version of a source file from the
Chris@94 17 Rosegarden MIDI and audio sequencer and notation editor.
Chris@94 18 This file copyright 2000-2006 Chris Cannam and QMUL.
Chris@94 19 */
Chris@94 20
Chris@94 21 #include "OSCQueue.h"
Chris@94 22
Chris@94 23 #include <iostream>
Chris@94 24
Chris@94 25 #define OSC_MESSAGE_QUEUE_SIZE 1023
Chris@94 26
Chris@94 27 #ifdef HAVE_LIBLO
Chris@94 28
Chris@94 29 void
Chris@94 30 OSCQueue::oscError(int num, const char *msg, const char *path)
Chris@94 31 {
Chris@94 32 std::cerr << "ERROR: OSCQueue::oscError: liblo server error " << num
Chris@94 33 << " in path " << path << ": " << msg << std::endl;
Chris@94 34 }
Chris@94 35
Chris@94 36 int
Chris@94 37 OSCQueue::oscMessageHandler(const char *path, const char *types, lo_arg **argv,
Chris@94 38 int argc, lo_message, void *user_data)
Chris@94 39 {
Chris@94 40 OSCQueue *queue = static_cast<OSCQueue *>(user_data);
Chris@94 41
Chris@94 42 int target;
Chris@94 43 int targetData;
Chris@94 44 QString method;
Chris@94 45
Chris@94 46 if (!queue->parseOSCPath(path, target, targetData, method)) {
Chris@94 47 return 1;
Chris@94 48 }
Chris@94 49
Chris@94 50 OSCMessage message;
Chris@94 51 message.setTarget(target);
Chris@94 52 message.setTargetData(targetData);
Chris@94 53 message.setMethod(method);
Chris@94 54
Chris@94 55 int i = 0;
Chris@94 56
Chris@94 57 while (types && i < argc && types[i]) {
Chris@94 58
Chris@94 59 char type = types[i];
Chris@94 60 lo_arg *arg = argv[i];
Chris@94 61
Chris@94 62 switch (type) {
Chris@94 63 case 'i': message.addArg(arg->i); break;
Chris@170 64 // This conversion fails to compile in 64-bit environments
Chris@170 65 // at present, and we don't use the h type anyway so we
Chris@170 66 // can safely omit it
Chris@170 67 // case 'h': message.addArg(arg->h); break;
Chris@94 68 case 'f': message.addArg(arg->f); break;
Chris@94 69 case 'd': message.addArg(arg->d); break;
Chris@94 70 case 'c': message.addArg(arg->c); break;
Chris@94 71 case 't': message.addArg(arg->i); break;
Chris@94 72 case 's': message.addArg(&arg->s); break;
Chris@94 73 default: std::cerr << "WARNING: OSCQueue::oscMessageHandler: "
Chris@94 74 << "Unsupported OSC type '" << type << "'"
Chris@94 75 << std::endl;
Chris@94 76 break;
Chris@94 77 }
Chris@94 78
Chris@94 79 ++i;
Chris@94 80 }
Chris@94 81
Chris@94 82 queue->postMessage(message);
Chris@94 83 return 0;
Chris@94 84 }
Chris@94 85
Chris@94 86 #endif
Chris@94 87
Chris@94 88 OSCQueue::OSCQueue() :
Chris@94 89 #ifdef HAVE_LIBLO
Chris@94 90 m_thread(0),
Chris@94 91 #endif
Chris@94 92 m_buffer(OSC_MESSAGE_QUEUE_SIZE)
Chris@94 93 {
Chris@94 94 #ifdef HAVE_LIBLO
Chris@94 95 m_thread = lo_server_thread_new(NULL, oscError);
Chris@94 96
Chris@94 97 lo_server_thread_add_method(m_thread, NULL, NULL,
Chris@94 98 oscMessageHandler, this);
Chris@94 99
Chris@94 100 lo_server_thread_start(m_thread);
Chris@94 101
Chris@94 102 std::cout << "OSCQueue::OSCQueue: Base OSC URL is "
Chris@94 103 << lo_server_thread_get_url(m_thread) << std::endl;
Chris@94 104 #endif
Chris@94 105 }
Chris@94 106
Chris@94 107 OSCQueue::~OSCQueue()
Chris@94 108 {
Chris@94 109 #ifdef HAVE_LIBLO
Chris@94 110 if (m_thread) {
Chris@94 111 lo_server_thread_stop(m_thread);
Chris@94 112 }
Chris@94 113 #endif
Chris@94 114
Chris@94 115 while (m_buffer.getReadSpace() > 0) {
Chris@94 116 delete m_buffer.readOne();
Chris@94 117 }
Chris@94 118 }
Chris@94 119
Chris@94 120 bool
Chris@94 121 OSCQueue::isOK() const
Chris@94 122 {
Chris@94 123 #ifdef HAVE_LIBLO
Chris@94 124 return (m_thread != 0);
Chris@94 125 #else
Chris@94 126 return false;
Chris@94 127 #endif
Chris@94 128 }
Chris@94 129
Chris@94 130 QString
Chris@94 131 OSCQueue::getOSCURL() const
Chris@94 132 {
Chris@94 133 QString url = "";
Chris@94 134 #ifdef HAVE_LIBLO
Chris@94 135 url = lo_server_thread_get_url(m_thread);
Chris@94 136 #endif
Chris@94 137 return url;
Chris@94 138 }
Chris@94 139
Chris@94 140 size_t
Chris@94 141 OSCQueue::getMessagesAvailable() const
Chris@94 142 {
Chris@94 143 return m_buffer.getReadSpace();
Chris@94 144 }
Chris@94 145
Chris@94 146 OSCMessage
Chris@94 147 OSCQueue::readMessage()
Chris@94 148 {
Chris@94 149 OSCMessage *message = m_buffer.readOne();
Chris@94 150 OSCMessage rmessage = *message;
Chris@94 151 delete message;
Chris@94 152 return rmessage;
Chris@94 153 }
Chris@94 154
Chris@94 155 void
Chris@94 156 OSCQueue::postMessage(OSCMessage message)
Chris@94 157 {
Chris@94 158 int count = 0, max = 5;
Chris@94 159 while (m_buffer.getWriteSpace() == 0) {
Chris@94 160 if (count == max) {
Chris@94 161 std::cerr << "ERROR: OSCQueue::postMessage: OSC message queue is full and not clearing -- abandoning incoming message" << std::endl;
Chris@94 162 return;
Chris@94 163 }
Chris@94 164 std::cerr << "WARNING: OSCQueue::postMessage: OSC message queue (capacity " << m_buffer.getSize() << " is full!" << std::endl;
Chris@94 165 std::cerr << "Waiting for something to be processed" << std::endl;
Chris@94 166 #ifdef _WIN32
Chris@94 167 Sleep(1);
Chris@94 168 #else
Chris@94 169 sleep(1);
Chris@94 170 #endif
Chris@94 171 count++;
Chris@94 172 }
Chris@94 173
Chris@94 174 OSCMessage *mp = new OSCMessage(message);
Chris@94 175 m_buffer.write(&mp, 1);
Chris@94 176 std::cerr << "OSCQueue::postMessage: Posted OSC message: target "
Chris@94 177 << message.getTarget() << ", target data " << message.getTargetData()
Chris@94 178 << ", method " << message.getMethod().toStdString() << std::endl;
Chris@94 179 emit messagesAvailable();
Chris@94 180 }
Chris@94 181
Chris@94 182 bool
Chris@94 183 OSCQueue::parseOSCPath(QString path, int &target, int &targetData,
Chris@94 184 QString &method)
Chris@94 185 {
Chris@94 186 while (path.startsWith("/")) {
Chris@94 187 path = path.right(path.length()-1);
Chris@94 188 }
Chris@94 189
Chris@94 190 int i = 0;
Chris@94 191
Chris@94 192 bool ok = false;
Chris@94 193 target = path.section('/', i, i).toInt(&ok);
Chris@94 194
Chris@94 195 if (!ok) {
Chris@94 196 target = 0;
Chris@94 197 } else {
Chris@94 198 ++i;
Chris@94 199 targetData = path.section('/', i, i).toInt(&ok);
Chris@94 200 if (!ok) {
Chris@94 201 targetData = 0;
Chris@94 202 } else {
Chris@94 203 ++i;
Chris@94 204 }
Chris@94 205 }
Chris@94 206
Chris@94 207 method = path.section('/', i, -1);
Chris@94 208
Chris@94 209 if (method.contains('/')) {
Chris@94 210 std::cerr << "ERROR: OSCQueue::parseOSCPath: malformed path \""
Chris@94 211 << path.toStdString() << "\" (should be target/data/method or "
Chris@94 212 << "target/method or method, where target and data "
Chris@94 213 << "are numeric)" << std::endl;
Chris@94 214 return false;
Chris@94 215 }
Chris@94 216
Chris@94 217 std::cerr << "OSCQueue::parseOSCPath: good path \"" << path.toStdString()
Chris@94 218 << "\"" << std::endl;
Chris@94 219
Chris@94 220 return true;
Chris@94 221 }
Chris@94 222