Subscribe to DSC Newsletter

How to produce sounds in Python, R, Java, C, Perl, Javascript or even Linux?

I want to create music generated by mathematical algorithms, or even turning big data files into sound files, just like NASA turned electromagnetic signals from space into music. Producing artificially generated music is a popular subject, see for instance Composing Music With Recurrent Neural Networks, or Using Machine Learning to Generate Music.

My question is how to access my laptop's speaker from a script written in Python or Perl. I used to do it long ago in C language, using the command sound available in the Borland package. Today I tried various system calls from within Perl, or directly from the command line, to non avail. All I need is a function that produces a sound with a given frequency, and a given duration. Then I can produce thousands of notes, each computed with  a mathematical algorithm.

My next question is how to save this data (frequency and duration, for each note) as a sound file? I need some utilities which will allow you to do that. Which software do you recommend?

I did some search, but could not find anything useful, except a Perl library Audio::Beep that seems very easy to use. I installed it but got the following error message Couldn't find a working player at line 4 when running the sample code below (saved as It is a problem with my laptop, not with the Perl library or my Cygwin environment, I guess.

#functional simple way
use Audio::Beep;

beep($freq, $milliseconds);

#OO more musical way
use Audio::Beep;

my $beeper = Audio::Beep->new();

# lilypond subset syntax accepted
# relative notation is the default 
# (now correctly implemented)
my $music = "g' f bes' c8 f d4 c8 f d4 bes c g f2";
# Pictures at an Exhibition by Modest Mussorgsky

$beeper->play( $music );

Interestingly, many of the answers I've found on the Internet are dealing with producing a Beep on your machine, to make you aware of when a long program running in the background has completed. There was even one offering a solution that produces one beep if completion was successful, two beeps otherwise. And another guy talking about issuing a beep remotely to surprise the girl working on the target machine - just like you can command a remote printer in a foreign country to print out some stuff.

Anyway, none of the many solutions offered - some as simple as Beep (412, 100) or echo -e '\a' issued from the command line - worked on my Windows/Cygwin laptop, despite the fact that I routinely watch videos with audible sound on it. Do a search for Cygwin ring bell if you are interested in this.

Finally, I am wondering if the reverse solution - getting a device that produces sounds, and can be programmed - might be easier than programming a laptop to produce sounds. Also, it would be nice to create data sound tracks that goes well with these silent videos produced with R (see also here). 

DSC Resources

Additional Reading

Follow us on Twitter: @DataScienceCtrl | @AnalyticBridge

Views: 6590

Reply to This

Replies to This Discussion

Besides frequency (for sounds it is on a logarithmic scale) and duration, you should add volume. When generating many notes with these three parameters in a short period of time (like one second), you can replicate sound textures, that is, the sounds of various instruments.  

To answer your question, you might want to look at editors for audio files, especially those that accepts text files as input. Or maybe google WAV file converter.

Have you considered doing that in two stages?
- generate a midi file (
- use any of the many tools available to play the midi file


Also, if you read  NASA turned electromagnetic signals from space into music and listen to the sound tracks, it looks very similar to the "sound" that you hear when you put a big shell next to your ear. My father used to say, when I was a kid, that you would hear the sounds of deep see - the abyss. Interestingly, people produced "sounds" from the abyss, captured 6 miles below sea surface, and it looks very similar to NASA space sounds, or the the shell sounds. Maybe what these people captured is the "sound of randomness", that is, the "sound of noise".


© 2021   TechTarget, Inc.   Powered by

Badges  |  Report an Issue  |  Privacy Policy  |  Terms of Service