views:

271

answers:

3

I'm trying to write a program to randomly generate music based on a simple set of rules. I would like the program to be able to generate its own sounds, as opposed to having a file with audio for each note. Does anyone know a simple way of doing this? It would be nice (but not essential) for the sound to be polytonal, and I would like a solution for Linux, using C. Thanks.

A: 

You can try to find a C midi sequencer (such as MIDI Sequencer). Also look into building .au formatted audio files (i.e. look at the specs for .au headers and sound data format). You won't be able to use .wav format because it requires a length in the header to be filled in before playback.

bkritzer
+3  A: 

I suggest you try the PortAudio library. It is a lean cross-platform library that abstracts the audio-output functionality.

It comes with a bunch of small examples. One of them plays a single sine-wave, one plays a bunch of sine-waves at the same time. Since the examples already do 90% of what you need you should have your audio up and running in less than half an hour.

Hint: The best documentation of PortAudio is in the headerfile!

Nils Pipenbrinck
A: 

Here is an ALSA example that plays a pure sine-wave tone. Accidentally, I guess, it also demonstrates why you might not want to do this directly against the ALSA library.

unwind
Alsa does seem to be fairly bulky in this scene, strangely I find the OSS version of a similar thing much more understandable; it is here "http://manuals.opensound.com/developer/singen.c.html". If there is one thing I've learnt from this it is that working with sound for Linux is a little confusing.
hungry_joe