File: singchord.py

package info (click to toggle)
solfege 2.0.4-4
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 3,200 kB
  • ctags: 1,844
  • sloc: python: 12,160; xml: 5,458; ansic: 1,486; makefile: 562; sh: 233
file content (127 lines) | stat: -rw-r--r-- 5,559 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
# GNU Solfege - eartraining for GNOME
# Copyright (C) 2000, 2001, 2002, 2003  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 2 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, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

import gtk, gnome
import gu, const, soundcard, mpd, mpd.musicdisplayer, abstract
import lessonfile, string
import widgets
import utils
import dataparser

class Teacher(abstract.LessonbasedTeacher):
    def __init__(self, exname, app):
        abstract.LessonbasedTeacher.__init__(self, exname, app, const.USE_SING_CHORD)
        self.lessonfileclass = lessonfile.SingChordLessonfile
        try: # the lessonfile might contain errors.
            self.parse_lessonfile()
        except dataparser.DataparserException, e:
            self.m_app.m_ui.display_error_message(str(e))
    def new_question(self):
        self.q_status = const.QSTATUS_NEW
        self.m_P.select_random_question()
    def play_answer(self):
        self.play_question()
    def play_440hz(self):
        soundcard.play_note(self.get_int('config/preferred_instrument'),
                       4, 0, mpd.notename_to_int("a'"),
                       self.get_int('config/preferred_instrument_velocity'))
    def play_answer_arpeggio(self):
        if self.q_status == const.QSTATUS_NO:
            return
        m = soundcard.Track()
        m.set_bpm(self.get_int('config/default_bpm'))
        m.set_patch(0, self.get_int('config/preferred_instrument'))
        vel = self.get_int('config/preferred_instrument_velocity')
        for x in [0, 1]:
            v = self.m_P.get_satb_vector()[x]
            s = string.split(string.strip(v), " ")
            for n in s:
                if self.get_string('user/sex') == 'female':
                    m.note(4, 0, mpd.notename_to_int(n), vel)
                else:
                    m.note(4, 0, mpd.notename_to_int(n)-12, vel)
        for x in [2, 3]:
            v = self.m_P.get_satb_vector()[x]
            s = string.split(string.strip(v), " ")
            for n in s:
                if self.get_string('user/sex') == 'male':
                    m.note(4, 0, mpd.notename_to_int(n), vel)
                else:
                    m.note(4, 0, mpd.notename_to_int(n)+12, vel)
        soundcard.synth.play_track(m)


class Gui(abstract.LessonbasedGui):
    def __init__(self, teacher, window):
        abstract.LessonbasedGui.__init__(self, teacher, window)
        ################
        # practise_box #
        ################
        self.g_music_displayer = mpd.musicdisplayer.MusicDisplayer(utils.play_tone)
        self.g_music_displayer.set_size_request(-1, 170)
        self.practise_box.pack_start(self.g_music_displayer)

        self.g_new = gu.bButton(self.action_area, _("_New"), self.new_question)
        gu.bButton(self.action_area, _("440h_z"), lambda f, s=self: s.m_t.play_440hz())
        self.g_play_answer = gu.bButton(self.action_area, _("_Play answer"), lambda f, s=self: s.m_t.play_answer_arpeggio())
        self.practise_box.show_all()
        ##############
        # config_box #
        ##############
        self.g_lessonfile_selector = widgets.SelectLessonfileWidget(
             self.m_t, self.g_win, self.update_gui_after_lessonfile_change)
        self.g_lessonfile_selector.show()
        self.config_box.pack_start(self.g_lessonfile_selector)
        self.update_gui_after_lessonfile_change()
    def new_question(self, widget=None):
        # if we have no lessonfile, then we have no questions.
        if not self.m_t.m_P:
            return
        try:
            self.m_t.new_question()
        except mpd.MpdException, e:
            # since we could not play the new question, it is possible,
            # if the user are fast, that the last question is still playing.
            soundcard.synth.stop()
            self.g_win.display_error_message(
                self.m_t.m_P._idx, self.get_string('lessonfile'), e)
        else:
            fontsize = self.get_int('config/feta_font_size=20')
            self.g_music_displayer.display(self.m_t.m_P.get_music(), fontsize)
            self.g_play_answer.set_sensitive(gtk.TRUE)
            self.m_t.play_440hz()
    def on_start_practise(self):
        # if we have not m_P, then parsing of the lessonfile failed,
        # and then we have no questions.
        if not self.m_t.m_P:
            self.g_new.set_sensitive(gtk.FALSE)
        else:
            self.g_new.set_sensitive(gtk.TRUE)
        self.g_new.grab_focus()
    def on_end_practise(self):
        self.m_t.end_practise()
        self.g_play_answer.set_sensitive(gtk.FALSE)
        self.g_music_displayer.clear(2)
    def update_gui_after_lessonfile_change(self):
        self.g_music_displayer.clear(2)
        if self.m_t.m_P:
            self.g_new.set_sensitive(gtk.TRUE)
        else:
            self.g_new.set_sensitive(gtk.FALSE)
        self.g_play_answer.set_sensitive(gtk.FALSE)
        self.g_win.set_title("Solfege - " + self.get_pretty_name())