File: polyrhythm.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 (84 lines) | stat: -rw-r--r-- 3,223 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
# 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, widgets
import soundcard, mpd
import abstract, const
import random, string

class Teacher(abstract.Teacher):
    def __init__(self, exname, app):
        abstract.Teacher.__init__(self, exname, app)
        self.m_timeout = None
    def timeout_x_y(self, x, y, bpm):
        A = 60; B = 61
        t = soundcard.Track()
        for n in range(x):
            t.note(x, 9, A, 100)
        t2 = soundcard.Track()
        for n in range(y):
            t2.note(y, 9, B, 100)
        t.merge_with(t2)
        t.prepend_bpm(bpm)
        soundcard.synth.addplay_track(t)
        return 1
    def stop(self, _o):
        if self.m_timeout:
            gtk.timeout_remove(self.m_timeout)
            self.m_timeout = None
        soundcard.solfege_c_midi.sndctl_seq_reset()
    def play(self, x, y, bpm):
        if self.m_timeout:
            gtk.timeout_remove(self.m_timeout)
            self.m_timeout = None
        m = soundcard.Track()
        soundcard.synth.play_track(m)
        self.timeout_x_y(x, y, bpm)
        self.m_timeout = gtk.timeout_add(300, lambda self=self, x=x, y=y, bpm=bpm: self.more_xy(x, y, bpm))
        self.m_pos = 0
        self.m_count = 0
    def more_xy(self, x, y, bpm):
        i = soundcard.solfege_c_midi.sndctl_seq_gettime()
        if self.m_count * 96*4 < i:
            self.timeout_x_y(x, y, bpm)
            self.m_count = self.m_count + 1
        return 1

class Gui(abstract.Gui):
    def __init__(self, teacher, window):
        abstract.Gui.__init__(self, teacher, window, no_notebook=1)
        self.m_correct = [0, 0, 0]
        ################
        # practice_box #
        ################
        hbox = gu.bHBox(self.practise_box)
        adj = gtk.Adjustment(2, 1, 20, 1, 10)
        self.g_x = gtk.SpinButton(adj, digits=0)
        hbox.pack_start(self.g_x)
        adj = gtk.Adjustment(3, 1, 20, 1, 10)
        self.g_y = gtk.SpinButton(adj, digits=0)
        hbox.pack_start(self.g_y)
        adj = gtk.Adjustment(130, 40, 400, 1, 10)
        self.g_tempo = gtk.SpinButton(adj, digits=0)
        hbox.pack_start(self.g_tempo)
        gu.bButton(self.practise_box, _("Go"), self.go)
        gu.bButton(self.practise_box, _("Stop"), self.m_t.stop)
        self.practise_box.show_all()
    def go(self, _o):
        self.m_t.play(self.g_x.get_value_as_int(), self.g_y.get_value_as_int(),
                      self.g_tempo.get_value_as_int())