Feeds:
Posts
Comments

Posts Tagged ‘Boulanger’

We combine the power of tfork and text2sf with the magic of unix to construct a sound file for an A major chord.

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

A Chord

In Audio Programming 1, we compiled the tfork.c program in chapter 1 of the Audio Programming Book, Richard Boulanger and Victor Lazzarini, editors. This program , together with text2sf, gave us the tools needed to algorithmically construct simple sounds, namely an exponentially decaying sine wave. We used these tools to fabricate a file that represents the sound of a tuning fork.

Simple as these two tools are, they gives us the means to construct more complicated sounds without any additional C programming (fun though that is!). We will use the magic of unix. To begin, a short shell script:


# Language: unix/sh
# File: sound.sh
# Example: sh sound.sh 440 a --- writes text representation of
# a 0.2 second 220 Herz sound to a file a.txt

./tfork $2.txt 0.2 $1 44100 0.2

Using sound.sh, we make four sounds, each 0.2 seconds in duration, with frequencies of 220, 275, 330, and 440 Hertz. These correspond to the notes A, C#, E, and A’ = A one octave higher. The frequency ratios are C#/A = 5/4, E/A = 3/2, and A’/A = 2. Thus we are using Pythagorean tuning, in which pitch ratios in the scale are rational numbers with small numerator and denominator.

Let us now execute the following commands:


% sh sound.sh 220 a
% sh sound.sh 275 c#
% sh sound.sh 330 e
% sh sounds.sh 440 a2

The result is the creation of text files a.txt, c#.txt, etc., which represent the given sounds. Next, we concatenate these files, putting a.txt first, c#.txt next, etc:


% cat a.txt c#.txt e.txt a2.txt >chord.txt

Then, we convert chord.txt into a .wav file:


% text2sf chord.txt chord.wav 44100 1 1.0

To conclude, we play the file:


play chord.wav

Here is the sound:

A MAJOR CHORD

Clearly there is more to do, among which are  (1) Clean up this sound: it needs to fade cleanly into silence; (2) Develop a mini language for transforming a sequence of pitch names into a sound file; (3) make more complex sounds.

HH

Read Full Post »

A 440

We will use the magic of unix to make a tuning fork

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

Things are a little slow at the shop these days – the aftermath of getting our big software development project done. With some time to spare, I’ve been reading The Audio Programming Book, edited by Richard Boulanger and Victor Lazzarini. As a substiture for note-taking as I work my way through this excellent text, I’ve to decided to blog about it.

The first task, from Chapter 1, around page 162, is to write a playable file for the sound of a tuning fork. The end result sounds like what you will hear if you click this link:

OUR FIRST SOUND

Wasn’t that nice? You did play it didn’t you? Here is how we did it – proceed only if you speak C and enjoy the power and elegance of Unix:

Step 1. Compile the program tfork.c using gcc tfork.c -o tfork. You will find it on the CD in chapters/01..., or here. Then execute this command:

./tfork tfork.txt 1.0 440.0 44100 0.2

The result is a 44,000 line text file, tfork.txt, whose first three lines are as follows:


0.00000000
0.06277625
0.12529051

It looks like this:

Tuning fork sound: A 440, exponential decay.

The file represents a sine wave at 440 Herz that decays exponentially to a small fraction of the starting amplitude after 1.0 seconds. The sample rate for the sound is 44100 Herz. The tfork command is used this way:

./tfork outfile duration frequency sample_rate decay_constant

Step 2. Compile the code in text2sf.c and install the binary somewhere in your search path. Execute the command

text2sf tfork.txt tfork.wav 44100 1 1.0

You now have a one-second CD-quality “recording” of a tuning fork! It is in the file tfork.wav You can play it using whatever means suits you and your computer best. On a mac, the command

play tfork.wav

works fine.

Isn’t it interesting that a sound can be represented so many different ways? As a list of numbers, that is, a text file. As an image. As the digital sound file that the computer can directly play. As vibrations in the air that tickle the hair cells in our inner ear. As a memory …

There must be some deep philosophical meaning in all this.

HH

PS. Related links: Barry Threw: Art and Technology

Read Full Post »