#!/usr/pkg/bin/python """coerce brainwaves to a particular beat frequency the theory is that by playing, say, 500 Hz into the left ear and 505 Hz into the right, your brain picks up on the 'beat frequency' of 5 Hz and will attempt to 'tune' itself to that frequency. You can thus supposedly control your level of consciousness, producing certain chemicals on demand, even assist an out-of-body experience. We shall see. The levels, IIRC: Beta: 14 Hz+ Alpha: 8 - 14 Hz Delta: 4 - 8 Hz Theta: 0 - 4 Hz""" Copyright = """ brainwave - coerce brainwaves to a particular beat frequency Copyright (C) 2004 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.sep.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 import time from com.jcomeau import arcane from com.jcomeau.midi import midi, pymidi def brainwave(*args): "generate slightly different notes in each ear" defaults = ['C5', 12] try: while type(args[0]) is types.ListType or type(args[0]) is types.TupleType: args = args[0] except: args = () if len(args) < 2: sys.stderr.write("Usage: %s NOTE BEAT_FREQUENCY [DURATION(ms)]\n" % self) sys.stderr.write("Example: %s C#5 5 500 # plays for 1/2 sec\n" % self) sys.stderr.write(" %s C#5 5 # plays until control-C\n" % self) sys.exit(0) if DebugPrint(): midi.DebugPrint = DebugPrint # cascade debugging to other modules notes = arcane.ReverseDict(midi.NoteMapping('all', 'midi')) try: note = notes[args[0]] except: sys.stderr.write("Could not map note %s to MIDI note\n" % args[0]) sys.stderr.write("Using %s instead\n" % defaults[0]) note = notes[defaults[0]] try: beat_frequency = float(args[1]) except: sys.stderr.write("Could not understand beat frequency of %s\n" % args[1]) sys.stderr.write("Using frequency %f instead\n" % float(defaults[1])) sys.stderr.write("If this is undesirable, Control-C to stop\n") beat_frequency = float(defaults[1]) try: duration = float(args[2]) except: sys.stderr.write("No duration or invalid duration specified\n") sys.stderr.write("Playing note 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') # one channel for each ear; needless to say, headphones are needed for this midi.SetChannelPan(midihandle, 0, 0) # set channel 0 pan to hard left midi.SetChannelPan(midihandle, 1, 127) # set channel 1 pan to hard right # now alter frequency to right ear, within accuracy of the routine midi.SetChannelPitchFrequency(midihandle, 1, note, beat_frequency) # time to face the music pymidi.write(midihandle, (0x90, note, 64)) # tone to left ear if DebugPrint("Delaying before starting tone to right ear..."): time.sleep(5) if DebugPrint("Shutting down tone to left ear before starting right..."): pymidi.write(midihandle, (0x90, note, 0)) pymidi.write(midihandle, (0x91, note, 64)) # tone to right ear if duration != 0: time.sleep(duration / 1000) pymidi.write(midihandle, (0x90, note, 0)) # end tone to left ear pymidi.write(midihandle, (0x91, note, 0)) # end tone to right ear pymidi.close(midihandle) else: arcane.pause("Hit to stop...") pymidi.write(midihandle, (0x90, note, 0)) # end tone to left ear pymidi.write(midihandle, (0x91, note, 0)) # end tone to right ear 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... brainwave(sys.argv[1:])