summaryrefslogtreecommitdiffstats
path: root/src/sound/MappedEvent.h
blob: 674029c68bafbdbbdca1b522d1059170d7cf0274 (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
533
534
535
536
537
538
539
540
541
542
543
544
/*
  Rosegarden
  A 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 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.
*/

#include <tqdatastream.h>

#include "Composition.h" // for RealTime
#include "Event.h"


#ifndef _MAPPEDEVENT_H_
#define _MAPPEDEVENT_H_

// Used as a transformation stage between Composition, Events and output
// at the Sequencer this class and MidiComposition eliminate the notion
// of the Segment and Track for ease of Event access.  The MappedEvents
// are ready for playing or routing through an Instrument or Effects
// boxes.
//
// MappedEvents can also represent instructions for playback of audio
// samples - if the m_type is Audio then the sequencer will attempt to
// map the Pitch (m_data1) to the audio id.  Note that this limits us
// to 256 audio files in the Composition unless we use a different
// parameter for storing these IDs.
// 
// The MappedEvent/Instrument relationship is interesting - we don't
// want to duplicate the entire Instrument at the Sequencer level as
// it'd be messy and unnecessary.  Instead we use a MappedInstrument
// which is just a very cut down Sequencer-side version of an Instrument.
//
// Some of these Events are unidirectional, some are bidirectional -
// that is they only have a meaning in one direction (they are still
// legal at either end).  They are broadcast in both directions using
// the "getSequencerSlice" and "processAsync/Recorded" interfaces on
// which the control messages can piggyback and eventually stripped out.
//

namespace Rosegarden
{
class MappedEvent;

class DataBlockRepository
{
public:
    friend class MappedEvent;
    typedef unsigned long blockid;

    static DataBlockRepository* getInstance();
    static std::string getDataBlockForEvent(MappedEvent*);
    static void setDataBlockForEvent(MappedEvent*, const std::string&);
    /**
     * Clear all block files
     */
    static void clear();
    bool hasDataBlock(blockid);

protected:
    DataBlockRepository();

    std::string getDataBlock(blockid);

    void addDataByteForEvent(MidiByte byte, MappedEvent*);
    void addDataStringForEvent(const std::string&, MappedEvent*);


    blockid registerDataBlock(const std::string&);
    void unregisterDataBlock(blockid);

    void registerDataBlockForEvent(const std::string&, MappedEvent*);
    void unregisterDataBlockForEvent(MappedEvent*);


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

    static DataBlockRepository* m_instance;
};


class MappedEvent
{
public:
    typedef enum
    {
        // INVALID
        //
        InvalidMappedEvent       = 0,

        // Keep the MidiNotes bit flaggable so that filtering works
        //
        MidiNote                 = 1 << 0,
        MidiNoteOneShot          = 1 << 1,  // doesn't need NOTE OFFs
        MidiProgramChange        = 1 << 2,
        MidiKeyPressure          = 1 << 3,
        MidiChannelPressure      = 1 << 4,
        MidiPitchBend            = 1 << 5,
        MidiController           = 1 << 6,
        MidiSystemMessage        = 1 << 7,

        // Sent from the gui to play an audio file
        Audio                    = 1 << 8,
        // Sent from gui to cancel playing an audio file
        AudioCancel              = 1 << 9,
        // Sent to the gui with audio level on Instrument
        AudioLevel               = 1 << 10,
        // Sent to the gui to inform an audio file stopped
        AudioStopped             = 1 << 11,
        // The gui is clear to generate a preview for a new audio file
        AudioGeneratePreview     = 1 << 12,

        // Update Instruments - new ALSA client detected
        SystemUpdateInstruments  = 1 << 13,
        // Set RG as JACK master/slave
        SystemJackTransport      = 1 << 14,
        // Set RG as MMC master/slave
        SystemMMCTransport       = 1 << 15,
        // Set System Messages and MIDI Clock
        SystemMIDIClock          = 1 << 16,
        // Set Record device
        SystemRecordDevice       = 1 << 17,
        // Set Metronome device
        SystemMetronomeDevice    = 1 << 18,
        // Set Audio inputs/outputs: data1 num inputs, data2 num submasters
        SystemAudioPortCounts    = 1 << 19,
        // Set whether we create various Audio ports (data1 is an AudioOutMask)
        SystemAudioPorts         = 1 << 20,
        // Some failure has occurred: data1 contains FailureCode
        SystemFailure            = 1 << 21,

        // Time sig. event (from time sig. composition reference segment)
        TimeSignature            = 1 << 22,
        // Tempo event (from tempo composition reference segment)
        Tempo                    = 1 << 23,
        
        // Panic function
        Panic                    = 1 << 24,

        // Set RG as MTC master/slave
        SystemMTCTransport       = 1 << 25,
        // Auto-connect sync outputs
        SystemMIDISyncAuto       = 1 << 26,
        // File format used for audio recording (data1 is 0=PCM,1=float)
        SystemAudioFileFormat    = 1 << 27

    } MappedEventType;

    typedef enum
    {
        // These values are OR'd to produce the data2 field in a
        // SystemAudioPorts event.
        FaderOuts                = 1 << 0,
        SubmasterOuts            = 1 << 1

    } MappedEventAudioOutMask;

    typedef enum
    {
        // JACK is having some xruns - warn the user maybe
        FailureXRuns             = 0,
        // JACK has died or kicked us out
        FailureJackDied          = 1,
        // Audio subsystem failed to read from disc fast enough
        FailureDiscUnderrun      = 2,
        // Audio subsystem failed to write to disc fast enough
        FailureDiscOverrun       = 3,
        // Audio subsystem failed to mix busses fast enough
        FailureBussMixUnderrun   = 4,
        // Audio subsystem failed to mix instruments fast enough
        FailureMixUnderrun       = 5,
        // Using a timer that has too low a resolution (e.g. 100Hz system timer)
        WarningImpreciseTimer    = 6,
        // Too much CPU time spent in audio processing -- risk of xruns and lockup
        FailureCPUOverload       = 7,
        // JACK kicked us out, but we've reconnected
        FailureJackRestart       = 8,
        // JACK kicked us out, and now the reconnection has failed
        FailureJackRestartFailed = 9,
        // A necessary ALSA call has returned an error code
        FailureALSACallFailed    = 10,
        // Using a timer that has too low a resolution, but RTC might work
        WarningImpreciseTimerTryRTC = 11,
    } FailureCode;      

    MappedEvent(): m_trackId(0),
                   m_instrument(0),
                   m_type(MidiNote),
                   m_data1(0),
                   m_data2(0),
                   m_eventTime(0, 0),
                   m_duration(0, 0),
                   m_audioStartMarker(0, 0),
                   m_dataBlockId(0),
                   m_isPersistent(false),
                   m_runtimeSegmentId(-1),
                   m_autoFade(false),
                   m_fadeInTime(RealTime::zeroTime),
                   m_fadeOutTime(RealTime::zeroTime),
                   m_recordedChannel(0),
                   m_recordedDevice(0) {}

    // Construct from Events to Internal (MIDI) type MappedEvent
    //
    MappedEvent(const Event &e);

    // Another Internal constructor from Events
    MappedEvent(InstrumentId id,
                const Event &e,
                const RealTime &eventTime,
                const RealTime &duration);

    // A general MappedEvent constructor for any MappedEvent type
    //
    MappedEvent(InstrumentId id,
                MappedEventType type,
                MidiByte pitch,
                MidiByte velocity,
                const RealTime &absTime,
                const RealTime &duration,
                const RealTime &audioStartMarker):
        m_trackId(0),
        m_instrument(id),
        m_type(type),
        m_data1(pitch),
        m_data2(velocity),
        m_eventTime(absTime),
        m_duration(duration),
        m_audioStartMarker(audioStartMarker),
        m_dataBlockId(0),
        m_isPersistent(false),
        m_runtimeSegmentId(-1),
        m_autoFade(false),
        m_fadeInTime(RealTime::zeroTime),
        m_fadeOutTime(RealTime::zeroTime),
        m_recordedChannel(0),
        m_recordedDevice(0) {}

    // Audio MappedEvent shortcut constructor
    //
    MappedEvent(InstrumentId id,
                unsigned short audioID,
                const RealTime &eventTime,
                const RealTime &duration,
                const RealTime &audioStartMarker):
         m_trackId(0),
         m_instrument(id),
         m_type(Audio),
         m_data1(audioID % 256),
         m_data2(audioID / 256),
         m_eventTime(eventTime),
         m_duration(duration),
         m_audioStartMarker(audioStartMarker),
         m_dataBlockId(0),
         m_isPersistent(false),
         m_runtimeSegmentId(-1),
         m_autoFade(false),
         m_fadeInTime(RealTime::zeroTime),
         m_fadeOutTime(RealTime::zeroTime),
         m_recordedChannel(0),
         m_recordedDevice(0) {}

    // More generalised MIDI event containers for
    // large and small events (one param, two param)
    //
    MappedEvent(InstrumentId id,
                MappedEventType type,
                MidiByte data1,
                MidiByte data2):
         m_trackId(0),
         m_instrument(id),
         m_type(type),
         m_data1(data1),
         m_data2(data2),
         m_eventTime(RealTime(0, 0)),
         m_duration(RealTime(0, 0)),
         m_audioStartMarker(RealTime(0, 0)),
         m_dataBlockId(0),
         m_isPersistent(false),
         m_runtimeSegmentId(-1),
         m_autoFade(false),
         m_fadeInTime(RealTime::zeroTime),
         m_fadeOutTime(RealTime::zeroTime),
         m_recordedChannel(0),
         m_recordedDevice(0) {}

    MappedEvent(InstrumentId id,
                MappedEventType type,
                MidiByte data1):
        m_trackId(0),
        m_instrument(id),
        m_type(type),
        m_data1(data1),
        m_data2(0),
        m_eventTime(RealTime(0, 0)),
        m_duration(RealTime(0, 0)),
        m_audioStartMarker(RealTime(0, 0)),
        m_dataBlockId(0),
        m_isPersistent(false),
        m_runtimeSegmentId(-1),
        m_autoFade(false),
        m_fadeInTime(RealTime::zeroTime),
        m_fadeOutTime(RealTime::zeroTime),
        m_recordedChannel(0),
        m_recordedDevice(0) {}


    // Construct SysExs say
    //
    MappedEvent(InstrumentId id,
                MappedEventType type):
        m_trackId(0),
        m_instrument(id),
        m_type(type),
        m_data1(0),
        m_data2(0),
        m_eventTime(RealTime(0, 0)),
        m_duration(RealTime(0, 0)),
        m_audioStartMarker(RealTime(0, 0)),
        m_dataBlockId(0),
        m_isPersistent(false),
        m_runtimeSegmentId(-1),
        m_autoFade(false),
        m_fadeInTime(RealTime::zeroTime),
        m_fadeOutTime(RealTime::zeroTime),
        m_recordedChannel(0),
        m_recordedDevice(0) {}

    // Copy constructor
    //
    // Fix for 674731 by Pedro Lopez-Cabanillas (20030531)
    MappedEvent(const MappedEvent &mE):
        m_trackId(mE.getTrackId()),
        m_instrument(mE.getInstrument()),
        m_type(mE.getType()),
        m_data1(mE.getData1()),
        m_data2(mE.getData2()),
        m_eventTime(mE.getEventTime()),
        m_duration(mE.getDuration()),
        m_audioStartMarker(mE.getAudioStartMarker()),
        m_dataBlockId(mE.getDataBlockId()),
        m_isPersistent(false),
        m_runtimeSegmentId(mE.getRuntimeSegmentId()),
        m_autoFade(mE.isAutoFading()),
        m_fadeInTime(mE.getFadeInTime()),
        m_fadeOutTime(mE.getFadeOutTime()),
        m_recordedChannel(mE.getRecordedChannel()),
        m_recordedDevice(mE.getRecordedDevice()) {}

    // Copy from pointer
    // Fix for 674731 by Pedro Lopez-Cabanillas (20030531)
    MappedEvent(MappedEvent *mE):
        m_trackId(mE->getTrackId()),
        m_instrument(mE->getInstrument()),
        m_type(mE->getType()),
        m_data1(mE->getData1()),
        m_data2(mE->getData2()),
        m_eventTime(mE->getEventTime()),
        m_duration(mE->getDuration()),
        m_audioStartMarker(mE->getAudioStartMarker()),
        m_dataBlockId(mE->getDataBlockId()),
        m_isPersistent(false),
        m_runtimeSegmentId(mE->getRuntimeSegmentId()),
        m_autoFade(mE->isAutoFading()),
        m_fadeInTime(mE->getFadeInTime()),
        m_fadeOutTime(mE->getFadeOutTime()),
        m_recordedChannel(mE->getRecordedChannel()),
        m_recordedDevice(mE->getRecordedDevice()) {}

    // Construct perhaps without initialising, for placement new or equivalent
    MappedEvent(bool initialise) {
        if (initialise) *this = MappedEvent();
    }

    // Event time
    //
    void setEventTime(const RealTime &a) { m_eventTime = a; }
    RealTime getEventTime() const { return m_eventTime; }

    // Duration
    //
    void setDuration(const RealTime &d) { m_duration = d; }
    RealTime getDuration() const { return m_duration; }

    // Instrument
    void setInstrument(InstrumentId id) { m_instrument = id; }
    InstrumentId getInstrument() const { return m_instrument; }

    // Track
    void setTrackId(TrackId id) { m_trackId = id; }
    TrackId getTrackId() const { return m_trackId; }

    MidiByte getPitch() const { return m_data1; }

    // Keep pitch within MIDI limits
    //
    void setPitch(MidiByte p)
    {
        m_data1 = p;
        if (m_data1 > MidiMaxValue) m_data1 = MidiMaxValue;
    }

    void setVelocity(MidiByte v) { m_data2 = v; }
    MidiByte getVelocity() const { return m_data2; }

    // And the trendy names for them
    //
    MidiByte getData1() const { return m_data1; }
    MidiByte getData2() const { return m_data2; }
    void setData1(MidiByte d1) { m_data1 = d1; }
    void setData2(MidiByte d2) { m_data2 = d2; }

    void setAudioID(unsigned short id) { m_data1 = id % 256; m_data2 = id / 256; }
    int getAudioID() const { return m_data1 + 256 * m_data2; }

    // A sample doesn't have to be played from the beginning.  When
    // passing an Audio event this value may be set to indicate from
    // where in the sample it should be played.  Duration is measured
    // against total sounding length (not absolute position).
    //
    void setAudioStartMarker(const RealTime &aS)
        { m_audioStartMarker = aS; }
    RealTime getAudioStartMarker() const
        { return m_audioStartMarker; }

    MappedEventType getType() const { return m_type; }
    void setType(const MappedEventType &value) { m_type = value; }

    // Data block id
    //
    DataBlockRepository::blockid getDataBlockId() const { return m_dataBlockId; }
    void setDataBlockId(DataBlockRepository::blockid dataBlockId) { m_dataBlockId = dataBlockId; }
    
    // How MappedEvents are ordered in the MappedComposition
    //
    struct MappedEventCmp
    {
        bool operator()(const MappedEvent *mE1, const MappedEvent *mE2) const
        {
            return *mE1 < *mE2;
        }
    };

    friend bool operator<(const MappedEvent &a, const MappedEvent &b);

    MappedEvent& operator=(const MappedEvent &mE);

    friend TQDataStream& operator>>(TQDataStream &dS, MappedEvent *mE);
    friend TQDataStream& operator<<(TQDataStream &dS, MappedEvent *mE);
    friend TQDataStream& operator>>(TQDataStream &dS, MappedEvent &mE);
    friend TQDataStream& operator<<(TQDataStream &dS, const MappedEvent &mE);

    /// Add a single byte to the event's datablock (for SysExs)
    void addDataByte(MidiByte byte);
    /// Add several bytes to the event's datablock
    void addDataString(const std::string& data);

    void setPersistent(bool value) { m_isPersistent = value; }
    bool isPersistent() const { return m_isPersistent; }

    /// Size of a MappedEvent in a stream
    static const size_t streamedSize;

    // The runtime segment id of an audio file
    //
    int getRuntimeSegmentId() const { return m_runtimeSegmentId; }
    void setRuntimeSegmentId(int id) { m_runtimeSegmentId = id; }

    bool isAutoFading() const { return m_autoFade; }
    void setAutoFade(bool value) { m_autoFade = value; }

    RealTime getFadeInTime() const { return m_fadeInTime; }
    void setFadeInTime(const RealTime &time)
            { m_fadeInTime = time; }

    RealTime getFadeOutTime() const { return m_fadeOutTime; }
    void setFadeOutTime(const RealTime &time)
            { m_fadeOutTime = time; }
    
    // Original event input channel as it was recorded
    //
    unsigned int getRecordedChannel() const { return m_recordedChannel; }
    void setRecordedChannel(const unsigned int channel) 
            { m_recordedChannel = channel; }
            
    // Original event record device as it was recorded
    //
    unsigned int getRecordedDevice() const { return m_recordedDevice; }
    void setRecordedDevice(const unsigned int device) { m_recordedDevice = device; }

private:
    TrackId          m_trackId;
    InstrumentId     m_instrument;
    MappedEventType  m_type;
    MidiByte         m_data1;
    MidiByte         m_data2;
    RealTime         m_eventTime;
    RealTime         m_duration;
    RealTime         m_audioStartMarker;

    // Use this when we want to store something in addition to the
    // other bytes in this type, e.g. System Exclusive.
    //
    DataBlockRepository::blockid m_dataBlockId;

    // Should a MappedComposition try and delete this MappedEvent or
    // if it persistent?
    //
    bool             m_isPersistent;


    // Id of the segment that this (audio) event is derived from
    //
    int              m_runtimeSegmentId;

    // Audio autofading
    //
    bool                  m_autoFade;
    RealTime  m_fadeInTime;
    RealTime  m_fadeOutTime;

    // input event original data,
    // stored as it was recorded
    //
    unsigned int          m_recordedChannel;
    unsigned int          m_recordedDevice;
};


}

#endif