@@ -157,135 +157,6 @@ def update(self,time,frequency,power):
157157 except Exception ,e :
158158 print traceback .format_exc ()
159159
160-
161- class SoundSpectrum :
162- """
163- Obtain the spectrum in a time interval from a sound file.
164- """
165-
166- left = None
167- right = None
168-
169- def __init__ (self , filename , force_mono = False ):
170- """
171- Create a new SoundSpectrum instance given the filename of
172- a sound file pygame can read. If the sound is stereo, two
173- spectra are available. Optionally mono can be forced.
174- """
175- # Get playback frequency
176- nu_play , format , stereo = pygame .mixer .get_init ()
177- self .nu_play = 1. / nu_play
178- self .format = format
179- self .stereo = stereo
180-
181- # Load sound and convert to array(s)
182- sound = pygame .mixer .Sound (filename )
183- a = pygame .sndarray .array (sound )
184- a = numpy .array (a )
185- if stereo :
186- if force_mono :
187- self .stereo = 0
188- self .left = (a [:,0 ] + a [:,1 ])* 0.5
189- else :
190- self .left = a [:,0 ]
191- self .right = a [:,1 ]
192- else :
193- self .left = a
194-
195- def get (self , data , start , stop ):
196- """
197- Return spectrum of given data, between start and stop
198- time in seconds.
199- """
200- duration = stop - start
201- # Filter data
202- start = int (start / self .nu_play )
203- stop = int (stop / self .nu_play )
204- N = stop - start
205- data = data [start :stop ]
206-
207- # Get frequencies
208- frequency = numpy .arange (N / 2 )/ duration
209-
210- # Calculate spectrum
211- spectrum = fft (data )[1 :1 + N / 2 ]
212- power = (spectrum ).real
213-
214- return frequency , power
215-
216- def get_left (self , start , stop ):
217- """
218- Return spectrum of the left stereo channel between
219- start and stop times in seconds.
220- """
221- return self .get (self .left , start , stop )
222-
223- def get_right (self , start , stop ):
224- """
225- Return spectrum of the left stereo channel between
226- start and stop times in seconds.
227- """
228- return self .get (self .right , start , stop )
229-
230- def get_mono (self , start , stop ):
231- """
232- Return mono spectrum between start and stop times in seconds.
233- Note: this only works if sound was loaded as mono or mono
234- was forced.
235- """
236- return self .get (self .left , start , stop )
237-
238- class LogSpectrum (SoundSpectrum ):
239- """
240- A SoundSpectrum where the spectrum is divided into
241- logarithmic bins and the logarithm of the power is
242- returned.
243- """
244-
245- def __init__ (self , filename , force_mono = False , bins = 20 , start = 1e2 , stop = 1e4 ):
246- """
247- Create a new LogSpectrum instance given the filename of
248- a sound file pygame can read. If the sound is stereo, two
249- spectra are available. Optionally mono can be forced.
250- The number of spectral bins as well as the frequency range
251- can be specified.
252- """
253- SoundSpectrum .__init__ (self , filename , force_mono = force_mono )
254- start = log10 (start )
255- stop = log10 (stop )
256- step = (stop - start )/ bins
257- self .bins = 10 ** numpy .arange (start , stop + step , step )
258-
259- def get (self , data , start , stop ):
260- """
261- Return spectrum of given data, between start and stop
262- time in seconds. Spectrum is given as the log of the
263- power in logatithmically equally sized bins.
264- """
265- f , p = SoundSpectrum .get (self , data , start , stop )
266- bins = self .bins
267- length = len (bins )
268- result = numpy .zeros (length )
269- ind = numpy .searchsorted (bins , f )
270- for i ,j in zip (ind , p ):
271- if i < length :
272- result [i ] += j
273- return bins , result
274-
275- def load_files ():
276- files = []
277- workingdir = os .getcwd ()
278- try :
279- os .chdir ("radio" )
280- except :
281- try : os .chdir ("../radio" )
282- except : pass
283- for file in os .listdir ("." ):
284- if file .endswith (".ogg" ):
285- files .append (os .path .abspath (file ))
286- os .chdir (workingdir )
287- return files
288-
289160def play_pygame (file ):
290161
291162 clock = pygame .time .Clock ()
0 commit comments