Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I play a tone in Linux using C?

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.

like image 539
hungry_joe Avatar asked Feb 04 '10 20:02

hungry_joe


2 Answers

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!

like image 72
Nils Pipenbrinck Avatar answered Oct 29 '22 21:10

Nils Pipenbrinck


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.

like image 40
unwind Avatar answered Oct 29 '22 22:10

unwind