[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

Re: [pygame] "game" idea (sound output)



Initially I wrote this in response to Greg Ewing's recent query, but it might be more generally useful to pygame programmers who want do complex or cool sound stuff, or even simple stuff...

Hi Greg,

Did you want to attach a midi-capable instrument to your computer and use pygame to control it? Or did you just want to get sounds out, intending to use the built-in midi synthesizer to do it?

If the first, then I'm sorry I don't know enough to help, but if the second and you're still unable to get pygame to generate sounds you might be interested in using pygame to send instructions to sox.

Sox is a free, open source, cross-platform program that processes and generates sounds. Most people think it's only a sound player or format converter that can work on almost any sound file format in existence, but it can also synthesize sounds.

For instance:
	play -n synth 4 pluck A3
which sounds like a single guitar string being plucked

	play -n synth 0.1 sine 500-2000
generates a 0.1 second sine wave sweeping from 500Hz to 2000Hz, sounding a little like a water drop. But adding chorus to it can make it sound like a bunch of bubbles:
	play -n synth 0.1 sine 500-2000 chorus 0.7 0.9 55 0.4 0.25 2 -t


You can make up quite complex sounds, like:
play -n synth -j 3 sin %3 sin %-2 sin %-5 sin %-9 sin %-14 sin %-21 fade h .01 2 1.5 delay 1.3 1 .76 .54 .27 remix - fade h 0 2.7 2.5 norm -1 which sounds like the polite alert tones used in some hotels preceding a message over the public-address system.

play -n synth pl G2 pl B2 pl D3 pl G3 pl D4 pl G4 delay 0 .05 .1 .15 .2 .25 remix - fade 0 4 .1 norm -1
which sounds astonishingly like a guitar being strummed.

The syntax looks complex, but is actually fairly simple and is ideal for using a program to send stuff to it. In fact it most of the actual heavy lifting is done by the sox library which is open to programmers, so could conceivably be used directly from inside pygame.

Sox has a mind-blowing number of options that let you process sounds (synthesized or sound files) while playing them. (It can also write result to a file instead of simply playing them.) Here is a quick list of effects: llpass band bandpass bandreject bass bend biquad chorus channels compand contrast crop+ dcshift deemph delay dither divide+ earwax echo echos equalizer fade filter fir firfit flanger gain highpass input key loudness lowpass mcompand mixer noiseprof noisered norm oops output overdrive pad pan phaser pitch polyphase rabbit rate remix repeat resample reverb reverse riaa silence sinc spectrogram speed splice stat stats stretch swap synth tempo treble tremolo trim vad vol

Earlier I mentioned that sox can play or convert almost any sound file format. Here is a quick list of formats it can handle: 8svx aif aifc aiff aiffc al amb au avi avr caf cdda cdr cvs cvsd cvu dat dvms f32 f4 f64 f8 fap ffmpeg flac fssd gsm hcom htk ima ircam la lpc lpc10 lu m4a mat mat4 mat5 maud mp2 mp3 mp4 mpg nist ogg paf prc pvf raw s1 s16 s2 s24 s3 s32 s4 s8 sb sd2 sds sf sl smp snd sndfile sndr sndt sou sox sph sw txw u1 u16 u2 u24 u3 u32 u4 u8 ub ul uw vms voc vorbis vox w64 wav wavpcm wmv wve xa xi

It can even use a couple of playlist formats:
m3u pls

Cheers,

	- Miriam

Greg Ewing wrote:
Bill Coderre wrote:

Make one of these for Pygame: http://lab.andre-michelle.com/pulsate

I decided to have a go at making one of these as a pyweek
warmup, but I've hit a bit of a stumbling block.

How to generate the sounds?

My first thought was to use MIDI, so I installed portmidi
and got my pygame configured so that pygame.midi is available.
However, on my system (MacOSX 10.6), pygame.midi.get_count()
reports a total of 0 MIDI devices available. :-(

I was hoping that the built-in midi synthesizer that lurks
somewhere in the bowels of MacOSX would appear as a MIDI
device here, but it seems not.

Anyone have any ideas on how I can get MIDI sounds out of
pygame?



--
If you don't have any failures then you're not trying hard enough.
 - Dr. Charles Elachi, director of NASA's Jet Propulsion Laboratory
-----
Website: http://miriam-english.org
Blogs:   http://miriam-e.dreamwidth.org
         http://miriam-e.livejournal.com