summaryrefslogtreecommitdiff
path: root/muse2/muse/structure.cpp
blob: 6c9d25dd22d87be1d323d3d783a648ef631527ea (plain)
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
//=========================================================
//  MusE
//  Linux Music Editor
//  $Id: structure.cpp,v 1.113.2.68 2009/12/21 14:51:51 spamatica Exp $
//
//  (C) Copyright 1999-2004 Werner Schweer (ws@seh.de)
//  (C) Copyright 2011  Robert Jonsson (rj@spamatica.se)
//
//  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; version 2 of
//  the License, or (at your option) any later version.
//
//  This program is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU General Public License for more details.
//
//  You should have received a copy of the GNU General Public License
//  along with this program; if not, write to the Free Software
//  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
//
//=========================================================

#include <qmessagebox.h>
#include "app.h"
#include "track.h"
#include "song.h"
#include "tempo.h"
#include "al/sig.h"
#include "keyevent.h"
#include "audio.h"
#include "marker/marker.h"
#include "structure.h"
#include "globals.h"

#include <set>
using std::set;

//---------------------------------------------------------
//   adjustGlobalLists
//    helper that adjusts tempo, sig, key and marker
//    lists everything from startPos is adjusted
//    'diff' number of ticks.
//---------------------------------------------------------

void adjustGlobalLists(Undo& operations, int startPos, int diff)
{
  const TempoList* t = &tempomap;
  const AL::SigList* s   = &AL::sigmap;
  const KeyList* k   = &keymap;

  criTEvent it   = t->rbegin();
  AL::criSigEvent is = s->rbegin();
  criKeyEvent ik = k->rbegin();

  // key
  for (; ik != k->rend(); ik++) {
    const KeyEvent &ev = (KeyEvent)ik->second;
    int tick = ev.tick;
    int key = ev.key;
    if (tick < startPos )
      break;

    if (tick > startPos && tick +diff < startPos ) { // remove
      operations.push_back(UndoOp(UndoOp::DeleteKey, tick, key));
    }
    else {
      operations.push_back(UndoOp(UndoOp::DeleteKey,tick, key));
      operations.push_back(UndoOp(UndoOp::AddKey,tick+diff, key));
      }
  }

  // tempo
  for (; it != t->rend(); it++) {
    const TEvent* ev = (TEvent*)it->second;
    int tick = ev->tick;
    int tempo = ev->tempo;
    if (tick < startPos )
      break;

    if (tick > startPos && tick +diff < startPos ) { // remove
      operations.push_back(UndoOp(UndoOp::DeleteTempo,tick, tempo));
    }
    else {
      operations.push_back(UndoOp(UndoOp::DeleteTempo,tick, tempo));
      operations.push_back(UndoOp(UndoOp::AddTempo,tick+diff, tempo));
      }
  }

  // sig
  for (; is != s->rend(); is++) {
    const AL::SigEvent* ev = (AL::SigEvent*)is->second;
    int tick = ev->tick;
    if (tick < startPos )
      break;

    int z = ev->sig.z;
    int n = ev->sig.n;
    if (tick > startPos && tick +diff < startPos ) { // remove
      operations.push_back(UndoOp(UndoOp::DeleteSig,tick, z, n));
    }
    else {
      operations.push_back(UndoOp(UndoOp::DeleteSig,tick, z, n));
      operations.push_back(UndoOp(UndoOp::AddSig,tick+diff, z, n));
    }
  }

  MarkerList *markerlist = song->marker();
  for(iMarker i = markerlist->begin(); i != markerlist->end(); ++i)
  {
      Marker* m = &i->second;
      int tick = m->tick();
      if (tick > startPos)
      {
        if (tick + diff < startPos ) { // these ticks should be removed
          Marker *oldMarker = new Marker();
          *oldMarker = *m;
          markerlist->remove(m);
          operations.push_back(UndoOp(UndoOp::ModifyMarker,oldMarker, 0));
        } else {
          Marker *oldMarker = new Marker();
          *oldMarker = *m;
          m->setTick(tick + diff);
          operations.push_back(UndoOp(UndoOp::ModifyMarker,oldMarker, m));
        }
      }
  }

}

//---------------------------------------------------------
//   globalCut
//    - remove area between left and right locator
//    - cut master track
//---------------------------------------------------------

