Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/miniTamTam/KeyboardStandAlone.py
blob: b783070224a7eca5db22933d2bf87e31d398c9c5 (plain)
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
import pygtk
pygtk.require( '2.0' )
import gtk

import Config
from Generation.GenerationConstants import GenerationConstants
from Util.NoteDB  import Note
from Util.CSoundNote import CSoundNote
from Util.CSoundClient import new_csound_client

KEY_MAP_PIANO = Config.KEY_MAP_PIANO

class KeyboardStandAlone:
    def __init__( self, recordingFunction, adjustDurationFunction, getCurrentTick, getPlayState, loop ):
        self.csnd = new_csound_client()        
        self.recording = recordingFunction
        self.adjustDuration = adjustDurationFunction
        self.getPlayState = getPlayState
        self.key_dict = dict()
        self.onset_dict = dict()
        self.trackCount = 0
        self.instrument = 'flute'
        self.reverb = 0
        self.loop = loop
        self.loopSustain = False
        self.sustainedLoop = []
    
    def setInstrument( self , instrument ):
        self.instrument = instrument
        
    def setReverb(self , reverb):
        self.reverb = reverb
        
    def onKeyPress(self,widget,event, volume):
        key = event.hardware_keycode
        if key == 50: #Left Shift
            self.loopSustain = True
            
        # If the key is already in the dictionnary, exit function (to avoir key repeats)
        if self.key_dict.has_key(key):
            return

        if key in Config.LOOP_KEYS:
            if key in self.sustainedLoop:
                self.loop.stop(key)
                self.sustainedLoop.remove(key)
            elif self.loopSustain:
                self.loop.start(key, self.instrument, self.reverb)
                self.sustainedLoop.append(key)
            else:
                self.loop.start(key, self.instrument, self.reverb)
            return

        # Assign on which track the note will be created according to the number of keys pressed    
        if self.trackCount >= 9:
            self.trackCount = 1
        track = self.trackCount
        self.trackCount += 1
        # If the pressed key is in the keymap
        if KEY_MAP_PIANO.has_key(key):
            # CsoundNote parameters
            pitch = KEY_MAP_PIANO[key]
            duration = -1
            instrument = self.instrument
            
            if event.state == gtk.gdk.MOD1_MASK:
                pitch = pitch+5

            if instrument[ 0: 4 ] == 'drum':
                if GenerationConstants.DRUMPITCH.has_key( pitch ):
                    pitch = GenerationConstants.DRUMPITCH[ pitch ]

                if Config.INSTRUMENTS[instrument].kit != None:
                    instrument = Config.INSTRUMENTS[instrument].kit[pitch].name
                pitch = 36
                duration = 100

            if Config.INSTRUMENTS[instrument].csoundInstrumentId == Config.INST_PERC:    #Percussions resonance
                duration = 60

            # Create and play the note
            self.key_dict[key] = CSoundNote(onset = 0, 
                                            pitch = pitch, 
                                            amplitude = volume, 
                                            pan = 0.5, 
                                            duration = duration, 
                                            trackId = track, 
                                            instrumentId = Config.INSTRUMENTS[instrument].instrumentId, 
                                            reverbSend = self.reverb,
                                            tied = True,
                                            mode = 'mini') 
            self.csnd.play(self.key_dict[key], 0.3)
            if self.getPlayState():
                recOnset = self.csnd.loopGetTick()
                self.onset_dict[key] = recOnset
                self.recording( CSoundNote(
                                     onset = recOnset, 
                                     pitch = pitch, 
                                     amplitude = volume, 
                                     pan = 0.5, 
                                     duration = 100, 
                                     trackId = 0,
                                     decay = .1, 
                                     instrumentId = Config.INSTRUMENTS[instrument].instrumentId, 
                                     reverbSend = self.reverb,
                                     tied = False,
                                     mode = 'mini'))
            
    def onKeyRelease(self,widget,event):
        key = event.hardware_keycode
        if key == 50:
            self.loopSustain = False
            
        if key in Config.LOOP_KEYS:
            if key in self.sustainedLoop:
                return
            else:
                self.loop.stop(key)
            return
       
        if KEY_MAP_PIANO.has_key(key):
            csnote = self.key_dict[key]
            if Config.INSTRUMENTSID[ csnote.instrumentId ].csoundInstrumentId == Config.INST_TIED:
                csnote.duration = .5
                csnote.decay = 0.7
                #csnote.amplitude = 1
                csnote.tied = False
                csnote.mode = 'mini'
                self.csnd.play(csnote, 0.3)
            if self.getPlayState():
                self.adjustDuration(csnote.pitch, self.onset_dict[key])
            del self.key_dict[key]
        if self.getPlayState():
            if self.onset_dict.has_key(key):
                del self.onset_dict[key]
    
    def onButtonPress( self, widget, event ):
        pass