home *** CD-ROM | disk | FTP | other *** search
/ Amiga Developer CD 2.1 / Amiga Developer CD v2.1.iso / Extras / IFF / IFF_Forms / AIFF.doc < prev    next >
Encoding:
Text File  |  1993-03-01  |  42.2 KB  |  982 lines

  1. Audio 1-32 bit samples (Mac,AppleII,Synthia Pro)
  2.  
  3. provided by Steve Milne and Matt Deatherage, Apple Computer, Inc.
  4.  
  5. AIFF: Audio Interchange File Format File
  6. ----------------------------------------
  7.  
  8. The Audio Interchange File Format (Audio IFF) provides a standard for storing 
  9. sampled sounds.  The format is quite flexible, allowing the storage of 
  10. monaural or multichannel sampled sounds at a variety of sample rates and 
  11. sample widths.
  12.  
  13. Audio IFF conforms to the "`EA IFF 85' Standard for Interchange Format Files" 
  14. developed by Electronic Arts.
  15.  
  16. Audio IFF is primarily an interchange format, although application designers 
  17. should find it flexible enough to use as a data storage format as well.  If an 
  18. application does choose to use a different storage format, it should be able 
  19. to convert to and from the format defined in this document.  This ability to 
  20. convert will facilitate the sharing of sound data between applications.
  21.  
  22. Audio IFF is the result of several meetings held with music developers over a 
  23. period of ten months during 1987 and 1988.  Apple Computer greatly appreciates 
  24. the comments and cooperation provided by all developers who helped define this 
  25. standard.
  26.  
  27. Another "EA IFF 85" sound storage format is "`8SVX' IFF 8-bit Sampled Voice", 
  28. by Electronic Arts.  "8SVX," which handles eight-bit monaural samples, is 
  29. intended mainly for storing sound for playback on personal computers.  Audio 
  30. IFF is intended for use with a larger variety of computers, sampled sound 
  31. instruments, sound software applications, and high fidelity recording devices.
  32.  
  33. Data Types
  34.  
  35. A C-like language will be used to describe the data structures in this document
  36. The data types used are listed below.
  37.  
  38.   char:           8 bits signed.  A char can contain more than just ASCII
  39.                   characters.  It can contain any number from -128 to 127 
  40.                   (inclusive).
  41.   unsigned char:  8 bits signed. Contains any number from 0 to 255 (inclusive).
  42.   short:          16 bits signed.  Contains any number from -32,768 to 32,767
  43.                   (inclusive).
  44.   unsigned short: 16 bits unsigned.  Contains any number from 0 to 65,535 
  45.                   (inclusive).
  46.   long:           32 bits signed. Contains any number from -2,147,483,648
  47.                   to 2,147,483,647 (inclusive).
  48.   unsigned long:  32 bits unsigned. Contains any number from 0 to 
  49.                   4,294,967,295 (inclusive).
  50.   extended:       80 bit IEEE Standard 754 floating point number (Standard
  51.                   Apple Numeric Environment [SANE] data type Extended)
  52.   pstring:        Pascal-style string, a one-byte count followed by text 
  53.                   bytes.  The total number of bytes in this data type should
  54.                   be even.  A pad byte can be added to the end of the text to 
  55.                   accomplish this.  This pad byte is not reflected in the 
  56.                   count.
  57.   ID:             32 bits, the concatenation of four printable ASCII characters
  58.                   in the range " " (space, 0x20) through "~" (tilde, 0x7E).
  59.                   Leading spaces are not allowed in the ID but trailing spaces
  60.                   are OK.  Control characters are forbidden.
  61.  
  62.  
  63. Constants
  64.  
  65. Decimal values are referred to as a string of digits, for example 123, 0, 100 
  66. are all decimal numbers.  Hexadecimal values are preceded by a 0x - e.g., 0x0A,
  67. 0x1, 0x64.
  68.  
  69. Data Organization
  70.  
  71. All data is stored in Motorola 68000 format.  The bytes of multiple-byte 
  72. values are stored with the high-order bytes first.  Data is organized as 
  73. follows:
  74.  
  75.                 7  6  5  4  3  2  1  0
  76.           +-----------------------+
  77.     char: | msb              lsb  |
  78.           +-----------------------+
  79.  
  80.                15 14 13 12 11 10  9  8  7  6  5  4  3  2  1  0
  81.           +-----------------------+-----------------------+
  82.     char: | msb    byte 0         |        byte 1     lsb |
  83.           +-----------------------+-----------------------+
  84.  
  85.                15 14 13 12 11 10  9  8  7  6  5  4  3  2  1  0
  86.           +-----------------------+-----------------------+
  87.     char: | msb    byte 0         |        byte 1         |
  88.           +-----------------------+-----------------------+
  89.     char: |        byte 2         |        byte 3     lsb |
  90.           +-----------------------+-----------------------+
  91.  
  92.         Figure 1: IFF data storage formats
  93.  
  94. Referring to Audio IFF
  95.  
  96. The official name for this standard is Audio Interchange File Format.  If an 
  97. application program needs to present the name of this format to a user, such 
  98. as in a "Save As..." dialog box, the name can be abbreviated to Audio IFF.  
  99. Referring to Audio IFF files by a four-letter abbreviation (i.e., "AIFF") in 
  100. user-level documentation or program-generated messages should be avoided.
  101.  
  102.  
  103. File Structure
  104.  
  105. The "`EA IFF 85' Standard for Interchange Format Files" defines an overall 
  106. structure for storing data in files.  Audio IFF conforms to those portions 
  107. of "EA IFF 85" that are germane to Audio IFF.  For a more complete discussion 
  108. of "EA IFF 85", please refer to the document "`EAIFF 85', Standard for 
  109. Interchange Format Files."
  110.  
  111. An "EA IFF 85" file is made up of a number of chunks of data.  Chunks are the
  112. building blocks of "EA IFF 85" files.  A chunk consists of some header 
  113. information followed by data:
  114.  
  115.         +--------------------+
  116.         |       ckID         |\
  117.         +--------------------+ } header info
  118.             |      ckSize        |/
  119.         +--------------------+
  120.             |                    |
  121.             |                    |
  122.             |       data         |
  123.             |                    |
  124.             |                    |
  125.         +--------------------+
  126.  
  127.         Figure 2: IFF Chunk structure
  128.  
  129. A chunk can be represented using our C-like language in the following manner:
  130.  
  131.     typedef struct {
  132.     ID        ckID;        /* chunk ID        */ 
  133.     long        ckSize;        /* chunk Size        */
  134.  
  135.     char        ckData[];    /* data            */
  136.     } Chunk;
  137.  
  138. The ckID describes the format of the data portion of a chunk.  A program can 
  139. determine how to interpret the chunk data by examining ckID. 
  140.  
  141. The ckSize is the size of the data portion of the chunk, in bytes.  It does 
  142. not include the 8 bytes used by ckID and ckSize.
  143.  
  144. The ckData contains the data stored in the chunk.  The format of this data is
  145. determined by ckID.  If the data is an odd number of bytes in length, a zero pad
  146. byte must be added at the end.  The pad byte is not included in ckSize.
  147.  
  148. Note that an array with no size specification (e.g., char ckData[];) indicates a
  149. variable-sized array in our C-like language.  This differs from standard C.
  150.  
  151. An Audio IFF file is a collection of a number of different types of chunks.  
  152. There is a Common Chunk which contains important parameters describing the 
  153. sampled sound, such as its length and sample rate.  There is a Sound Data 
  154. Chunk which contains the actual audio samples.  There are several other 
  155. optional chunks which define markers, list instrument parameters, store 
  156. application-specific information, etc.  All of these chunks are described in 
  157. detail in later sections of this document.
  158.  
  159. The chunks in an Audio IFF file are grouped together in a container chunk.  
  160. "EA IFF 85" Standard for Interchange Format Files  defines a number of 
  161. container chunks, but the one used by Audio IFF is called a FORM.  A FORM has 
  162. the following format:
  163.  
  164.     typedef struct {
  165.     ID    ckID;
  166.     long    ckSize;
  167.  
  168.     ID    formType;
  169.     char    chunks[];
  170.     }
  171.  
  172. The ckID is always 'FORM'.  This indicates that this is a FORM chunk.
  173.  
  174. The ckSize contains the size of data portion of the 'FORM' chunk.  Note that 
  175. the data portion has been broken into two parts, formType and chunks[].
  176.  
  177. The formType field describes what's in the 'FORM' chunk.  For Audio IFF files, 
  178. formType is always 'AIFF'.  This indicates that the chunks within the FORM 
  179. pertain to sampled sound.  A FORM chunk of formType 'AIFF' is called a FORM 
  180. AIFF.
  181.  
  182. The chunks field are the chunks contained within the FORM.  These chunks are 
  183. called local chunks.  A FORM AIFF along with its local chunks make up an 
  184. Audio IFF file.
  185.  
  186. Here is an example of a simple Audio IFF file.  It consists of a file containing
  187. single FORM AIFF which contains two local chunks, a Common Chunk and a Sound
  188. Data Chunk.
  189.  
  190.                         __________________________
  191.                        | FORM AIFF Chunk          |
  192.                        |   ckID  = 'FORM'         |
  193.                        |   formType = 'AIFF'      |
  194.                        |    __________________    |
  195.                        |   | Common Chunk     |   |
  196.                        |   |   ckID = 'COMM'  |   |
  197.                        |   |__________________|   |
  198.                        |    __________________    |
  199.                        |   | Sound Data Chunk |   |
  200.                        |   |   ckID = 'SSND'  |   |
  201.                        |   |__________________|   |
  202.                        |__________________________|
  203.    
  204.                       Figure 3: Simple Audio IFF File
  205.  
  206. There are no restrictions on the ordering of local chunks within a FORM AIFF.
  207.  
  208. A more detailed example of an Audio IFF file can be found in Appendix A.  Please
  209. refer to this example as often as necessary while reading the remainder of this
  210. document.
  211.  
  212.  
  213. Storage of AIFF on Apple and Other Platforms
  214.  
  215. On a Macintosh, the FORM AIFF, is stored in the data fork of an Audio IFF file. 
  216. The Macintosh file type of an Audio IFF file is 'AIFF'.  This is the same as 
  217. the formType of the FORM AIFF.  Macintosh applications should not store any 
  218. information in Audio IFF file's resource fork, as this information may not be 
  219. preserved by all applications.  Applications can use the Application Specific 
  220. Chunk, defined later in this document, to store extra information specific to 
  221. their application.
  222.  
  223. Audio IFF files may be identified in other Apple file systems as well.  On a 
  224. Macintosh under MFS or HFS, the FORM AIFF is stored in the data fork of a file 
  225. with file type "AIFF."  This is the same as the formType of the FORM AIFF.
  226.  
  227. On an operating system such as MS-DOS or UNIX, where it is customary to use a 
  228. file name extension, it is recommended that Audio IFF file names use ".AIF" 
  229. for the extension.
  230.  
  231. On an Apple II, FORM AIFF is stored in a file with file type $D8 and auxiliary 
  232. type $0000.  Versions 1.2 and earlier of the Audio IFF standard used file type 
  233. $CB and auxiliary type $0000.  This is incorrect; the assignment listed in 
  234. this document is the correct assignment.  
  235.  
  236. On the Apple IIGS stereo data is stored with right data on even channels and 
  237. left data on odd channels.  Some portions of AIFF do not follow this 
  238. convention.  Even where it does follow the convention, AIFF usually uses 
  239. channel two for right data instead of channel zero as most Apple IIGS 
  240. standards do.  Be prepared to interpret data accordingly.
  241.  
  242.  
  243. Local Chunk Types
  244.  
  245. The formats of the different local chunk types found within a FORM AIFF are 
  246. described in the following sections, as are their ckIDs.
  247.  
  248. There are two types of chunks:  required and optional.  The Common Chunk is 
  249. required.  The Sound Data chunk is required if the sampled sound has a length 
  250. greater than zero.  All other chunks are optional.  All applications that use 
  251. FORM AIFF must be able to read the required chunks and can choose to 
  252. selectively ignore the optional chunks.  A program that copies a FORM AIFF 
  253. should copy all the chunks in the FORM AIFF, even those it chooses not to 
  254. interpret.
  255.  
  256.  
  257. The Common Chunk
  258.  
  259. The Common Chunk describes fundamental parameters of the sampled sound.
  260.  
  261.     #define    CommonID    'COMM'    /* ckID for Common Chunk */
  262.  
  263.     typedef struct {
  264.     ID        ckID;
  265.     long        ckSize;
  266.  
  267.     short        numChannels;
  268.     unsigned long    numSampleFrames;
  269.     short        sampleSize;
  270.     extended    sampleRate;
  271.     } CommonChunk;
  272.  
  273. The ckID is always 'COMM'.  The ckSize is the size of the data portion of the 
  274. chunk, in bytes.  It does not include the 8 bytes used by ckID and ckSize.  
  275. For the Common Chunk, ckSize is always 18.
  276.  
  277. The numChannels field contains the number of audio channels for the sound.  
  278. A value of 1 means monophonic sound, 2 means stereo, and 4 means four channel 
  279. sound, etc.  Any number of audio channels may be represented.  For
  280. multichannel sounds, single sample points from each channel are interleaved.  
  281. A set of interleaved sample points is called a sample frame.
  282.  
  283. The actual sound samples are stored in another chunk, the Sound Data Chunk,
  284. which will be described shortly. 
  285.  
  286. Single sample points from each channel are interleaved such that each 
  287. sample frame is a sample point from the same moment in time for each channel 
  288. available.
  289.  
  290. The numSampleFrames field contains the number of sample frames.  This is not 
  291. necessarily the same as the number of bytes nor the number of samplepoints in 
  292. the Sound Data Chunk.  The total number of sample points in the file is 
  293. numSampleFrames times numChannels.
  294.  
  295. The sampleSize is the number of bits in each sample point.  It can be any 
  296. number from 1 to 32.  The format of a sample point will be described in the
  297. next section.
  298.  
  299. The sampleRate field is the sample rate at which the sound is to be played 
  300. back in sample frames per second.
  301.  
  302. One, and only one, Common Chunk is required in every FORM AIFF.
  303.  
  304.  
  305. Sound Data Chunk
  306.  
  307. The Sound Data Chunk contains the actual sample frames.
  308.  
  309.     #define     SoundDataID    'SSND'    /* ckID for Sound Data Chunk     */
  310.  
  311.     typedef struct {
  312.     ID        ckID;
  313.     long        ckSize;
  314.  
  315.     unsigned long    offset;
  316.     unsigned long    blockSize;
  317.     unsigned char    SoundData [];
  318.     }  SoundDataChunk;
  319.  
  320. The ckID is always 'SSND'.  The ckSize is the size of the data portion of the 
  321. chunk, in bytes.  It does not include the 8 bytes used by ckID and ckSize.
  322.  
  323. The offset field determines where the first sample frame in the soundData 
  324. starts.  The offset is in bytes.  Most applications won't use offset and 
  325. should set it to zero.  Use for a non-zero offset is explained in the 
  326. Block-Aligning Sound Data section below.
  327.  
  328. The blockSize is used in conjunction with offset for block-aligning sound 
  329. data.  It contains the size in bytes of the blocks that sound data is aligned 
  330. to.  As with offset, most applications won't use blockSize and should set it 
  331. to zero.  More information on blockSize is in the Block-Aligning Sound Data 
  332. section below.
  333.  
  334. The soundData field contains the sample frames that make up the sound.  The 
  335. number of sample frames in the soundData is determined by the numSampleFrames 
  336. field in the Common Chunk.  Sample points and sample frames are explained in
  337. detail in the next section.
  338.  
  339. The Sound Data Chunk is required unless the numSampleFrames field in the 
  340. Common Chunk is zero.  A maximum of one Sound Data Chunk may appear in a FORM 
  341. AIFF.
  342.  
  343.  
  344. Sample Points and Sample Frames
  345.  
  346. A large part of interpreting Audio IFF files revolves around the two concepts 
  347. of sample points and sample frames.
  348.  
  349. A sample point is a value representing a sample of a sound at a given point in 
  350. time.  Each sample point is stored as a linear, 2's-complement value which may 
  351. be from 1 to 32 bits wide, as determined by sampleSize in the Common Chunk.  
  352.  
  353. Sample points are stored in an integral number of contiguous bytes.  One- to 
  354. eight-bit wide sample points are stored in one byte, 9- to 16-bit wide sample 
  355. points are stored in two bytes, 17- to 24-bit wide sample points are stored 
  356. in three bytes, and 25- to 32-bit wide sample points are stored in four bytes 
  357. (most significant byte first).  When the width of a sample point is not a 
  358. multiple of eight bits, the sample point data is left justified, with the 
  359. remaining bits zeroed.  An example case is illustrated in Figure 4.  A 12-bit 
  360. sample point, binary 101000010111, is stored left justified in two bytes.  
  361. The remaining bits are set to zero.
  362.  
  363.      ___ ___ ___ ___ ___ ___ ___ ___ ___ ___ ___ ___ ___ ___ ___ ___
  364.     |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |
  365.     | 1   0   1   0   0   0   0   1 | 0   1   1   1   0   0   0   0 |
  366.     |___|___|___|___|___|___|___|___|___|___|___|___|___|___|___|___|
  367.      <---------------------------------------------> <------------->
  368.           12 bit sample point is left justified         rightmost
  369.                                                         4 bits are
  370.                                                         zero padded
  371.                       Figure 4: 12-Bit Sample Point
  372.  
  373.  
  374. For multichannel sounds, single sample points from each channel are 
  375. interleaved.  A set of interleaved sample points is called a sample frame.
  376. Single sample points from each channel are interleaved such that each 
  377. sample frame is a sample point from the same moment in time for each channel 
  378. available.  This is illustrated in Figure 5 for the stereo (two channel) case.
  379.  
  380.                    sample      sample              sample
  381.                    frame 0     frame 1             frame N
  382.                  _____ _____ _____ _____         _____ _____
  383.                 | ch1 | ch2 | ch1 | ch2 | . . . | ch1 | ch2 |
  384.                 |_____|_____|_____|_____|       |_____|_____|
  385.                              _____
  386.                             |     | = one sample point
  387.                             |_____|
  388.  
  389.                 Figure 5: Sample Frames for Multichannel Sound
  390.  
  391. For monophonic sound, a sample frame is a single sample point.  For 
  392. multichannel sounds, you should follow the conventions in Figure 6.
  393.  
  394.                                        channel
  395.                1          2          3          4          5          6
  396.              _________ _________ _________ _________ _________ _________
  397.             | left    | right   |         |         |         |         |
  398.   stereo    |         |         |         |         |         |         |
  399.             |_________|_________|_________|_________|_________|_________|
  400.             | left    | right   | center  |         |         |         |
  401.   3 channel |         |         |         |         |         |         |
  402.             |_________|_________|_________|_________|_________|_________|
  403.             | front   | front   | rear    | rear    |         |         |
  404.   quad      | left    | right   | left    | right   |         |         |
  405.             |_________|_________|_________|_________|_________|_________|
  406.             | left    | center  | right   | surround|         |         |
  407.   4 channel |         |         |         |         |         |         |
  408.             |_________|_________|_________|_________|_________|_________|
  409.             | left    | left    | center  | right   | right   |surround |
  410.   6 channel |         | center  |         |         | center  |         |
  411.             |_________|_________|_________|_________|_________|_________|
  412.  
  413.              Figure 6: Sample Frame Conventions for Multichannel Sound
  414.  
  415. Sample frames are stored contiguously in order of increasing time.  The sample 
  416. points within a sample frame are packed together; there are no unused bytes 
  417. between them.  Likewise, the sample frames are packed together with no pad 
  418. bytes.
  419.  
  420.  
  421. Block-Aligning Sound Data
  422.  
  423. There may be some applications that, to ensure real time recording and 
  424. playback of audio, wish to align sampled sound data with fixed-size blocks.  
  425. This alignment can be accomplished with the offset and blockSize parameters of 
  426. the Sound Data Chunk, as shown in Figure 7.
  427.  
  428.         ____________ __________________________________ ____________
  429.        |\\ unused \\|          sample frames           |\\ unused \\|
  430.        |____________|__________________________________|____________|
  431.        <-- offset --><- numSampleFrames sample frames ->
  432.  
  433.     |   blockSize   |               |               |               |
  434.     |<- bytes     ->|               |               |               |
  435.     |_______________|_______________|_______________|_______________|
  436.        block N-1       block N         block N+1       block N+2
  437.  
  438.                      Figure 7: Block-Aligned Sound Data
  439.  
  440. In Figure 7, the first sample frame starts at the beginning of block N.  This 
  441. is accomplished by skipping the first offset bytes of the soundData.  Note 
  442. too, that the soundData bytes can extend beyond valid sample frames, allowing 
  443. the soundData bytes to end on a block boundary as well.
  444.  
  445. The blockSize specifies the size in bytes of the block to which you would 
  446. align the sound data.  A blockSize of zero indicates that the sound data does 
  447. not need to be block-aligned.  Applications that don't care about block 
  448. alignment should set the blockSize and offset to zero when creating Audio IFF 
  449. files.  Applications that write block-aligned sound data should set blockSize 
  450. to the appropriate block size.  Applications that modify an existing Audio IFF 
  451. file should try to preserve alignment of the sound data, although this is not 
  452. required.  If an application does not preserve alignment, it should set the 
  453. blockSize and offset to zero.  If an application needs to realign sound data 
  454. to a different sized block, it should update blockSize and offset accordingly.
  455.  
  456.  
  457. The Marker Chunk
  458.  
  459. The Marker Chunk contains markers that point to positions in the sound data.  
  460. Markers can be used for whatever purposes an application desires.  The 
  461. Instrument Chunk, defined later in this Note, uses markers to mark loop 
  462. beginning and end points.
  463.  
  464. Markers
  465.  
  466. A marker has the following format.
  467.  
  468.     typedef    short        MarkerId;
  469.  
  470.     typedef    struct    {
  471.         MarkerID    id;
  472.         unsigned long    position;
  473.         pstring        markerName;
  474.     } Marker;
  475.  
  476. The id is a number that uniquely identifies that marker within a FORM AIFF.  
  477. The id can be any positive non-zero integer, as long as no other marker 
  478. within the same FORM AIFF has the same id.
  479.  
  480. The marker's position in the sound data is determined by the position field.  
  481. Markers conceptually fall between two sample frames.  A marker that falls 
  482. before the first sample frame in the sound data is at position zero, while a 
  483. marker that falls between the first and second sample frame in the sound data 
  484. is at position 1.  Note that the units for position  are sample frames, not 
  485. bytes nor sample points.
  486.  
  487.                               Sample Frames
  488.              ___ ___ ___ ___ ___ ___ ___ ___ ___ ___ ___ ___ 
  489.             |   |   |   |   |   |   |   |   |   |   |   |   |
  490.             |___|___|___|___|___|___|___|___|___|___|___|___|
  491.             ^                   ^                           ^
  492.         position 0          position 5                  position 12
  493.  
  494.                  Figure 8: Sample Frame Marker Positions
  495.  
  496.  
  497. The markerName field is a Pascal-style text string containing the name of the 
  498. mark.
  499.  
  500. Note: Some "EA IFF 85" files store strings a C-strings (text bytes followed by
  501. a null terminating character) instead of Pascal-style strings.  Audio IFF uses 
  502. pstrings because they are more efficiently skipped over when scanning through 
  503. chunks.  Using pstrings, a program can skip over a string by adding the string 
  504. count to the address of the first character.  C strings require that each 
  505. character in the string be examined for the null terminator.  
  506.  
  507.  
  508. Marker Chunk Format
  509.  
  510. The format for the data within a Marker Chunk is shown below.
  511.  
  512.     #define    MarkerID     'MARK'    /* ckID for Marker Chunk */
  513.  
  514.     typedef  struct {
  515.     ID                ckID;
  516.     long                ckSize;
  517.  
  518.     unsigned short        numMarkers;
  519.     Marker            Markers [];
  520.     } MarkerChunk; 
  521.  
  522. The ckID is always 'MARK'.  The ckSize is the size of the data portion of the 
  523. chunk in bytes.  It does not include the 8 bytes used by ckID and ckSize.
  524.  
  525. The numMarkers field is the number of markers in the Marker Chunk.  If 
  526. numMarkers is non-zero, it is followed by the markers themselves.  Because 
  527. all fields in a marker are an even number of bytes, the length of any marker 
  528. will always be even.  Thus, markers are packed together with no unused bytes 
  529. between them.  The markers need not be ordered in any particular manner.
  530.  
  531. The Marker Chunk is optional.  No more than one Marker Chunk can appear in a
  532. FORM AIFF.
  533.  
  534.  
  535. The Instrument Chunk
  536.  
  537. The Instrument Chunk defines basic parameters that an instrument, such as a 
  538. sample, could use to play the sound data.
  539.  
  540. Looping
  541.  
  542. Sound data can be looped, allowing a portion of the sound to be repeated in
  543. order to lengthen the sound.  The structure below describes a loop.
  544.  
  545.     typedef struct {
  546.     short    PlayMode;
  547.     MarkerId beginLoop;
  548.     MarkerId endLoop;
  549.     } Loop;
  550.  
  551. A loop is marked with two points, a begin position and an end position.  There 
  552. are two ways to play a loop, forward looping and forward/backward looping.  
  553. In the case of forward looping, playback begins at the beginning of the sound, 
  554. continues past the begin position and continues to the end position, at which 
  555. point playback starts again at the begin position.  The segment between the 
  556. begin and end positions, called the loop segment, is played repeatedly until 
  557. interrupted by a user action, such as the release of a key on a sampling 
  558. instrument.
  559.  
  560.                    ___ ___ ___ ___ ___ ___ ___ ___ ___ ___ ___ ___ 
  561.     sample frames |   |   |   |<--- loop segment ---->|   |   |   |
  562.                   |___|___|___|___|___|___|___|___|___|___|___|___|
  563.                               ^                       ^
  564.                         begin position           end position
  565.  
  566.                         Figure 9: Sample Frame Looping
  567.  
  568. With forward/backward looping, the loop segment is first played from the begin 
  569. position to the end position, and then played backwards from the end position 
  570. to the begin position.  This flip-flop pattern is repeated over and over again 
  571. until interrupted.
  572.  
  573. The playMode specifies which type of looping is to be performed:
  574.  
  575.     #define NoLooping        0
  576.     #define ForwardLooping        1
  577.     #define ForwardBackwardLooping  2
  578.  
  579. If NoLooping is specified, then the loop points are ignored during playback.
  580.  
  581. The beginLoop is a marker id that marks the begin position of the loop segment.
  582.  
  583. The endLoop marks the end position of a loop.  The begin position must be 
  584. less than the end position.  If this is not the case, then the loop segment 
  585. has zero or negative length and no looping takes place.
  586.  
  587.  
  588. The Instrument Chunk Format
  589.  
  590. The format of the data within an Instrument Chunk is described below.
  591.  
  592.     #define        InstrumentID    'INST'    /*ckID for Instruments Chunk */
  593.  
  594.     typedef struct {
  595.     ID        ckID;
  596.     long        ckSize;
  597.     
  598.     char        baseNote;
  599.     char        detune;
  600.     char        lowNote;
  601.     char        highNote;
  602.     char        lowvelocity;
  603.     char        highvelocity;
  604.     short        gain;
  605.     Loop        sustainLoop;
  606.     Loop        releaseLoop;
  607.     } InstrumentChunk;
  608.  
  609. The ckID is always 'INST'.  ckSize is the size of the data portion of the 
  610. chunk, in bytes.  For the Instrument Chunk, ckSize is always 20.
  611.  
  612. The baseNote is the note at which the instrument plays back the sound data 
  613. without pitch modification.  Units are MIDI (MIDI is an acronym for Musical 
  614. Instrument Digital Interface) note numbers, and are in the range 0 through 
  615. 127.  Middle C is 60.
  616.  
  617. The detune field determines how much the instrument should alter the pitch of 
  618. the sound when it is played back.  Units are in cents (1/100 of a semitone) 
  619. and range from -50 to +50.  Negative numbers mean that the pitch of the sound 
  620. should be lowered, while positive numbers mean that it should be raised.
  621.  
  622. The lowNote and highNote fields specify the suggested range on a keyboard for 
  623. playback of the sound data.  The sound data should be played if the instrument 
  624. is requested to play a note between the low and high, inclusive.  The base 
  625. note does not have to be within this range.  Units for lowNote and highNote 
  626. are MIDI note values.
  627.  
  628. The lowVelocity and highVelocity fields specify the suggested range of 
  629. velocities for playback of the sound data.  The sound data should be played 
  630. if the note-on velocity is between low and high velocity, inclusive.  Units 
  631. are MIDI velocity values, 1 (lowest velocity) through 127 (highest velocity).
  632.  
  633. The gain is the amount by which to change the gain of the sound when it is 
  634. played.  Units are decibels.  For example, 0db means no change, 6db means 
  635. double the value of each sample point, while -6db means halve the value of 
  636. each sample point.
  637.  
  638. The sustainLoop field specifies a loop that is to be played when an instrument 
  639. is sustaining a sound.  
  640.  
  641. The releaseLoop field specifies a loop that is to be played when an instrument 
  642. is in the release phase of playing back a sound.  The release phase usually 
  643. occurs after a key on an instrument is released.
  644.  
  645. The Instrument Chunk is optional.  No more than one Instrument Chunk can 
  646. appear in a FORM AIFF.
  647.  
  648. ASIF Note:    The Apple IIGS Sampled Instrument Format also defines a 
  649.               chunk with ID of "INST," which is not the same as the Audio 
  650.               IFF Instrument Chunk.  A good way to tell the two chunks 
  651.               apart in generic IFF-style readers is by the ckSize fields.  
  652.               The Audio IFF Instrument Chunk's ckSize field is always 20, 
  653.               whereas the Apple IIGS Sampled Instrument Format Instrument 
  654.               Chunk's ckSize field, for structural reasons, can never be 
  655.               20.
  656.  
  657.  
  658. The MIDI Data Chunk
  659.  
  660. The MIDI Data Chunk can be used to store MIDI data.  Please refer to Musical 
  661. Instrument Digital Interface Specification 1.0, available from the 
  662. International MIDI Association, for more details on MIDI.
  663.  
  664. The primary purpose of this chunk is to store MIDI System Exclusive messages, 
  665. although other types of MIDI data can be stored in the block as well.  As more 
  666. instruments come to market, they will likely have parameters that have not 
  667. been included in the Audio IFF specification.  The MIDI System Exclusive 
  668. messages for these instruments may contain many parameters that are not 
  669. included in the Instrument Chunk.  For example, a new sampling instrument may 
  670. have more than the two loops defined in the Instrument Chunk.  These loops 
  671. will likely be represented in the MIDI System Exclusive message for the new 
  672. machine.  This MIDI System Exclusive message can be stored in the MIDI Data 
  673. Chunk.
  674.  
  675.     #define        MIDIDataID    'MIDI' /* ckID for MIDI Data Chunk */
  676.  
  677.     typedef struct {
  678.     ID        ckID;
  679.     long        ckSize;
  680.     
  681.     unsigned char    MIDIdata[];
  682.     } MIDIDataChunk;
  683.  
  684. The ckID is always 'MIDI'.  ckSize of the data portion of the chunk, in bytes. 
  685. It does not include the 8 bytes used by ckID and ckSize.
  686.  
  687. The MIDIData field contains a stream of MIDI data.
  688.  
  689. The MIDI Data Chunk is optional.  Any number of MIDI Data Chunks may exist in 
  690. a FORM AIFF.  If MIDI System Exclusive messages for several instruments are to 
  691. be stored in a FORM AIFF, it is better to use one MIDI Data Chunk per 
  692. instrument than one big MIDI Data Chunk for all of the instruments.
  693.  
  694.  
  695. The Audio Recording Chunk
  696.  
  697. The Audio Recording Chunk contains information pertinent to audio recording 
  698. devices.
  699.  
  700.     #define    AudioRecordingID 'AESD'        /* ckID for Audio Recording */
  701.                         /*  Chunk.            */
  702.     typedef struct {
  703.     ID            ckID
  704.     long            ckSize;
  705.  
  706.     unsigned char    AESChannelStatusData[24];
  707.     } AudioRecordingChunk;
  708.  
  709. The ckID is always 'AESD'. The ckSize is the size of the data portion of the 
  710. chunk, in bytes For the Audio Recording Chunk, ckSize is always 24.
  711.  
  712. The 24 bytes of AESCChannelStatusData are specified in the "AES Recommended
  713. Practice for Digital Audio Engineering - Serial Transmission Format for Linearly
  714. Represented Digital Audio Data", transmission of digital audio between audio 
  715. devices.  This information is duplicated in the Audio Recording Chunk for 
  716. convenience.  Of general interest would be bits 2, 3, and 4 of byte 0, which 
  717. describe recording emphasis.
  718.  
  719. The Audio Recording Chunk is optional.  No more than one Audio Recording Chunk 
  720. may appear in a FORM AIFF.
  721.  
  722.  
  723. The Application Specific Chunk
  724.  
  725. The Application Specific Chunk can be used for any purposes whatsoever by 
  726. developers and application authors.  For example, an application that edits 
  727. sounds might want to use this chunk to store editor state parameters such as 
  728. magnification levels, last cursor position, etc.
  729.  
  730.     #define    ApplicationSpecificID 'APPL' /* ckID for Application */
  731.                          /*  Specific Chunk.     */
  732.     typedef struct {
  733.     ID        ckID;
  734.     long        ckSize;
  735.  
  736.     OSType        applicationSignature;
  737.     char        data[];
  738.     } ApplicationSpecificChunk;
  739.  
  740. The ckID is always 'APPL'.  The ckSize is the size of the data portion of the 
  741. chunk, in bytes.  It does not include the 8 bytes used by ckID and ckSize.
  742.  
  743. The applicationSignature identifies a particular application.  For Macintosh 
  744. applications, this will be the application's four character signature.
  745.  
  746. The OSType field is used by applications which run on platforms from Apple 
  747. Computer, Inc.  For the Apple II, the OStype field should be set to 'pdos'.
  748. For the Macintosh, this field should be set to the four character signature 
  749. as registered with Apple Technical Support.
  750.  
  751. The data field is the data specific to the application.
  752.  
  753. The Application Specific Chunk is optional.  Any number of Application 
  754. Specific Chunks may exist in a single FORM AIFF.
  755.  
  756.  
  757. The Comments Chunk
  758.  
  759. The Comments Chunk is used to store comments in the FORM AIFF.  "EA IFF 85" 
  760. has an Annotation Chunk (used in ASIF) that can be used for comments, but the 
  761. Comments Chunk has two features not found in the "EA IFF 85" chunk.  They are 
  762. a time-stamp for the comment and a link to a marker.
  763.  
  764. Comment
  765.  
  766. A comment consists of a time stamp, marker id, and a text count followed by
  767. text.
  768.  
  769.     typedef struct {
  770.     unsigned long    timeStamp;
  771.     MarkerID    marker;
  772.     unsigned short    count;
  773.     char        text;
  774.     } Comment;
  775.  
  776. The timeStamp indicates when the comment was created.  On the Amiga, units 
  777. are the number of seconds since January 1, 1978.  On the Macintosh, units are 
  778. the number of seconds since January 1, 1904.  
  779.  
  780. A comment can be linked to a marker.  This allows applications to store long
  781. descriptions of markers as a comment.  If the comment is referring to a marker,
  782. then the marker field is the ID of that marker.  Otherwise, marker is zero, 
  783. indicating that this comment is not linked to a marker.
  784.  
  785. The count is the length of the text that makes up the comment.  This is a 16-bit
  786. quantity, allowing much longer comments than would be available with a pstring.
  787.  
  788. The text field contains the comment itself.  
  789.  
  790. The Comments Chunk is optional.  No more than one Comments Chunk may appear in 
  791. a single FORM AIFF.
  792.  
  793.  
  794. Comments Chunk Format
  795.  
  796.     #define     CommentID    'COMT'    /* ckID for Comments Chunk  */
  797.  
  798.     typedef struct {
  799.     ID        ckID;
  800.     long        ckSize;
  801.  
  802.     unsigned short  numComments;
  803.     Comment        comments[];
  804.     }CommentsChunk;
  805.  
  806. The ckID is always 'COMT'.  The ckSize is the size of the data portion of 
  807. the chunk, in bytes.  It does not include the 8 bytes used by ckID and ckSize.
  808.  
  809. The numComments field contains the number of comments in the Comments Chunk.
  810. This is followed by the comments themselves.  Comments are always even 
  811. numbers of bytes in length, so there is no padding between comments in
  812. the Comments Chunk.
  813.  
  814. The Comments Chunk is optional.  No more than one Comments Chunk may appear 
  815. in a single FORM AIFF.
  816.  
  817.  
  818. The Text Chunks, Name, Author, Copyright, Annotation
  819.  
  820. These four chunks are included in the definition of every "EA IFF 85" file.  
  821. All are text chunks; their data portion consists solely of text.  Each of 
  822. these chunks is optional.
  823.  
  824.     #define     NameID 'NAME'     /* ckID for Name Chunk */
  825.     #define     NameID 'AUTH'     /* ckID for Author Chunk */
  826.     #define     NameID '(c) '     /* ckID for Copyright Chunk */
  827.     #define     NameID 'ANNO'     /* ckID for Annotation Chunk */
  828.  
  829.     typedef struct {
  830.     ID    ckID;
  831.     long    ckSize;
  832.     char     text[];
  833.     }TextChunk;
  834.  
  835.  
  836. The ckID is either 'NAME', 'AUTH', '(c) ', or 'ANNO' depending on whether the
  837. chunk is a Name Chunk, Author Chunk, Copyright Chunk, or  Annotation Chunk,
  838. respectively.  For the Copyright Chunk, the 'c' is lowercase and there is a 
  839. space (0x20) after the close parenthesis.
  840.  
  841. The ckSize is the size of the data portion of the chunk, in this case the text.
  842.  
  843. The text field contains pure ASCII characters.  it is not a pstring or a C 
  844. string.  The number of characters in text is determined by ckSize.  The 
  845. contents of text depend on the chunk, as described below:
  846.  
  847. Name Chunk. The text contains the name of the sampled sound.  The Name Chunk 
  848. is optional.  No more than one Name Chunk may exist within a FORM AIFF.
  849.  
  850. Author Chunk. The text contains one or more author names.  An author in this 
  851. case is the creator of a sampled sound.  The Author Chunk is optional.  No 
  852. more than one Author Chunk may exist within a FORM AIFF.
  853.  
  854. Copyright Chunk.  The Copyright Chunk contains a copyright notice for the 
  855. sound.  The text field contains a date followed by the name of the copyright 
  856. owner.  The chunk ID '(c) ' serves as the copyright character.  For example, 
  857. a Copyright Chunk containing the text "1991 Commodore-Amiga, Inc." means 
  858. "(c) 1991 Commodore-Amiga, Inc."  The Copyright Chunk is optional.  No more 
  859. than one Copyright Chunk may exist within a FORM AIFF.
  860.  
  861. Annotation Chunk.  The text contains a comment.  Use of this chunk is 
  862. discouraged within a FORM AIFF.  The more powerful Comments Chunk should be 
  863. used instead.  The Annotation Chunk is optional.  Many Annotation Chunks may 
  864. exist within a FORM AIFF.
  865.  
  866. Chunk Precedence
  867.  
  868. Several of the local chunks for FORM AIFF may contain duplicate information.  
  869. For example, the Instrument Chunk defines loop points and MIDI System 
  870. Exclusive data in the MIDI Data Chunk may also define loop points.  What 
  871. happens if these loop points are different?  How is an application supposed to 
  872. loop the sound?  Such conflicts are resolved by defining a precedence for 
  873. chunks.  This precedence is illustrated in Figure 10.
  874.  
  875.                        Common Chunk           Highest Precedence
  876.                             |
  877.                      Sound Data Chunk
  878.                             |
  879.                        Marker Chunk
  880.                             |
  881.                      Instrument Chunk
  882.                             |
  883.                        Comment Chunk
  884.                             |
  885.                         Name Chunk
  886.                             |
  887.                        Author Chunk
  888.                             |
  889.                       Copyright Chunk
  890.                             |
  891.                       Annotation Chunk
  892.                             |
  893.                    Audio Recording Chunk
  894.                             |
  895.                       MIDI Data Chunk
  896.                             |
  897.                  Application Specific Chunk   Lowest Precedence
  898.  
  899.                          Figure 10: Chunk Precedence
  900.  
  901. The Common Chunk has the highest precedence, while the Application Specific 
  902. Chunk has the lowest.  Information in the Common Chunk always takes precedence 
  903. over conflicting information in any other chunk.  The Application Specific 
  904. Chunk always loses in conflicts with other chunks.  By looking at the chunk 
  905. hierarchy, for example, one sees that the loop points in the Instrument Chunk 
  906. take precedence over conflicting loop points found in the MIDI Data Chunk.
  907.  
  908. It is the responsibility of applications that write data into the lower 
  909. precedence chunks to make sure that the higher precedence chunks are updated 
  910. accordingly.
  911.  
  912. Figure 11 illustrates an example of a FORM AIFF.  An Audio IFF file is simple 
  913. a file containing a single FORM AIFF.  The FORM AIFF is stored in the data 
  914. fork of Macintosh file systems that can handle resource forks.
  915.  
  916.      _____________________________________________________________________
  917.     | FORM AIFF                                                           |
  918.     |                          _____________                              |
  919.     |                    ckID |_ 'FORM' ____|                             |
  920.     |                  ckSize |_ 176516 ____|                             |
  921.     |  _____________ formType |_ 'AIFF' ____| __________________________  |
  922.     | | Common           ckID |_ 'COMM' ____|                           | |
  923.     | | Chunk          ckSize |_ 18 ________|                           | |
  924.     | |           numChannels |_ 2 ___|_____                            | |
  925.     | |       numSampleFrames |_ 88200 _____|                           | |
  926.     | |            sampleSize |_ 16 __|_______________________________  | |
  927.     | |___________ sampleRate |_ 44100.00 ____________________________| | |
  928.     | | Marker           ckID |_ 'MARK' _____|                          | |
  929.     | | Chunk          ckSize |_ 34 _________|                          | |
  930.     | |            numMarkers |_ 2 ___|                                 | |
  931.     | |                    id |_ 1 ___|_______                          | |
  932.     | |              position |_ 44100 ___ ___|___ ___ ___ ___ ___ ___  | |
  933.     | |            markerName | 8 |'b'|'e'|'g'|' '|'l'|'o'|'o'|'p'| 0 | | |
  934.     | |                    id |_ 2 ___|_______                          | |
  935.     | |              position |_ 88200 _______|___ ___ ___ ___ ___ ___  | |
  936.     | |___________ markerName | 8 |'e'|'n'|'d'|' '|'l'|'o'|'o'|'p'| 0 | | |
  937.     | | Instrument       ckID |_ 'INST' ______|                         | |
  938.     | | Chunk          ckSize |_ 20 __________|                         | |
  939.     | |              baseNote | 60|                                     | |
  940.     | |                detune | -3|                                     | |
  941.     | |               lowNote | 57|                                     | |
  942.     | |              highNote | 63|                                     | |
  943.     | |           lowVelocity | 1 |                                     | |
  944.     | |          highVelocity |127|__                                   | |
  945.     | |                  gain |_ 6 __|                                  | |
  946.     | |  sustainLoop.playMode |_ 1 __|                                  | |
  947.     | | sustainLoop.beginLoop |_ 1 __|                                  | |
  948.     | |   sustainLoop.endLoop |_ 2 __|                                  | |
  949.     | |  releaseLoop.playMode |_ 0 __|                                  | |
  950.     | | releaseLoop.beginLoop |_ - __|                                  | |
  951.     | |__ releaseLoop.endLoop |_ - __|__________________________________| |
  952.     | | Sound            ckID |_ 'SSND' ______|                         | |
  953.     | | Data           ckSize |_ 176408 ______|                         | |
  954.     | | Chunk          offset |_ 0 ___________|                         | |
  955.     | |             blockSize |_ 0 ___________|        _______ _______  | |
  956.     | |             soundData |_ch 1 _|_ch 2 _| . . . |_ch 1 _|_ch 2 _| | |
  957.     | |                       first sample frame   88200th sample frame | |
  958.     | |_________________________________________________________________| |
  959.     |_____________________________________________________________________|
  960.  
  961.                          Figure 11: Sample FORM AIFF 
  962.  
  963.  
  964.  
  965. Further Reference
  966. _____________________________________________________________________________
  967.     o    "Inside Macintosh", Volume II, Apple Computer, Inc.
  968.     o    "Apple Numerics Manual", Second Edition, Apple Computer, Inc.
  969.     o    "File Type Note: File Type $D8, Auxiliary Type $0002, Apple IIGS 
  970.       Sampled Instrument Format", Apple Computer, Inc.
  971.     o    "Audio Interchange File Format v1.3",  APDA
  972.     o    "AES Recommended Practice for Digital Audio Engineering--Serial 
  973.          Transmission Format for Linearly Represented Digital Audio Data", 
  974.      Audio Engineering Society, 60 East 42nd Street, New York, NY 10165
  975.     o    "MIDI:  Musical Instrument Digital Interface, Specification 1.0", the 
  976.          International MIDI Association.
  977.     o    "`EA IFF 85' Standard for Interchange Format Files", Electronic Arts
  978.     o    "`8SVX' IFF 8-bit Sampled Voice", Electronic Arts
  979.  
  980.  
  981.  
  982.