void globalCut()
      {
      int lpos = song->lpos();
      int rpos = song->rpos();
      if ((lpos - rpos) >= 0)
            return;

      Undo operations;
      TrackList* tracks = song->tracks();
      bool at_least_one_selected=false;
      
      for (iTrack it = tracks->begin(); it != tracks->end(); ++it)
            if ( (*it)->selected() ) {
                  at_least_one_selected=true;
                  break;
                  }
      
      for (iTrack it = tracks->begin(); it != tracks->end(); ++it) {
            MidiTrack* track = dynamic_cast<MidiTrack*>(*it);
            if (track == 0 || (at_least_one_selected && !track->selected()))
                  continue;
            PartList* pl = track->parts();
            for (iPart p = pl->begin(); p != pl->end(); ++p) {
                  Part* part = p->second;
                  int t = part->tick();
                  int l = part->lenTick();
                  if (t + l <= lpos)
                        continue;
                  if ((t >= lpos) && ((t+l) <= rpos)) {
                        operations.push_back(UndoOp(UndoOp::DeletePart,part));
                        }
                  else if ((t < lpos) && ((t+l) > lpos) && ((t+l) <= rpos)) {
                        // remove part tail
                        int len = lpos - t;
                        MidiPart* nPart = new MidiPart(*(MidiPart*)part);
                        nPart->setLenTick(len);
                        //
                        // cut Events in nPart
                        EventList* el = nPart->events();
                        for (iEvent ie = el->lower_bound(len); ie != el->end(); ++ie)
                              operations.push_back(UndoOp(UndoOp::DeleteEvent,ie->second, nPart, false, false));

                        operations.push_back(UndoOp(UndoOp::ModifyPart,part, nPart, true, true));
                        }
                  else if ((t < lpos) && ((t+l) > lpos) && ((t+l) > rpos)) {
                        //----------------------
                        // remove part middle
                        //----------------------

                        MidiPart* nPart = new MidiPart(*(MidiPart*)part);
                        EventList* el = nPart->events();
                        iEvent is = el->lower_bound(lpos-t);
                        iEvent ie = el->lower_bound(rpos-t); //lower bound, because we do NOT want to erase the events at rpos-t
                        for (iEvent i = is; i != ie; ++i)
                              operations.push_back(UndoOp(UndoOp::DeleteEvent,i->second, nPart, false, false));

                        for (iEvent i = el->lower_bound(rpos-t); i != el->end(); ++i) {
                              Event event = i->second;
                              Event nEvent = event.clone();
                              nEvent.setTick(nEvent.tick() - (rpos-lpos));
                              // Indicate no undo, and do not do port controller values and clone parts.
                              operations.push_back(UndoOp(UndoOp::ModifyEvent,nEvent, event, nPart, false, false));
                              }
                        nPart->setLenTick(l - (rpos-lpos));
                        // Indicate no undo, and do port controller values and clone parts.
                        operations.push_back(UndoOp(UndoOp::ModifyPart,part, nPart, true, true));
                        }
                  else if ((t >= lpos) && (t < rpos) && (t+l) > rpos) {
                        // remove part head
                        
                        MidiPart* nPart = new MidiPart(*(MidiPart*)part);
                        EventList* el = nPart->events();
                        iEvent i_end = el->lower_bound(rpos-t); //lower bound, because we do NOT want to erase the events at rpos-t
                        for (iEvent it = el->begin(); it!=i_end; it++)
                              operations.push_back(UndoOp(UndoOp::DeleteEvent,it->second, nPart, false, false));
                        
                        for (iEvent it = el->lower_bound(rpos-t); it!=el->end(); it++) {
                              Event event = it->second;
                              Event nEvent = event.clone();
                              nEvent.setTick(nEvent.tick() - (rpos-t));
                              // Indicate no undo, and do not do port controller values and clone parts.
                              operations.push_back(UndoOp(UndoOp::ModifyEvent,nEvent, event, nPart, false, false));
                              }
                        
                        nPart->setLenTick(l - (rpos-t));
                        operations.push_back(UndoOp(UndoOp::ModifyPart,part, nPart, true, true));
                        }
                  else if (t >= rpos) {
                        MidiPart* nPart = new MidiPart(*(MidiPart*)part);
                        int nt = part->tick();
                        nPart->setTick(nt - (rpos -lpos));
                        // Indicate no undo, and do port controller values but not clone parts.
                        operations.push_back(UndoOp(UndoOp::ModifyPart,part, nPart, true, false));
                        }
                  }
            }
      int diff = lpos - rpos;
      adjustGlobalLists(operations, lpos, diff);

      song->applyOperationGroup(operations);
      }

