[go: up one dir, main page]

File: singanswer.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 (115 lines) | stat: -rw-r--r-- 4,895 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
# GNU Solfege - free ear training software
# Copyright (C) 2004, 2005, 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/>.

import gobject
import gtk
import gu
import abstract
import lessonfile
import mpd
import utils
import soundcard

class Teacher(abstract.Teacher):
    OK = 1
    ERR_NO_QUESTION = 2
    def __init__(self, exname, app):
        abstract.Teacher.__init__(self, exname, app)
        self.lessonfileclass = lessonfile.SingAnswerLessonfile
    def new_question(self):
        assert self.m_P
        self.m_P.select_random_question()
        self.q_status = self.QSTATUS_NEW
        return self.OK

class Gui(abstract.LessonbasedGui):
    def __init__(self, teacher, window):
        abstract.Gui.__init__(self, teacher, window)
        self.g_music_displayer = mpd.musicdisplayer.MusicDisplayer(utils.play_tone)
        self.practise_box.pack_start(self.g_music_displayer)
        self.g_flashbar = gu.FlashBar()
        self.practise_box.pack_start(self.g_flashbar)
        self.g_music_displayer.clear()
        self.g_new = gu.bButton(self.action_area, _("_New"), self.new_question)
        self.g_repeat = gu.bButton(self.action_area, _("_Repeat"),
            lambda w: self.run_exception_handled(self.m_t.m_P.play_question))
        self.g_repeat_arpeggio = gu.bButton(self.action_area, 
            _("Repeat _arpeggio"),
            lambda w: self.run_exception_handled(self.m_t.m_P.play_question_arpeggio))
        self.g_play_answer = gu.bButton(self.action_area, 
                    _("_Play answer"), self.hear_answer)
        ##############
        # config_box #
        ##############
        self.add_random_transpose_gui()
        self.practise_box.show_all()
    def new_question(self, widget):
        def exception_cleanup():
            soundcard.synth.stop()
            self.g_music_displayer.clear()
            self.g_repeat.set_sensitive(False)
            self.g_repeat_arpeggio.set_sensitive(False)
            self.g_play_answer.set_sensitive(False)
        if self.m_t.m_P.header.have_music_displayer:
            self.g_music_displayer.clear()
        try:
            g = self.m_t.new_question()
            self.g_flashbar.push(self.m_t.m_P.get_question().question_text)
            if isinstance(self.m_t.m_P.get_question().music, lessonfile.MpdDisplayable):
                self.g_music_displayer.display(self.m_t.m_P.get_music(), 20)
            else:
                self.g_music_displayer.clear()
            self.m_t.m_P.play_question()
            self.g_repeat.set_sensitive(True)
            self.g_repeat_arpeggio.set_sensitive(True)
            self.g_play_answer.set_sensitive(True)
            self.g_play_answer.grab_focus()
        except Exception, e:
            if isinstance(e, mpd.MpdException):
                e.m_mpd_varname = 'music'
                e.m_mpd_badcode = self.m_t.m_P.get_question().music.get_err_context(e, self.m_t.m_P)
            if not self.standard_exception_handler(e, exception_cleanup):
                raise
    def hear_answer(self, widget):
        try:
            self.m_t.m_P.play_question(varname='answer')
        except Exception, e:
            if not self.standard_exception_handler(e):
                raise
        self.g_new.grab_focus()
    def update_gui_after_lessonfile_change(self):
        self.g_random_transpose.set_text(str(self.m_t.m_P.header.random_transpose))
        if self.m_t.m_P.header.have_repeat_arpeggio_button:
            self.g_repeat_arpeggio.show()
        else:
            self.g_repeat_arpeggio.hide()
        if self.m_t.m_P.header.have_music_displayer:
            self.g_music_displayer.show()
        else:
            self.g_music_displayer.hide()
        self.g_repeat.set_sensitive(False)
        self.g_repeat_arpeggio.set_sensitive(False)
        self.g_play_answer.set_sensitive(False)
        self.g_flashbar.clear()
        self.g_new.set_sensitive(bool(self.m_t.m_P))
    def on_start_practise(self):
        self.update_gui_after_lessonfile_change()
        self.g_new.grab_focus()
        self.g_flashbar.delayed_flash(self.short_delay,
            _("Click 'New' to begin."))
        self.g_flashbar.require_size(
            [q['question_text'] for q in self.m_t.m_P.m_questions]
            + [_("Click 'New' to begin.")])