Python:将两个脚本合二为一

时间:2010-04-06 02:10:59

标签: python class partial-classes

我有两个单独制作的python脚本,一个根据时间产生正弦波声音,另一个产生基于相同时间因子的正弦波图形。我需要帮助将它们组合成一个正在运行的文件。

这是第一个:

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')

,第二个是:

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()

1 个答案:

答案 0 :(得分: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()