[go: up one dir, main page]

File: mpdutils.py

package info (click to toggle)
solfege 3.10.3-1
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 12,408 kB
  • ctags: 4,270
  • sloc: python: 22,161; xml: 7,536; ansic: 4,442; makefile: 685; sh: 308
file content (118 lines) | stat: -rw-r--r-- 4,013 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
# GNU Solfege - free ear training software
# Copyright (C) 2000, 2001, 2002, 2003, 2004, 2007, 2008  Tom Cato Amundsen
#
# 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 3 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, see <http://www.gnu.org/licenses/>.

"""
Only utility functions and classes that are private to the mpd module
should go into this file.
"""

import re
from musicalpitch import MusicalPitch, InvalidNotenameException
import parser

def int_to_notename(i):
    p = MusicalPitch()
    p.set_from_int(i)
    return p.get_octave_notename()

def int_to_user_notename(i):
    p = MusicalPitch()
    p.set_from_int(i)
    return p.get_user_octave_notename()

def notename_to_int(n):
    p = MusicalPitch.new_from_notename(n)
    return p.semitone_pitch()


def key_to_accidentals(key):
    i = ['aeses', 'eeses', 'beses', 'fes', 'ces', 'ges', 'des', 'aes',
         'ees', 'bes', 'f', 'c', 'g', 'd', 'a', 'e', 'b', 'fis', 'cis',
         'gis', 'dis', 'ais', 'eis', 'bis'].index(key[0])-11
    if key[1] == 'minor':
        i = i - 3
    if i > 0:
        r = ['fis', 'cis', 'gis', 'dis', 'ais', 'eis',
             'bis', 'fis', 'cis', 'gis', 'dis'][:i]
        m = 'is'
    elif i < 0:
        r = ['bes', 'ees', 'aes', 'des', 'ges', 'ces',
             'fes', 'bes', 'ees', 'aes', 'des'][:-i]
        m = 'es'
    else:
        r = []
    retval = []
    for a in r:
        if a not in retval:
            retval.append(a)
        else:
            del retval[retval.index(a)]
            retval.append(a+m)
    return retval


def find_possible_first_note(music):
    """
    Return a tuple of 2 integer locating what we believe is the first
    pitch (but do not include the duration).

    Return the location of the text we don't understand if we are not able
    to parse the music.
    """
    i = 0
    v = music.split()
    # FIXME regexes are modified copies from the mpd Lexer. Try to reuse
    # code in the future.
    re_white = re.compile(r"\s+")
    re_clef = re.compile(r"\\clef\s+(\w*)", re.UNICODE)
    re_clef_quoted = re.compile(r"\\clef\s+\"([A-Za-z1-9]+[_^1-9]*)\"", re.UNICODE)
    re_time = re.compile(r"\\time\s+(\d+)\s*/\s*(\d+)", re.UNICODE)
    re_times = re.compile(r"\\times\s+(\d+)\s*/\s*(\d+)\s*{", re.UNICODE)
    re_key = re.compile(r"\\key\s+([a-z]+)\s*\\(major|minor)", re.UNICODE)
    re_note = re.compile("(?P<beamstart>(\[\s*)?)(?P<chordstart>(\<\s*)?)(?P<pitchname>[a-zA-Z]+[',]*)(\d+\.*)?")
    i = 0
    re_list = re_white, re_clef_quoted, re_clef, re_key, re_times, re_time, re_note
    while 1:
        for r in re_list:
            m = r.match(music[i:])
            if m:
                if r != re_note:
                    i += m.end()
                    break
                elif r == re_note:
                    assert m
                    i += len(m.group("beamstart"))
                    i += len(m.group("chordstart"))
                    return i, i + len(m.group('pitchname'))
            elif r == re_list[-1]:
                    return i, i+1

def validate_only_notenames(s):
    """
    Return (None, None, None) if the string s is only notenames
    (pitch and duration). No ties or other tokens are allowed.
    """
    lex = parser.Lexer(s)
    while 1:
        try:
            toc, toc_data = lex.get()
        except InvalidNotenameException, e:
            return lex.get_error_location()
        if not toc:
            break
        if toc != lex.NOTE:
            return lex.get_error_location()
    return None, None, None