/
guitar.py
275 lines (213 loc) · 7.41 KB
/
guitar.py
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
"""
**********************************************************
Author :Charles Brissac
Email :cdbrissac@gmail.com
License :GPLv3
***********************************************************
"""
import java
from javax.swing import *
from java import awt
from java.awt import *
import math,random
from mfcinstrument import *
DEBUG=False
class Guitar(MFCInstrument,java.awt.event.ActionListener,JPanel):
def __init__(self,orch,hostname,W,H,x_pad,y_pad,W_bridge,W_nut,L0,SF):
MFCInstrument.__init__(self,'Guitar',hostname,[Color.BLUE],False)
self.channelNo=1
self.orch=orch
if DEBUG:self.debug_panel=self.orch.debug_panel
self.setLayout(BorderLayout())
self.cp=JPanel()
self.cp.setLayout(FlowLayout())
self.cp.add(JCheckBox('Active',self.ACTIVE,actionPerformed=self.activeCB))
greenLine=BorderFactory.createLineBorder(java.awt.Color(0,200,0))
border=BorderFactory.createTitledBorder(greenLine,'Guitar')
self.cp.setBorder(border)
#self.add(self.cp,'South')
# self.cb=JComboBox(self.instlist,actionListener=self)
# self.cp.add(self.cb)
#framesizeY=awt.Dimension(0,H)
#vfill=Box.Filler(framesizeY,framesizeY,framesizeY)
#self.add(vfill,'West')
if DEBUG:print 'Guitar'
self.SHOW_GUITAR=True
self.LARGE_SPOTS=False
self.x_pad=x_pad
self.y_pad=y_pad
self.W=W
self.H=H
self.W_bridge=W_bridge
self.W_nut=W_nut
self.L0=L0
self.k=k=pow(2.,1./12.)
self.SF=SF
self.open_freqs=[659.25,587.33,493.86,392.0,220.0,164.81]
self.open_midis=[76,71,67,62,57,52]
self.open_names=['E','B','G','D','A','E']
self.open_octaves=[5,4,4,4,3,3]
self.strings=[]
dy=self.W_bridge-self.W_nut
self.octave_notes=['C','C#','D','D#','E','F','F#','G','G#','A','A#','B']
oidx_max=len(self.octave_notes)
for sidx in range(len(self.open_freqs)):
f0=self.open_freqs[sidx]
string=[]
oidx=self.octave_notes.index(self.open_names[sidx])
octave=self.open_octaves[sidx]
for fidx in range(24):
notename=self.octave_notes[oidx]
oidx+=1
if oidx>=oidx_max:
oidx=0
octave+=1
L_prev=self.L0*(1.-1./pow(k,fidx-1))
L=self.L0*(1.-1./pow(k,fidx))
y0=dy/2*(1.-L/L0)
y1=self.W_bridge-dy/2*(1.-L/L0)
fret={
'x0':int(self.SF*L),
'y0':int(self.SF*y0),
'x1':int(self.SF*L),
'y1':int(self.SF*y1),
'xc':int((self.SF*L_prev+self.SF*L)/2.),
'yc':int(self.SF*(y0+.15+(y1-y0-.25)*float(sidx)/5.)),
'freq':f0*math.pow(k,fidx),
'fretnum':fidx,
'sidx':sidx,
'notename':notename,
'octave':octave,
'midi':self.open_midis[sidx]+fidx,
'xscroll':0.0,
'played':False,
'toff':0.
}
#if sidx==1:print fret['x0'],fret['y0'],fret['y1']
string.append(fret)
self.strings.append(string)
self.tlcx=int((self.W+2*self.x_pad-self.strings[0][len(self.strings[0])-1]['x0'])/2.)
self.tlcy=0#self.y_pad
self.fretboard=awt.Polygon()
self.fretboard.addPoint(self.tlcx,self.tlcy+self.strings[0][0]['y0'])
self.fretboard.addPoint(self.tlcx+self.strings[0][22]['x0'],self.tlcy+self.strings[0][22]['y0'])
self.fretboard.addPoint(self.tlcx+self.strings[0][22]['x0'],self.tlcy+self.strings[0][22]['y1'])
self.fretboard.addPoint(self.tlcx,self.tlcy+self.strings[0][0]['y1'])
self.fretboard.addPoint(self.tlcx,self.tlcy+self.strings[0][0]['y0'])
self.dot_frets=[0,3,5,7,9,12,15,17,19,21]
self.mousePressed=self.pressCB
self.hilighted=[]
def actionPerformed(self,e):
self.instrumentCB(e)
def pressCB(self,e):
#mouse_position=self.getMousePosition()
mouse_position=java.awt.Point(e.getX(),e.getY())
mx=mouse_position.x
my=mouse_position.y
fret=self.getNote(mx,my)
if not fret:return
if DEBUG:print fret['notename'],fret['octave']
self.clear_hilighted()
while len(self.hilighted):
self.hilighted.pop()
self.hilighted.append(fret)
#PLAY MIDI ON GTR CHANNEL w/GTR INSTRUMENT:
self.orch.send(fret['midi'],-1)
"""
if self.keyboardChannel==None:
msg=ShortMessage()
msg.setMessage(ShortMessage.NOTE_ON,self.channelNo,fret['midi'],80)
self.orch.send(msg,-1)
else:
self.keyboardChannel.noteOn(fret['midi'],80)
"""
# if DEBUG:self.debug_panel.append("gtr -> cpCB")
self.cpCB(fret['midi'])
# if DEBUG:self.debug_panel.append("gtr -> updateUI")
self.updateUI()
def getNote(self,xc,yc):
for string in self.strings:
for fret in string:
if math.sqrt(math.pow(self.tlcx+fret['xc']-xc,2)+math.pow(self.tlcy+fret['yc']-yc,2)) < 10.:
return fret
return None
def randNote(self):
sidx=int(random.random()*6)
fidx=int(random.random()*23)
if DEBUG:print 'gtr.randNote: ',sidx,fidx
return(sidx,fidx)
def take_sfidx(self,sidx,fidx):
self.hilighted.append(self.strings[sidx][fidx])
self.updateUI()
#print dir(self)
def hilight_by_midi(self,midi):
if DEBUG:print 'GTR: hilight_by_midi'
for sidx in range(6):
for fidx in range(len(self.strings[0])):
if self.strings[sidx][fidx]['midi']==midi:
self.hilighted.append(self.strings[sidx][fidx])
def apply_dots(self,g):
if DEBUG:print 'apply_dots'
g.setColor(awt.Color.WHITE)
for dfidx in range(len(self.dot_frets)):
fretnum=self.dot_frets[dfidx]
xc=self.tlcx+self.strings[3][fretnum]['xc']
yc=self.tlcy
if fretnum==12 or fretnum==0:
yc=int(self.tlcy+(self.strings[1][fretnum]['yc']+self.strings[2][fretnum]['yc'])/2.)
g.fillOval(xc-4,yc-4,8,8)
yc=int(self.tlcy+(self.strings[3][fretnum]['yc']+self.strings[4][fretnum]['yc'])/2.)
g.fillOval(xc-4,yc-4,8,8)
else:
yc=int(self.tlcy+(self.strings[3][fretnum]['yc']+self.strings[2][fretnum]['yc'])/2.)
g.fillOval(xc-4,yc-4,8,8)
def paintComponent(self,g):
if DEBUG:print 'GTR:paintComponent'
if self.LARGE_SPOTS:dr2=9
else:dr2=4#spot halfsize
g.setColor(awt.Color.BLACK)
g.fillPolygon(self.fretboard)
self.apply_dots(g)
g.setColor(awt.Color.GREEN)
for fidx in range(22):
x0=self.tlcx+self.strings[0][fidx]['x0']
y0=self.tlcy+self.strings[0][fidx]['y0']
x1=self.tlcx+self.strings[0][fidx]['x1']
y1=self.tlcy+self.strings[0][fidx]['y1']
g.drawLine(x0,y0,x1,y1)
g.setColor(awt.Color.GRAY)
for sidx in range(6):
x0=self.tlcx+self.strings[sidx][0]['xc']
y0=self.tlcy+self.strings[sidx][0]['yc']
x1=self.tlcx+self.strings[sidx][23]['xc']
y1=self.tlcy+self.strings[sidx][23]['yc']
g.drawLine(x0,y0,x1,y1)
"""
g.setColor(awt.Color.GREEN)
for sidx in range(6):
for fidx in range(0,22):
xc=self.tlcx+self.strings[sidx][fidx]['xc']
yc=self.tlcy+self.strings[sidx][fidx]['yc']
g.fillOval(xc-dr2,yc-dr2,2*dr2,2*dr2)
"""
g.setColor(awt.Color.RED)
"""
if not len(self.hilighted):
sidx,fidx=self.randNote()
self.take_sfidx(sidx,fidx)
"""
msg=""
for idx in range(len(self.hilighted)):
xc=self.tlcx+self.hilighted[idx]['xc']
yc=self.tlcy+self.hilighted[idx]['yc']
g.fillOval(xc-dr2,yc-dr2,2*dr2,2*dr2)
msg="%s%d %d %d"%(self.hilighted[idx]['notename'],self.hilighted[idx]['octave'],self.hilighted[idx]['sidx']+1,self.hilighted[idx]['fretnum'])
#print "%s%d"%(self.hilighted[idx]['notename'],self.hilighted[idx]['octave'])
if DEBUG:print msg
g.setColor(awt.Color.WHITE)
g.fillRect(self.W/2-50,int(self.W_bridge*self.SF),100,20)
g.setColor(awt.Color.BLACK)
g.drawChars(msg,0,len(msg),self.W/2-50,int(self.W_bridge*self.SF)+11)#11=offset to align inside label
#print dir(g)
if __name__=='__main__':
x=Guitar()