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
|
/* -*- 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 <glaurent@telegraph-road.org>,
Chris Cannam <cannam@all-day-breakfast.com>,
Richard Bown <richard.bown@ferventsoftware.com>
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 "HydrogenXMLHandler.h"
#include "base/Event.h"
#include "base/BaseProperties.h"
#include <klocale.h>
#include "misc/Debug.h"
#include "misc/Strings.h"
#include "base/Composition.h"
#include "base/Instrument.h"
#include "base/MidiProgram.h"
#include "base/NotationTypes.h"
#include "base/Segment.h"
#include "base/Track.h"
#include <tqstring.h>
namespace Rosegarden
{
HydrogenXMLHandler::HydrogenXMLHandler(Composition *composition,
InstrumentId drumIns):
m_composition(composition),
m_drumInstrument(drumIns),
m_inNote(false),
m_inInstrument(false),
m_inPattern(false),
m_inSequence(false),
m_patternName(""),
m_patternSize(0),
m_sequenceName(""),
m_position(0),
m_velocity(0.0),
m_panL(0.0),
m_panR(0.0),
m_pitch(0.0),
m_instrument(0),
m_id(0),
m_muted(false),
m_fileName(""),
m_bpm(0),
m_volume(0.0),
m_name(""),
m_author(""),
m_notes(""),
m_songMode(false),
m_version(""),
m_currentProperty(""),
m_segment(0),
m_currentTrackNb(0),
m_segmentAdded(false),
m_currentBar(0),
m_newSegment(false)
{}
bool
HydrogenXMLHandler::startDocument()
{
RG_DEBUG << "HydrogenXMLHandler::startDocument" << endl;
m_inNote = false;
m_inInstrument = false;
m_inPattern = false;
m_inSequence = false;
// Pattern attributes
//
m_patternName = "";
m_patternSize = 0;
// Sequence attributes
//
m_sequenceName = "";
// Note attributes
//
m_position = 0;
m_velocity = 0.0;
m_panL = 0.0;
m_panR = 0.0;
m_pitch = 0.0;
m_instrument = 0;
// Instrument attributes
//
m_id = 0;
m_muted = false;
m_instrumentVolumes.clear();
m_fileName = "";
// Global attributes
//
m_bpm = 0;
m_volume = 0.0;
m_name = "";
m_author = "";
m_notes = "";
m_songMode = false;
m_version = "";
m_currentProperty = "";
m_segment = 0;
m_currentTrackNb = 0;
m_segmentAdded = 0;
m_currentBar = 0;
m_newSegment = false;
return true;
}
bool
HydrogenXMLHandler::startElement(const TQString& /*namespaceURI*/,
const TQString& /*localName*/,
const TQString& qName,
const TQXmlAttributes& /*atts*/)
{
TQString lcName = qName.lower();
if (lcName == "note") {
if (m_inInstrument)
return false;
m_inNote = true;
} else if (lcName == "instrument") {
// Beware instrument attributes inside Notes
if (!m_inNote)
m_inInstrument = true;
} else if (lcName == "pattern") {
m_inPattern = true;
m_segmentAdded = false; // flag the segments being added
} else if (lcName == "sequence") {
// Create a new segment and set some flags
//
m_segment = new Segment();
m_newSegment = true;
m_inSequence = true;
}
m_currentProperty = lcName;
return true;
}
bool
HydrogenXMLHandler::endElement(const TQString& /*namespaceURI*/,
const TQString& /*localName*/,
const TQString& qName)
{
TQString lcName = qName.lower();
if (lcName == "note") {
RG_DEBUG << "HydrogenXMLHandler::endElement - Hydrogen Note : position = " << m_position
<< ", velocity = " << m_velocity
<< ", panL = " << m_panL
<< ", panR = " << m_panR
<< ", pitch = " << m_pitch
<< ", instrument = " << m_instrument
<< endl;
timeT barLength = m_composition->getBarEnd(m_currentBar) -
m_composition->getBarStart(m_currentBar);
timeT pos = m_composition->getBarStart(m_currentBar) +
timeT(
double(m_position) / double(m_patternSize) * double(barLength));
// Insert a rest if we've got a new segment
//
if (m_newSegment) {
Event *restEvent = new Event(Note::EventRestType,
m_composition->getBarStart(m_currentBar),
pos - m_composition->getBarStart(m_currentBar),
Note::EventRestSubOrdering);
m_segment->insert(restEvent);
m_newSegment = false;
}
// Create and insert this event
//
Event *noteEvent = new Event(Note::EventType,
pos, Note(Note::Semiquaver).getDuration());
// get drum mapping from instrument and calculate velocity
noteEvent->set
<Int>(
BaseProperties::PITCH, 36 + m_instrument);
noteEvent->set
<Int>(BaseProperties::VELOCITY,
int(127.0 * m_velocity * m_volume *
m_instrumentVolumes[m_instrument]));
m_segment->insert(noteEvent);
m_inNote = false;
} else if (lcName == "instrument" && m_inInstrument) {
RG_DEBUG << "HydrogenXMLHandler::endElement - Hydrogen Instrument : id = " << m_id
<< ", muted = " << m_muted
<< ", volume = " << m_instrumentVolumes[m_instrument]
<< ", filename = \"" << m_fileName << "\""
<< endl;
m_inInstrument = false;
} else if (lcName == "pattern") {
m_inPattern = false;
if (m_segmentAdded) {
// Add a blank track to demarcate patterns
//
Track *track = new Track
(m_currentTrackNb, m_drumInstrument, m_currentTrackNb,
"<blank spacer>", false);
m_currentTrackNb++;
m_composition->addTrack(track);
m_segmentAdded = false;
// Each pattern has it's own bar so that the imported
// song shows off each pattern a bar at a time.
//
m_currentBar++;
}
} else if (lcName == "sequence") {
// If we're closing out a sequencer tab and we have a m_segment then
// we should close up and add that segment. Only create if we have
// some Events in it
//
if (m_segment->size() > 0) {
m_segment->setTrack(m_currentTrackNb);
Track *track = new Track
(m_currentTrackNb, m_drumInstrument, m_currentTrackNb,
m_patternName, false);
m_currentTrackNb++;
// Enforce start and end markers for this bar so that we have a
// whole bar unit segment.
//
m_segment->setEndMarkerTime(m_composition->getBarEnd(m_currentBar));
TQString label = TQString("%1 - %2 %3 %4").tqarg(strtoqstr(m_patternName))
.tqarg(strtoqstr(m_sequenceName))
.tqarg(i18n(" imported from Hydrogen ")).tqarg(strtoqstr(m_version));
m_segment->setLabel(qstrtostr(label));
m_composition->addTrack(track);
m_composition->addSegment(m_segment);
m_segment = 0;
m_segmentAdded = true;
}
m_inSequence = false;
}
return true;
}
bool
HydrogenXMLHandler::characters(const TQString& chars)
{
TQString ch = chars.stripWhiteSpace();
if (ch == "")
return true;
if (m_inNote) {
if (m_currentProperty == "position") {
m_position = ch.toInt();
} else if (m_currentProperty == "velocity") {
m_velocity = qstrtodouble(ch);
} else if (m_currentProperty == "pan_L") {
m_panL = qstrtodouble(ch);
} else if (m_currentProperty == "pan_R") {
m_panR = qstrtodouble(ch);
} else if (m_currentProperty == "pitch") {
m_pitch = qstrtodouble(ch);
} else if (m_currentProperty == "instrument") {
m_instrument = ch.toInt();
// Standard kit conversion - hardcoded conversion for Hyrdogen's default
// drum kit. The m_instrument mapping for low values maps well onto the
// kick drum GM kit starting point (MIDI pitch = 36).
//
switch (m_instrument) {
case 11: // Cowbell
m_instrument = 20;
break;
case 12: // Ride Jazz
m_instrument = 15;
break;
case 14: // Ride Rock
m_instrument = 17;
break;
case 15: // Crash Jazz
m_instrument = 16;
break;
default:
break;
}
}
} else if (m_inInstrument) {
if (m_currentProperty == "id") {
m_id = ch.toInt();
} else if (m_currentProperty == "ismuted") {
if (ch.lower() == "true")
m_muted = true;
else
m_muted = false;
} else if (m_currentProperty == "filename") {
m_fileName = qstrtostr(chars); // don't strip whitespace from the filename
} else if (m_currentProperty == "volume") {
m_instrumentVolumes.push_back(qstrtodouble(ch));
}
} else if (m_inPattern) {
// Pattern attributes
if (m_currentProperty == "name") {
if (m_inSequence)
m_sequenceName = qstrtostr(chars);
else
m_patternName = qstrtostr(chars);
} else if (m_currentProperty == "size") {
m_patternSize = ch.toInt();
}
} else {
// Global attributes
if (m_currentProperty == "version") {
m_version = qstrtostr(chars);
} else if (m_currentProperty == "bpm") {
m_bpm = qstrtodouble(ch);
m_composition->addTempoAtTime
(0, Composition::getTempoForQpm(m_bpm));
} else if (m_currentProperty == "volume") {
m_volume = qstrtodouble(ch);
} else if (m_currentProperty == "name") {
m_name = qstrtostr(chars);
} else if (m_currentProperty == "author") {
m_author = qstrtostr(chars);
} else if (m_currentProperty == "notes") {
m_notes = qstrtostr(chars);
} else if (m_currentProperty == "mode") {
if (ch.lower() == "song")
m_songMode = true;
else
m_songMode = false;
}
}
return true;
}
bool
HydrogenXMLHandler::endDocument()
{
RG_DEBUG << "HydrogenXMLHandler::endDocument" << endl;
return true;
}
}
|