1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"
"http://www.w3.org/TR/REC-html40/loose.dtd">
<HTML>
<HEAD>
<TITLE>MP4-SA: A Simple Example</TITLE>
<META name="keywords" content="MP4-SA, example, SAOL, SASL,
MP4">
<META name="description" content="A simple example showing
SAOL and SASL files that generate a tone. Using sfront,
we turn these files into audio.">
</HEAD>
<BODY BGCOLOR="#FFFFFF" TEXT="#000000" LINK="0000EE" ALINK="FF6666"
VLINK="551A8B">
<TABLE BGCOLOR="#CCCCFF" WIDTH="100%" CLASS=navbar>
<TR>
<TD>
<FONT FACE="Verdana, Lucida Sans, Arial, Helvetica, Geneva,
sans-serif"><SMALL>
<A HREF="../../index.html">mp4-sa</A>-><STRONG>a simple example</STRONG>
</SMALL></FONT>
</TD></TR>
</TABLE>
<H1>MP4-SA: A Simple Example </H1>
<TABLE WIDTH="100%" CELLPADDING=12 CELLSPACING=0>
<TR>
<TD WIDTH="50%" VALIGN=top BGCOLOR="#CCFFCC">
<H2>Introduction</H2>
<P>
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.
</TD>
<TD WIDTH="50%" VALIGN=top BGCOLOR="#FFCCCC">
<H2><pre> </pre></H2>
</TD>
</TR>
<TR>
<TD WIDTH="50%" VALIGN=top BGCOLOR="#CCFFCC">
<H2>The SASL Score</H2>
<P>
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 <I>score time</I>, which
is measured in beats; a global tempo value converts beats to time.
<P>
The score begins with an <B>instr</B> command, which triggers that
start of a new note. The first field indicates the start time of the
note. The second field (<TT>tone</TT>) indicates the instrument to
play; this name corresponds to code written in the SAOL file. The
final number is the duration of the note.
<P>
The final line is an <B>end</B> command, that indicates when to stop
computing audio output.
<P>
</TD>
<TD WIDTH="50%" VALIGN=top BGCOLOR="#FFCCCC">
<TT>
<H2><A HREF="sine/sine.sasl">sine.sasl</A> [26 bytes]</H2>
<pre>
0.25 tone 4.0
4.50 end
</pre>
</TT>
</TD>
</TR>
<TR>
<TD WIDTH="50%" VALIGN=top BGCOLOR="#CCFFCC">
<H2>The SAOL Program</H2>
<P>
The panel on the right shows the SAOL program for this example. The
program defines the instrument <TT>tone</TT> (a sine wave oscillator)
that is used in the SASL score.
<P>
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.
<P>
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
<TT>asig</TT>, compute at the audio sampling rate (32 kHz by
default). SAOL variables are 32-bit floating point numbers initialized
to 0.
<P>
Next is the code to compute the behavior of the instrument, that runs
at the audio sampling rate. The code begins with an <B>if</B>
statement, to initialize the state variables at the start of a
note. The <B>if</B> 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.
<P>
Finally, the <TT>output</TT> command puts the current sine wave
sample on the system audio output bus.
</TD>
<TD WIDTH="50%" VALIGN=top BGCOLOR="#FFCCCC">
<TT>
<H2><A HREF="sine/sine.saol">sine.saol</A>
[280 bytes]</H2>
<pre>
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);
}
</pre>
</TT>
</TD>
</TR>
<TD WIDTH="50%" VALIGN=top BGCOLOR="#CCFFCC">
<H2>Running the Example</H2>
<P>
The right panel shows how to use the SAOL to C
translator <B>sfront</B> to generate audio
for this example.
<P>
Sfront generates the C file sa.c. We compile and execute this file to
create audio output, saved in the WAV file <A
HREF="http://www.cs.berkeley.edu/~lazzaro/sa/book/simple/sine/output.wav">output.wav</A>
(click on the link to hear audio, if you are connected to the
Internet, and if your browser supports audio playback).
<P>
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.
<P>
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!
</TD>
<TD WIDTH="50%" VALIGN=top BGCOLOR="#FFCCCC">
<H2>Running sfront:</H2>
<pre>
<TT>
sfront -orc sine.saol -sco sine.sasl\
-bitout sine.mp4 -aout output.wav
</TT>
</pre>
<H2>Creates the files:</H2>
<pre>
<TT>
<A HREF="sine/sa.c">sa.c</A> [30603 bytes]
sine.mp4 [131 bytes]
</TT>
<H2>Compiling and executing sa.c:</H2>
<pre>
<TT>
gcc -O2 sa.c -lm -o sa ; ./sa
</TT>
</pre>
<H2>Creates the file:</H2>
<pre>
<TT>
<A HREF="http://www.cs.berkeley.edu/~lazzaro/sa/book/simple/sine/output.wav">output.wav</A> [288044 bytes, on the Web]
</TT>
</pre>
</TD>
</TR>
</TABLE>
<TABLE BGCOLOR="#CCCCFF" WIDTH="100%" CLASS=navbar>
<TR>
<TD>
<FONT FACE="Verdana, Lucida Sans, Arial, Helvetica, Geneva,
sans-serif"><SMALL>
Return to <STRONG><A HREF="../../index.html">mp4-sa</A>.
</STRONG></SMALL></FONT>
</TD></TR>
</TABLE>
<P>
<A HREF="../../copyright/index.html">Copyright 1999 John Lazzaro and John
Wawrzynek.</A>
</BODY>
</HTML>
|