~ubuntu-branches/ubuntu/saucy/solfege/saucy

« back to all changes in this revision

Viewing changes to solfege/mpd/__init__.py

  • Committer: Bazaar Package Importer
  • Author(s): Tom Cato Amundsen
  • Date: 2010-03-28 06:34:28 UTC
  • mfrom: (1.1.10 upstream) (2.1.7 sid)
  • Revision ID: james.westby@ubuntu.com-20100328063428-wg2bqvoce2aq4xfb
Tags: 3.15.9-1
* New upstream release.
* Redo packaging. 

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# GNU Solfege - free ear training software
 
2
# Copyright (C) 2000, 2001, 2002, 2003, 2004, 2006, 2007, 2008  Tom Cato Amundsen
 
3
#
 
4
# This program is free software: you can redistribute it and/or modify
 
5
# it under the terms of the GNU General Public License as published by
 
6
# the Free Software Foundation, either version 3 of the License, or
 
7
# (at your option) any later version.
 
8
#
 
9
# This program is distributed in the hope that it will be useful,
 
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
12
# GNU General Public License for more details.
 
13
#
 
14
# You should have received a copy of the GNU General Public License
 
15
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
16
 
 
17
from __future__ import absolute_import
 
18
"""
 
19
FIXME update.
 
20
In addition to the names in this file, only this is public in
 
21
the mpd module:
 
22
 
 
23
  Classes:
 
24
        MusicalPitch
 
25
        MusicDisplayer
 
26
"""
 
27
 
 
28
 
 
29
LOWEST_NOTENAME="c,,,,"
 
30
HIGHEST_NOTENAME="g''''''"
 
31
 
 
32
from solfege.mpd.musicalpitch import MusicalPitch, InvalidNotenameException
 
33
from solfege.mpd.interval import Interval
 
34
from solfege.mpd.mpdutils import notename_to_int, int_to_notename, int_to_user_notename
 
35
from solfege.mpd.rat import Rat
 
36
from solfege.mpd.track import Track, PercussionTrack
 
37
from solfege.mpd._exceptions import MpdException
 
38
 
 
39
def music_to_tracklist(music, start=None, end=None):
 
40
    """
 
41
    return a list of tracks, where track[0] use only channel 0,
 
42
    track[1] only use channel 1 etc.
 
43
    """
 
44
    score = parser.parse_to_score_object(music)
 
45
    tracks = score.get_midi_events(start, end)
 
46
    return tracks
 
47
 
 
48
def music_to_track(music, start=None, end=None):
 
49
    score = parser.parse_to_score_object(music)
 
50
    tracklist = score.get_midi_events(start, end)
 
51
    track = tracklist[0]
 
52
    for x in range(1, len(tracklist)):
 
53
        track.merge_with(tracklist[x])
 
54
    return track
 
55
 
 
56
 
 
57
##################
 
58
# midi functions #
 
59
##################
 
60
 
 
61
def transpose_notename(n, t):
 
62
    assert isinstance(n, basestring)
 
63
    assert isinstance(t, int)
 
64
    # 1 2 sekund
 
65
    # 3 4 ters
 
66
    # 5 6 kvart
 
67
    # 7   kvint
 
68
    # 8 9 sekst
 
69
    # 10 11 septim
 
70
    return int_to_notename(notename_to_int(n) + t)
 
71
 
 
72
def compare_notenames(n1, n2):
 
73
    return notename_to_int(n1) - notename_to_int(n2)
 
74
 
 
75
def select_clef(s):
 
76
    """
 
77
    argument s is a string with notenames like this: " c e g c' f' g''"
 
78
    """
 
79
    lowest = HIGHEST_NOTENAME
 
80
    highest = LOWEST_NOTENAME
 
81
    for n in s.split():
 
82
        if compare_notenames(n, lowest) < 0:
 
83
            lowest = n
 
84
        if compare_notenames(n, highest) > 1:
 
85
            highest = n
 
86
    if compare_notenames(highest, "c'") < 0:
 
87
        return "bass"
 
88
    if compare_notenames(lowest, "c'") >= 0:
 
89
        return "violin"
 
90
    if compare_notenames(highest, "c'") > compare_notenames("c'", lowest):
 
91
        return "violin"
 
92
    else:
 
93
        return "bass"
 
94