# MP4-SA: A Simple Example

## Introduction In this example, we show a SAOL instrument (a sine wave oscillator) and a SASL score that uses the instrument. We use sfront to translate the SAOL and SASL files into a C program to generate audio. We also use sfront to encode the ASCII SAOL and SASL files into a compact binary format suitable for storage and transmission. | ##
## The SASL Score The panel on the right shows the SASL score for this example. A SASL score is a list of commands (one per line) that script a sequence of audio events. Events happen at a particular score time, which is measured in beats; a global tempo value converts beats to time. The score begins with an instr command, which triggers that start of a new note. The first field indicates the start time of the note. The second field (`tone`) indicates the instrument to play; this name corresponds to code written in the SAOL file. The final number is the duration of the note. The final line is an end command, that indicates when to stop computing audio output. | `## sine.sasl [26 bytes]
    0.25 tone 4.0
    4.50 end
`
## The SAOL Program The panel on the right shows the SAOL program for this example. The program defines the instrument `tone` (a sine wave oscillator) that is used in the SASL score. A SAOL instrument begins with a preamble similar to a C function, as shown on the first line. Also note that comments in SAOL use the C++ syntax: all text on a line after // is ignored. After the preamble are declarations for the variables that hold the state of the oscillator. SAOL type definitions declare the time scale for computing with the defined variables. These variables, typed as `asig`, compute at the audio sampling rate (32 kHz by default). SAOL variables are 32-bit floating point numbers initialized to 0. Next is the code to compute the behavior of the instrument, that runs at the audio sampling rate. The code begins with an if statement, to initialize the state variables at the start of a note. The if statement is followed by two assignment statements, that implement an iterative algorithm to compute sine waves. Note that SAOL uses a C-like syntax for assignment, expression evaluation, and conditionals. Finally, the `output` command puts the current sine wave sample on the system audio output bus. | `## sine.saol [280 bytes]
    instr tone ()

    {

      // variable declaration

      asig x, y, init;

      // computing starts here

      if (init == 0)
        {
          init = 1;
          x = 0.5;
        }

      x = x - 0.196307*y;
      y = y + 0.196307*x;

      output(y);

    }
`
| ## Running the Example The right panel shows how to use the SAOL to C translator sfront to generate audio for this example. Sfront generates the C file sa.c. We compile and execute this file to create audio output, saved in the WAV file output.wav (click on the link to hear audio, if you are connected to the Internet, and if your browser supports audio playback). Sfront also encodes the ASCII SAOL and SASL files into a compact binary file suitable for storage and transmission. In this example, sfront creates the binary file is sine.mp4. Dividing the size of sine.mp4 file (131 bytes) into the size of the output.wav file (288044 bytes) yields a lossless compression ratio of 2198. This compression ratio is about 100 times better than a perceptual audio encoder (such as MPEG 2 Level 3) would provide! | ## Running sfront:
    sfront -orc sine.saol -sco sine.sasl\
    -bitout sine.mp4 -aout output.wav
## Creates the files:

##

##

    sa.c [30603 bytes]

    sine.mp4 [131 bytes]

    Compiling and executing sa.c:

    gcc -O2 sa.c -lm -o sa ; ./sa

    Creates the file:

    output.wav [288044 bytes, on the Web]

Copyright 1999 John Lazzaro and John Wawrzynek.