#!/usr/pkg/bin/python """facilitate spirit communications using set of particular frequencies spirit communications powered by 13 simulaneous audio frequencies 131, 141, 151, 241, 272, 282, 292, 302, 415, 433, 515, 653, 701 Hz see www.worlditc.org/k_06_spiricom.htm""" Copyright = """ whitenoise.py -- generate frequencies to aid spirit communications Copyright (C) 2005 John Comeau 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., 675 Mass Ave, Cambridge, MA 02139, USA. """ errormessage = "Not all needed libraries found, upgrade or check path" try: True # not defined in older Python releases except: True, False = 1, 0 try: import sys, os, types, re, pwd sys.path.append(os.path.join(pwd.getpwuid(os.geteuid())[5], 'lib', 'python')) from com.jcomeau import gpl, jclicense except: try: sys.stderr.write("%s\n" % errormessage) except: print errormessage raise # get name this program was called as self = sys.argv[0].split(os.sep)[-1] # now get name we gave it when we wrote it originalself = Copyright.split()[0] # globals and routines that should be in every program # (yes, you could import them, but there are problems in that approach too) def DebugPrint(*whatever): pass # defined instead by pytest module, use that for debugging # other globals, specific to this program spirit_hz = [131, 141, 151, 241, 272, 282, 292, 302, 415, 433, 515, 653, 701] # skip drum channel or we'll be missing a frequency channels = [0, 1, 2, 3, 4, 5, 6, 7, 8, 10, 11, 12, 13] import time from com.jcomeau import arcane from com.jcomeau.midi import midi, pymidi def whitenoise(*args): """generate frequencies for specified time period: 131, 141, 151, 241, 272, 282, 292, 302, 415, 433, 515, 653, 701 Hz""" try: while type(args[0]) is types.ListType or type(args[0]) is types.TupleType: args = args[0] except: args = () if len(args) < 1: sys.stderr.write("Usage: %s DURATION(ms) (use 0 for continuous)\n" % self) sys.exit(0) if DebugPrint(): midi.DebugPrint = DebugPrint # cascade debugging to other modules try: duration = float(args[1]) except: sys.stderr.write("No duration or invalid duration specified\n") sys.stderr.write("Playing notes forever until key is hit\n") duration = 0 midihandle = pymidi.open("/dev/midi", "wb") # piano (default) sound decays too rapidly, try something else... midi.SetChannelProgram(midihandle, 0, 'Flute') midi.SetChannelProgram(midihandle, 1, 'Flute') tones = midi.NoteMapping('all', 'frequency').values() # time to face the music channel = 0 for index in range(0, len(spirit_hz)): tone = spirit_hz[index] channel = channels[index] note = arcane.NearestValueIndex(tone, tones) # index happens to be MIDI note midi.SetChannelPitchFrequency(midihandle, channel, note, tone - tones[note]) pymidi.write(midihandle, (0x90 + channel, note, 64)) # sound tone if duration != 0: time.sleep(duration / 1000) else: arcane.pause("Hit to stop...") for index in range(0, len(spirit_hz)): tone = spirit_hz[index] channel = channels[index] note = arcane.NearestValueIndex(tone, tones) # index happens to be MIDI note pymidi.write(midihandle, (0x90 + channel, note, 0)) # turn it off pymidi.close(midihandle) if __name__ == '__main__': # if this program was imported by another, the above test will fail, # and this following code won't be used... whitenoise(sys.argv[1:])