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
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
|
// -*- c-indentation-style:"stroustrup" c-basic-offset: 4 -*-
/*
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.
*/
#include <tqdir.h>
#include <tqfile.h>
#include <tqfileinfo.h>
#include <kstddirs.h>
#include "MappedEvent.h"
#include "BaseProperties.h"
#include "Midi.h"
#include "MidiTypes.h"
#define DEBUG_MAPPEDEVENT 1
namespace Rosegarden
{
MappedEvent::MappedEvent(InstrumentId id,
const Event &e,
const RealTime &eventTime,
const RealTime &duration):
m_trackId(0),
m_instrument(id),
m_type(MidiNote),
m_data1(0),
m_data2(0),
m_eventTime(eventTime),
m_duration(duration),
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)
{
try {
// For each event type, we set the properties in a particular
// order: first the type, then whichever of data1 and data2 fits
// less well with its default value. This way if one throws an
// exception for no data, we still have a good event with the
// defaults set.
if (e.isa(Note::EventType)) {
m_type = MidiNoteOneShot;
long v = MidiMaxValue;
e.get<Int>(BaseProperties::VELOCITY, v);
m_data2 = v;
m_data1 = e.get<Int>(BaseProperties::PITCH);
} else if (e.isa(PitchBend::EventType)) {
m_type = MidiPitchBend;
PitchBend pb(e);
m_data1 = pb.getMSB();
m_data2 = pb.getLSB();
} else if (e.isa(Controller::EventType)) {
m_type = MidiController;
Controller c(e);
m_data1 = c.getNumber();
m_data2 = c.getValue();
} else if (e.isa(ProgramChange::EventType)) {
m_type = MidiProgramChange;
ProgramChange pc(e);
m_data1 = pc.getProgram();
} else if (e.isa(KeyPressure::EventType)) {
m_type = MidiKeyPressure;
KeyPressure kp(e);
m_data1 = kp.getPitch();
m_data2 = kp.getPressure();
} else if (e.isa(ChannelPressure::EventType)) {
m_type = MidiChannelPressure;
ChannelPressure cp(e);
m_data1 = cp.getPressure();
} else if (e.isa(SystemExclusive::EventType)) {
m_type = MidiSystemMessage;
m_data1 = MIDI_SYSTEM_EXCLUSIVE;
SystemExclusive s(e);
std::string dataBlock = s.getRawData();
DataBlockRepository::getInstance()->registerDataBlockForEvent(dataBlock, this);
} else {
m_type = InvalidMappedEvent;
}
} catch (MIDIValueOutOfRange r) {
#ifdef DEBUG_MAPPEDEVENT
std::cerr << "MIDI value out of range in MappedEvent ctor"
<< std::endl;
#else
;
#endif
} catch (Event::NoData d) {
#ifdef DEBUG_MAPPEDEVENT
std::cerr << "Caught Event::NoData in MappedEvent ctor, message is:"
<< std::endl << d.getMessage() << std::endl;
#else
;
#endif
} catch (Event::BadType b) {
#ifdef DEBUG_MAPPEDEVENT
std::cerr << "Caught Event::BadType in MappedEvent ctor, message is:"
<< std::endl << b.getMessage() << std::endl;
#else
;
#endif
} catch (SystemExclusive::BadEncoding e) {
#ifdef DEBUG_MAPPEDEVENT
std::cerr << "Caught bad SysEx encoding in MappedEvent ctor"
<< std::endl;
#else
;
#endif
}
}
bool
operator<(const MappedEvent &a, const MappedEvent &b)
{
return a.getEventTime() < b.getEventTime();
}
MappedEvent&
MappedEvent::operator=(const MappedEvent &mE)
{
if (&mE == this)
return * this;
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_runtimeSegmentId = mE.getRuntimeSegmentId();
m_autoFade = mE.isAutoFading();
m_fadeInTime = mE.getFadeInTime();
m_fadeOutTime = mE.getFadeOutTime();
m_recordedChannel = mE.getRecordedChannel();
m_recordedDevice = mE.getRecordedDevice();
return *this;
}
// Do we use this? It looks dangerous so just commenting it out - rwb
//
//const size_t MappedEvent::streamedSize = 12 * sizeof(unsigned int);
TQDataStream&
operator<<(TQDataStream &dS, MappedEvent *mE)
{
dS << (unsigned int)mE->getTrackId();
dS << (unsigned int)mE->getInstrument();
dS << (unsigned int)mE->getType();
dS << (unsigned int)mE->getData1();
dS << (unsigned int)mE->getData2();
dS << (unsigned int)mE->getEventTime().sec;
dS << (unsigned int)mE->getEventTime().nsec;
dS << (unsigned int)mE->getDuration().sec;
dS << (unsigned int)mE->getDuration().nsec;
dS << (unsigned int)mE->getAudioStartMarker().sec;
dS << (unsigned int)mE->getAudioStartMarker().nsec;
dS << (unsigned long)mE->getDataBlockId();
dS << mE->getRuntimeSegmentId();
dS << (unsigned int)mE->isAutoFading();
dS << (unsigned int)mE->getFadeInTime().sec;
dS << (unsigned int)mE->getFadeInTime().nsec;
dS << (unsigned int)mE->getFadeOutTime().sec;
dS << (unsigned int)mE->getFadeOutTime().nsec;
dS << (unsigned int)mE->getRecordedChannel();
dS << (unsigned int)mE->getRecordedDevice();
return dS;
}
TQDataStream&
operator<<(TQDataStream &dS, const MappedEvent &mE)
{
dS << (unsigned int)mE.getTrackId();
dS << (unsigned int)mE.getInstrument();
dS << (unsigned int)mE.getType();
dS << (unsigned int)mE.getData1();
dS << (unsigned int)mE.getData2();
dS << (unsigned int)mE.getEventTime().sec;
dS << (unsigned int)mE.getEventTime().nsec;
dS << (unsigned int)mE.getDuration().sec;
dS << (unsigned int)mE.getDuration().nsec;
dS << (unsigned int)mE.getAudioStartMarker().sec;
dS << (unsigned int)mE.getAudioStartMarker().nsec;
dS << (unsigned long)mE.getDataBlockId();
dS << mE.getRuntimeSegmentId();
dS << (unsigned int)mE.isAutoFading();
dS << (unsigned int)mE.getFadeInTime().sec;
dS << (unsigned int)mE.getFadeInTime().nsec;
dS << (unsigned int)mE.getFadeOutTime().sec;
dS << (unsigned int)mE.getFadeOutTime().nsec;
dS << (unsigned int)mE.getRecordedChannel();
dS << (unsigned int)mE.getRecordedDevice();
return dS;
}
TQDataStream&
operator>>(TQDataStream &dS, MappedEvent *mE)
{
unsigned int trackId = 0, instrument = 0, type = 0, data1 = 0, data2 = 0;
long eventTimeSec = 0, eventTimeNsec = 0, durationSec = 0, durationNsec = 0,
audioSec = 0, audioNsec = 0;
std::string dataBlock;
unsigned long dataBlockId = 0;
int runtimeSegmentId = -1;
unsigned int autoFade = 0,
fadeInSec = 0, fadeInNsec = 0, fadeOutSec = 0, fadeOutNsec = 0,
recordedChannel = 0, recordedDevice = 0;
dS >> trackId;
dS >> instrument;
dS >> type;
dS >> data1;
dS >> data2;
dS >> eventTimeSec;
dS >> eventTimeNsec;
dS >> durationSec;
dS >> durationNsec;
dS >> audioSec;
dS >> audioNsec;
dS >> dataBlockId;
dS >> runtimeSegmentId;
dS >> autoFade;
dS >> fadeInSec;
dS >> fadeInNsec;
dS >> fadeOutSec;
dS >> fadeOutNsec;
dS >> recordedChannel;
dS >> recordedDevice;
mE->setTrackId((TrackId)trackId);
mE->setInstrument((InstrumentId)instrument);
mE->setType((MappedEvent::MappedEventType)type);
mE->setData1((MidiByte)data1);
mE->setData2((MidiByte)data2);
mE->setEventTime(RealTime(eventTimeSec, eventTimeNsec));
mE->setDuration(RealTime(durationSec, durationNsec));
mE->setAudioStartMarker(RealTime(audioSec, audioNsec));
mE->setDataBlockId(dataBlockId);
mE->setRuntimeSegmentId(runtimeSegmentId);
mE->setAutoFade(autoFade);
mE->setFadeInTime(RealTime(fadeInSec, fadeInNsec));
mE->setFadeOutTime(RealTime(fadeOutSec, fadeOutNsec));
mE->setRecordedChannel(recordedChannel);
mE->setRecordedDevice(recordedDevice);
return dS;
}
TQDataStream&
operator>>(TQDataStream &dS, MappedEvent &mE)
{
unsigned int trackId = 0, instrument = 0, type = 0, data1 = 0, data2 = 0;
long eventTimeSec = 0, eventTimeNsec = 0, durationSec = 0, durationNsec = 0,
audioSec = 0, audioNsec = 0;
std::string dataBlock;
unsigned long dataBlockId = 0;
int runtimeSegmentId = -1;
unsigned int autoFade = 0,
fadeInSec = 0, fadeInNsec = 0, fadeOutSec = 0, fadeOutNsec = 0,
recordedChannel = 0, recordedDevice = 0;
dS >> trackId;
dS >> instrument;
dS >> type;
dS >> data1;
dS >> data2;
dS >> eventTimeSec;
dS >> eventTimeNsec;
dS >> durationSec;
dS >> durationNsec;
dS >> audioSec;
dS >> audioNsec;
dS >> dataBlockId;
dS >> runtimeSegmentId;
dS >> autoFade;
dS >> fadeInSec;
dS >> fadeInNsec;
dS >> fadeOutSec;
dS >> fadeOutNsec;
dS >> recordedChannel;
dS >> recordedDevice;
mE.setTrackId((TrackId)trackId);
mE.setInstrument((InstrumentId)instrument);
mE.setType((MappedEvent::MappedEventType)type);
mE.setData1((MidiByte)data1);
mE.setData2((MidiByte)data2);
mE.setEventTime(RealTime(eventTimeSec, eventTimeNsec));
mE.setDuration(RealTime(durationSec, durationNsec));
mE.setAudioStartMarker(RealTime(audioSec, audioNsec));
mE.setDataBlockId(dataBlockId);
mE.setRuntimeSegmentId(runtimeSegmentId);
mE.setAutoFade(autoFade);
mE.setFadeInTime(RealTime(fadeInSec, fadeInNsec));
mE.setFadeOutTime(RealTime(fadeOutSec, fadeOutNsec));
mE.setRecordedChannel(recordedChannel);
mE.setRecordedDevice(recordedDevice);
return dS;
}
void
MappedEvent::addDataByte(MidiByte byte)
{
DataBlockRepository::getInstance()->addDataByteForEvent(byte, this);
}
void
MappedEvent::addDataString(const std::string& data)
{
DataBlockRepository::getInstance()->addDataStringForEvent(data, this);
}
//--------------------------------------------------
class DataBlockFile
{
public:
DataBlockFile(DataBlockRepository::blockid id);
~DataBlockFile();
TQString getFileName()
{
return m_fileName;
}
void addDataByte(MidiByte);
void addDataString(const std::string&);
void clear()
{
m_cleared = true;
}
bool exists();
void setData(const std::string&);
std::string getData();
protected:
void prepareToWrite();
void prepareToRead();
//--------------- Data members ---------------------------------
TQString m_fileName;
TQFile m_file;
bool m_cleared;
};
DataBlockFile::DataBlockFile(DataBlockRepository::blockid id)
: m_fileName(KGlobal::dirs()->resourceDirs("tmp").first() + TQString("/rosegarden_datablock_%1").tqarg(id)),
m_file(m_fileName),
m_cleared(false)
{
// std::cerr << "DataBlockFile " << m_fileName.latin1() << std::endl;
}
DataBlockFile::~DataBlockFile()
{
if (m_cleared) {
// std::cerr << "~DataBlockFile : removing " << m_fileName.latin1() << std::endl;
TQFile::remove
(m_fileName);
}
}
bool DataBlockFile::exists()
{
return TQFile::exists(m_fileName);
}
void DataBlockFile::setData(const std::string& s)
{
// std::cerr << "DataBlockFile::setData() : setting data to " << m_fileName << std::endl;
prepareToWrite();
TQDataStream stream(&m_file);
stream.writeRawBytes(s.data(), s.length());
}
std::string DataBlockFile::getData()
{
if (!exists())
return std::string();
prepareToRead();
TQDataStream stream(&m_file);
// std::cerr << "DataBlockFile::getData() : file size = " << m_file.size() << std::endl;
char* tmp = new char[m_file.size()];
stream.readRawBytes(tmp, m_file.size());
std::string res(tmp, m_file.size());
delete[] tmp;
return res;
}
void DataBlockFile::addDataByte(MidiByte byte)
{
prepareToWrite();
m_file.putch(byte);
}
void DataBlockFile::addDataString(const std::string& s)
{
prepareToWrite();
TQDataStream stream(&m_file);
stream.writeRawBytes(s.data(), s.length());
}
void DataBlockFile::prepareToWrite()
{
// std::cerr << "DataBlockFile[" << m_fileName << "]: prepareToWrite" << std::endl;
if (!m_file.isWritable()) {
m_file.close();
m_file.open(IO_WriteOnly | IO_Append);
assert(m_file.isWritable());
}
}
void DataBlockFile::prepareToRead()
{
// std::cerr << "DataBlockFile[" << m_fileName << "]: prepareToRead" << std::endl;
if (!m_file.isReadable()) {
m_file.close();
m_file.open(IO_ReadOnly);
assert(m_file.isReadable());
}
}
//--------------------------------------------------
DataBlockRepository* DataBlockRepository::getInstance()
{
if (!m_instance)
m_instance = new DataBlockRepository;
return m_instance;
}
std::string DataBlockRepository::getDataBlock(DataBlockRepository::blockid id)
{
DataBlockFile dataBlockFile(id);
if (dataBlockFile.exists())
return dataBlockFile.getData();
return std::string();
}
std::string DataBlockRepository::getDataBlockForEvent(MappedEvent* e)
{
blockid id = e->getDataBlockId();
if (id == 0) {
// std::cerr << "WARNING: DataBlockRepository::getDataBlockForEvent called on event with data block id 0" << std::endl;
return "";
}
return getInstance()->getDataBlock(id);
}
void DataBlockRepository::setDataBlockForEvent(MappedEvent* e, const std::string& s)
{
blockid id = e->getDataBlockId();
if (id == 0) {
// std::cerr << "Creating new datablock for event" << std::endl;
getInstance()->registerDataBlockForEvent(s, e);
} else {
// std::cerr << "Writing " << s.length() << " chars to file for datablock " << id << std::endl;
DataBlockFile dataBlockFile(id);
dataBlockFile.setData(s);
}
}
bool DataBlockRepository::hasDataBlock(DataBlockRepository::blockid id)
{
return DataBlockFile(id).exists();
}
DataBlockRepository::blockid DataBlockRepository::registerDataBlock(const std::string& s)
{
blockid id = 0;
while (id == 0 || DataBlockFile(id).exists())
id = (blockid)random();
// std::cerr << "DataBlockRepository::registerDataBlock: " << s.length() << " chars, id is " << id << std::endl;
DataBlockFile dataBlockFile(id);
dataBlockFile.setData(s);
return id;
}
void DataBlockRepository::unregisterDataBlock(DataBlockRepository::blockid id)
{
DataBlockFile dataBlockFile(id);
dataBlockFile.clear();
}
void DataBlockRepository::registerDataBlockForEvent(const std::string& s, MappedEvent* e)
{
e->setDataBlockId(registerDataBlock(s));
}
void DataBlockRepository::unregisterDataBlockForEvent(MappedEvent* e)
{
unregisterDataBlock(e->getDataBlockId());
}
DataBlockRepository::DataBlockRepository()
{}
void DataBlockRepository::clear()
{
#ifdef DEBUG_MAPPEDEVENT
std::cerr << "DataBlockRepository::clear()\n";
#endif
// Erase all 'datablock_*' files
//
TQString tmpPath = KGlobal::dirs()->resourceDirs("tmp").first();
TQDir segmentsDir(tmpPath, "rosegarden_datablock_*");
for (unsigned int i = 0; i < segmentsDir.count(); ++i) {
TQString segmentName = tmpPath + '/' + segmentsDir[i];
TQFile::remove
(segmentName);
}
}
void DataBlockRepository::addDataByteForEvent(MidiByte byte, MappedEvent* e)
{
DataBlockFile dataBlockFile(e->getDataBlockId());
dataBlockFile.addDataByte(byte);
}
void DataBlockRepository::addDataStringForEvent(const std::string& s, MappedEvent* e)
{
DataBlockFile dataBlockFile(e->getDataBlockId());
dataBlockFile.addDataString(s);
}
DataBlockRepository* DataBlockRepository::m_instance = 0;
}
|