Debian Codegraph @

snd/snd.htm

raw
1 <html>
2 <head> <title>Sndlib: Audio File, Format Conversion, and I/O Utilities</title> </head> <body> <h1>Sndlib: Audio File, Format Conversion, and I/O Utilities</h1> <h3>Roger B. Dannenberg</h3> <h2>History</h2> <ul> <li>18 June 97: Created.</li> <li>6 July 97: Revised </li> <li>7 May 00: Revised with multiple interface support and inner architecture documentation</li> </ul> <h2>Abstract</h2> <p>This document describes a set of portable C utilities for digital audio input and output to and from files and audio interfaces. The purpose is to read and write sound files in a variety of formats and to play and record audio. This code is intended for use in interactive and general purpose audio systems, and should be portable to virtually any computer system that supports C and has a file system. </p> <h2>Overview</h2> <p>There is basically one interesting data type: snd_type is a pointer to a descriptor for an audio stream, which is either being read from or written to a file or audio interface. The snd_type contains a structure that describes the sample format, sample rate, number of channels, etc. </p> <p>Routines exist to initialize sound transfer (<tt>snd_open()</tt>), perform transfers (<tt>snd_read()</tt>, <tt>snd_write()</tt>) and to finalize a transfer (<tt>snd_close()</tt>). Other routines allow you to transfer data to/from buffers and to convert formats. Sample rate conversion is not currently supported, but would be a welcome addition.</p> <pre>typedef struct { long channels; /* number of channels */ long mode; /* ADPCM, PCM, ULAW, ALAW, FLOAT, UPCM */ long bits; /* bits per sample */ double srate; /* sample rate */ } format_node; #define snd_string_max 258 /* the snd_type structure for applications to use: */ typedef struct snd_struct { short device; /* file, audio, or memory */ short write_flag; /* SND_READ, SND_WRITE, SND_OVERWRITE */ format_node format; /* sample format: channels, mode, bits, srate */ snd_fns_type dictionary; /* for internal use only */ union { struct { char filename[snd_string_max]; /* file name */ char filetype[snd_string_max]; /* file type if known */ int file; /* OS file number */ long header; /* None, AIFF, IRCAM, NEXT, WAVE */ long byte_offset; /* file offset of first sample */ long end_offset; /* byte_offset of last byte + 1 */ long current_offset; /* current (computed) file offset */ int swap; /* flag to swap bytes on input/output */ /* fields from AIFF sample files: */ int loop_info; /* Boolean: is this loop info valid? */ double native_hz; /* original pitch in hz */ float gain; /* gain: scale factor */ double low_hz; double high_hz; char low_velocity; char high_velocity; loop_node sustain_loop; loop_node release_loop; } file; struct { char interfacename[snd_string_max]; /* (optional) to specify interface */ char devicename[snd_string_max]; /* (optional) to specify device */ void *descriptor; long protocol; /* SND_REALTIME or SND_COMPUTEAHEAD */ double latency; /* app + os worst case latency (seconds) */ double granularity; /* expected period of app computation (s) */ /* note: pass 0.0 for default latency and granularity */ } audio; struct { long buffer_max; /* size of buffer memory */ char *buffer; /* memory buffer */ long buffer_len; /* length of data in buffer */ long buffer_pos; /* current location in buffer */ } mem; } u; } snd_node, *snd_type; </pre> <p>The meanings of fields are as follows: <ul> <li><tt>device</tt>: one of <tt>SND_DEVICE_FILE</tt> (data to/from file), <tt>SND_DEVICE_AUDIO</tt> (data to/from audio I/O device), <tt>SND_DEVICE_MEM</tt> (data to/from in-memory buffer), or <tt>SND_DEVICE_NONE</tt> (records that snd_open failed).</li> <li><tt>write_flag</tt>: one of <tt>SND_WRITE</tt> (create a file and write to it), <tt>SND_READ</tt> (read from a file), <tt>SND_OVERWRITE</tt> (overwrite some samples within a file, leaving the header and other samples untouched).</li> <li><tt>format</tt>: contains </li> <ul> <li><tt>channels</tt>: number of audio channels </li> <li><tt>mode</tt>: one of <tt>SND_MODE_ADPCM</tt> (adaptive delta modulation), <tt>SND_MODE_PCM</tt> (pulse code modulation, i.e. simple linear encoding), <tt>SND_MODE_ULAW</tt> (Mu-Law), <tt>SND_MODE_ALAW</tt> (A-Law), <tt>SND_MODE_FLOAT</tt> (float), or <tt>SND_MODE_UPCM</tt> (unsigned PCM).</li> <li><tt>bits</tt>: number of per sample.</li> <li><tt>srate</tt>: sample rate in samples per second (Hz). </li> </ul> </ul> <p>These are additional fields for when <tt>device</tt> is <tt>SND_DEVICE_FILE</tt>: <ul> <li><tt>filename</tt>: string name for file. </li> <li><tt>filetype</tt>: string name for file type (see &quot;Unknown File Types&quot; below)</li> <li><tt>file</tt>: the file number or handle returned by the operating system. (In some implementations, this may be a pointer to a file descriptor object cast into an integer.) </li> <li><tt>header</tt>: the type and format of header, one of <tt>SND_HEAD_NONE</tt> (no header), <tt>SND_HEAD_AIFF</tt>, <tt>SND_HEAD_IRCAM</tt>, <tt>SND_HEAD_NEXT</tt> (Sun and NeXT format), or <tt>SND_HEAD_WAVE</tt>. </li> <li><tt>byte_offset</tt>: the byte offset in the file. After opening the file, this is the offset of the first sample. This value is updated after each read or write. </li> <li><tt>end_offset</tt>: offset of the byte just beyond the last byte of the file. </li> </ul> <p>These are additional fields for when <tt>device</tt> is <tt>SND_DEVICE_AUDIO</tt>: <ul> <li><tt>devicename</tt>: string name for device (to select among multiple devices). This may be set to the empty string (devicename[0] = 0;) to indicate the default audio device, or it may be set to a name obtained from <tt>snd_devicename()</tt>. </li> <li><tt>descriptor</tt>: a field to store system-dependent data protocol: <tt>SND_REALTIME</tt> (use this if you are trying to compute ahead by a constant amount, especially for low-latency output) or <tt>SND_COMPUTEAHEAD</tt> (use this if you want to keep output buffers as full as possible, which will cause greater compute-ahead). </li> <li><tt>latency</tt>: (minimum) amount to be kept in buffer(s). This should be at least as great as the longest computational delay of the application PLUS the worst case latency for scheduling the application to run. </li> <li><tt>granularity</tt>: expected period of the periodic computation that generates samples. Also, granularity indicates the largest number of samples that will be written with snd_write or read with snd_read by the application.</li> </ul> <p>The following fields are for when <tt>device</tt> is SND_DEVICE_MEM (in-memory data): <ul> <li><tt>buffer_max</tt>: the size of the buffer memory (in bytes) </li> <li><tt>buffer</tt>: the memory buffer address</li> <li><tt>buffer_len</tt>: the length of data in the buffer </li> <li><tt>buffer_pos</tt>: the current location of the input/output in memory. </li> </ul> <h2>Routine descriptions</h2> <h3>Terminology</h3> <p>A <em>sample</em> is one amplitude measurement from one <em>channel. </em>Ordinarily, a sound file can contain multiple channels. All channels have the same sample rate and length, and samples are treated as occurring simultaneously across all channels. The collection of samples occurring simultaneously is called a <em>frame</em>. Samples are stored in frame order. Within a frame, samples are stored in order of increasing channel number.</p> <p>All routines described here measure data in units of frames (not samples, not bytes). One exception is <code>snd_seek()</code>, which measures file position in seconds of time, expressed as a double.</p> <tt> <p><strong>int snd_open(snd_type snd, long *flags);</tt> </strong></p> <blockquote> <p>To open a file, fill in fields of a <tt>snd_type</tt> and call <tt>snd_open()</tt>. If there is header information in the file or device characteristics for the audio interface, fields of <tt>snd</tt> are filled in. The flags parameter tells which fields were specified by the <tt>snd_open()</tt> process. E.g. if you open a raw file, there is no header info, so the format will be as specified in <tt>snd</tt>. On the other hand, if you open an AIFF file, the file will specify the sample rate, channels, bits, etc., so all these values will be written into <tt>snd</tt>, and bits will be set in <tt>flags</tt> to indicate the information was picked up from the file.</p> <p>Returns <tt>SND_SUCCESS</tt> iff successful. If not successful, attempts to open a file will place the (system dependent) return code from <tt>open()</tt> into the <tt>u.file.file</tt> field.</p> <p>Before calling <tt>snd_open()</tt>, all general fields and fields corresponding to the device (e.g. <tt>u.file</tt> for <tt>SND_DEVICE_FILE)</tt> should be set, with the following exceptions: <tt>u.file.header</tt> (for <tt>SND_WRITE</tt>), <tt>byte_offset</tt>, <tt>end_offset</tt>, <tt>descriptor</tt>. The field <tt>filetype</tt> is set but not read by <tt>snd_open().</tt></p> <p>NOTE: For <tt>SND_DEVICE_MEM</tt>, fill in the <tt>u.mem</tt> fields directly and call <tt>snd_open()</tt>, which merely sets the dictionary field with function pointers. The application is responsible for maintaining <tt>u.mem</tt>: <tt>u.mem.buffer_len</tt> is the write pointer (<tt>snd_write()</tt> data goes here), and <tt>u.mem.buffer_pos</tt> is the read pointer (<tt>snd_read()</tt> data comes from here).</p> <p>NOTE 2: for <tt>SND_DEVICE_MEM</tt>, you can set <tt>write_flag</tt> to <tt>SND_WRITE</tt>, write data into the buffer, then set <tt>write_flag</tt> to <tt>SND_READ</tt> and read the buffer. Use <tt>snd_reset()</tt> before reading the buffer again to read from the beginning of the buffer, or simply reset read and write pointers directly to read/write different parts of the buffer.</p> </blockquote> <p><strong><tt>int snd_close(snd_type snd);</tt> </strong></p> <blockquote> <p>Closes a file or audio device. There is no need to call <tt>snd_close</tt> for <tt>SND_DEVICE_MEMORY</tt>, but this is not an error.</p> <p>Returns <tt>SND_SUCCESS</tt> iff successful.</p> </blockquote> <p><strong><tt>int snd_seek(snd_type snd, double when);</tt> </strong></p> <blockquote> <p>After opening a file for reading or overwriting, you can seek ahead to a specific time point by calling <tt>snd_seek()</tt>. The when parameter is in seconds and indicates seconds of time relative to the beginning of the sound.</p> <p>Returns <tt>SND_SUCCESS</tt> iff successful.</p> </blockquote> <tt> <p><strong>int snd_reset(snd_type snd);</tt> </strong></p> <blockquote> <p>Resets non-file buffers. If <tt>snd</tt> has <tt>SND_DEVICE_AUDIO</tt>, then the sample buffers are flushed. This might be a good idea before reading samples after a long pause that would cause buffers to overflow and contain old data, or before writing samples if you want the samples to play immediately, overriding anything already in the buffers.</p> <p>If snd has <tt>SND_DEVICE_MEM</tt> and <tt>SND_READ</tt>, then the buffer read pointer (buffer_pos) is reset to zero. If <tt>SND_WRITE</tt> is set, then the buffer read pointer (buffer_pos) and write pointer (buffer_len) are reset to zero.</p> <p>If snd has <tt>SND_DEVICE_FILE</tt>, nothing happens.</p> <p>Returns <tt>SND_SUCCESS</tt> iff successful.</p> </blockquote> <p><strong><tt>long snd_read(snd_type snd, void *buffer, long length);</tt> </strong></p> <blockquote> <p>Read up to length frames into <tt>buffer</tt>. </p> <p>Returns the number of frames actually read.</p> </blockquote> <p><strong><tt>int snd_write(snd_type snd, void *buffer, long length);</tt> </strong></p> <blockquote> <p>Writes length frames from buffer to file or device. </p> <p>Returns number of frames actually written.</p> </blockquote> <p><strong><tt>long snd_convert(snd_type snd1, void *buffer1,<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; snd_type snd2, void *buffer2, long length);</tt></strong></p> <blockquote> <p>To read from a source and write to a sink, you may have to convert formats. This routine provides simple format conversions according to what is specified in <tt>snd1</tt> and <tt>snd2</tt>. The number of frames to convert is given by length, and the number of frames is returned.</p> </blockquote> <p><strong><tt>long snd_poll(snd_type snd);</tt> </strong></p> <blockquote> <p>The standard way to play files is to put something in the event loop that refills an output buffer managed by the device driver. This routine allows you to ask whether there is space to output more samples. If <tt>SND_REALTIME</tt> is selected, the number returned by <tt>snd_poll()</tt> will grow fairly smoothly at the sample rate, i.e. if the sample rate is 8KHz, then the result of <tt>snd_poll()</tt> will increase by 8 per millisecond. On the other hand, if <tt>SND_COMPUTEAHEAD</tt> is selected, then <tt>snd_poll()</tt> will return zero until a sample buffer becomes available, at which time the value returned will be the entire buffer size.</p> <p>In some implementations, with <tt>SND_REALTIME</tt>, <tt>snd_poll()</tt> can be used to furnish a time reference that is synchronized to the sample clock. In other words, the number of frames written plus the value returned by <tt>snd_poll()</tt> increases steadily in steps no larger than <tt>granularity</tt>.</p> <p>Note: some low-level functions are implemented for conversion from buffers of floats to various representations and from these representations back to floats. See <tt>snd.h</tt> for their declarations.</p> </blockquote> <p><strong><tt>int snd_flush(snd_type snd);</tt> </strong></p> <blockquote> <p>When the device is <tt>SND_DEVICE_AUDIO</tt>, writes are buffered. After the last write, call <tt>snd_flush()</tt> to transfer samples from the buffer to the output device. <tt>snd_flush()</tt> returns immediately, but it only returns <tt>SND_SUCCESS</tt> after the data has been output to the audio device. Since calling <tt>snd_close()</tt> will terminate output, the proper way to finish audio output is to call <tt>snd_flush()</tt> repeatedly until it returns <tt>SND_SUCCESS</tt>. Then call <tt>snd_close()</tt> to close the audio device and free buffers.</p> <p>If snd_flush is called on any open snd_type other than a <tt>SND_DEVICE_AUDIO</tt> opened for output, it returns <tt>SND_SUCCESS</tt>. Results are undefined if <tt>snd_flush()</tt> is called on a non-open <tt>snd_type</tt>.</p> </blockquote> <p><strong><tt>long snd_bytes_per_frame(snd_type snd);</tt> </strong></p> <blockquote> <p>Calculates the number of bytes in a frame (a frame has one sample per channel; sound files are stored as a sequence of frames).</p> </blockquote> <p><strong><tt>char *snd_mode_to_string(long mode);</tt> </strong></p> <blockquote> <p>Returns a string describing the mode (<tt>SND_MODE_PCM</tt>, etc.).</p> </blockquote> <p><strong><tt>int snd_device(int n, char *interf, char *device);</tt> </strong></p> <blockquote> <p>Sets strings describing the n-th audio device. <code>interf</code> is set to the interface name and <code>device</code> is set to the device name. Both should be allocated to be at least <code>snd_string_max</code> bytes in length. Returns <tt>NULL</tt> if n is greater or equal to the number of audio devices. Available devices are numbered, starting with the default device at n=0. Before opening an audio device, an application can use this to enumerate all possible devices, select one (e.g. by presenting a list to the user), and then copy the strings into the <tt>devicename</tt> and <code>interfacename</code> fields of the <tt>snd_type</tt> structure. If the <tt>devicename</tt> field is the empty string, device 0 will be opened.</p> </blockquote> <h2>Unknown File Types</h2> <p>Normally, the caller does not need to know anything about a file other than its name in order to open and read it. The library figures out the file encoding from header information. Only in the case of headerless files does the caller need to supply format parameters. If the file format is not recognized, <code>snd_open()</code> opens the file according to the supplied format parameters. If the file format is recognized but not supported, <code>snd_open()</code> will return <code>SND_FAILURE</code>, and the <code>flags</code> parameter will indicate what format information was detected. For example, if the number of <code>channels</code> is 4, <code>format.channels</code> is set to 4 and the <code>SND_HEAD_CHANNELS</code> bit is set in <code>flags</code>. If the name of the format is known, e.g. &quot;MP3,&quot; but there is no decoder for the format, then the <code>filetype</code> field is set to that string name, and the <code>SND_HEAD_FILETYPE</code> bit is set in <code>flags</code>.</p> <p>To detect and report an unknown file type, test the result of <code>snd_open()</code> for <code>SND_SUCCESS</code>. If not successful, test <code>flags</code> to see what information is valid. You may wish to report back to the user, especially if the <code>SND_HEAD_FILETYPE</code> bit is set.</p> <h2>Examples</h2> <p>See <tt>convert.c</tt> for examples of: <ul> <li>Printing information about a sound file </li> <li>Converting sound file formats </li> <li>Playing audio from a file </li> <li>Reading audio from audio input</li> </ul> <h2>Compiling the source code</h2> <p>To compile <tt>convert.c</tt> under Visual C++, add all the <tt>.c</tt> files to a console application project and add these libraries to the Object/library modules list under the Link tab in the Project Settings dialog box: <tt>winmm.lib wsock32.lib</tt>. If <tt>wsock32.lib</tt> does not work, try <tt>ws2_32.lib</tt>. The purpose of these libraries is to get functions that perform byte swapping in a system-independent way.</p> <h2>Inner architecture description</h2> <p>To modify or extend the Sndlib code, it is important to understand the architecture and design. The main issues are the structure used to obtain portability, and the support for multiple device interfaces within a given system.</p> <h3>Portability</h3> <p>The include file <tt>snd.h</tt> declares most of the library structures and routines. <tt>snd.h</tt> includes <tt>sndconfig.h</tt>, which handles system dependencies.</p> <p>System-dependent code is selected using conditional compilation. The following compile-time symbols are defined: <ul> <li><tt>WIN32</tt>: Some version of Windows (95, 98, or NT)</li> <li><tt>UNIX</tt>: Some version of Unix. One of the following should also be defined:<ul> <li><tt>LINUX</tt>: The Linux Operating System. </li> <li><tt>IRIX</tt>: The SGI Irix Operating System. </li> </ul> </li> <li><tt>MACINTOSH</tt>: Some version of MacOS.</li> </ul> <p><tt>sndconfig.h</tt> is responsible for defining a number of routines and/or macros, including the macro that selects the system. E.g. under Visual C++, the macro <tt>_WIN32</tt> is defined, so <tt>sndconfig.h</tt> defines <tt>WIN32</tt>. If <tt>_WIN32</tt> is defined. The other routines and macros to be defined are described in <tt>sndconfig.h</tt> itself. To avoid too many conditional compilation statements that make code hard to read, <tt>sndconfig.h</tt> works by conditionally including another <tt>.h</tt> file. The files are named <tt>sndwin32.h</tt>, <tt>sndlinux.h</tt>, <tt>sndirix.h</tt>, and <tt>sndmac.h</tt>, and other systems should implement include files in the same manner. </p> <p>In addition to the Unix, Windows, Macintosh distinction, this library also supports WX, a graphical user interface library. WX provides its own routines for file IO, and WX runs under Windows, Unix, and the Macintosh. This adds some confusion because WX functions cut across the Windows, Unix, Macintosh spectrum for things like file IO, but WX functions do not implement audio, so we still need to distinguish systems. To handle WX, a set of IO functions have been created, e.g. <tt>snd_open()</tt>, <tt>snd_read()</tt>, and <tt>snd_write()</tt>, and these are defined in <tt>sndwin32.h</tt> etc. <em>only</em> if <tt>WX</tt> is <em>not</em> defined. If <tt>WX</tt> is defined, then another include file <tt>sndwx.h</tt> is included into <tt>sndconfig.h</tt> to define the IO routines independently of what system is being compiled.</p> <h3>Multiple interface support</h3> <p>In the original designed, it was assumed that each operating system would provide one and only one audio interface, and this library would provide an abstract layer above that. It turned out that many operating systems offer multiple interfaces, e.g. Windows has both the multimedia interface and DirectSound, and Linux has several competing interfaces. Windows machines also have ASIO and other interface possibilities.</p> <p>To support multiple interfaces, the library has the call <tt>snd_devicename()</tt> which returns the name of the n<em>th</em> audio device. Where do these names come from? System-specific parts of the library call a non-system specific function as follows: </p> <pre> /* these types are for internal use: */ typedef int (*snd_reset_fn)(snd_type snd); typedef long (*snd_poll_fn)(snd_type snd); typedef long (*snd_read_fn)(snd_type snd, void *buffer, long length); typedef long (*snd_write_fn)(snd_type snd, void *buffer, long length); typedef int (*snd_open_fn)(snd_type snd, long *flags); typedef int (*snd_close_fn)(snd_type snd); typedef int (*snd_reset_fn)(snd_type snd); typedef int (*snd_flush_fn)(snd_type snd); typedef struct { snd_reset_fn reset; snd_poll_fn poll; snd_read_fn read; snd_write_fn write; snd_open_fn open; snd_close_fn close; snd_reset_fn reset; snd_flush_fn flush; } snd_fns_node, *snd_fns_type; void snd_add_device(char *devicename, snd_fns_type dictionary); </pre> <p>This is called for each different device or interface. In the general case, there might be several physical devices, each supporting several logical devices (front stereo, rear stereo, and quad), and there might be several different system APIs that access these (MM and DirectSound). The system-specific code provides a string name for each of these and a dictionary of function pointers for each.</p> <p>When does the system-specific code call <tt>snd_add_device()</tt>? When either <tt>snd_open()</tt> or <tt>snd_devicename()</tt> is called for the first time, a call is made to <tt>snd_init()</tt>, which is defined in system-specific code. <tt>snd_init()</tt> is responsible for calling detection and initialization code for each supported device. </p> <p>The <tt>snd_fns_node</tt> structure contains function pointers that implement the library functions. A pointer to this structure is found in the <tt>snd_type</tt> structure which is passed to nearly every library function. These library functions are implemented by making indirect calls through these function pointers.</p> <p><em>Note that most of these functions take byte-counts as length parameters rather than frames. This is because standard system calls such as read() and write() use bytes.</em></p> </body> </html> !sys/switches.h.template
3 /* switches.h template file
4 */
5
6 #undef HAS_STDLIB_H
7 #undef HAS_SYS_TYPES_H
8 #undef HAS_SYS_STAT_H
9 #undef HAS_STAT_H
10 #undef HAS_MALLOC_H
11
12 #undef HAS_GETTIMEOFDAY
13
14 #undef READLINE
15
16 #undef XL_BIG_ENDIAN
17 #undef XL_LITTLE_ENDIAN
18
19 #undef USE_RAND
20 #undef USE_RANDOM
21
22 /* define this to be printf, or define your own fn of the form
23 void nyquist_printf(char *format, ...);
24 (for a GUI)
25 */
26 #undef nyquist_printf
27
28 #undef NEED_ULONG
29 #undef NEED_USHORT
30 #undef NEED_BYTE
31
32 #undef NEED_DEFINE_MALLOC
33
34 #undef NEED_ROUND
35
36 /* explicitly choose a platform */
37 #undef UNIX
38 #undef WINDOWS
39 #undef MICROSOFT
40 #undef DOS
41 #undef MACINTOSH
42
43 #undef BUFFERED_SYNCHRONOUS_INPUT
44 #define SPACE_FOR_PLAY 10000
45 #define MAX_CHANNELS 16
46
47 /* this will enable code to read midi files, etc. */
48 #define CMTSTUFF
49
50 /* NYQUIST tells some CMT code that we're really in
51 * XLISP and NYQUIST
52 */
53 #define NYQUIST
54
55 #include "swlogic.h"