summaryrefslogtreecommitdiffstats
path: root/src/commands/segment/SegmentAutoSplitCommand.cpp
blob: 5f3630f5f77460a94b1152e3cd8882e56d142e7c (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
/* -*- 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 "SegmentAutoSplitCommand.h"

#include "base/Event.h"
#include "misc/Debug.h"
#include "misc/Strings.h"
#include "base/Composition.h"
#include "base/NotationTypes.h"
#include "base/Segment.h"
#include <tqstring.h>


namespace Rosegarden
{

struct AutoSplitPoint
{
    timeT time;
    timeT lastSoundTime;
    Clef clef;
    Rosegarden::Key key;
    AutoSplitPoint(timeT t, timeT lst, Clef c, Rosegarden::Key k) :
	time(t), lastSoundTime(lst), clef(c), key(k) { }
};

SegmentAutoSplitCommand::SegmentAutoSplitCommand(Segment *segment) :
        KNamedCommand(getGlobalName()),
        m_segment(segment),
        m_composition(segment->getComposition()),
        m_detached(false)
{}

SegmentAutoSplitCommand::~SegmentAutoSplitCommand()
{
    if (m_detached) {
        delete m_segment;
    } else {
        for (unsigned int i = 0; i < m_newSegments.size(); ++i) {
            delete m_newSegments[i];
        }
    }
}

void
SegmentAutoSplitCommand::execute()
{
    if (m_newSegments.size() == 0) {

        std::vector<AutoSplitPoint> splitPoints;

        Clef clef;
        Key key;
        timeT segmentStart = m_segment->getStartTime();
        timeT lastSoundTime = segmentStart;
        timeT lastSplitTime = segmentStart - 1;

        for (Segment::iterator i = m_segment->begin();
                m_segment->isBeforeEndMarker(i); ++i) {

            timeT myTime = (*i)->getAbsoluteTime();
            int barNo = m_composition->getBarNumber(myTime);

            if ((*i)->isa(Clef::EventType)) {
                clef = Clef(**i);
            } else if ((*i)->isa(Key::EventType)) {
                key = Key(**i);
            }

            if (myTime <= lastSplitTime)
                continue;

            bool newTimeSig = false;
            TimeSignature tsig =
                m_composition->getTimeSignatureInBar(barNo, newTimeSig);

            if (newTimeSig) {

                // If there's a new time sig in this bar and we haven't
                // already made a split in this bar, make one

                if (splitPoints.size() == 0 ||
                        m_composition->getBarNumber
                        (splitPoints[splitPoints.size() - 1].time) < barNo) {

                    splitPoints.push_back(AutoSplitPoint(myTime, lastSoundTime,
                                                         clef, key));
                    lastSoundTime = lastSplitTime = myTime;
                }

            } else if ((*i)->isa(Note::EventRestType)) {

                // Otherwise never start a subsegment on a rest

                continue;

            } else {

                // When we meet a non-rest event, start a new split
                // if an entire bar has passed since the last one

                int lastSoundBarNo = m_composition->getBarNumber(lastSoundTime);

                if (lastSoundBarNo < barNo - 1 ||
                        (lastSoundBarNo == barNo - 1 &&
                         m_composition->getBarStartForTime(lastSoundTime) ==
                         lastSoundTime &&
                         lastSoundTime > segmentStart)) {

                    splitPoints.push_back
                    (AutoSplitPoint
                     (m_composition->getBarStartForTime(myTime), lastSoundTime,
                      clef, key));
                    lastSplitTime = myTime;
                }
            }

            lastSoundTime = std::max(lastSoundTime, myTime + (*i)->getDuration());
        }

        for (unsigned int split = 0; split <= splitPoints.size(); ++split) {

            Segment *newSegment = new Segment();
            newSegment->setTrack(m_segment->getTrack());
            newSegment->setLabel(qstrtostr(i18n("%1 (part)").arg
                                           (strtoqstr(m_segment->getLabel()))));
            newSegment->setColourIndex(m_segment->getColourIndex());

            timeT startTime = segmentStart;
            if (split > 0) {

                RG_DEBUG << "Auto-split point " << split - 1 << ": time "
                << splitPoints[split - 1].time << ", lastSoundTime "
                << splitPoints[split - 1].lastSoundTime << endl;

                startTime = splitPoints[split - 1].time;
                newSegment->insert(splitPoints[split - 1].clef.getAsEvent(startTime));
                newSegment->insert(splitPoints[split - 1].key.getAsEvent(startTime));
            }

            Segment::iterator i = m_segment->findTime(startTime);

            // A segment has to contain at least one note to be a worthy
            // candidate for adding back into the composition
            bool haveNotes = false;

            while (m_segment->isBeforeEndMarker(i)) {
                timeT t = (*i)->getAbsoluteTime();
                if (split < splitPoints.size() &&
                        t >= splitPoints[split].lastSoundTime)
                    break;
                if ((*i)->isa(Note::EventType))
                    haveNotes = true;
                newSegment->insert(new Event(**i));
                ++i;
            }

            if (haveNotes)
                m_newSegments.push_back(newSegment);
            else
                delete newSegment;
        }
    }

    m_composition->detachSegment(m_segment);
    for (unsigned int i = 0; i < m_newSegments.size(); ++i) {
        m_composition->addSegment(m_newSegments[i]);
    }
    m_detached = true;
}

void
SegmentAutoSplitCommand::unexecute()
{
    for (unsigned int i = 0; i < m_newSegments.size(); ++i) {
        m_composition->detachSegment(m_newSegments[i]);
    }
    m_composition->addSegment(m_segment);
    m_detached = false;
}

}