summaryrefslogtreecommitdiffstats
path: root/src/sequencer/RosegardenSequencerApp.h
blob: 2dc01380b65b2e93c685199defeff49a8c7e8f27 (plain)
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

/* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */

/*
    Rosegarden
    A MIDI and audio sequencer and musical notation editor.

    This program is Copyright 2000-2008
        Guillaume Laurent   <[email protected]>,
        Chris Cannam        <[email protected]>,
        Richard Bown        <[email protected]>

    The moral rights of Guillaume Laurent, Chris Cannam, and Richard
    Bown to claim authorship of this work have been asserted.

    Other copyrights also apply to some parts of this work.  Please
    see the AUTHORS file and individual file headers for details.

    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.
*/

#ifndef _ROSEGARDEN_SEQUENCER_APP_H_
#define _ROSEGARDEN_SEQUENCER_APP_H_
 
// RosegardenSequencerApp is the sequencer application for Rosegarden.
// It owns a Sequencer object which wraps the ALSA 
// and JACK funtionality.  At this level we deal with comms with
// the Rosegarden GUI application, the high level marshalling of data 
// and main event loop of the sequencer.  [rwb]
//


// include files for TQt
#include <tqstrlist.h>

// include files for KDE 
#include <kapp.h>
#include <tdemainwindow.h>
#include <tdeaccel.h>

#include <tqtimer.h>

#include "base/Composition.h"
#include "gui/application/RosegardenDCOP.h"

#include "RosegardenSequencerIface.h"

#include "sound/MappedComposition.h"
#include "base/Event.h"
#include "sound/MappedStudio.h"
#include "sound/ExternalTransport.h"

#include "MmappedSegment.h"
#include "SequencerMmapper.h"

#include <deque>

class KURL;
class TDERecentFilesAction;

