summaryrefslogtreecommitdiffstats
path: root/src/gui/editors/guitar/Fingering.cpp
blob: fb0d92c2c7d92f4ecbe0179a03e1b518f62c61b4 (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
/*
    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 "Fingering.h"

#include "misc/Debug.h"

#include <tqstringlist.h>
#include <sstream>
#include <algorithm>
#include <tdelocale.h>

namespace Rosegarden
{

namespace Guitar
{
    
Fingering::Fingering(unsigned int nbStrings) :
    m_strings(nbStrings, MUTED)
{
}

Fingering::Fingering(TQString s)
{
    TQString errString;
    Fingering t = parseFingering(s, errString);
    m_strings = t.m_strings;
}

unsigned int
Fingering::getStartFret() const
{
    int min = 999, max = 0;
    for(std::vector<int>::const_iterator i = m_strings.begin(); i != m_strings.end(); ++i) {
        if (*i < min && *i > 0)
            min = *i;
        if (*i > max)
            max = *i;
    }
    
    if (max < 4)
        min = 1;
    
    return min == 999 ? 1 : min;
}

bool
Fingering::hasBarre() const
{
    int lastStringStatus = m_strings[getNbStrings() - 1];
    
    return ((m_strings[0] > OPEN && m_strings[0] == lastStringStatus) ||
            (m_strings[1] > OPEN && m_strings[1] == lastStringStatus) ||
            (m_strings[2] > OPEN && m_strings[2] == lastStringStatus));
}

Fingering::Barre
Fingering::getBarre() const
{
    int lastStringStatus = m_strings[getNbStrings() - 1];

    Barre res;
    
    res.fret = lastStringStatus;
    
    for(unsigned int i = 0; i < 3; ++i) {
        if (m_strings[i] > OPEN && m_strings[i] == lastStringStatus)
            res.start = i;
            break;
    }

    res.end = 5;
    
    return res;        
}

Fingering
Fingering::parseFingering(const TQString& ch, TQString& errorString)
{
    TQStringList tokens = TQStringList::split(' ', ch);

    unsigned int idx = 0;
    Fingering fingering;
    
    for(TQStringList::iterator i = tokens.begin(); i != tokens.end() && idx < fingering.getNbStrings(); ++i, ++idx) {
        TQString t = *i;
        bool b = false;
        unsigned int fn = t.toUInt(&b);
        if (b) {
//            NOTATION_DEBUG << "Fingering::parseFingering : '" << t << "' = " << fn << endl;  
            fingering[idx] = fn;
        } else if (t.lower() == "x") {
//            NOTATION_DEBUG << "Fingering::parseFingering : '" << t << "' = MUTED\n";  
            fingering[idx] = MUTED;
        } else {
            errorString = i18n("couldn't parse fingering '%1' in '%2'").arg(t).arg(ch);            
        }
    }

    return fingering;
}


std::string Fingering::toString() const
{
    std::stringstream s;
    
    for(std::vector<int>::const_iterator i = m_strings.begin(); i != m_strings.end(); ++i) {
        if (*i >= 0)
            s << *i << ' ';
        else
            s << "x ";
    }

    return s.str();
}

bool operator<(const Fingering& a, const Fingering& b)
{
    for(unsigned int i = 0; i < Fingering::DEFAULT_NB_STRINGS; ++i) {
        if (a.getStringStatus(i) != b.getStringStatus(i)) {
            return a.getStringStatus(i) < b.getStringStatus(i);
        }
    }
    return false;
}    

}

}