//---------------------------------------------------------
//   globalInsert
//    - insert empty space at left locator position upto
//      right locator
//    - insert in master track
//---------------------------------------------------------

void globalInsert()
      {
      Undo operations=movePartsTotheRight(song->lpos(), song->rpos()-song->lpos(), true);
      song->applyOperationGroup(operations);
      }


Undo movePartsTotheRight(unsigned int startTicks, int moveTicks, bool only_selected, set<Track*>* tracklist)
      {
      if (moveTicks<=0)
            return Undo();

      Undo operations;
      TrackList* tracks = song->tracks();
      bool at_least_one_selected=false;
      
      for (iTrack it = tracks->begin(); it != tracks->end(); ++it)
            if ( (*it)->selected() ) {
                  at_least_one_selected=true;
                  break;
                  }
      
      
      for (iTrack it = tracks->begin(); it != tracks->end(); ++it) {
            MidiTrack* track = dynamic_cast<MidiTrack*>(*it);
            if ( (track == 0) ||
                 (only_selected && at_least_one_selected && !track->selected()) ||
                 (tracklist && tracklist->find(track)==tracklist->end()) )
                  continue;
            PartList* pl = track->parts();
            for (riPart p = pl->rbegin(); p != pl->rend(); ++p) {
                  Part* part = p->second;
                  unsigned t = part->tick();
                  int l = part->lenTick();
                  if (t + l <= startTicks)
                        continue;
                  if (startTicks > t && startTicks < (t+l)) {
                        MidiPart* nPart = new MidiPart(*(MidiPart*)part);
                        nPart->setLenTick(l + moveTicks);
                        EventList* el = nPart->events();

                        for (riEvent i = el->rbegin(); i!=el->rend(); ++i)
                        {
                              if (i->first < startTicks-t)
                                    break;
                              Event event  = i->second;
                              Event nEvent = i->second.clone();
                              nEvent.setTick(nEvent.tick() + moveTicks);
                              operations.push_back(UndoOp(UndoOp::ModifyEvent, nEvent, event, nPart, false, false));
                              }
                        operations.push_back(UndoOp(UndoOp::ModifyPart, part, nPart, true, true));
                        }
                  else if (t >= startTicks) {
                        MidiPart* nPart = new MidiPart(*(MidiPart*)part);
                        nPart->setTick(t + moveTicks);
                        operations.push_back(UndoOp(UndoOp::ModifyPart, part, nPart, true, false));
                        }
                  }
            }

      adjustGlobalLists(operations, startTicks, moveTicks);

      return operations;
      }


//---------------------------------------------------------
//   globalSplit
//    - split all parts at the song position pointer
//---------------------------------------------------------

void globalSplit()
      {
      int pos = song->cpos();
      Undo operations;
      TrackList* tracks = song->tracks();
      bool at_least_one_selected=false;
      
      for (iTrack it = tracks->begin(); it != tracks->end(); ++it)
            if ( (*it)->selected() ) {
                  at_least_one_selected=true;
                  break;
                  }
      

      for (iTrack it = tracks->begin(); it != tracks->end(); ++it) {
            Track* track = *it;
            if (track == 0 || (at_least_one_selected && !track->selected()))
                  continue;

            PartList* pl = track->parts();
            for (iPart p = pl->begin(); p != pl->end(); ++p) {
                  Part* part = p->second;
                  int p1 = part->tick();
                  int l0 = part->lenTick();
                  if (pos > p1 && pos < (p1+l0)) {
                        Part* p1;
                        Part* p2;
                        track->splitPart(part, pos, p1, p2);

                        p1->events()->incARef(-1); // the later song->applyOperationGroup() will increment it
                        p2->events()->incARef(-1); // so we must decrement it first :/

                        //song->informAboutNewParts(part, p1); // is unneccessary because of ModifyPart
                        song->informAboutNewParts(part, p2);
                        operations.push_back(UndoOp(UndoOp::ModifyPart,part, p1, true, false));
                        operations.push_back(UndoOp(UndoOp::AddPart,p2));
                        break;
                        }
                  }
            }
      song->applyOperationGroup(operations);
      }