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
| # -*- coding: cp1252 -*-
import math, numarray as NA, wave, audioop, sys, pyaudio
from Tkinter import*
def sound_creator():
varFreq = float(eval(Freq.get())) # Reception des valeures
varDuree = float(eval(Duree.get()))
freqech = 44100
omega = 2 * math.pi * varFreq
son = NA.arange(0., varDuree, 1./freqech, NA.Float64)
son = NA.sin(omega * son)
son *= 32768.
son = son.astype(NA.Int16)
wavfile = wave.open('AutreAmplitude.wav','w')
wavfile.setparams((1, 2, freqech , len(son), 'NONE', 'not compressed'))
wavfile.writeframes(son.tostring())
wavfile.close()
sound_play()
def sound_play():
chunk = 1024
wf = wave.open("AutreAmplitude.wav",'r')
p = pyaudio.PyAudio()
# open stream
stream = p.open(format =
p.get_format_from_width(wf.getsampwidth()),
channels = wf.getnchannels(),
rate = wf.getframerate(),
output = True)
# read data
data = wf.readframes(chunk)
# play stream
while data!='' :
stream.write(data)
data = wf.readframes(chunk)
stream.close()
p.terminate()
def sound_break(): # Fonction pour stop
stop=2
######################### Création de la fenètre #########################
fen1 = Tk()
fen1.title("Sound Player")
# 3ème Colonne.
TextFreq = Label(fen1, text="Fréquence [Hz]").grid(row=1, column=3)
TextDuty = Label(fen1, text="Durée [s]").grid(row=2, column=3)
#Frequency
varFreq =StringVar()
Freq = Entry(fen1, textvariable= varFreq)
Freq.grid(row=1, column=4)
varFreq.set("500")
#Durée
varDuree =StringVar()
Duree = Entry(fen1, textvariable= varDuree)
Duree.grid(row=2, column=4)
varDuree.set("10")
stop=0
#Bouton n°1 (Lecture)
Inter = Button(fen1,text="Lecture", command=sound_creator)
Inter.grid(row=3, column=3, columnspan=2)
#Bouton n°2 (Stop)
Inter2 = Button(fen1,text="Stop", command=sound_break)
Inter2.grid(row=3, column=4, columnspan=2)
fen1.mainloop() |
Partager