Hier werden die Unterschiede zwischen zwei Versionen gezeigt.
Beide Seiten der vorigen Revision Vorhergehende Überarbeitung Nächste Überarbeitung | Vorhergehende Überarbeitung | ||
ws1617:the_art_of_music:protokolle:07 [2017/02/09 17:39] cm.mint.2016 |
ws1617:the_art_of_music:protokolle:07 [2017/02/23 11:46] (aktuell) cm.mint.2016 |
||
---|---|---|---|
Zeile 1: | Zeile 1: | ||
====== 2017-02-09: ====== | ====== 2017-02-09: ====== | ||
- | test_moving_circles_1.2___4-Kreise-Frequenzband | + | test_moving_circles_1.2 mit 4 Kreisen |
+ | <code python> | ||
+ | from __future__ import division | ||
+ | |||
+ | import numpy as np | ||
+ | import moving_circles | ||
+ | from scipy.fftpack import * | ||
+ | from moving_circles import Canvas, Circle | ||
+ | from microlistener import MicroListener | ||
+ | |||
+ | # chose 'VISPY' or 'MATPLOTLIB' | ||
+ | |||
+ | PREFERRED_BACKEND = 'MATPLOTLIB_WX' | ||
+ | |||
+ | ###################################################### | ||
+ | # One possible use case: | ||
+ | # Use timer event of backend to change graphic objects. | ||
+ | # Here 10 circles are created and a callback function | ||
+ | # action(...) is registered to be called at each timer | ||
+ | # event. | ||
+ | ###################################################### | ||
+ | ###micolistener### | ||
+ | CHANNELS=2 | ||
+ | RATE=44100 | ||
+ | CHUNK=2**11 | ||
+ | z=[] | ||
+ | |||
+ | def micro_callback(in_data, frame_count, time_info,status): | ||
+ | global z | ||
+ | y=np.array(np.fromstring(in_data,dtype=np.short),dtype=np.float) | ||
+ | yf = fft(y) | ||
+ | xf = np.linspace(0.0, (2.0*RATE), CHUNK/2) | ||
+ | if CHANNELS==2: | ||
+ | y=y.reshape((y.shape[0]//2,2)) | ||
+ | else: | ||
+ | y=y.reshape((y.shape[0],1)) | ||
+ | #print int(np.linalg.norm(y[:,0])/2000.)*'*' | ||
+ | z=np.abs(yf) | ||
+ | return (in_data, status) | ||
+ | |||
+ | listen=MicroListener(RATE,CHANNELS,CHUNK,micro_callback) | ||
+ | ### | ||
+ | |||
+ | def action(objects, event): | ||
+ | global z | ||
+ | z_100=z[17:20] #194-215 Hz | ||
+ | z_1000=z[176:196] #1906-2110 Hz | ||
+ | z_6000=z[928:1300] #10002-13997 Hz | ||
+ | #print z[2048]/1000000.0 | ||
+ | print z_100 | ||
+ | circ.set_radius((sum(z_1000)/20)/1000000.0) ##1000 Hz (?) | ||
+ | circ2.set_radius((sum(z_100)/3)/10000000.0) ##100 Hz (?) | ||
+ | circ3.set_radius((sum(z_6000)/372)/50000.0) ##6000 Hz (?) | ||
+ | #dt, elapsed = event.dt, event.elapsed | ||
+ | #for i,circ in enumerate(objects): | ||
+ | # circ.move(0.7*dt*np.sin(elapsed*(i+1)), 0.7*dt*np.cos(elapsed*(i+1))) | ||
+ | # circ.set_color( (np.abs(np.sin(elapsed*(i+1))),np.abs(np.sin(elapsed*2*(i+1))), np.abs(np.sin(elapsed*3*(i+1))),0.3) ) | ||
+ | |||
+ | |||
+ | # create objects, register callback and start program. | ||
+ | |||
+ | win = Canvas() | ||
+ | objects = [] | ||
+ | |||
+ | #for i in range(10): | ||
+ | # circ = Circle(win, pos=(-0.6+0.05*i,0), radius=0.1,color = (1,0,0,0.2)) | ||
+ | # objects.append(circ) | ||
+ | |||
+ | #coordinates | ||
+ | x1=0.0 | ||
+ | y1=0.25 | ||
+ | x2=-0.5/np.sqrt(5) | ||
+ | y2=-0.25/np.sqrt(5) | ||
+ | x3=0.5/np.sqrt(5) | ||
+ | y3=-0.25/np.sqrt(5) | ||
+ | |||
+ | circ = Circle(win, pos=(x1,y1),radius = 0.1, color=(1,0,0,0.4)) | ||
+ | circ2 = Circle(win, pos=(x2,y2),radius = 0.1, color=(0,1,0,0.4)) | ||
+ | circ3 = Circle(win, pos=(x3,y3),radius = 0.1, color=(0,0,1,0.4)) | ||
+ | |||
+ | while True: | ||
+ | win.set_action(action, objects) | ||
+ | #print z[0] | ||
+ | win.run() | ||
+ | </code> |