Archive for the ‘Code’ Category

JC and I have added mtalk to our sf2a — solfege to audio — distribution. Once installed, you can do this:

% mtalk 'Please get me a quart of milk at \
   the store.  Thanks.' -p -t tempo:144

The option -p means “play the audio file immediately,” and -t temp:144 sets the temp. Here is what the result sounds like:

Please get me a quart of milk at the store. Thanks.

This little programming project (we have to get our fun somehow:-) was inspired by James Gleick’s book, The Information. See previous post


Read Full Post »

Many changes. (1) New name –sf2a (2) source code is at github. (3) At github click on download button if you wish to download. (4) Installation on mac: untar or unzip downoaded file, cd to the resulting folder, run sudo sh setup.sh -install YOUR_USER_NAME; (5) after install, run sf2a 'do re mi' A file out.wav should be created. This is the audio file. (6) There is a musical dictation program, dict that creates audio files and a web page for dictation exercises based on the data in a text file. Use the file dictation.txt in the install folder for an example. Just run dict -m in that folder, then open the web page index.html. (7) For a draft manual, see this web page.

All this works on a mac. Adapting it to Linux is easy. Just change the values of $INSTALL_DIR and $BIN_DIR in setup.sh. I don’t know enough about PC’s to advise on this — one ought to be able to modify the file setup.sh

Read Full Post »

s2sound now supports up to ten independent voices. Here is a two-voice example

voice:1 decay 2.0
h mi q re ti_ h do

h do_ q sol_ sol__ h do_

See github for source code, including the 10-channel mixer in mix.c

Read Full Post »

I’ve set up a git repository for sf2sound. There is a two-voice example there, and JC has written some commentary on it, as well as posted the audio files.

The superposition principle makes this all quite easy — we just add together the waveform files that sf2sound produces for the two voices. That file represents the combined voices, and we use text2sf to produce the audio file. For more voices, simply add more waveform files!

Our work so far is quite primitive, both musically and as a software product. But we are having a lot of fun, and learning a lot. Eventually we hope something polished and elegant will come out of this.


Read Full Post »


The above is a visual representation of the opening measures of Muzio Clementi’s sonatina, Op 26, No1 as rendered by sf2sound — a kind of command line synthesizer that takes a stream of solfa symbols as input. This is homework for an eventual ear-training program. In any case, here is the audio file:


One of the challenges has been in shaping the waveforms of the individual notes so that they fit together without making annoying pops. A simple exponential decay did not work, although that is good for making the sound more or less percussive. What I discovered is that (at a minimum), one has to shape the attack and release of the note. For the moment I have done this by shaping the wave form with a simple quadratic function.

I’ve been experimenting with various settings and algorithms in order to get a better or more interesting sound. The sound file you hear is slightly more complex than the other ones I have posted. In previous version the sound was either (1) an exponentially damped sine wave, or (2) the former with some kind of shaping of the amplitude profile as mentioned above. In the current version, higher harmonics are mixed with the fundamental tone. Here is the code snippet of quad2samp where the mixing occurs:

// Form the sine wave and add harmonics to it
samp = sin(W*phase);
samp += -0.4*sin(2*W*phase);
samp += +0.2*sin(3*W*phase);
samp += -0.1*sin(4*W*phase);

I’ve observed an odd but likely well-known phenomenon (or is it an illusion?). When the sound consists of a shaped sine wave, i.e. no (deliberate) harmonic mixture, I find it painful to listen to it, even at relatively low volumes. Painful in the most elementary sense of the word, not because the poor artistry of sf2sound! When I mix in higher harmonics in some degree, the (physical) pain diminishes. I suspect this because the acoustic energy in the first instance is concentrated near a single frequency, so a small number of hair cells in the inner ear are overstimulated. When the same energy is spread among the various harmonics, albeit in unequal proportions, it is also spread over more hair cells, so that individual cells are not overstimulated. Perhaps someone who really knows what is going on can comment.

I’ll close with one more image — a close-up shot that shows how the wave forms from two adjacent notes join smoothly. The jaggedness of the sound wave reflects the addition of higher harmonics to the sine wave representing the fundamental tone.


Sonatina: close-up

Read Full Post »

Improvements — more shapely waveforms produced by quad2samp eliminates those durn popping sounds — and much more! Source code for quad2samp and sf2sound.py

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

Many changes, including new names for the commands. Here is an example:

% sf2sound test '|| fundamental:261 f: tempo:120 | stacc: q do re mi fa | leg: p: h sol sol ||'

And here is the audio:


The dynamics f: and p:, for forte and piano, now work thanks to the improved back-end program quad. I have renamed it quad2samp. The command fundamental:261 sets the frequency in Hertz of do. For longer examples, you can take a file as input:

sf2sound -f theme.solfa

The file theme.solfa represents the first 15 measures of a sonatina in C by Muzio Clementi (Op. 36 No. 1). See the code pages. Here is the sound file:


previous version

Notice that those terrible popping sounds are gone. Eliminating them turned out to be a problem in waveform shaping, which is accomplished in quad2samp using some nice little quadratic functions. I will discuss these improvements and others in the next few posts, as well as give all the source code.

One of the major changes is a clearer idea of what the input language is and can do. I am dubbing the language SF — for solfa, of course. It has three basic entities: note symbols, rhythm symbols, and commands. The symbols can be accented. Thus do+ is an accented do which is interpreted as C#, while do^ is one of two ways to write C an octave above the given C, the other being do2. An important accent for a rhythm symbol is the dot. Just as in music, it increases the value by one-half. Thus q. is a dotted quarter note.

Commands may or may not take arguments. Thus we have tempo:144 but also allegro:. Some commands take more than one argument. An example is cresc:4:f, which means crescendo over four beats to forte. One can also say cresc:4, which means crescendo over four beats from the current level to whatever level results. The rapidity of the crescendo is a default constant which of course can be adjusted: crescendo-speed:1.2. The commands change the values of the “SF” machine that transforms a stream of tokens in the SF language into quadruples. So far this architecture seems to remarkably easy to extend and maintain.

This little project is more work than I bargained for, but it beats playing video games. I am having fun and learning a lot. The Audio Programming Book by Boualanger and Lazzarini is a fantastic resource.


Read Full Post »

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 »

Older Posts »