-
Notifications
You must be signed in to change notification settings - Fork 0
/
frequencies_V.py
88 lines (83 loc) · 3.01 KB
/
frequencies_V.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
from math import sin,pi
from tkinter import Tk,Canvas
## from pylab import linspace,sin
import sys
if sys.version_info.major == 2 and sys.version_info.minor == 7 :
print(sys.version)
import Tkinter as tk
import tkFileDialog as filedialog
elif sys.version_info.major == 3 and sys.version_info.minor == 6 :
print(sys.version)
import tkinter as tk
from tkinter import filedialog
else :
print("Your python version is : ")
print(sys.version_info.major,sys.version_info.minor)
print("... I guess it will work !")
class View :
def __init__(self,parent,bg="white",width=600,height=300):
self.canvas=Canvas(parent,bg=bg,width=width,height=height)
self.a,self.f,self.p=1.0,2.0,0.0
self.signal=[]
self.width,self.height=width,height
self.units=1
self.canvas.bind("<Configure>",self.resize)
def vibration(self,t,harmoniques=1,impair=True):
a,f,p=self.a,self.f,self.p
f=1.0
somme=0
for h in range(1,harmoniques+1) :
somme=somme + (a/h)*sin(2*pi*(f*h)*t-p)
return somme
def generate_signal(self,period=2,samples=100):
del self.signal[0:]
echantillons=range(int(samples)+1)
Tech = period/samples
print("Tech",Tech,period,samples)
for t in echantillons :
self.signal.append([t*Tech,self.vibration(t*Tech)])
print(self.signal)
return self.signal
def update(self):
print("View : update()")
self.generate_signal()
if self.signal :
self.plot_signal(self.signal)
def plot_signal(self,signal,color="red"):
w,h=self.width,self.height
signal_id=None
if signal and len(signal) > 1:
print(self.units)
plot = [(x*w,h/2.0*(1-y/(self.units/2.0))) for (x, y) in signal]
signal_id=self.canvas.create_line(plot, fill=color, smooth=1, width=3,tags="sound")
return signal_id
def grid(self,steps=2):
self.units=steps
tile_x=self.width/steps
for t in range(1,steps+1):
x =t*tile_x
self.canvas.create_line(x,0,x,self.height,tags="grid")
self.canvas.create_line(x,self.height/2-10,x,self.height/2+10,width=3,tags="grid")
tile_y=self.height/steps
for t in range(1,steps+1):
y =t*tile_y
self.canvas.create_line(0,y,self.width,y,tags="grid")
self.canvas.create_line(self.width/2-10,y,self.width/2+10,y,width=3,tags="grid")
def resize(self,event):
if event:
print("event")
self.width,self.height=event.width,event.height
self.canvas.delete("grid")
self.canvas.delete("sound")
self.plot_signal(self.signal)
self.grid(self.units)
def packing(self) :
self.canvas.pack(expand=1,fill="both",padx=6)
if __name__ == "__main__" :
root=Tk()
root.title("Piano : Nom-Prenom")
view=View(root)
view.grid(4)
view.packing()
view.update()
root.mainloop()