1 //=========================================================
2 //  MusE
3 //  Linux Music Editor
4 //  $Id: part.h,v 1.5.2.4 2009/05/24 21:43:44 terminator356 Exp $
5 //
6 //  (C) Copyright 1999/2000 Werner Schweer (ws@seh.de)
7 //  Additions, modifications (C) Copyright 2011 Tim E. Real (terminator356 on users DOT sourceforge DOT net)
8 //
9 //  This program is free software; you can redistribute it and/or
10 //  modify it under the terms of the GNU General Public License
11 //  as published by the Free Software Foundation; version 2 of
12 //  the License, or (at your option) any later version.
13 //
14 //  This program is distributed in the hope that it will be useful,
15 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
16 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 //  GNU General Public License for more details.
18 //
19 //  You should have received a copy of the GNU General Public License
20 //  along with this program; if not, write to the Free Software
21 //  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
22 //
23 //=========================================================
24 
25 #ifndef __PART_H__
26 #define __PART_H__
27 
28 #include <map>
29 #include <vector>
30 #include <climits>
31 
32 #include <QUuid>
33 #include <QString>
34 
35 #include "pos.h"
36 #include "event.h"
37 
38 namespace MusECore {
39 
40 
41 // Forward declarations:
42 class Track;
43 class MidiTrack;
44 class WaveTrack;
45 class Xml;
46 
47 //---------------------------------------------------------
48 //   MidiCtrlViewState
49 //   Stores the initial controller view state
50 //---------------------------------------------------------
51 
52 struct MidiCtrlViewState
53 {
54   // Midi controller number.
55   int _num;
56   // Per note velocity display.
57   bool _perNoteVel;
58 
MidiCtrlViewStateMidiCtrlViewState59   MidiCtrlViewState() : _num(0), _perNoteVel(false) { }
_numMidiCtrlViewState60   MidiCtrlViewState(int num, bool perNoteVel = false) : _num(num), _perNoteVel(perNoteVel) { }
61 
62   void read(Xml&);
63   void write(int, Xml&) const;
64 };
65 
66 typedef std::vector<MidiCtrlViewState> MidiCtrlViewStateList;
67 typedef MidiCtrlViewStateList::iterator iMidiCtrlViewState;
68 typedef MidiCtrlViewStateList::const_iterator ciMidiCtrlViewState;
69 
70 //---------------------------------------------------------
71 //   MidiPartViewState
72 //   Stores the initial view state
73 //---------------------------------------------------------
74 
75 class MidiPartViewState
76 {
77 private:
78   int _xscroll;
79   int _yscroll;
80   int _xscale;
81   int _yscale;
82   MidiCtrlViewStateList _controllers;
83 
84 public:
MidiPartViewState()85   MidiPartViewState() : _xscroll(INT_MAX), _yscroll(INT_MAX), _xscale(INT_MAX), _yscale(INT_MAX) { }
MidiPartViewState(int xscroll,int yscroll,int xscale,int yscale)86   MidiPartViewState(int xscroll, int yscroll, int xscale, int yscale) :
87         _xscroll(xscroll), _yscroll(yscroll), _xscale(xscale), _yscale(yscale) { }
MidiPartViewState(const MidiPartViewState & vs)88   MidiPartViewState(const MidiPartViewState& vs) :
89     _xscroll(vs._xscroll), _yscroll(vs._yscroll),
90     _xscale(vs._xscale), _yscale(vs._yscale),
91     _controllers(vs._controllers) { }
92   MidiPartViewState& operator=(const MidiPartViewState& vs) {
93     _xscroll = vs._xscroll; _yscroll = vs._yscroll;
94     _xscale = vs._xscale; _yscale = vs._yscale;
95     _controllers = vs._controllers; return *this;
96     }
97 
isValid()98   bool isValid() const { return _xscroll != INT_MAX && _yscroll != INT_MAX && _xscale != INT_MAX && _yscale != INT_MAX; }
99 
xscroll()100   int xscroll() const { return _xscroll; }
yscroll()101   int yscroll() const { return _yscroll; }
xscale()102   int xscale() const { return _xscale; }
yscale()103   int yscale() const { return _yscale; }
104 
setXScroll(int x)105   void setXScroll(int x) { _xscroll = x; }
setYScroll(int y)106   void setYScroll(int y) { _yscroll = y; }
setXScale(int xs)107   void setXScale(int xs) { _xscale = xs; }
setYScale(int ys)108   void setYScale(int ys) { _yscale = ys; }
109 
controllers()110   const MidiCtrlViewStateList& controllers() { return _controllers; }
addController(const MidiCtrlViewState & viewState)111   void addController(const MidiCtrlViewState& viewState) { _controllers.push_back(viewState); }
clearControllers()112   void clearControllers() { _controllers.clear(); }
113 
114   void read(Xml&);
115   void write(int, Xml&) const;
116 };
117 
118 //---------------------------------------------------------
119 //   Part
120 //---------------------------------------------------------
121 
122 class Part : public PosLen {
123    public:
124       enum HiddenEventsType { NoEventsHidden = 0x00, LeftEventsHidden = 0x01, RightEventsHidden = 0x02};
125       enum PartType { MidiPartType = 0x01, WavePartType = 0x02 };
126 
127       static Part* readFromXml(Xml&, Track*, bool doClone = false, bool toTrack = true);
128 
129    private:
130       static int snGen;
131       int _sn;
132       int _clonemaster_sn; // the serial number of some clone in the chain. every member of the chain has the same value here.
133 
134       QString _name;
135       bool _selected;
136       bool _mute;
137       int _colorIndex;
138 
139    protected:
140       Track* _track;
141       EventList _events;
142       Part* _prevClone;
143       Part* _nextClone;
144       Part* _backupClone; // when a part gets removed, it's still there; and for undo-ing the remove, it must know about where it was clone-chained to.
145       mutable int _hiddenEvents;   // Combination of HiddenEventsType.
146       MidiPartViewState _viewState;
147 
148    public:
149       Part(Track*);
150       virtual ~Part();
151 
152       virtual PartType partType() const = 0;
153 
154       virtual Part* duplicate() const;
155       virtual Part* duplicateEmpty() const = 0;
156       virtual Part* createNewClone() const; // this does NOT chain clones yet. Chain is updated only when the part is really added!
157       virtual void splitPart(unsigned int tickpos, Part*& p1, Part*& p2) const;
158 
setSn(int n)159       void setSn(int n)                { _sn = n; }
clonemaster_sn()160       int clonemaster_sn() const       { return _clonemaster_sn; }
sn()161       int sn() const                   { return _sn; }
newSn()162       int newSn()                      { return snGen++; }
163 
name()164       const QString& name() const      { return _name; }
setName(const QString & s)165       void setName(const QString& s)   { _name = s; }
selected()166       bool selected() const            { return _selected; }
setSelected(bool f)167       void setSelected(bool f)         { _selected = f; }
168       // Select or unselect a range of events. If t0 == t1, ALL events will be affected.
169       // The t0 and t1 can be ticks or frames depending on the type of events. Unused for now.
170       // Returns true if anything changed.
171       bool selectEvents(bool select, unsigned long t0 = 0, unsigned long t1 = 0);
mute()172       bool mute() const                { return _mute; }
setMute(bool b)173       void setMute(bool b)             { _mute = b; }
track()174       Track* track() const             { return _track; }
setTrack(Track * t)175       void setTrack(Track*t)           { _track = t; }
events()176       const EventList& events() const  { return _events; }
nonconst_events()177       EventList& nonconst_events()     { return _events; }
colorIndex()178       int colorIndex() const           { return _colorIndex; }
setColorIndex(int idx)179       void setColorIndex(int idx)      { _colorIndex = idx; }
180 
181       bool isCloneOf(const Part*) const;
hasClones()182       bool hasClones() const           { return _prevClone!=this || _nextClone!=this; }
183       int nClones() const;
prevClone()184       Part* prevClone() const          { return _prevClone; } // FINDMICHJETZT make it const Part*!
nextClone()185       Part* nextClone() const          { return _nextClone; }
backupClone()186       Part* backupClone() const        { return _backupClone; }
187 
188       void unchainClone();
189       void chainClone(Part* p); // *this is made a sibling of p! p is not touched (except for its clone-chain), whereas this->events will get altered
190       void rechainClone(); // re-chains the part to the same clone chain it was unchained before
191 
192       // Returns combination of HiddenEventsType enum.
hasHiddenEvents()193       virtual int hasHiddenEvents() const { return _hiddenEvents; }
194 
195       iEvent addEvent(Event& p); // this does not care about clones! If the part is a clone, be sure to execute this on all clones (with duplicated Events, that is!)
196       // Returns true if any event was opened. Does not operate on the part's clones, if any.
openAllEvents()197       virtual bool openAllEvents() { return false; };
198       // Returns true if any event was closed. Does not operate on the part's clones, if any.
closeAllEvents()199       virtual bool closeAllEvents() { return false; };
200 
201       virtual void write(int, Xml&, bool isCopy = false, bool forceWavePaths = false) const;
202 
203       virtual void dump(int n = 0) const;
204 
viewState()205       const MidiPartViewState& viewState() const { return _viewState; }
viewState()206       MidiPartViewState& viewState() { return _viewState; }
207       virtual void setViewState(const MidiPartViewState& vs);
208       };
209 
210 
211 //---------------------------------------------------------
212 //   MidiPart
213 //---------------------------------------------------------
214 
215 class MidiPart : public Part {
216 
217    public:
MidiPart(MidiTrack * t)218       MidiPart(MidiTrack* t) : Part((Track*)t) {}
~MidiPart()219       virtual ~MidiPart() {}
220 
partType()221       virtual PartType partType() const { return MidiPartType; }
222 
223       virtual MidiPart* duplicate() const;
224       virtual MidiPart* duplicateEmpty() const;
225       virtual MidiPart* createNewClone() const;
226 
227 
track()228       MidiTrack* track() const   { return (MidiTrack*)Part::track(); }
229       // Returns combination of HiddenEventsType enum.
230       int hasHiddenEvents() const;
231 
232       virtual void dump(int n = 0) const;
233       };
234 
235 
236 //---------------------------------------------------------
237 //   WavePart
238 //---------------------------------------------------------
239 
240 class WavePart : public Part {
241 
242       // p3.3.31
243       //AudioConvertMap _converters;
244 
245    public:
246       WavePart(WaveTrack* t);
~WavePart()247       virtual ~WavePart() {}
248 
partType()249       virtual PartType partType() const { return WavePartType; }
250 
251       virtual WavePart* duplicate() const;
252       virtual WavePart* duplicateEmpty() const;
253       virtual WavePart* createNewClone() const;
254 
track()255       WaveTrack* track() const   { return (WaveTrack*)Part::track(); }
256       // Returns combination of HiddenEventsType enum.
257       int hasHiddenEvents() const;
258       // Returns true if any event was opened. Does not operate on the part's clones, if any.
259       bool openAllEvents();
260       // Returns true if any event was closed. Does not operate on the part's clones, if any.
261       bool closeAllEvents();
262 
263       virtual void dump(int n = 0) const;
264       };
265 
266 struct ClonePart {
267       const Part* cp;
268       int id;
269       QUuid _uuid;
270       bool is_deleted;
271       ClonePart(const Part*, int i = -1);
272       };
273 
274 typedef std::list<ClonePart> CloneList;
275 typedef CloneList::iterator iClone;
276 
277 //---------------------------------------------------------
278 //   PartList
279 //---------------------------------------------------------
280 
281 typedef std::pair<unsigned int, Part*> PartListInsertPair_t;
282 typedef std::multimap<unsigned int, Part*, std::less<unsigned int> > PartList_t;
283 
284 class PartList : public PartList_t {
285    public:
286       iterator findPart(unsigned tick);
287       iterator add(Part*);
288       void remove(Part* part);
289       int index(const Part*) const;
290       Part* find(int idx);
clearDelete()291       void clearDelete() {
292             for (iterator i = begin(); i != end(); ++i)
293                   delete i->second;
294             clear();
295             }
296       };
297 
298 typedef PartList_t::iterator iPart;
299 typedef PartList_t::reverse_iterator riPart;
300 typedef PartList_t::const_iterator ciPart;
301 
302 extern void chainCheckErr(Part* p);
303 extern void unchainTrackParts(Track* t);
304 extern void chainTrackParts(Track* t);
305 extern void addPortCtrlEvents(Part* part, bool doClones);
306 extern void removePortCtrlEvents(Part* part, bool doClones);
307 
308 } // namespace MusECore
309 
310 namespace MusEGlobal {
311 extern MusECore::CloneList cloneList;
312 }
313 
314 #endif
315 
316