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
|
/* -*- 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.
*/
#include "MetronomeMmapper.h"
#include "misc/Debug.h"
#include <kapplication.h>
#include "sound/Midi.h"
#include <kstddirs.h>
#include "document/ConfigGroups.h"
#include "base/Event.h"
#include "base/MidiProgram.h"
#include "base/NotationTypes.h"
#include "base/RealTime.h"
#include "base/Segment.h"
#include "base/Studio.h"
#include "base/TriggerSegment.h"
#include "document/RosegardenGUIDoc.h"
#include "SegmentMmapper.h"
#include "sound/MappedEvent.h"
#include <tdeconfig.h>
#include <kglobal.h>
#include <tqstring.h>
#include <algorithm>
namespace Rosegarden
{
MetronomeMmapper::MetronomeMmapper(RosegardenGUIDoc* doc)
: SegmentMmapper(doc, 0, createFileName()),
m_metronome(0), // no metronome to begin with
m_tickDuration(0, 100000000)
{
SETQMAN_DEBUG << "MetronomeMmapper ctor : " << this << endl;
// get metronome device
Studio &studio = m_doc->getStudio();
int device = studio.getMetronomeDevice();
const MidiMetronome *metronome =
m_doc->getStudio().getMetronomeFromDevice(device);
if (metronome) {
SETQMAN_DEBUG << "MetronomeMmapper: have metronome, it's on instrument " << metronome->getInstrument() << endl;
m_metronome = new MidiMetronome(*metronome);
} else {
m_metronome = new MidiMetronome
(SystemInstrumentBase);
SETQMAN_DEBUG << "MetronomeMmapper: no metronome for device " << device << endl;
}
Composition& c = m_doc->getComposition();
timeT t = c.getBarStart( -20); // somewhat arbitrary
int depth = m_metronome->getDepth();
if (depth > 0) {
while (t < c.getEndMarker()) {
TimeSignature sig = c.getTimeSignatureAt(t);
timeT barDuration = sig.getBarDuration();
std::vector<int> divisions;
if (depth > 0)
sig.getDivisions(depth - 1, divisions);
int ticks = 1;
for (int i = -1; i < (int)divisions.size(); ++i) {
if (i >= 0)
ticks *= divisions[i];
for (int tick = 0; tick < ticks; ++tick) {
if (i >= 0 && (tick % divisions[i] == 0))
continue;
timeT tickTime = t + (tick * barDuration) / ticks;
m_ticks.push_back(Tick(tickTime, i + 1));
}
}
t = c.getBarEndForTime(t);
}
}
TDEConfig *config = kapp->config();
config->setGroup(SequencerOptionsConfigGroup);
int midiClock = config->readNumEntry("midiclock", 0);
int mtcMode = config->readNumEntry("mtcmode", 0);
if (midiClock == 1) {
timeT quarterNote = Note(Note::Crotchet).getDuration();
// Insert 24 clocks per quarter note
//
for (timeT insertTime = c.getStartMarker();
insertTime < c.getEndMarker();
insertTime += quarterNote / 24) {
m_ticks.push_back(Tick(insertTime, 3));
}
}
if (mtcMode > 0) {
// do something
}
sortTicks();
if (m_ticks.size() == 0) {
SETQMAN_DEBUG << "MetronomeMmapper : WARNING no ticks generated\n";
}
// Done by init()
// m_mmappedSize = computeMmappedSize();
// if (m_mmappedSize > 0) {
// setFileSize(m_mmappedSize);
// doMmap();
// dump();
// }
}
MetronomeMmapper::~MetronomeMmapper()
{
SETQMAN_DEBUG << "~MetronomeMmapper " << this << endl;
delete m_metronome;
}
InstrumentId MetronomeMmapper::getMetronomeInstrument()
{
return m_metronome->getInstrument();
}
TQString MetronomeMmapper::createFileName()
{
return TDEGlobal::dirs()->resourceDirs("tmp").last() + "/rosegarden_metronome";
}
void MetronomeMmapper::dump()
{
RealTime eventTime;
Composition& comp = m_doc->getComposition();
SETQMAN_DEBUG << "MetronomeMmapper::dump: instrument is " << m_metronome->getInstrument() << endl;
MappedEvent* bufPos = m_mmappedEventBuffer, *mE;
for (TickContainer::iterator i = m_ticks.begin(); i != m_ticks.end(); ++i) {
/*
SETQMAN_DEBUG << "MetronomeMmapper::dump: velocity = "
<< int(velocity) << endl;
*/
eventTime = comp.getElapsedRealTime(i->first);
if (i->second == 3) // MIDI Clock
{
mE = new (bufPos) MappedEvent(0, MappedEvent::MidiSystemMessage);
mE->setData1(MIDI_TIMING_CLOCK);
mE->setEventTime(eventTime);
} else {
MidiByte velocity;
MidiByte pitch;
switch (i->second) {
case 0:
velocity = m_metronome->getBarVelocity();
pitch = m_metronome->getBarPitch();
break;
case 1:
velocity = m_metronome->getBeatVelocity();
pitch = m_metronome->getBeatPitch();
break;
default:
velocity = m_metronome->getSubBeatVelocity();
pitch = m_metronome->getSubBeatPitch();
break;
}
new (bufPos) MappedEvent(m_metronome->getInstrument(),
MappedEvent::MidiNoteOneShot,
pitch,
velocity,
eventTime,
m_tickDuration,
RealTime::zeroTime);
}
++bufPos;
}
// Store the number of events at the start of the shared memory region
*(size_t *)m_mmappedRegion = (bufPos - m_mmappedEventBuffer);
SETQMAN_DEBUG << "MetronomeMmapper::dump: - "
<< "Total events written = " << *(size_t *)m_mmappedRegion
<< endl;
}
void MetronomeMmapper::sortTicks()
{
sort(m_ticks.begin(), m_ticks.end());
}
size_t MetronomeMmapper::computeMmappedSize()
{
TDEConfig *config = kapp->config();
config->setGroup(Rosegarden::SequencerOptionsConfigGroup);
int midiClock = config->readNumEntry("midiclock", 0);
int mtcMode = config->readNumEntry("mtcmode", 0);
// base size for Metronome ticks
size_t size = m_ticks.size() * sizeof(MappedEvent);
Composition& comp = m_doc->getComposition();
if (midiClock == 1)
{
using Rosegarden::Note;
// Allow room for MIDI clocks
int clocks = ( 24 * ( comp.getEndMarker() - comp.getStartMarker() ) ) /
Note(Note::Crotchet).getDuration();
/*
SETQMAN_DEBUG << "MetronomeMmapper::computeMmappedSize - "
<< "Number of clock events catered for = " << clocks
<< endl;
*/
size += clocks * sizeof(MappedEvent);
}
if (mtcMode > 0)
{
// Allow room for MTC timing messages (how?)
}
return size;
}
unsigned int MetronomeMmapper::getSegmentRepeatCount()
{
return 1;
}
}
|