Created
July 2, 2010 05:45
-
-
Save rtanglao/460982 to your computer and use it in GitHub Desktop.
playrandom sounds based on GPS coordinates!
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import location | |
import gobject | |
import pygame | |
import sys | |
from pygame.locals import * | |
import Numeric | |
import random | |
Fs=11025 # sample rate | |
pygame.mixer.init(Fs, -16, 0) # mono, 16-bit | |
pygame.init() | |
pygame.display.set_mode((800, 480)) | |
sound=[] # array of Sounds | |
# sound 0 -------------------- | |
length = Fs * 1.001 # 1.5 seconds | |
freq = 440.0 | |
amp = 16.0 | |
tmp = [] | |
for t in range(int(length)): | |
v= amp * Numeric.sin(t*freq/Fs*2*Numeric.pi) | |
tmp.append(v) | |
sound.append(pygame.sndarray.make_sound(Numeric.array(tmp,Numeric.Int0))) | |
# sound 1 -------------------- | |
length = Fs * 1.001 # 1.5 seconds | |
freq = 440.0 * (5.0/4.0) | |
amp = 16.0 | |
tmp = [] | |
for t in range(int(length)): | |
v= amp * Numeric.sin(t*freq/Fs*2*Numeric.pi) | |
tmp.append(v) | |
sound.append(pygame.sndarray.make_sound(Numeric.array(tmp,Numeric.Int0))) | |
# sound 2 -------------------- | |
length = Fs * 1.001 # 1.5 seconds | |
freq = 440.0 * (4.0/3.0) | |
amp = 16.0 | |
tmp = [] | |
for t in range(int(length)): | |
v= amp * Numeric.sin(t*freq/Fs*2*Numeric.pi) | |
tmp.append(v) | |
sound.append(pygame.sndarray.make_sound(Numeric.array(tmp,Numeric.Int0))) | |
# sound 3 -------------------- tremelo | |
length = Fs * 1.001 # 1.5 seconds | |
freq = 440.0 * (4.0/3.0) | |
freq2 = 2.0 | |
amp = 16.0 | |
tmp = [] | |
for t in range(int(length)): | |
v = amp * Numeric.sin(t*freq/Fs*2*Numeric.pi) | |
v2 = Numeric.sin(t*freq2/Fs*2*Numeric.pi) | |
tmp.append(v*v2) | |
sound.append(pygame.sndarray.make_sound(Numeric.array(tmp,Numeric.Int0))) | |
# sound 4 -------------------- vibrato | |
length = Fs * 1.001 # 1.5 seconds | |
freq = 440.0 * (4.0/3.0) | |
freq2 = 2.0 | |
amp = 16.0 | |
tmp = [] | |
for t in range(int(length)): | |
v = 1 + 0.1 * Numeric.sin(t*freq2/Fs*2*Numeric.pi) | |
v2 = amp * Numeric.sin(t*freq/Fs*2*Numeric.pi * v) | |
tmp.append(v2) | |
sound.append(pygame.sndarray.make_sound(Numeric.array(tmp,Numeric.Int0))) | |
# sound 5 -------------------- vibrato | |
length = Fs * 1.001 # 1.5 seconds | |
freq = 440.0 * (4.0/3.0) | |
freq2 = 17.5 | |
amp = 16.0 | |
tmp = [] | |
for t in range(int(length)): | |
v = 1 + 0.02 * Numeric.sin(t*freq2/Fs*2*Numeric.pi) | |
v2 = amp * Numeric.sin(t*freq/Fs*2*Numeric.pi * v) | |
tmp.append(v2) | |
sound.append(pygame.sndarray.make_sound(Numeric.array(tmp,Numeric.Int0))) | |
def on_error(control, error, data): | |
print "location error: %d... quitting" % error | |
data.quit() | |
def on_changed(device, data): | |
if not device: | |
return | |
if device.fix: | |
if device.fix[1] & location.GPS_DEVICE_LATLONG_SET: | |
lat = device.fix[4] | |
long = device.fix[5] | |
tone = int ((abs(long) * 10000) % 6) | |
print "lat = %f, long = %f" % device.fix[4:6] | |
print "tone = %d" % tone | |
sound[tone].play() | |
## data.stop() | |
def on_stop(control, data): | |
print "quitting from on_stop()" | |
data.quit() | |
def start_location(data): | |
data.start() | |
return False | |
loop = gobject.MainLoop() | |
control = location.GPSDControl.get_default() | |
device = location.GPSDevice() | |
control.set_properties(preferred_method=location.METHOD_USER_SELECTED, | |
preferred_interval=location.INTERVAL_DEFAULT) | |
control.connect("error-verbose", on_error, loop) | |
device.connect("changed", on_changed, control) | |
control.connect("gpsd-stopped", on_stop, loop) | |
gobject.idle_add(start_location, control) | |
loop.run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment