File: harmonicprogressiondictation.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 (176 lines) | stat: -rw-r--r-- 8,172 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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# 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 abstract, idbyname, const, gu, statisticsviewer, widgets
import soundcard, mpd, mpd.musicdisplayer
import utils
import dataparser

class Teacher(idbyname.Teacher):
    def __init__(self, exname, app):
        idbyname.Teacher.__init__(self, exname, app, const.USE_HARMONY)
    def play_question(self):
        if self.q_status == const.QSTATUS_NO:
            return
        tracklist = mpd.music_to_tracklist(self.m_P.get_music(),
                 self.get_int('config/preferred_instrument_velocity'))
        l, lv, m, mv, h, hv = self.get_instrument_config(3)
        tracklist[0].prepend_patch(0, h)
        t = tracklist[0]
        for x in range(1, len(tracklist)-1):
            tracklist[x].prepend_patch(x, m)
            t.merge_with(tracklist[x])
        tracklist[1].prepend_patch(0, h)
        tracklist[-1].prepend_patch(len(tracklist)-1, l)
        t.merge_with(tracklist[-1])
        soundcard.synth.play_track(t)

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_flashbar = gu.FlashBar()
        self.g_flashbar.show()
        self.practise_box.pack_start(self.g_flashbar, gtk.FALSE)
        self.practise_box.set_spacing(gnome.ui.PAD)

        self.g_entry = gtk.Entry()
        self.g_entry.set_activates_default(gtk.TRUE)
        self.practise_box.pack_start(self.g_entry, gtk.FALSE)
        self.g_new = gu.bButton(self.action_area, _("_New"), self.new_question)
        self.g_repeat = gu.bButton(self.action_area, _("_Repeat"),
                                   lambda _o, self=self: self.m_t.play_question())
        self.g_repeat.set_sensitive(gtk.FALSE)
        self.g_guess_answer = gu.bButton(self.action_area, _("Guess _answer"),
                                         self.guess_answer)
        self.g_guess_answer.set_sensitive(gtk.FALSE)
        self.g_guess_answer.set_flags(gtk.CAN_DEFAULT)
        self.g_show = gu.bButton(self.action_area, _("_Show"), self.show_answer)
        self.g_show.set_sensitive(gtk.FALSE)
        self.g_give_up = gu.bButton(self.action_area, _("_Give up"), self.give_up)
        self.g_give_up.set_sensitive(gtk.FALSE)
        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.g_instrument_configurator \
            = widgets.InstrumentConfigurator(self.m_exname, 3)
        self.config_box.pack_start(self.g_instrument_configurator, gtk.FALSE)
        self.g_instrument_configurator.show()
        # ------------------------------------------
        self._add_auto_new_question_gui(self.config_box)
        # ----------------------------------------------

        ###############
        # statistics
        ###############
        self.setup_statisticsviewer(statisticsviewer.StatisticsViewer,
                                   _("Harmonic progression dictation"))

        self.update_gui_after_lessonfile_change()
    def guess_answer(self, widget=None):
        if self.m_t.q_status == const.QSTATUS_NO:
            return
        if self.m_t.q_status == const.QSTATUS_SOLVED:
            if self.m_t.guess_answer(self.g_entry.get_text()):
                self.g_flashbar.flash(_("Correct, but you have already solved this question"))
            else:
                self.g_flashbar.flash(_("Wrong, but you have already solved this question"))
        else:
            if self.m_t.guess_answer(self.g_entry.get_text()):
                self.g_flashbar.flash(_("Correct"))
                self.g_give_up.set_sensitive(gtk.FALSE)
                self.g_new.set_sensitive(gtk.TRUE)
                self.g_new.grab_focus()
            else:
                self.g_flashbar.flash(_("Wrong"))
                self.g_give_up.set_sensitive(gtk.TRUE)
    def show_answer(self, widget=None):#FIXME rename to show_music??
        if self.m_t.q_status != const.QSTATUS_NO:
            self.g_music_displayer.display(self.m_t.m_P.get_music(),
                               self.get_int('config/feta_font_size=20'))
    def new_question(self, widget=None):
        if not self.m_t.m_P:
            return
        # pop just in case there is something in the stack.
        self.g_flashbar.pop()
        if self.m_t.new_question() == self.m_t.OK:
            try:
                self.g_music_displayer.display(self.m_t.m_P.get_music(),
                            self.get_int('config/feta_font_size=20'), mpd.FIRST)
            except mpd.MpdException, e:
                soundcard.synth.stop()
                self.g_win.display_question_music_error_message(
                   self.m_t.m_P._idx, self.get_string('lessonfile'), e)
                self.g_repeat.set_sensitive(gtk.FALSE)
                self.g_show.set_sensitive(gtk.FALSE)
                self.g_guess_answer.set_sensitive(gtk.FALSE)
                self.g_entry.set_text("")
                self.g_music_displayer.clear()
            else:
                self.g_repeat.set_sensitive(gtk.TRUE)
                self.g_show.set_sensitive(gtk.TRUE)
                self.g_guess_answer.set_sensitive(gtk.TRUE)
                self.g_entry.set_text("")
                self.g_new.set_sensitive(
                     not self.get_bool('config/picky_on_new_question'))
                self.m_t.play_question()
                self.g_entry.grab_focus()
    def give_up(self, widget=None):
        self.m_t.give_up()
        self.g_new.set_sensitive(gtk.TRUE)
        self.g_guess_answer.set_sensitive(gtk.FALSE)
        self.g_show.set_sensitive(gtk.FALSE)
        self.g_give_up.set_sensitive(gtk.FALSE)
        self.g_flashbar.push(self.m_t.m_P.get_untranslated_name())
        self.show_answer()
    def on_start_practise(self):
        self.g_guess_answer.grab_default()
        self.g_new.grab_focus()
    def on_end_practise(self):
        self.g_new.set_sensitive(gtk.TRUE)
        self.g_repeat.set_sensitive(gtk.FALSE)
        self.g_guess_answer.set_sensitive(gtk.FALSE)
        self.g_show.set_sensitive(gtk.FALSE)
        self.g_music_displayer.clear()
        self.m_t.end_practise()
    def update_gui_after_lessonfile_change(self):
        self.g_music_displayer.clear()
        if self.m_t.m_P:
            self.g_new.set_sensitive(gtk.TRUE)
        else:
            self.g_new.set_sensitive(gtk.FALSE)
        self.g_repeat.set_sensitive(gtk.FALSE)
        self.g_guess_answer.set_sensitive(gtk.FALSE)
        self.g_show.set_sensitive(gtk.FALSE)
        self.m_t.m_statistics.lessonfile_changed(
                     self.get_string('lessoncollection'),
                     self.get_string('lessonfile'))
        self.g_statview.m_descriptive_paragraph = \
         _("Statistics for the lesson file '%s'") % self.get_string('lessonfile')
        self.g_win.set_title("Solfege - " + self.get_pretty_name())