import pygame
from Tkinter import *
from pygame.locals import *
import math
import numpy
pygame.init()
size = (50, 50)
bits = 16
pygame.mixer.pre_init(4096, -bits, 2, 4)
pygame.init()
_display_surf = pygame.display.set_mode(size, pygame.HWSURFACE | pygame.DOUBLEBUF)
# Merci Python... --'
volume = 1
freq = 0
onde = 1
attack = 0
release = 0
octave = 0
Wave = 0
def ApplyChanges() :
global onde
global attack
global release
global volume
global octave
attack = AttackSlider.get()
attack = int(attack)
print attack
#We define our window
Fen_Controls = Tk()
#Fen_Controls.configure(width=500,height=400)
Fen_Controls.title('Syrapi')
Canvas = Canvas(Fen_Controls, background='black')
Canvas.pack()
#These are the Frames, inside them is where the magic happens
Wavfram = Frame(Canvas, relief = GROOVE, background='black')
Wavfram.pack(side=LEFT, padx=30, pady=30)
Octfram = Frame(Canvas, relief = GROOVE, background='black')
Octfram.pack(side=LEFT, padx=10, pady=10)
Attfram = Frame(Canvas, relief = GROOVE, background='black')
Attfram.pack(side = LEFT, padx=10, pady=10)
Relfram = Frame(Canvas, relief = GROOVE, background='black')
Relfram.pack(side = LEFT, padx=10,pady=10)
Volfram = Frame(Canvas, relief = GROOVE, background='black')
Volfram.pack(side = LEFT, padx=10,pady=10)
Exfram = Frame(Canvas,relief = GROOVE, background='black')
Exfram.pack(side=LEFT, padx=30, pady=30)
#Now for all the buttons, scales and all the good stuff
Wave =StringVar()
Sine =Radiobutton(Wavfram, text="Sinusoïdale", variable=Wave, value =1,background='black', fg='red')
Square =Radiobutton(Wavfram, text="Carrée", variable=Wave, value =2,background='black', fg='red')
Sine.pack()
Square.pack()
Label(Wavfram, text="Forme d'onde", fg='snow2',background='black').pack()
Octave = Scale(Octfram,from_=2, to=-2,background='black', fg='red',highlightbackground='gray4',activebackground='red',troughcolor='snow2')
Octave.pack()
Label(Octfram,text="Octave", fg='snow2',background='black').pack()
AttackSlider = DoubleVar()
Attack = Scale(Attfram, from_=127, to=0,background='black', fg='red',highlightbackground='gray4',activebackground='red',troughcolor='snow2',variable=AttackSlider)
Attack.pack()
Label(Attfram,text="Attack", fg='snow2',background='black').pack()
Release = Scale(Relfram, from_=127, to=0,background='black', fg='red',highlightbackground='gray4',activebackground='red',troughcolor='snow2')
Release.pack()
Label(Relfram, text="Release", fg='snow2',background='black').pack()
Volume = Scale(Volfram, from_=100, to=0,background='black', fg='red',highlightbackground='gray4',activebackground='red',troughcolor='snow2')
Volume.pack()
Label(Volfram, text="Volume", fg='snow2', background='black').pack()
Apply = Button(Exfram, text="Appliquer", command=ApplyChanges, background='black', fg='green')
Apply.pack()
Exit = Button(Exfram, text="Exit", command=Fen_Controls.quit,background='black', fg='red')
Exit.pack()
def PlaySound(freq, volume, onde, attack, release, octave) :
temps = 1 # en secondes
volume = float(volume)
freq = int(freq)
onde = int(onde)
attack = int(attack*16) # On multiplie par 16 car le slider va de 0 ?? 128, alors que le son est de 4096 b/s
release = int(4096-release*16)
ampli = 1 # Variable servant pour l'attaque et release
octave = 0
samples = 4096 # bits par seconde. Ne pas toucher !
n_samples = int(round(temps*samples)) # Nombre total de bits
tableau = numpy.zeros((n_samples, 2), dtype = numpy.int16) # Cr??ation du tableau, rempli de 0
max_sample = 2**(bits - 1) - 1
for s in range(n_samples):
x = float(s)/samples # le x pour les fonctions math??matiques
# Attaque
if s < attack : # Si on est dans la phase attaque : ok
ampli = float(s+1)/float(attack+1)
# Release
if s > release : # Si on est dans la phase release : ok
ampli = float(samples-s+1)/float(samples-release+1)
if onde == 1 : # sinuso??dale
tableau[s][0] = int(round(max_sample*math.sin(math.pi*freq*x)*volume*ampli))
tableau[s][1] = int(round(max_sample*math.sin(math.pi*freq*x)*volume*ampli))
elif onde == 2 : # carr??e
if (int(round(max_sample*math.sin(math.pi*freq*x)*volume*ampli))) >= 0 : # si oui = phase 1
tableau[s][0] = int(round(max_sample*volume*ampli))
tableau[s][1] = int(round(max_sample*volume*ampli))
else : # sinon = phase 0
tableau[s][0] = int(0)
tableau[s][1] = int(0)
sound = pygame.sndarray.make_sound(tableau)
sound.play(loops = 0)
root = Tk()
topframe=Frame(root)
topframe.pack()
root.title('PIANO')
num1=StringVar()
topframe=Frame(root)
topframe.pack(side=TOP)
txtdisplay=Entry(topframe,textvariable = num1,bd=20,insertwidth=1,font=30 ,justify= 'center',width=4,)
txtdisplay.pack(side=TOP)
def printa() :
print attack
button1 = Button(topframe,padx=0, height = 6, pady=0, bd=0, text="C#", bg="black",fg="white",command=lambda x=100,y=1,z=onde,a=int(attack),b=release,c=0:PlaySound(x,y,z,a,b,c))
#button1 = Button(topframe,padx=0, height = 6, pady=0, bd=0, text="C#", bg="black",fg="white",command=lambda:printa())
button1.pack(side=LEFT)
button22 = Button(topframe, state=DISABLED,height=7,width=1,padx=0, pady=0, relief=RIDGE)
button22.pack(side=LEFT)
button2= Button(topframe,padx=0, height = 6, pady=0, bd=0, text="D#", bg="black",fg="white")
button2.pack(side=LEFT)
button22= Button(topframe,state=DISABLED,height=7, width=1,padx=0,pady=0, relief= RIDGE)
button22.pack(side=LEFT)
button3 = Button(topframe,padx=0, height = 6, pady=0, bd=0, text="f#" , bg="black",fg="white")
button3.pack(side=LEFT)
button22= Button(topframe,state=DISABLED,height=7, width=1,padx=0,pady=0, relief=RIDGE)
button22.pack(side=LEFT)
button4= Button(topframe,padx=0,height= 6,pady=0,bd=0,text="G#", bg="black",fg="white")
button22= Button(topframe,state=DISABLED,height=7, width=1,padx=0,pady=0, relief=RIDGE)
button22.pack(side=LEFT)
button2= Button(topframe,padx=0, height = 6, pady=0, bd=0, text="D#", bg="black",fg="white")
button2.pack(side=LEFT)
Fen_Controls.mainloop()
root.mainloop()
Fen_Controls.destroy()
pygame.quit()