drumstick  1.0.2
ossoutput.cpp
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 <QFile>
00021 #include <QDir>
00022 #include <QDebug>
00023 
00024 #include "ossoutput.h"
00025 
00026 namespace drumstick {
00027 namespace rt {
00028 
00029 static QString DEFAULT_PUBLIC_NAME(QLatin1String("MIDI Out"));
00030 
00031 class OSSOutput::OSSOutputPrivate
00032 {
00033 public:
00034     bool m_advanced;
00035     QIODevice *m_device;
00036     QString m_publicName;
00037     QString m_currentOutput;
00038     QStringList m_outputDevices;
00039     QStringList m_excludedNames;
00040 
00041     OSSOutputPrivate() :
00042         m_advanced(false),
00043         m_device(0),
00044         m_publicName(DEFAULT_PUBLIC_NAME)
00045     {
00046         reloadDeviceList();
00047     }
00048 
00049     ~OSSOutputPrivate()
00050     {
00051         close();
00052     }
00053 
00054     void reloadDeviceList(bool advanced = false)
00055     {
00056         QDir dir("/dev");
00057         QStringList filters;
00058         m_advanced = advanced;
00059         filters << "dmmidi*" << "admmidi*";
00060         if (advanced) {
00061             filters << "midi*" << "amidi*";
00062         }
00063         dir.setNameFilters(filters);
00064         dir.setFilter(QDir::System);
00065         dir.setSorting(QDir::Name);
00066         m_outputDevices.clear();
00067         QFileInfoList listInfo = dir.entryInfoList();
00068         foreach(const QFileInfo &info, listInfo) {
00069             m_outputDevices << info.absoluteFilePath();
00070         }
00071     }
00072 
00073     void open(QString portName)
00074     {
00075         //qDebug() << Q_FUNC_INFO << portName;
00076         m_device = new QFile(portName);
00077         m_device->open(QIODevice::WriteOnly | QIODevice::Unbuffered);
00078         m_currentOutput = portName;
00079     }
00080 
00081     void close()
00082     {
00083         if (m_device != 0) {
00084             m_device->close();
00085             delete m_device;
00086             m_device = 0;
00087         }
00088         m_currentOutput.clear();
00089     }
00090 
00091     void sendMessage(int m0)
00092     {
00093         QByteArray m;
00094         m.resize(1);
00095         m[0] = m0;
00096         sendMessage(m);
00097     }
00098 
00099     void sendMessage(int m0, int m1)
00100     {
00101         QByteArray m;
00102         m.resize(2);
00103         m[0] = m0;
00104         m[1] = m1;
00105         sendMessage(m);
00106     }
00107 
00108     void sendMessage(int m0, int m1, int m2)
00109     {
00110         QByteArray m;
00111         m.resize(3);
00112         m[0] = m0;
00113         m[1] = m1;
00114         m[2] = m2;
00115         sendMessage(m);
00116     }
00117 
00118     void sendMessage(const QByteArray& message )
00119     {
00120         if (m_device == 0) {
00121             qDebug() << "qfile is null";
00122             return;
00123         }
00124         m_device->write(message);
00125         //m_device->flush();
00126     }
00127 };
00128 
00129 OSSOutput::OSSOutput(QObject *parent) : MIDIOutput(parent),
00130     d(new OSSOutputPrivate)
00131 {}
00132 
00133 OSSOutput::~OSSOutput()
00134 {
00135     delete d;
00136 }
00137 
00138 void OSSOutput::initialize(QSettings *settings)
00139 {
00140     Q_UNUSED(settings)
00141 }
00142 
00143 QString OSSOutput::backendName()
00144 {
00145     return QLatin1String("OSS");
00146 }
00147 
00148 QString OSSOutput::publicName()
00149 {
00150     return d->m_publicName;
00151 }
00152 
00153 void OSSOutput::setPublicName(QString name)
00154 {
00155     d->m_publicName = name;
00156 }
00157 
00158 QStringList OSSOutput::connections(bool advanced)
00159 {
00160     d->reloadDeviceList(advanced);
00161     return d->m_outputDevices;
00162 }
00163 
00164 void OSSOutput::setExcludedConnections(QStringList conns)
00165 {
00166     Q_UNUSED(conns)
00167 }
00168 
00169 void OSSOutput::open(QString name)
00170 {
00171     d->open(name);
00172 }
00173 
00174 void OSSOutput::close()
00175 {
00176     d->close();
00177 }
00178 
00179 QString OSSOutput::currentConnection()
00180 {
00181     return d->m_currentOutput;
00182 }
00183 
00184 void OSSOutput::sendNoteOff(int chan, int note, int vel)
00185 {
00186     d->sendMessage(MIDI_STATUS_NOTEOFF + chan, note, vel);}
00187 
00188 void OSSOutput::sendNoteOn(int chan, int note, int vel)
00189 {
00190     d->sendMessage(MIDI_STATUS_NOTEON + chan, note, vel);
00191 }
00192 
00193 void OSSOutput::sendKeyPressure(int chan, int note, int value)
00194 {
00195     d->sendMessage(MIDI_STATUS_KEYPRESURE + chan, note, value);
00196 }
00197 
00198 void OSSOutput::sendController(int chan, int control, int value)
00199 {
00200     d->sendMessage(MIDI_STATUS_CONTROLCHANGE + chan, control, value);
00201 }
00202 
00203 void OSSOutput::sendProgram(int chan, int program)
00204 {
00205     d->sendMessage(MIDI_STATUS_PROGRAMCHANGE + chan, program);
00206 }
00207 
00208 void OSSOutput::sendChannelPressure(int chan, int value)
00209 {
00210     d->sendMessage(MIDI_STATUS_CHANNELPRESSURE + chan, value);
00211 }
00212 
00213 void OSSOutput::sendPitchBend(int chan, int value)
00214 {
00215     d->sendMessage(MIDI_STATUS_PITCHBEND + chan, MIDI_LSB(value), MIDI_MSB(value));
00216 }
00217 
00218 void OSSOutput::sendSysex(const QByteArray &data)
00219 {
00220     d->sendMessage(data);
00221 }
00222 
00223 void OSSOutput::sendSystemMsg(const int status)
00224 {
00225     d->sendMessage(status);
00226 }
00227 
00228 }}