namespace Rosegarden { 

// forward declaration of the RosegardenGUI classes
class RosegardenGUIDoc;
class RosegardenGUIView;
class ControlBlockMmapper;

class MappedInstrument;
class SoundDriver;

/**
 * The sequencer application
 */
class RosegardenSequencerApp : public TDEMainWindow,
                               virtual public RosegardenSequencerIface,
                               public ExternalTransport
{
    Q_OBJECT
  

public:
    RosegardenSequencerApp();
    ~RosegardenSequencerApp();

    //      -------- START OF DCOP INTERFACE METHODS --------
    //
    //


    // Quit
    virtual void quit();

    // Based on RealTime timestamps
    //
    int play(const RealTime &position,
             const RealTime &readAhead,
             const RealTime &audioMix,
             const RealTime &audioRead,
             const RealTime &audioWrite,
             long smallFileSize);

    // recording
    int record(const RealTime &position,
               const RealTime &readAhead,
               const RealTime &audioMix,
               const RealTime &audioRead,
               const RealTime &audioWrite,
               long smallFileSize,
               long recordMode);

    virtual int punchOut();

    // looping
    void setLoop(const RealTime &loopStart,
                 const RealTime &loopEnd);


    // Play wrapper for DCOP
    //
    virtual int play(long timeSec,
                     long timeNsec,
                     long readAheadSec,
                     long readAheadNsec,
                     long audioMixSec,
                     long audioMixNsec,
                     long audioReadSec,
                     long audioReadNsec,
                     long audioWriteSec,
                     long audioWriteNsec,
                     long smallFileSize);

    // Record wrapper for DCOP
    //
    virtual int record(long timeSec,
                       long timeNsec,
                       long readAheadSec,
                       long readAheadNsec,
                       long audioMixSec,
                       long audioMixNsec,
                       long audioReadSec,
                       long audioReadNsec,
                       long audioWriteSec,
                       long audioWriteNsec,
                       long smallFileSize,
                       long recordMode);

    
    // Jump to a pointer in the playback (uses longs instead
    // of RealTime for DCOP)
    //
    //
    virtual void jumpTo(long posSec, long posNsec);

    // Set a loop on the Sequencer
    //
    virtual void setLoop(long loopStartSec, long loopStartNsec,
                         long loopEndSec, long loopEndNsec);
 
    // Return the Sound system status (audio/MIDI)
    //
    virtual unsigned int getSoundDriverStatus(const TQString &guiVersion);

    // Add and remove Audio files on the sequencer
    //
    virtual int addAudioFile(const TQString &fileName, int id);
    virtual int removeAudioFile(int id);

    // Deletes all the audio files and clears down any flapping i/o handles
    //
    virtual void clearAllAudioFiles();

    // stops the sequencer
    //
    virtual void stop();

    // Set a MappedInstrument at the Sequencer
    //
    virtual void setMappedInstrument(int type, unsigned char channel,
                                     unsigned int id);

    // The sequencer will process the MappedComposition as soon as it
    // gets the chance.
    //
    virtual void processSequencerSlice(MappedComposition mC);

    // Yeuch!
    //
    virtual void processMappedEvent(unsigned int id,
                                    int type,
                                    unsigned char pitch,
                                    unsigned char velocity,
                                    long absTimeSec,
                                    long absTimeNsec,
                                    long durationSec,
                                    long durationNsec,
                                    long audioStartMarkerSec,
                                    long audioStartMarkerNsec);

    // And now do it properly
    //
    virtual void processMappedEvent(MappedEvent mE);

    virtual unsigned int getDevices();
    virtual MappedDevice getMappedDevice(unsigned int id);

    virtual int canReconnect(int deviceType);
    virtual unsigned int addDevice(int type, unsigned int direction);
    virtual void removeDevice(unsigned int id);
    virtual void renameDevice(unsigned int id, TQString name);
    virtual unsigned int getConnections(int type, unsigned int direction);
    virtual TQString getConnection(int type, unsigned int direction,
                                  unsigned int connectionNo);
    virtual void setConnection(unsigned int deviceId, TQString connection);
    virtual void setPlausibleConnection(unsigned int deviceId,
                                        TQString idealConnection);
    
    virtual unsigned int getTimers();
    virtual TQString getTimer(unsigned int n);
    virtual TQString getCurrentTimer();
    virtual void setCurrentTimer(TQString timer);

    virtual void setLowLatencyMode(bool);

    // Audio latencies
    //
    virtual MappedRealTime getAudioPlayLatency();
    virtual MappedRealTime getAudioRecordLatency();

    // Set a MappedObject 
    //
    virtual void setMappedProperty(int id,
                                   const TQString &property,
                                   float value);

    // Set many properties on many MappedObjects
    //
    virtual void setMappedProperties(const MappedObjectIdList &ids,
                                     const MappedObjectPropertyList &properties,
                                     const MappedObjectValueList &values);

    // Set a MappedObject to a string
    //
    virtual void setMappedProperty(int id,
                                   const TQString &property,
                                   const TQString &value);

    // Set a MappedObject to a property list
    //
    virtual void setMappedPropertyList(int id,
                                       const TQString &property,
                                       const MappedObjectPropertyList &values);

    // Get a MappedObject for a type
    //
    virtual int getMappedObjectId(int type);

    // Get a Property list from an Object
    //
    virtual std::vector<TQString> getPropertyList(int id,
                                                 const TQString &property);

    virtual std::vector<TQString> getPluginInformation();

    virtual TQString getPluginProgram(int id, int bank, int program);

    virtual unsigned long getPluginProgram(int id, const TQString &name);

    // Set a plugin port
    //
    virtual void setMappedPort(int pluginId,
                               unsigned long portId,
                               float value);

    virtual float getMappedPort(int pluginId,
                                unsigned long portId);

    // Create a MappedObject
    virtual int createMappedObject(int type);

    // Destroy an object
    //
    virtual int destroyMappedObject(int id);

    // Connect two objects
    //
    virtual void connectMappedObjects(int id1, int id2);
    
    // Disconnect two objects
    //
    virtual void disconnectMappedObjects(int id1, int id2);

    // Disconnect an object from everything
    //
    virtual void disconnectMappedObject(int id);

    // Sample rate
    //
    virtual unsigned int getSampleRate() const;

    // Clear the studio
    //
    virtual void clearStudio();

    // Debug stuff, to check MmappedSegment::iterator
    virtual void dumpFirstSegment();

    virtual void remapSegment(const TQString& filename, size_t newSize);
    virtual void addSegment(const TQString& filename);
    virtual void deleteSegment(const TQString& filename);
    virtual void closeAllSegments();
    virtual void remapTracks();

    // Set Quarter note length
    //
    virtual void setQuarterNoteLength(long timeSec, long timeNsec);

    // Get a status report
    // 
    virtual TQString getStatusLog();

    //
    //
    //
    //      -------- END OF DCOP INTERFACE --------




    void setStatus(TransportStatus status)
            { m_transportStatus = status; }
    TransportStatus getStatus() { return m_transportStatus; }
   
    // Process the first chunk of Sequencer events
    bool startPlaying();

    // Process all subsequent events
    bool keepPlaying();

    // Update internal clock and send GUI position pointer movement
    void updateClocks();

    bool checkExternalTransport();

    // Sends status changes up to GUI
    void notifySequencerStatus();

    // Send latest slice information back to GUI for display
    void notifyVisuals(MappedComposition *mC);

    // These two methods process any pending MIDI or audio
    // and send them up to the gui for storage and display
    //
    void processRecordedMidi();
    void processRecordedAudio();

    // Called during stopped or playing operation to process
    // any pending incoming MIDI events that aren't being
    // recorded (i.e. for display in Transport or on Mixer)
    //
    void processAsynchronousEvents();

    // Sleep for the given time, approximately.  Called from the main
    // loop in order to lighten CPU load (i.e. the timing quality of
    // the sequencer does not depend on this being accurate).  A good
    // implementation of this call would return right away when an
    // incoming MIDI event needed to be handled.
    //
    void sleep(const RealTime &rt);

    // Removes from a MappedComposition the events not matching
    // the supplied filer.
    //
    void applyFiltering(MappedComposition *mC,
                        MidiFilter filter,
                        bool filterControlDevice);

    // This method assigns an Instrument to each MappedEvent 
    // belongin to the MappedComposition, and sends the 
    // transformed events to the driver to be played.
    //
    void routeEvents(MappedComposition *mC, bool useSelectedTrack);

    // Are we looping?
    //
    bool isLooping() const { return !(m_loopStart == m_loopEnd); }

    // the call itself
    void sequencerAlive();

    /*
    // Audio latencies
    //
    RealTime getAudioPlaybackLatency()
        { return m_audioPlayLatency; }
    void setAudioPlaybackLatency(const RealTime &latency)
        { m_audioPlayLatency = latency; }

    RealTime getAudioRecordLatency()
        { return m_audioRecordLatency; }
    void setAudioRecordLatency(const RealTime &latency)
        { m_audioRecordLatency = latency; }
    */

    // Initialise the virtual studio at this end of the link
    //
    void initialiseStudio();


    // --------- EXTERNAL TRANSPORT INTERFACE METHODS --------
    //
    // Whereas the DCOP interface (above) is for the GUI to call to
    // make the sequencer follow its wishes, this interface is for
    // external clients to call (via some low-level audio callback)
    // and requires sychronising with the GUI.
    
    TransportToken transportChange(TransportRequest);
    TransportToken transportJump(TransportRequest, RealTime);
    bool isTransportSyncComplete(TransportToken token);
    TransportToken getInvalidTransportToken() const { return 0; }

public slots:

    // Check for new clients - on timeout
    //
    void slotCheckForNewClients();

protected:

    // get events whilst handling loop
    //
    void fetchEvents(MappedComposition &,
                     const RealTime &start,
                     const RealTime &end,
                     bool firstFetch);

    // just get a slice of events between markers
    //
    void getSlice(MappedComposition &,
                  const RealTime &start,
                  const RealTime &end,
                  bool firstFetch);

    // adjust event times according to relative instrument latencies
    //
    void applyLatencyCompensation(MappedComposition &);

    // mmap-related stuff
    MmappedSegment* mmapSegment(const TQString&);
    void cleanupMmapData();
    void initMetaIterator();

    void rationalisePlayingAudio();
    void setEndOfCompReached(bool e) { m_isEndOfCompReached = e; }
    bool isEndOfCompReached() { return m_isEndOfCompReached; }
    void incrementTransportToken();

    //--------------- Data members ---------------------------------

    SoundDriver *m_driver;
    TransportStatus m_transportStatus;

    // Position pointer
    RealTime m_songPosition;
    RealTime m_lastFetchSongPosition;

    RealTime m_readAhead;
    RealTime m_audioMix;
    RealTime m_audioRead;
    RealTime m_audioWrite;
    int m_smallFileSize;

    /*

    // Not required at the sequencer

    // Two more latencies for audio play and record - when we 
    // use an unsynchronised audio and MIDI system such as
    // ALSA and JACK we need to use these additional values
    // to help time-keeping.
    //
    RealTime m_audioPlayLatency;
    RealTime m_audioRecordLatency;

    */


    RealTime m_loopStart;
    RealTime m_loopEnd;

    std::vector<MappedInstrument*> m_instruments;

    // MappedStudio holds all of our session-persistent information -
    // sliders and what have you.  It's also streamable over DCOP
    // so you can reconstruct it at either end of the link for 
    // presentation, storage etc.
    //
    MappedStudio       *m_studio;

    // Slice revert storage
    //
    RealTime            m_oldSliceSize;
    TQTimer                         *m_sliceTimer;

    // Timer to check for new clients
    //
    TQTimer                         *m_newClientTimer;

    // mmap segments
    //
    TQString                         m_segmentFilesPath;
    MmappedSegmentsMetaIterator::mmappedsegments    m_mmappedSegments;
    MmappedSegmentsMetaIterator*    m_metaIterator;
    RealTime            m_lastStartTime;

    MappedComposition   m_mC;
    ControlBlockMmapper            *m_controlBlockMmapper;
    SequencerMmapper                m_sequencerMapper;

    typedef std::pair<TransportRequest, RealTime> TransportPair;
    std::deque<TransportPair>       m_transportRequests;
    TransportToken                  m_transportToken;

    bool                            m_isEndOfCompReached;
};

}
 
#endif // _ROSEGARDEN_SEQUENCER_APP_H_