drumstick
1.0.2
|
00001 /* 00002 Drumstick RT (realtime MIDI In/Out) 00003 Copyright (C) 2009-2015 Pedro Lopez-Cabanillas <plcl@users.sf.net> 00004 00005 This program is free software; you can redistribute it and/or modify 00006 it under the terms of the GNU General Public License as published by 00007 the Free Software Foundation; either version 2 of the License, or 00008 (at your option) any later version. 00009 00010 This program is distributed in the hope that it will be useful, 00011 but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00013 GNU General Public License for more details. 00014 00015 You should have received a copy of the GNU General Public License along 00016 with this program; if not, write to the Free Software Foundation, Inc., 00017 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 00018 */ 00019 00020 #include <QObject> 00021 #include <QDebug> 00022 00023 #include "netmidiinput_p.h" 00024 #include "netmidiinput.h" 00025 00026 namespace drumstick { 00027 namespace rt { 00028 00029 static QString DEFAULT_PUBLIC_NAME(QLatin1String("MIDI In")); 00030 00031 NetMIDIInputPrivate::NetMIDIInputPrivate(QObject *parent) : QObject(parent), 00032 m_inp(qobject_cast<NetMIDIInput *>(parent)), 00033 m_out(0), 00034 m_socket(0), 00035 m_parser(0), 00036 m_thruEnabled(false), 00037 m_port(0), 00038 m_publicName(DEFAULT_PUBLIC_NAME), 00039 m_groupAddress(QHostAddress(STR_ADDRESS)) 00040 { 00041 for(int i=MULTICAST_PORT; i<LAST_PORT; ++i) { 00042 m_inputDevices << QString::number(i); 00043 } 00044 } 00045 00046 void NetMIDIInputPrivate::open(QString portName) 00047 { 00048 int p = m_inputDevices.indexOf(portName); 00049 if (p > -1) 00050 { 00051 m_socket = new QUdpSocket(); 00052 m_parser = new MIDIParser(m_inp); 00053 m_port = MULTICAST_PORT + p; 00054 m_currentInput = portName; 00055 m_socket->bind(QHostAddress::AnyIPv4, m_port, QUdpSocket::ShareAddress); 00056 m_socket->setSocketOption(QAbstractSocket::MulticastLoopbackOption, 0); 00057 m_socket->setSocketOption(QAbstractSocket::MulticastTtlOption, 1); 00058 if (m_iface.isValid()) { 00059 m_socket->joinMulticastGroup(m_groupAddress, m_iface); 00060 } else { 00061 m_socket->joinMulticastGroup(m_groupAddress); 00062 } 00063 connect(m_socket, SIGNAL(readyRead()), this, SLOT(processIncomingMessages())); 00064 //qDebug() << Q_FUNC_INFO << portName; 00065 } 00066 } 00067 00068 void NetMIDIInputPrivate::close() 00069 { 00070 delete m_socket; 00071 delete m_parser; 00072 m_socket = 0; 00073 m_parser = 0; 00074 m_currentInput.clear(); 00075 } 00076 00077 void NetMIDIInputPrivate::initialize(QSettings *settings) 00078 { 00079 if (settings != 0) { 00080 settings->beginGroup("Network"); 00081 QString ifaceName = settings->value("interface", QString()).toString(); 00082 QString address = settings->value("address", STR_ADDRESS).toString(); 00083 settings->endGroup(); 00084 if (!ifaceName.isEmpty()) { 00085 m_iface = QNetworkInterface::interfaceFromName(ifaceName); 00086 } 00087 if (!address.isEmpty()) { 00088 m_groupAddress.setAddress(address); 00089 } 00090 } 00091 } 00092 00093 void NetMIDIInputPrivate::setMIDIThruDevice(MIDIOutput* device) 00094 { 00095 m_out = device; 00096 if (m_parser != 0) { 00097 m_parser->setMIDIThruDevice(device); 00098 } 00099 } 00100 00101 void NetMIDIInputPrivate::processIncomingMessages() 00102 { 00103 while (m_socket->hasPendingDatagrams()) { 00104 QByteArray datagram; 00105 datagram.resize(m_socket->pendingDatagramSize()); 00106 m_socket->readDatagram(datagram.data(), datagram.size()); 00107 if (m_parser != 0) { 00108 m_parser->parse(datagram); 00109 } 00110 } 00111 } 00112 00113 }}