2010-04-06 11 views
0

J'ai deux scripts python séparés qui font un son sinusoïdal basé sur le temps, et un autre qui produit un graphe sinusoïdal basé sur les mêmes facteurs temporels. J'ai besoin d'aide pour les combiner en un seul fichier en cours d'exécution.Python: combiner deux scripts en un seul

Voici le premier:

from struct import pack 
from math import sin, pi 
import time 

def au_file(name, freq, freq1, dur, vol): 
    fout = open(name, 'wb') 
    # header needs size, encoding=2, sampling_rate=8000, channel=1 
    fout.write('.snd' + pack('>5L', 24, 8*dur, 2, 8000, 1)) 
    factor = 2 * pi * freq/8000 
    factor1 = 2 * pi * freq1/8000 
    # write data 
    for seg in range(8 * dur): 
     # sine wave calculations 
     sin_seg = sin(seg * factor) + sin(seg * factor1) 
     fout.write(pack('b', vol * 64 * sin_seg)) 
    fout.close() 

t = time.strftime("%S", time.localtime()) 
ti = time.strftime("%M", time.localtime()) 
tis = float(t) 
tis = tis * 100 
tim = float(ti) 
tim = tim * 100 

if __name__ == '__main__': 
    au_file(name='timeSound.au', freq=tim, freq1=tis, dur=1000, vol=1.0) 

    import os 
    os.startfile('timeSound.au') 

et le second:

from Tkinter import * 
import math 
import time 

t = time.strftime("%S", time.localtime()) 
ti = time.strftime("%M", time.localtime()) 
tis = float(t) 
tis = tis/100 
tim = float(ti) 
tim = tim/100 

root = Tk() 
root.title("This very moment") 

width = 400 
height = 300 
center = height//2 
x_increment = 1 
# width stretch 
x_factor1 = tis 
x_factor2 = tim 
# height stretch 
y_amplitude = 50 

c = Canvas(width=width, height=height, bg='black') 
c.pack() 

str1 = "sin(x)=white" 
c.create_text(10, 20, anchor=SW, text=str1) 

center_line = c.create_line(0, center, width, center, fill='red') 

# create the coordinate list for the sin() curve, have to be integers 
xy1 = [] 
xy2 = [] 
for x in range(400): 
    # x coordinates 
    xy1.append(x * x_increment) 
    xy2.append(x * x_increment) 
    # y coordinates 
    xy1.append(int(math.sin(x * x_factor1) * y_amplitude) + center) 
    xy2.append(int(math.sin(x * x_factor2) * y_amplitude) + center) 

sinS_line = c.create_line(xy1, fill='white') 
sinM_line = c.create_line(xy2, fill='yellow') 



root.mainloop() 
+0

Remplacer '' write' avec return'. –

+1

J'aime votre marque de style. – Alex

Répondre

1
from Tkinter import * 
from struct import pack 
from math import sin, pi 
import math 
import time 
import os 


def wave(): 
    t = time.strftime("%S", time.localtime()) 
    ti = time.strftime("%M", time.localtime()) 
    tis = float(t) 
    tis = tis/100 
    tim = float(ti) 
    tim = tim/100 

    root = Tk() 
    root.title("This very moment") 

    width = 400 
    height = 300 
    center = height//2 
    x_increment = 1 
    # width stretch 
    x_factor1 = tis 
    x_factor2 = tim 
    # height stretch 
    y_amplitude = 50 

    c = Canvas(width=width, height=height, bg='black') 
    c.pack() 

    str1 = "sin(x)=white" 
    c.create_text(10, 20, anchor=SW, text=str1) 

    center_line = c.create_line(0, center, width, center, fill='red') 

    # create the coordinate list for the sin() curve, have to be integers 
    xy1 = [] 
    xy2 = [] 
    for x in range(400): 
     # x coordinates 
     xy1.append(x * x_increment) 
     xy2.append(x * x_increment) 
     # y coordinates 
     xy1.append(int(math.sin(x * x_factor1) * y_amplitude) + center) 
     xy2.append(int(math.sin(x * x_factor2) * y_amplitude) + center) 

    sinS_line = c.create_line(xy1, fill='white') 
    sinM_line = c.create_line(xy2, fill='yellow') 

    root.mainloop() 

def au_file(name, freq, freq1, dur, vol): 
    fout = open(name, 'wb') 
    # header needs size, encoding=2, sampling_rate=8000, channel=1 
    fout.write('.snd' + pack('>5L', 24, 8*dur, 2, 8000, 1)) 
    factor = 2 * pi * freq/8000 
    factor1 = 2 * pi * freq1/8000 
    # write data 
    for seg in range(8 * dur): 
     # sine wave calculations 
     sin_seg = sin(seg * factor) + sin(seg * factor1) 
     fout.write(pack('b', vol * 64 * sin_seg)) 
    fout.close() 

t = time.strftime("%S", time.localtime()) 
ti = time.strftime("%M", time.localtime()) 
tis = float(t) 
tis = tis * 100 
tim = float(ti) 
tim = tim * 100 
os.startfile('timeSound.au') 

def main(): 
    au_file(name='timeSound.au', freq=tim, freq1=tis, dur=1000, vol=1.0) 
    wave() 

main()