开发者

Python: combining making two scripts into one

开发者 https://www.devze.com 2022-12-25 23:48 出处:网络
I have two separately made python scripts one that makes a sine wave sound based off time, and another that produces a sine wave graph that is based off the same time factors.I need help combining the

I have two separately made python scripts one that makes a sine wave sound based off time, and another that produces a sine wave graph that is based off the same time factors. I need help combining them into one running file.

Here's the first:

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

and the second is this:

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


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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号