drumstick  1.0.0
ossoutput.cpp
1 /*
2  Drumstick RT (realtime MIDI In/Out)
3  Copyright (C) 2009-2014 Pedro Lopez-Cabanillas <plcl@users.sf.net>
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; either version 2 of the License, or
8  (at your option) any later version.
9 
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License along
16  with this program; if not, write to the Free Software Foundation, Inc.,
17  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 */
19 
20 #include <QFile>
21 #include <QDir>
22 #include <QDebug>
23 
24 #include "ossoutput.h"
25 
26 namespace drumstick {
27 namespace rt {
28 
29 static QString DEFAULT_PUBLIC_NAME(QLatin1String("MIDI Out"));
30 
31 class OSSOutput::OSSOutputPrivate
32 {
33 public:
34  bool m_advanced;
35  QIODevice *m_device;
36  QString m_publicName;
37  QString m_currentOutput;
38  QStringList m_outputDevices;
39  QStringList m_excludedNames;
40 
41  OSSOutputPrivate() :
42  m_advanced(false),
43  m_device(0),
44  m_publicName(DEFAULT_PUBLIC_NAME)
45  {
46  reloadDeviceList();
47  }
48 
49  ~OSSOutputPrivate()
50  {
51  close();
52  }
53 
54  void reloadDeviceList(bool advanced = false)
55  {
56  QDir dir("/dev");
57  QStringList filters;
58  m_advanced = advanced;
59  filters << "dmmidi*" << "admmidi*";
60  if (advanced) {
61  filters << "midi*" << "amidi*";
62  }
63  dir.setNameFilters(filters);
64  dir.setFilter(QDir::System);
65  dir.setSorting(QDir::Name);
66  m_outputDevices.clear();
67  QFileInfoList listInfo = dir.entryInfoList();
68  foreach(const QFileInfo &info, listInfo) {
69  m_outputDevices << info.absoluteFilePath();
70  }
71  }
72 
73  void open(QString portName)
74  {
75  //qDebug() << Q_FUNC_INFO << portName;
76  m_device = new QFile(portName);
77  m_device->open(QIODevice::WriteOnly | QIODevice::Unbuffered);
78  m_currentOutput = portName;
79  }
80 
81  void close()
82  {
83  if (m_device != 0) {
84  m_device->close();
85  delete m_device;
86  m_device = 0;
87  }
88  m_currentOutput.clear();
89  }
90 
91  void sendMessage(int m0)
92  {
93  QByteArray m;
94  m.resize(1);
95  m[0] = m0;
96  sendMessage(m);
97  }
98 
99  void sendMessage(int m0, int m1)
100  {
101  QByteArray m;
102  m.resize(2);
103  m[0] = m0;
104  m[1] = m1;
105  sendMessage(m);
106  }
107 
108  void sendMessage(int m0, int m1, int m2)
109  {
110  QByteArray m;
111  m.resize(3);
112  m[0] = m0;
113  m[1] = m1;
114  m[2] = m2;
115  sendMessage(m);
116  }
117 
118  void sendMessage(const QByteArray& message )
119  {
120  if (m_device == 0) {
121  qDebug() << "qfile is null";
122  return;
123  }
124  m_device->write(message);
125  //m_device->flush();
126  }
127 };
128 
129 OSSOutput::OSSOutput(QObject *parent) : MIDIOutput(parent),
130  d(new OSSOutputPrivate)
131 {}
132 
133 OSSOutput::~OSSOutput()
134 {
135  delete d;
136 }
137 
138 void OSSOutput::initialize(QSettings *settings)
139 {
140  Q_UNUSED(settings)
141 }
142 
143 QString OSSOutput::backendName()
144 {
145  return QLatin1String("OSS");
146 }
147 
148 QString OSSOutput::publicName()
149 {
150  return d->m_publicName;
151 }
152 
153 void OSSOutput::setPublicName(QString name)
154 {
155  d->m_publicName = name;
156 }
157 
158 QStringList OSSOutput::connections(bool advanced)
159 {
160  d->reloadDeviceList(advanced);
161  return d->m_outputDevices;
162 }
163 
164 void OSSOutput::setExcludedConnections(QStringList conns)
165 {
166  Q_UNUSED(conns)
167 }
168 
169 void OSSOutput::open(QString name)
170 {
171  d->open(name);
172 }
173 
174 void OSSOutput::close()
175 {
176  d->close();
177 }
178 
179 QString OSSOutput::currentConnection()
180 {
181  return d->m_currentOutput;
182 }
183 
184 void OSSOutput::sendNoteOff(int chan, int note, int vel)
185 {
186  d->sendMessage(MIDI_STATUS_NOTEOFF + chan, note, vel);}
187 
188 void OSSOutput::sendNoteOn(int chan, int note, int vel)
189 {
190  d->sendMessage(MIDI_STATUS_NOTEON + chan, note, vel);
191 }
192 
193 void OSSOutput::sendKeyPressure(int chan, int note, int value)
194 {
195  d->sendMessage(MIDI_STATUS_KEYPRESURE + chan, note, value);
196 }
197 
198 void OSSOutput::sendController(int chan, int control, int value)
199 {
200  d->sendMessage(MIDI_STATUS_CONTROLCHANGE + chan, control, value);
201 }
202 
203 void OSSOutput::sendProgram(int chan, int program)
204 {
205  d->sendMessage(MIDI_STATUS_PROGRAMCHANGE + chan, program);
206 }
207 
208 void OSSOutput::sendChannelPressure(int chan, int value)
209 {
210  d->sendMessage(MIDI_STATUS_CHANNELPRESSURE + chan, value);
211 }
212 
213 void OSSOutput::sendPitchBend(int chan, int value)
214 {
215  d->sendMessage(MIDI_STATUS_PITCHBEND + chan, MIDI_LSB(value), MIDI_MSB(value));
216 }
217 
218 void OSSOutput::sendSysex(const QByteArray &data)
219 {
220  d->sendMessage(data);
221 }
222 
223 void OSSOutput::sendSystemMsg(const int status)
224 {
225  d->sendMessage(status);
226 }
227 
228 }}
The QObject class is the base class of all Qt objects.