【问题标题】:Sine Tone that is constantly changing based on time related variables基于时间相关变量不断变化的正弦音
【发布时间】:2011-12-04 03:14:52
【问题描述】:
    #Sine wave calculations by vegaseat.
#The program will take in the current time that the computer has, and turn that into a
#tone that is unique to that moment in time.  It then makes an accompaning graph to
#view for easier comparison of the sound and an interesting view of the sound heard.

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


def wave():
    #time based variables
    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("The moment")

    #variables for canvas
    width = 800
    height = 600
    center = height//2
    x_increment = 2
    # width stretch
    x_factor1 = tis
    x_factor2 = tim
    # height stretch
    y_amplitude = 50

    #new canvas
    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)

    #create the lines
    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()
#time based variables
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")

#running it using main.
def main():
    au_file(name="timeSound.au", freq=tim, freq1=tis, dur=1000, vol=1.0)
    wave()

main()

这是我的程序,它需要几分钟,并让它们根据时间(种类)对音调产生正弦波音调。它创建声音文件,然后播放它。我想要的是一个实时流式正弦波,它受时间影响到毫秒,以获得恒定的波动音调。那可能吗?如果是这样,我可以使用什么来播放声音?如何让变量与时间相关联并让它们自己活起来?

【问题讨论】:

  • 你有一个函数来描述你希望在给定时间的频率吗?
  • 我只希望频率通过计算机时钟值作为布尔变量来改变。所以频率是'def au_file(name, freq, freq1, dur, vol): fout = open(name, "wb") # header需要大小, 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 # 为范围内的 seg 写入数据( 8 * dur): # 正弦波计算 sin_seg = sin(seg * factor) + sin(seg * factor1) fout.write(pack("b", vol * 64 * sin_seg)) fout.close()'

标签: python variables audio time live


【解决方案1】:

对于连续变化的频率,您可以采用以下方法:

def gen_scale(samples_per_wave, samples_per_change):
    pi2 = 2 * math.pi
    rad = 0
    while (samples_per_wave > 1):
        for i in range(0, samples_per_change):
            yield math.sin(rad)
            rad = rad + pi2 / samples_per_wave
            if (rad > pi2):
                rad = rad - pi2
        samples_per_wave = samples_per_wave - 1

samples_per_wave 基本上是Hz 测量的倒数(这个声音会增加频率)。 samples_per_change 是生成器在逐步调整频率之前产生的样本数。

【讨论】:

  • 这会随着时间的推移产生频率变化的音调,对吗?我的目标是让 python 生成一个音调,它是一个实时流,其频率受基于计算机时钟的变量的影响。另外我相信我需要另一个库来生成和播放音调,而不是依赖用户的默认程序。有关可以生成和播放音调的库的任何建议,也符合原始问题的要求。
  • @Weeds:这可以很容易地以您想要的方式进行调整。弧度测量提前pi2 / samples_per_wave。该参数不是在连续变化的循环中产生的。它可以从外部提供,也可以很好地上下摩擦。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多