Skip to content

Commit 9cb76c1

Browse files
committed
moved spectrum classes
1 parent 3d0256e commit 9cb76c1

File tree

2 files changed

+120
-129
lines changed

2 files changed

+120
-129
lines changed

game/radio.py

Lines changed: 0 additions & 129 deletions
Original file line numberDiff line numberDiff line change
@@ -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-
289160
def play_pygame(file):
290161

291162
clock = pygame.time.Clock()

pypboy/data.py

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
import xmltodict
22
import requests
3+
import numpy
4+
from numpy.fft import fft
5+
from math import log10
36
import math
7+
import pygame
48

59

610
class Maps(object):
@@ -147,3 +151,119 @@ def transpose_tags(self, dimensions, offset, flip_y=True):
147151
wp[2] += offset[1] * 2
148152
transtags.append(wp)
149153
return transtags
154+
155+
156+
157+
class SoundSpectrum:
158+
"""
159+
Obtain the spectrum in a time interval from a sound file.
160+
"""
161+
162+
left = None
163+
right = None
164+
165+
def __init__(self, filename, force_mono=False):
166+
"""
167+
Create a new SoundSpectrum instance given the filename of
168+
a sound file pygame can read. If the sound is stereo, two
169+
spectra are available. Optionally mono can be forced.
170+
"""
171+
# Get playback frequency
172+
nu_play, format, stereo = pygame.mixer.get_init()
173+
self.nu_play = 1./nu_play
174+
self.format = format
175+
self.stereo = stereo
176+
177+
# Load sound and convert to array(s)
178+
sound = pygame.mixer.Sound(filename)
179+
a = pygame.sndarray.array(sound)
180+
a = numpy.array(a)
181+
if stereo:
182+
if force_mono:
183+
self.stereo = 0
184+
self.left = (a[:,0] + a[:,1])*0.5
185+
else:
186+
self.left = a[:,0]
187+
self.right = a[:,1]
188+
else:
189+
self.left = a
190+
191+
def get(self, data, start, stop):
192+
"""
193+
Return spectrum of given data, between start and stop
194+
time in seconds.
195+
"""
196+
duration = stop-start
197+
# Filter data
198+
start = int(start/self.nu_play)
199+
stop = int(stop/self.nu_play)
200+
N = stop - start
201+
data = data[start:stop]
202+
203+
# Get frequencies
204+
frequency = numpy.arange(N/2)/duration
205+
206+
# Calculate spectrum
207+
spectrum = fft(data)[1:1+N/2]
208+
power = (spectrum).real
209+
210+
return frequency, power
211+
212+
def get_left(self, start, stop):
213+
"""
214+
Return spectrum of the left stereo channel between
215+
start and stop times in seconds.
216+
"""
217+
return self.get(self.left, start, stop)
218+
219+
def get_right(self, start, stop):
220+
"""
221+
Return spectrum of the left stereo channel between
222+
start and stop times in seconds.
223+
"""
224+
return self.get(self.right, start, stop)
225+
226+
def get_mono(self, start, stop):
227+
"""
228+
Return mono spectrum between start and stop times in seconds.
229+
Note: this only works if sound was loaded as mono or mono
230+
was forced.
231+
"""
232+
return self.get(self.left, start, stop)
233+
234+
class LogSpectrum(SoundSpectrum):
235+
"""
236+
A SoundSpectrum where the spectrum is divided into
237+
logarithmic bins and the logarithm of the power is
238+
returned.
239+
"""
240+
241+
def __init__(self, filename, force_mono=False, bins=20, start=1e2, stop=1e4):
242+
"""
243+
Create a new LogSpectrum instance given the filename of
244+
a sound file pygame can read. If the sound is stereo, two
245+
spectra are available. Optionally mono can be forced.
246+
The number of spectral bins as well as the frequency range
247+
can be specified.
248+
"""
249+
SoundSpectrum.__init__(self, filename, force_mono=force_mono)
250+
start = log10(start)
251+
stop = log10(stop)
252+
step = (stop - start)/bins
253+
self.bins = 10**numpy.arange(start, stop+step, step)
254+
255+
def get(self, data, start, stop):
256+
"""
257+
Return spectrum of given data, between start and stop
258+
time in seconds. Spectrum is given as the log of the
259+
power in logatithmically equally sized bins.
260+
"""
261+
f, p = SoundSpectrum.get(self, data, start, stop)
262+
bins = self.bins
263+
length = len(bins)
264+
result = numpy.zeros(length)
265+
ind = numpy.searchsorted(bins, f)
266+
for i,j in zip(ind, p):
267+
if i<length:
268+
result[i] += j
269+
return bins, result

0 commit comments

Comments
 (0)