Posts Tagged ‘C program’

A better way of creating .wav files given note data

1 | 2 | 3 | 4 | 5 | 6 |Source Code

Today’s post in my little saga of learning audio programming features a program quad.c. It creates a .wav file given a file of quadruples of the form (frequency in Hertz, duration in seconds, decay factor, amplitude). There are advantages of this approach as compared to to the previous one, which was based on concatenating the text files produced by tfork.  First, only a single intermediate text file containing waveform sample data is created.  In the first version,  one file was created for each note, plus one for the concatenation of all the former.  The concatenated file can be quite large. It is a file of 44,100 numbers for each second of audio which represents the sampled waveform. With quad, the file size is of corse the same as that of the concatenated file. The waveform which is sampled, however, has continuously varying phase. With the concatenated files produced by tfork, the phase begins at zero at the start of each note.  More importantly, in the new version, the volume of the individual notes can be controlled by setting the amplitude in foo.quad. You will notice the increase in volume in note to note when you play the file foo.wav.

In the next post, quad will be incorporated in solfa2sf.

Example. Here is a three-line file of quadruples that represents the notes A E A’, where A’ is an octave above A = 220 Hertz:

File: foo.quad
220 1.0 0.5 0.2
330 1.0 0.5 0.5
440 1.0 0.5 1.0

The comments in quad.c give more details, but suffice to say here running the following plays the sound represented by foo.quad

./quad foo.quad foo.samp
text2sf foo.samp foo.wav 44100 1 .90
rm foo.samp
play foo.wav


Note the increase in volume from note to note.


Read Full Post »

We combine C and Python programming to transform a sequence of solfege syllables, e.g., do re mi re do, into a sound file.

1 | 2 | 3 | 4 | 5 | Source Code

In our last post we used tfork and a few unix commands to construct a sound file for an A major chord, A C# E A’. The idea was (1) to make text files representing the sounds of the individual notes using tfork, (2) glue the text files end-to-end using cat, (3) convert the resulting big text file into a .wav file using text2sf.

This works fine for short “melodies,” but it soon gets tedious — and out of hand. A better way is to write a short program that does all this for you, given a text string of solfa syllables like “do re mi re do”. This is what we do in the Python program solfa2sf. Below is the sound produced by

./solfa2sf -w foo 0.3 0.1 do re mi re do


SOURCE CODE for solfa2sf

In running solfa2sf, the arguments are as follows: (1) an option: -d for “dry run”, i.e., no output, -v for an output file with verbose messages, -w for wet, the opposite of dry: produces the output .wav file with few messages; (2) the file name; foo results in an output file foo.wav, (3) the note duration in seconds, (4) the decay time, (*) the solfa syllables.

If you use a small decay time, the sound is percussive, like a marimba, or even a drum. If you use a large one, it is more like an organ. Try delay times of 0.01, 0.1, and 1.0 to see what the effect is.

Python is very good with strings, lists, and dictionaries, which is what we need to parse and handle an input string like “do re mi re do’. C is the best tool for fast computation. So we use them together! As you can see from the source code, we call on the C program tfork using the Python command os.system. Like a carpenter, we use saw, chisel, hammer, etc. as needed for the task at hand.


Read Full Post »