====== Der Finale Code ====== #!/usr/bin/env python # -*- coding: utf-8 -*- # # test_moving_circles.py # # Copyright 2017 Stefan Born # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, # MA 02110-1301, USA. # # from __future__ import division import numpy as np import moving_circles import time from scipy.fftpack import * from moving_circles import Canvas, Circle from microlistener import MicroListener from random import randint, uniform # 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=[] last_values1 = (0,0,0) last_values2 = (0,0,0) last_values3 = (0,0,0) 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) ### l_kreise=[] lasttime=0 last_values=[] def action(objects, event): global lasttime global z, last_values n = 50 #print z[2048]/1000000.0 last_values.append(list(z).index(max(z[19:41]))) ##440-880 Hz (?) if time.time()-lasttime>=2: avg_last_values=sum(last_values)/len(last_values) l_kreise.append(Circle(win,pos=WinkelPos(len(l_kreise),dftWinkel(avg_last_values)), radius=uniform(0.01,0.1), color=(randint(0,10)/10,randint(0,10)/10,randint(0,10)/10,0.2))) lasttime=time.time() print "_avg:_"+str(avg_last_values) last_values=[] ''' print "z"+str(z[19:41]) print "max:"+str(max(z[19:41])) print "index:"+str(list(z).index(max(z[19:41]))) ''' # last_values2 = (last_values2[1], last_values2[2], sum(z[19:19+n])/(n*1000000.0)) ##100 Hz (?) # last_values3 = (last_values3[1], last_values3[2], sum(z[1115:1115+n])/(n*10000.0)) ##6000 Hz (?) # circ.set_radius(sum(last_values1)/3) # circ2.set_radius(sum(last_values2)/3) # circ3.set_radius(sum(last_values3)/3) #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) ) ''' i = 0.01 for circ_temp in l_kreise: circ_temp.set_radius(i) i+=0.005 ''' last_alpha=0 dftWinkel_m=((-2/3)*np.pi/21) dftWinkel_n=dftWinkel_m*(-19)+(np.pi/3) def dftWinkel(index_of_max): # print str((dftWinkel_m*index_of_max+dftWinkel_n)/np.pi) return dftWinkel_m*index_of_max+dftWinkel_n def WinkelPos(i,alpha): global last_posx, last_posy, last_alpha #last_alpha=0 #last_posx=0 #last_posy=0 #Abstand konstant 0.1 abstand=0.1 if i==0: posx=0 posy=0 last_posx=posx last_posy=posy return (posx,posy) else: new_posx=last_posx+(np.cos(last_alpha+alpha)*abstand) new_posy=last_posy+(np.sin(last_alpha+alpha)*abstand) if new_posx<=1 and new_posx>=-1: posx=new_posx # print posx elif new_posx>1: posx=new_posx-2 elif new_posx<-1: posx=new_posx+2 if new_posy<=1 and new_posy>=-1: posy=new_posy # print posy elif new_posy>1: posy=new_posy-2 elif new_posy<-1: posy=new_posy+2 last_alpha+=alpha last_posx=posx last_posy=posy return (posx,posy) # 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) #for i in range(20): #l_kreise.append(Circle(win,pos=(randint(-400,400)/400,randint(-400,400)/400), radius=0, color=(randint(0,10)/10,randint(0,10)/10,randint(0,10)/10,0.2))) # l_kreise.append(Circle(win,pos=WinkelPos(i,uniform(-np.pi/2,np.pi/2)), radius=uniform(0.01,0.1), color=(randint(0,10)/10,randint(0,10)/10,randint(0,10)/10,0.2))) # print i #circ = Circle(win, pos=(x1,y1),radius = 0.1, color=(1,0,0,0.2)) #circ2 = Circle(win, pos=(x2,y2),radius = 0.1, color=(0,1,0,0.2)) #circ3 = Circle(win, pos=(x3,y3),radius = 0.1, color=(0,0,1,0.2)) while True: win.set_action(action, objects) #print z[0] win.run()