1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
|
/*
Rosegarden
A sequencer and musical notation editor.
This program is Copyright 2000-2008
Guillaume Laurent <glaurent@telegraph-road.org>,
Chris Cannam <cannam@all-day-breakfast.com>,
Richard Bown <bownie@bownie.com>
The moral right of the authors to claim authorship of this work
has been asserted.
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License as
published by the Free Software Foundation; either version 2 of the
License, or (at your option) any later version. See the file
COPYING included with this distribution for more information.
*/
// Specialisation of SoundDriver to support ALSA (http://www.alsa-project.org)
//
//
#ifndef _ALSADRIVER_H_
#define _ALSADRIVER_H_
#include <vector>
#include <set>
#include <map>
#ifdef HAVE_ALSA
#include <alsa/asoundlib.h> // ALSA
#include "SoundDriver.h"
#include "Instrument.h"
#include "Device.h"
#include "AlsaPort.h"
#include "Scavenger.h"
#include "RunnablePluginInstance.h"
#ifdef HAVE_LIBJACK
#include "JackDriver.h"
#endif
namespace Rosegarden
{
class AlsaDriver : public SoundDriver
{
public:
AlsaDriver(MappedStudio *studio);
virtual ~AlsaDriver();
// shutdown everything that's currently open
void shutdown();
virtual bool initialise();
virtual void initialisePlayback(const RealTime &position);
virtual void stopPlayback();
virtual void punchOut();
virtual void resetPlayback(const RealTime &oldPosition, const RealTime &position);
virtual void allNotesOff();
virtual void processNotesOff(const RealTime &time, bool now, bool everything = false);
virtual RealTime getSequencerTime();
virtual MappedComposition *getMappedComposition();
virtual bool record(RecordStatus recordStatus,
const std::vector<InstrumentId> *armedInstruments = 0,
const std::vector<TQString> *audioFileNames = 0);
virtual void startClocks();
virtual void startClocksApproved(); // called by JACK driver in sync mode
virtual void stopClocks();
virtual bool areClocksRunning() const { return m_queueRunning; }
virtual void processEventsOut(const MappedComposition &mC);
virtual void processEventsOut(const MappedComposition &mC,
const RealTime &sliceStart,
const RealTime &sliceEnd);
// Return the sample rate
//
virtual unsigned int getSampleRate() const {
#ifdef HAVE_LIBJACK
if (m_jackDriver) return m_jackDriver->getSampleRate();
else return 0;
#else
return 0;
#endif
}
// Define here to catch this being reset
//
virtual void setMIDIClockInterval(RealTime interval);
// initialise subsystems
//
bool initialiseMidi();
void initialiseAudio();
// Some stuff to help us debug this
//
void getSystemInfo();
void showQueueStatus(int queue);
// Process pending
//
virtual void processPending();
// We can return audio control signals to the gui using MappedEvents.
// Meter levels or audio file completions can go in here.
//
void insertMappedEventForReturn(MappedEvent *mE);
virtual RealTime getAudioPlayLatency() {
#ifdef HAVE_LIBJACK
if (m_jackDriver) return m_jackDriver->getAudioPlayLatency();
#endif
return RealTime::zeroTime;
}
virtual RealTime getAudioRecordLatency() {
#ifdef HAVE_LIBJACK
if (m_jackDriver) return m_jackDriver->getAudioRecordLatency();
#endif
return RealTime::zeroTime;
}
virtual RealTime getInstrumentPlayLatency(InstrumentId id) {
#ifdef HAVE_LIBJACK
if (m_jackDriver) return m_jackDriver->getInstrumentPlayLatency(id);
#endif
return RealTime::zeroTime;
}
virtual RealTime getMaximumPlayLatency() {
#ifdef HAVE_LIBJACK
if (m_jackDriver) return m_jackDriver->getMaximumPlayLatency();
#endif
return RealTime::zeroTime;
}
// Plugin instance management
//
virtual void setPluginInstance(InstrumentId id,
TQString identifier,
int position) {
#ifdef HAVE_LIBJACK
if (m_jackDriver) m_jackDriver->setPluginInstance(id, identifier, position);
#endif
}
virtual void removePluginInstance(InstrumentId id, int position) {
#ifdef HAVE_LIBJACK
if (m_jackDriver) m_jackDriver->removePluginInstance(id, position);
#endif
}
// Remove all plugin instances
//
virtual void removePluginInstances() {
#ifdef HAVE_LIBJACK
if (m_jackDriver) m_jackDriver->removePluginInstances();
#endif
}
virtual void setPluginInstancePortValue(InstrumentId id,
int position,
unsigned long portNumber,
float value) {
#ifdef HAVE_LIBJACK
if (m_jackDriver) m_jackDriver->setPluginInstancePortValue(id, position, portNumber, value);
#endif
}
virtual float getPluginInstancePortValue(InstrumentId id,
int position,
unsigned long portNumber) {
#ifdef HAVE_LIBJACK
if (m_jackDriver) return m_jackDriver->getPluginInstancePortValue(id, position, portNumber);
#endif
return 0;
}
virtual void setPluginInstanceBypass(InstrumentId id,
int position,
bool value) {
#ifdef HAVE_LIBJACK
if (m_jackDriver) m_jackDriver->setPluginInstanceBypass(id, position, value);
#endif
}
virtual TQStringList getPluginInstancePrograms(InstrumentId id,
int position) {
#ifdef HAVE_LIBJACK
if (m_jackDriver) return m_jackDriver->getPluginInstancePrograms(id, position);
#endif
return TQStringList();
}
virtual TQString getPluginInstanceProgram(InstrumentId id,
int position) {
#ifdef HAVE_LIBJACK
if (m_jackDriver) return m_jackDriver->getPluginInstanceProgram(id, position);
#endif
return TQString();
}
virtual TQString getPluginInstanceProgram(InstrumentId id,
int position,
int bank,
int program) {
#ifdef HAVE_LIBJACK
if (m_jackDriver) return m_jackDriver->getPluginInstanceProgram(id, position, bank, program);
#endif
return TQString();
}
virtual unsigned long getPluginInstanceProgram(InstrumentId id,
int position,
TQString name) {
#ifdef HAVE_LIBJACK
if (m_jackDriver) return m_jackDriver->getPluginInstanceProgram(id, position, name);
#endif
return 0;
}
virtual void setPluginInstanceProgram(InstrumentId id,
int position,
TQString program) {
#ifdef HAVE_LIBJACK
if (m_jackDriver) m_jackDriver->setPluginInstanceProgram(id, position, program);
#endif
}
virtual TQString configurePlugin(InstrumentId id,
int position,
TQString key,
TQString value) {
#ifdef HAVE_LIBJACK
if (m_jackDriver) return m_jackDriver->configurePlugin(id, position, key, value);
#endif
return TQString();
}
virtual void setAudioBussLevels(int bussId,
float dB,
float pan) {
#ifdef HAVE_LIBJACK
if (m_jackDriver) m_jackDriver->setAudioBussLevels(bussId, dB, pan);
#endif
}
virtual void setAudioInstrumentLevels(InstrumentId instrument,
float dB,
float pan) {
#ifdef HAVE_LIBJACK
if (m_jackDriver) m_jackDriver->setAudioInstrumentLevels(instrument, dB, pan);
#endif
}
virtual void claimUnwantedPlugin(void *plugin);
virtual void scavengePlugins();
virtual bool checkForNewClients();
virtual void setLoop(const RealTime &loopStart, const RealTime &loopEnd);
virtual void sleep(const RealTime &);
// ----------------------- End of Virtuals ----------------------
// Create and send an MMC command
//
void sendMMC(MidiByte deviceId,
MidiByte instruction,
bool isCommand,
const std::string &data);
// Check whether the given event is an MMC command we need to act on
// (and if so act on it)
//
bool testForMMCSysex(const snd_seq_event_t *event);
// Create and enqueue a batch of MTC quarter-frame events
//
void insertMTCTQFrames(RealTime sliceStart, RealTime sliceEnd);
// Create and enqueue an MTC full-frame system exclusive event
//
void insertMTCFullFrame(RealTime time);
// Parse and accept an incoming MTC quarter-frame event
//
void handleMTCTQFrame(unsigned int data_byte, RealTime the_time);
// Check whether the given event is an MTC sysex we need to act on
// (and if so act on it)
//
bool testForMTCSysex(const snd_seq_event_t *event);
// Adjust the ALSA clock skew for MTC lock
//
void tweakSkewForMTC(int factor);
// Recalibrate internal MTC factors
//
void calibrateMTC();
// Send a System message straight away
//
void sendSystemDirect(MidiByte command, int *arg);
// Scheduled system message with arguments
//
void sendSystemQueued(MidiByte command,
const std::string &args,
const RealTime &time);
// Set the record device
//
void setRecordDevice(DeviceId id, bool connectAction);
void unsetRecordDevices();
virtual bool canReconnect(Device::DeviceType type);
virtual DeviceId addDevice(Device::DeviceType type,
MidiDevice::DeviceDirection direction);
virtual void removeDevice(DeviceId id);
virtual void renameDevice(DeviceId id, TQString name);
// Get available connections per device
//
virtual unsigned int getConnections(Device::DeviceType type,
MidiDevice::DeviceDirection direction);
virtual TQString getConnection(Device::DeviceType type,
MidiDevice::DeviceDirection direction,
unsigned int connectionNo);
virtual void setConnection(DeviceId deviceId, TQString connection);
virtual void setPlausibleConnection(DeviceId deviceId, TQString connection);
virtual unsigned int getTimers();
virtual TQString getTimer(unsigned int);
virtual TQString getCurrentTimer();
virtual void setCurrentTimer(TQString);
virtual void getAudioInstrumentNumbers(InstrumentId &audioInstrumentBase,
int &audioInstrumentCount) {
audioInstrumentBase = AudioInstrumentBase;
#ifdef HAVE_LIBJACK
audioInstrumentCount = AudioInstrumentCount;
#else
audioInstrumentCount = 0;
#endif
}
virtual void getSoftSynthInstrumentNumbers(InstrumentId &ssInstrumentBase,
int &ssInstrumentCount) {
ssInstrumentBase = SoftSynthInstrumentBase;
#ifdef HAVE_DSSI
ssInstrumentCount = SoftSynthInstrumentCount;
#else
ssInstrumentCount = 0;
#endif
}
virtual TQString getStatusLog();
// To be called regularly from JACK driver when idle
void checkTimerSync(size_t frames);
virtual void runTasks();
// Report a failure back to the GUI
//
virtual void reportFailure(MappedEvent::FailureCode code);
protected:
typedef std::vector<AlsaPortDescription *> AlsaPortList;
ClientPortPair getFirstDestination(bool duplex);
ClientPortPair getPairForMappedInstrument(InstrumentId id);
int getOutputPortForMappedInstrument(InstrumentId id);
std::map<unsigned int, std::map<unsigned int, MappedEvent*> > m_noteOnMap;
/**
* Bring m_alsaPorts up-to-date; if newPorts is non-null, also
* return the new ports (not previously in m_alsaPorts) through it
*/
virtual void generatePortList(AlsaPortList *newPorts = 0);
virtual void generateInstruments();
virtual void generateTimerList();
virtual std::string getAutoTimer(bool &wantTimerChecks);
void addInstrumentsForDevice(MappedDevice *device);
MappedDevice *createMidiDevice(AlsaPortDescription *,
MidiDevice::DeviceDirection);
virtual void processMidiOut(const MappedComposition &mC,
const RealTime &sliceStart,
const RealTime &sliceEnd);
virtual void processSoftSynthEventOut(InstrumentId id,
const snd_seq_event_t *event,
bool now);
virtual bool isRecording(AlsaPortDescription *port);
virtual void processAudioQueue(bool /* now */) { }
virtual void setConnectionToDevice(MappedDevice &device, TQString connection);
virtual void setConnectionToDevice(MappedDevice &device, TQString connection,
const ClientPortPair &pair);
private:
RealTime getAlsaTime();
// Locally convenient to control our devices
//
void sendDeviceController(DeviceId device,
MidiByte byte1,
MidiByte byte2);
int checkAlsaError(int rc, const char *message);
AlsaPortList m_alsaPorts;
// ALSA MIDI/Sequencer stuff
//
snd_seq_t *m_midiHandle;
int m_client;
int m_inputPort;
typedef std::map<DeviceId, int> DeviceIntMap;
DeviceIntMap m_outputPorts;
int m_syncOutputPort;
int m_controllerPort;
int m_queue;
int m_maxClients;
int m_maxPorts;
int m_maxQueues;
// Because this can fail even if the driver's up (if
// another service is using the port say)
//
bool m_midiInputPortConnected;
bool m_midiSyncAutoConnect;
RealTime m_alsaPlayStartTime;
RealTime m_alsaRecordStartTime;
RealTime m_loopStartTime;
RealTime m_loopEndTime;
// MIDI Time Code handling:
unsigned int m_eat_mtc;
// Received/emitted MTC data breakdown:
RealTime m_mtcReceiveTime;
RealTime m_mtcEncodedTime;
int m_mtcFrames;
int m_mtcSeconds;
int m_mtcMinutes;
int m_mtcHours;
int m_mtcSMPTEType;
// Calculated MTC factors:
int m_mtcFirstTime;
RealTime m_mtcLastEncoded;
RealTime m_mtcLastReceive;
long long int m_mtcSigmaE;
long long int m_mtcSigmaC;
unsigned int m_mtcSkew;
bool m_looping;
bool m_haveShutdown;
#ifdef HAVE_LIBJACK
JackDriver *m_jackDriver;
#endif
Scavenger<RunnablePluginInstance> m_pluginScavenger;
//!!! -- hoist to SoundDriver w/setter?
typedef std::set<InstrumentId> InstrumentSet;
InstrumentSet m_recordingInstruments;
typedef std::map<DeviceId, ClientPortPair> DevicePortMap;
DevicePortMap m_devicePortMap;
typedef std::map<ClientPortPair, DeviceId> PortDeviceMap;
PortDeviceMap m_suspendedPortMap;
std::string getPortName(ClientPortPair port);
ClientPortPair getPortByName(std::string name);
DeviceId getSpareDeviceId();
struct AlsaTimerInfo {
int clas;
int sclas;
int card;
int device;
int subdevice;
std::string name;
long resolution;
};
std::vector<AlsaTimerInfo> m_timers;
std::string m_currentTimer;
// This auxiliary queue is here as a hack, to avoid stuck notes if
// resetting playback while a note-off is currently in the ALSA
// queue. When playback is reset by ffwd or rewind etc, we drop
// all the queued events (which is generally what is desired,
// except for note offs) and reset the queue timer (so the note
// offs would have the wrong time stamps even if we hadn't dropped
// them). Thus, we need to re-send any recent note offs before
// continuing. This queue records which note offs have been
// added to the ALSA queue recently.
//
NoteOffQueue m_recentNoteOffs;
void pushRecentNoteOffs(); // move from recent to normal queue after reset
void cropRecentNoteOffs(const RealTime &t); // remove old note offs
void weedRecentNoteOffs(unsigned int pitch, MidiByte channel,
InstrumentId instrument); // on subsequent note on
bool m_queueRunning;
bool m_portCheckNeeded;
enum { NeedNoJackStart, NeedJackReposition, NeedJackStart } m_needJackStart;
bool m_doTimerChecks;
bool m_firstTimerCheck;
double m_timerRatio;
bool m_timerRatioCalculated;
std::string getAlsaModuleVersionString();
std::string getKernelVersionString();
void extractVersion(std::string vstr, int &major, int &minor, int &subminor, std::string &suffix);
bool versionIsAtLeast(std::string vstr, int major, int minor, int subminor);
};
}
#endif // HAVE_ALSA
#endif // _ALSADRIVER_H_
|