2011-12-04 3 views
1
#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() 

これは分をとり、時間(種類)に基づいてトーンに正弦波トーンをかけるプログラムです。サウンドファイルを作成して再生します。私が望むのは、一定の変動する音のためにミリ秒までの時間によって影響を受けるライブストリーミングサイン波です。それは可能ですか?もしそうなら、私はサウンドをライブで演奏するために何を使うことができますか?また、変数を時間に関係させて自分自身で生きていくことができますか?ここで時間関連の変数に基づいて常に変化するサイントーン

+0

指定した時間にどのような頻度にするかを説明する関数がありますか? – Omnifarious

+0

私は、コンピュータのクロック値によってブール変数として周波数を変更したいだけです。したがって、周波数は 'def au_file(name、freq、freq1、dur、vol)です。 fout = open(名前、 "wb") #ヘッダーのサイズは、encoding = 2、sampling_rate = 8000、channel = 1です。 fout。 factor = 2 * pi * freq/8000 factor1 = 2 * pi * freq1/8000 #write( ".snd" + pack( "> 5L"、24、8 * dur、2,8000,1) (8 * dur): 範囲内のseg: #正弦波計算 sin_seg = sin(seg * factor)+ sin(seg * factor1) fout.write(pack( "b"、vol * 64 * sin_seg) ) fout.close() ' – Weeds

答えて

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は、周波数を段階的に調整する前に生成器が生成するサンプル数です。

+0

これは時間の経過とともに周波数が変化するトーンを作成しますが、正しいですか?私の目標は、コンピュータのクロックに基づいた変数によって周波数が影響されているライブストリームである、トーンを生成させることです。また、私は、ユーザーのデフォルトプログラムに頼るのではなく、トーンを生成して再生できる別のライブラリが必要になると考えています。元の質問の要件を満たす音色を生成して再生できるライブラリに関する提案。 – Weeds

+0

@雑草:これはあなたが望むやり方で容易に適合させることができます。ラジアン尺度は 'pi2/samples_per_wave'だけ進められます。このパラメータは、連続的に変化するループでは生成されません。それは外側から提供することができ、井戸の上で、そして下に揺れ動く。 – Omnifarious

関連する問題