~ubuntu-branches/ubuntu/lucid/kmidimon/lucid

« back to all changes in this revision

Viewing changes to src/instrument.h

  • Committer: Bazaar Package Importer
  • Author(s): Nick Ellery
  • Date: 2009-08-10 18:45:19 UTC
  • mfrom: (1.1.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20090810184519-9q6ouf7i7708ni7s
Tags: 0.7.1-0ubuntu1
* New upstream release (LP: #411739).
  - Optionally translate notes, controllers and program numbers into names.
  - Support for GM, GS and XG standards, using .INS definition files.
  - New context menu option: adjust column sizes.
  - Fine grained event filters, in addition to the old coarse filters.
  - Fixed unregistered bug: don't change the current sequence PPQ/Tempo when 
    applying the preferences dialog.
  - New dialog showing information about the current loaded sequence.
  - Support for the SMF events: Sequence Number, Forced Channel, Forced Port 
    and SMPTE Offset.
  - Playback speed control: tempo "zoom" slider allowing continuous scaling 
    from 50% to 200%.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/***************************************************************************
 
2
 *   KMidimon - ALSA sequencer based MIDI monitor                          *
 
3
 *   Copyright (C) 2005-2009 Pedro Lopez-Cabanillas                        *
 
4
 *   plcl@users.sourceforge.net                                            *
 
5
 *                                                                         *
 
6
 * For this file, the following copyright notice is also applicable:       *
 
7
 * Copyright (C) 2005-2009, rncbc aka Rui Nuno Capela. All rights reserved.*
 
8
 * See http://qtractor.sourceforge.net                                     *
 
9
 *                                                                         *
 
10
 *   This program is free software; you can redistribute it and/or modify  *
 
11
 *   it under the terms of the GNU General Public License as published by  *
 
12
 *   the Free Software Foundation; either version 2 of the License, or     *
 
13
 *   (at your option) any later version.                                   *
 
14
 *                                                                         *
 
15
 *   This program is distributed in the hope that it will be useful,       *
 
16
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 
17
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 
18
 *   GNU General Public License for more details.                          *
 
19
 *                                                                         *
 
20
 *   You should have received a copy of the GNU General Public License     *
 
21
 *   along with this program; if not, write to the Free Software           *
 
22
 *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,            *
 
23
 *   MA 02110-1301, USA                                                    *
 
24
 ***************************************************************************/
 
25
 
 
26
/*  Library of compatible instrument definitions:
 
27
    ftp://ftp.cakewalk.com/pub/InstrumentDefinitions/ */
 
28
 
 
29
#ifndef __instrument_h
 
30
#define __instrument_h
 
31
 
 
32
#include <QStringList>
 
33
#include <QMap>
 
34
 
 
35
// Forward declarations.
 
36
class QTextStream;
 
37
 
 
38
//----------------------------------------------------------------------
 
39
// class InstrumentData -- instrument definition data classes.
 
40
//
 
41
 
 
42
class InstrumentData
 
43
{
 
44
public:
 
45
 
 
46
        typedef QMap<int, QString> DataMap;
 
47
 
 
48
        // Constructor.
 
49
        InstrumentData()
 
50
                : m_pData(new DataRef()) {}
 
51
        // Copy constructor.
 
52
        InstrumentData(const InstrumentData& data)
 
53
                { attach(data); }
 
54
        // Destructor.
 
55
        ~InstrumentData()
 
56
                { detach(); }
 
57
 
 
58
        // Assignment operator.
 
59
        InstrumentData& operator= (const InstrumentData& data)
 
60
        {
 
61
                if (m_pData != data.m_pData) {
 
62
                        detach();
 
63
                        attach(data);
 
64
                }
 
65
                return *this;
 
66
        }
 
67
 
 
68
        // Accessor operator.
 
69
        QString& operator[] (int iIndex) const
 
70
                { return m_pData->map[iIndex]; }
 
71
 
 
72
        // Property accessors.
 
73
        void setName(const QString& sName)
 
74
                { m_pData->name = sName; }
 
75
        const QString& name() const { return m_pData->name; }
 
76
 
 
77
        void setBasedOn(const QString& sBasedOn)
 
78
                { m_pData->basedOn = sBasedOn; }
 
79
        const QString& basedOn() const { return m_pData->basedOn; }
 
80
 
 
81
        // Indirect iterator stuff.
 
82
        typedef DataMap::Iterator Iterator;
 
83
        Iterator begin() { return m_pData->map.begin(); }
 
84
        Iterator end()   { return m_pData->map.end(); }
 
85
 
 
86
        typedef DataMap::ConstIterator ConstIterator;
 
87
        ConstIterator begin() const { return m_pData->map.constBegin(); }
 
88
        ConstIterator end()   const { return m_pData->map.constEnd(); }
 
89
 
 
90
        unsigned int count() const { return m_pData->map.count(); }
 
91
 
 
92
        bool contains(int iKey) const
 
93
                { return m_pData->map.contains(iKey); }
 
94
 
 
95
protected:
 
96
 
 
97
        // Copy/clone method.
 
98
        void attach(const InstrumentData& data)
 
99
                {  m_pData = data.m_pData; m_pData->refCount++; }
 
100
        // Destroy method.
 
101
        void detach()
 
102
                { if (--(m_pData->refCount) == 0) delete m_pData; }
 
103
 
 
104
private:
 
105
 
 
106
        // The ref-counted data.
 
107
        struct DataRef
 
108
        {
 
109
                // Default payload constructor.
 
110
                DataRef() : refCount(1) {};
 
111
                // Payload members.
 
112
                int     refCount;
 
113
                QString name;
 
114
                QString basedOn;
 
115
                DataMap map;
 
116
 
 
117
        } * m_pData;
 
118
};
 
119
 
 
120
class InstrumentDataList
 
121
        : public QMap<QString, InstrumentData> {};
 
122
 
 
123
class InstrumentPatches
 
124
        : public QMap<int, InstrumentData> {};
 
125
 
 
126
class InstrumentNotes
 
127
        : public QMap<int, InstrumentData> {};
 
128
 
 
129
class InstrumentKeys
 
130
        : public QMap<int, InstrumentNotes> {};
 
131
 
 
132
class InstrumentDrumFlags
 
133
        : public QMap<int, int> {};
 
134
 
 
135
class InstrumentDrums
 
136
        : public QMap<int, InstrumentDrumFlags> {};
 
137
 
 
138
 
 
139
//----------------------------------------------------------------------
 
140
// class Instrument -- instrument definition instance class.
 
141
//
 
142
 
 
143
class Instrument
 
144
{
 
145
public:
 
146
 
 
147
        // Constructor.
 
148
        Instrument()
 
149
                : m_pData(new DataRef()) {}
 
150
        // Copy constructor.
 
151
        Instrument(const Instrument& instr)
 
152
                { attach(instr); }
 
153
        // Destructor.
 
154
        ~Instrument()
 
155
                { detach(); }
 
156
 
 
157
        // Assignment operator.
 
158
        Instrument& operator= (const Instrument& instr)
 
159
        {
 
160
                if (m_pData != instr.m_pData) {
 
161
                        detach();
 
162
                        attach(instr);
 
163
                }
 
164
                return *this;
 
165
        }
 
166
 
 
167
        // Instrument title property accessors.
 
168
        void setInstrumentName(const QString& sInstrumentName)
 
169
                { m_pData->instrumentName = sInstrumentName; }
 
170
        const QString& instrumentName() const
 
171
                { return m_pData->instrumentName; }
 
172
 
 
173
        // BankSelMethod accessors.
 
174
        void setBankSelMethod(int iBankSelMethod)
 
175
                { m_pData->bankSelMethod = iBankSelMethod; }
 
176
        int bankSelMethod() const { return m_pData->bankSelMethod; }
 
177
 
 
178
        void setUsesNotesAsControllers(bool bUsesNotesAsControllers)
 
179
                { m_pData->usesNotesAsControllers = bUsesNotesAsControllers; }
 
180
        bool usesNotesAsControllers() const
 
181
                { return m_pData->usesNotesAsControllers; }
 
182
 
 
183
        // Patch banks accessors.
 
184
        const InstrumentPatches& patches() const
 
185
                { return m_pData->patches; }
 
186
        const InstrumentData& patch(int iBank) const;
 
187
        void setPatch(int iBank, const InstrumentData& patch)
 
188
                { m_pData->patches[iBank] = patch; }
 
189
 
 
190
        // Control names accessors.
 
191
        void setControlName(const QString& sControlName)
 
192
                { m_pData->control.setName(sControlName); }
 
193
        const QString& controlName() const
 
194
                { return m_pData->control.name(); }
 
195
        void setControl(const InstrumentData& control)
 
196
                { m_pData->control = control; }
 
197
        const InstrumentData& control() const
 
198
                { return m_pData->control; }
 
199
 
 
200
        // RPN names accessors.
 
201
        void setRpnName(const QString& sRpnName)
 
202
                { m_pData->rpn.setName(sRpnName); }
 
203
        const QString& rpnName() const
 
204
                { return m_pData->rpn.name(); }
 
205
        void setRpn(const InstrumentData& rpn)
 
206
                { m_pData->rpn = rpn; }
 
207
        const InstrumentData& rpn() const
 
208
                { return m_pData->rpn; }
 
209
 
 
210
        // NRPN names accessors.
 
211
        void setNrpnName(const QString& sNrpnName)
 
212
                { m_pData->nrpn.setName(sNrpnName); }
 
213
        const QString& nrpnName() const
 
214
                { return m_pData->nrpn.name(); }
 
215
        void setNrpn(const InstrumentData& nrpn)
 
216
                { m_pData->nrpn = nrpn; }
 
217
        const InstrumentData& nrpn() const
 
218
                { return m_pData->nrpn; }
 
219
 
 
220
        // Keys banks accessors.
 
221
        const InstrumentData& notes(int iBank, int iProg) const;
 
222
        void setNotes(int iBank, int iProg, const InstrumentData& notes)
 
223
                { m_pData->keys[iBank][iProg] = notes; }
 
224
        const InstrumentKeys& keys() const
 
225
                { return m_pData->keys; }
 
226
 
 
227
        // Drumflags banks accessors.
 
228
        bool isDrum(int iBank, int iProg) const;
 
229
        void setDrum(int iBank, int iProg, bool bDrum)
 
230
                { m_pData->drums[iBank][iProg] = (int) bDrum; }
 
231
        const InstrumentDrums& drums() const
 
232
                { return m_pData->drums; }
 
233
 
 
234
protected:
 
235
 
 
236
        // Copy/clone method.
 
237
        void attach(const Instrument& instr)
 
238
                {  m_pData = instr.m_pData; m_pData->refCount++; }
 
239
        // Destroy method.
 
240
        void detach()
 
241
                { if (--(m_pData->refCount) == 0) delete m_pData; }
 
242
 
 
243
private:
 
244
 
 
245
        // The ref-counted data.
 
246
        struct DataRef
 
247
        {
 
248
                // Default payload constructor.
 
249
                DataRef() : refCount(1),
 
250
                        bankSelMethod(0), usesNotesAsControllers(false) {};
 
251
                // Payload members.
 
252
                int                       refCount;
 
253
                int                       bankSelMethod;
 
254
                bool                      usesNotesAsControllers;
 
255
                QString                   instrumentName;
 
256
                InstrumentPatches patches;
 
257
                InstrumentData    control;
 
258
                InstrumentData    rpn;
 
259
                InstrumentData    nrpn;
 
260
                InstrumentKeys    keys;
 
261
                InstrumentDrums   drums;
 
262
 
 
263
        } * m_pData;
 
264
};
 
265
 
 
266
 
 
267
//----------------------------------------------------------------------
 
268
// class InstrumentList -- A Cakewalk .ins file container class.
 
269
//
 
270
 
 
271
class InstrumentList : public QMap<QString, Instrument>
 
272
{
 
273
public:
 
274
 
 
275
        // Open file methods.
 
276
        bool load(const QString& sFilename);
 
277
        bool save(const QString& sFilename);
 
278
 
 
279
        // The official loaded file list.
 
280
        const QStringList& files() const;
 
281
 
 
282
        // Manage a file list (out of sync)
 
283
        void appendFile(const QString& sFilename)
 
284
            { m_files.append(sFilename); }
 
285
        void removeFile(const QString& sFilename)
 
286
        {
 
287
                int iFile = m_files.indexOf(sFilename);
 
288
                if (iFile >= 0)
 
289
                        m_files.removeAt(iFile);
 
290
        }
 
291
 
 
292
        // Patch Names definition accessors.
 
293
        const InstrumentDataList& patches() const
 
294
                { return m_patches; }
 
295
        const InstrumentData& patch(const QString& sName)
 
296
                { return m_patches[sName]; }
 
297
 
 
298
        // Note Names definition accessors.
 
299
        const InstrumentDataList& notes() const
 
300
                { return m_notes; }
 
301
        InstrumentData& note(const QString& sName)
 
302
                { return m_notes[sName]; }
 
303
 
 
304
        // Controller Names definition accessors.
 
305
        const InstrumentDataList& controllers() const
 
306
                { return m_controllers; }
 
307
        InstrumentData& controller(const QString& sName)
 
308
                { return m_controllers[sName]; }
 
309
 
 
310
        // RPN Names definition accessors.
 
311
        const InstrumentDataList& rpns() const
 
312
                { return m_rpns; }
 
313
        InstrumentData& rpn(const QString& sName)
 
314
                { return m_rpns[sName]; }
 
315
 
 
316
        // NRPN Names definition accessors.
 
317
        const InstrumentDataList& nrpns() const
 
318
                { return m_nrpns; }
 
319
        InstrumentData& nrpn(const QString& sName)
 
320
                { return m_nrpns[sName]; }
 
321
 
 
322
        // Clear all contents.
 
323
        void clearAll();
 
324
 
 
325
        // Special instrument list merge method.
 
326
        void merge(const InstrumentList& instruments);
 
327
 
 
328
protected:
 
329
 
 
330
        // Internal instrument data list save method helpers.
 
331
        void saveDataList(QTextStream& ts, const InstrumentDataList& list);
 
332
        void saveData(QTextStream& ts, const InstrumentData& data);
 
333
 
 
334
        // Special instrument data list merge method.
 
335
        void mergeDataList(InstrumentDataList& dst,
 
336
                const InstrumentDataList& src);
 
337
 
 
338
private:
 
339
 
 
340
        // To hold the names definition lists.
 
341
        InstrumentDataList m_patches;
 
342
        InstrumentDataList m_notes;
 
343
        InstrumentDataList m_controllers;
 
344
        InstrumentDataList m_rpns;
 
345
        InstrumentDataList m_nrpns;
 
346
 
 
347
        // To old the official file list.
 
348
        QStringList m_files;
 
349
};
 
350
 
 
351
 
 
352
#endif  // __instrument_h