File: importmidi_tie.cpp

package info (click to toggle)
musescore2 2.3.2%2Bdfsg4-16
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 170,464 kB
  • sloc: cpp: 262,612; xml: 176,707; sh: 3,377; ansic: 1,246; python: 356; makefile: 227; perl: 82; pascal: 78
file content (145 lines) | stat: -rw-r--r-- 4,832 bytes parent folder | download
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
//=============================================================================
//  MuseScore
//  Music Composition & Notation
//
//  Copyright (C) 2013 Werner Schweer
//
//  This program is free software; you can redistribute it and/or modify
//  it under the terms of the GNU General Public License version 2
//  as published by the Free Software Foundation and appearing in
//  the file LICENCE.GPL
//=============================================================================

#include "importmidi_tie.h"
#include "libmscore/element.h"
#include "libmscore/segment.h"
#include "libmscore/chordrest.h"
#include "libmscore/chord.h"
#include "libmscore/note.h"

#ifdef IMPORTMIDI_DEBUG
#include "libmscore/staff.h"
#include "libmscore/score.h"
#include "libmscore/measure.h"
#endif


namespace Ms {
namespace MidiTie {


bool isTied(const Segment *seg, int strack, int voice,
            Ms::Tie*(Note::*tieFunc)() const)
      {
      ChordRest *cr = static_cast<ChordRest *>(seg->element(strack + voice));
      if (cr && cr->type() == Element::Type::CHORD) {
            Chord *chord = static_cast<Chord *>(cr);
            const auto &notes = chord->notes();
            for (const Note *note: notes) {
                  if ((note->*tieFunc)())
                        return true;
                  }
            }
      return false;
      }

bool isTiedFor(const Segment *seg, int strack, int voice)
      {
      return isTied(seg, strack, voice, &Note::tieFor);
      }

bool isTiedBack(const Segment *seg, int strack, int voice)
      {
      return isTied(seg, strack, voice, &Note::tieBack);
      }


void TieStateMachine::addSeg(const Segment *seg, int strack)
      {
      bool isChord = false;
      for (int voice = 0; voice < VOICES; ++voice) {
            Element* el = seg->element(strack + voice);
            if (!el || el->type() != Element::Type::CHORD)
                  continue;
            if (!isChord)
                  isChord = true;

            bool tiedFor = isTiedFor(seg, strack, voice);
            bool tiedBack = isTiedBack(seg, strack, voice);

            if (tiedFor && !tiedBack)
                  tiedVoices.insert(voice);
            else if (!tiedFor && tiedBack)
                  tiedVoices.erase(voice);
            }
      if (!isChord)
            return;

      if (tiedVoices.empty() && (state_ == State::TIED_FOR
                                 || state_ == State::TIED_BOTH)) {
            state_ = State::TIED_BACK;
            }
      else if (tiedVoices.empty() && state_ == State::TIED_BACK) {
            state_ = State::UNTIED;
            }
      else if (!tiedVoices.empty() && (state_ == State::TIED_BACK
                                       || state_ == State::UNTIED)) {
            state_ = State::TIED_FOR;
            }
      else if (!tiedVoices.empty() && state_ == State::TIED_FOR) {
            state_ = State::TIED_BOTH;
            }
      }


#ifdef IMPORTMIDI_DEBUG

void printInconsistentTieLocation(int measureIndex, int staffIndex)
      {
      qDebug() << "Ties are inconsistent; measure number (from 1):"
               << measureIndex + 1
               << ", staff index (from 0):" << staffIndex;
      }

bool areTiesConsistent(const Staff *staff)
      {
      const int strack = staff->idx() * VOICES;

      for (int voice = 0; voice < VOICES; ++voice) {
            bool isTie = false;
            for (Segment *seg = staff->score()->firstSegment(); seg; seg = seg->next1()) {
                  if (seg->segmentType() == Segment::Type::ChordRest) {
                        ChordRest *cr = static_cast<ChordRest *>(seg->element(strack + voice));

                        if (cr && cr->type() == Element::Type::REST && isTie) {
                              printInconsistentTieLocation(seg->measure()->no(), staff->idx());
                              return false;
                              }
                        if (isTiedBack(seg, strack, voice)) {
                              if (!isTie) {
                                    printInconsistentTieLocation(seg->measure()->no(), staff->idx());
                                    return false;
                                    }
                              isTie = false;
                              }
                        if (isTiedFor(seg, strack, voice)) {
                              if (isTie) {
                                    printInconsistentTieLocation(seg->measure()->no(), staff->idx());
                                    return false;
                                    }
                              isTie = true;
                              }
                        }
                  }
            if (isTie)
                  return false;
            }
      return true;
      }

#endif


} // namespace MidiTie
} // namespace Ms