| 1 | /* |
| 2 | * libacm - Interplay ACM audio decoder. |
| 3 | * |
| 4 | * Copyright (c) 2004-2010, Marko Kreen |
| 5 | * |
| 6 | * Permission to use, copy, modify, and/or distribute this software for any |
| 7 | * purpose with or without fee is hereby granted, provided that the above |
| 8 | * copyright notice and this permission notice appear in all copies. |
| 9 | * |
| 10 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
| 11 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
| 12 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR |
| 13 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 14 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
| 15 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
| 16 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 17 | */ |
| 18 | |
| 19 | #ifndef __LIBACM_H |
| 20 | #define __LIBACM_H |
| 21 | |
| 22 | #ifdef __cplusplus |
| 23 | extern "C" { |
| 24 | #endif |
| 25 | |
| 26 | #define LIBACM_VERSION "1.3" |
| 27 | |
| 28 | #define ACM_ID 0x032897 |
| 29 | #define ACM_WORD 2 |
| 30 | |
| 31 | #define ACM_OK 0 |
| 32 | #define ACM_ERR_OTHER -1 |
| 33 | #define ACM_ERR_OPEN -2 |
| 34 | #define ACM_ERR_NOT_ACM -3 |
| 35 | #define ACM_ERR_READ_ERR -4 |
| 36 | #define ACM_ERR_BADFMT -5 |
| 37 | #define ACM_ERR_CORRUPT -6 |
| 38 | #define ACM_ERR_UNEXPECTED_EOF -7 |
| 39 | #define ACM_ERR_NOT_SEEKABLE -8 |
| 40 | |
| 41 | struct ACMInfo { |
| 42 | unsigned channels; /* number of sound channels (1: mono, 2: stereo */ |
| 43 | unsigned rate; /* samplerate */ |
| 44 | unsigned acm_id; |
| 45 | unsigned acm_version; |
| 46 | unsigned acm_channels; /* channels from header (often wrong) */ |
| 47 | unsigned acm_level; |
| 48 | unsigned acm_cols; /* 1 << acm_level */ |
| 49 | unsigned acm_rows; |
| 50 | }; |
| 51 | |
| 52 | struct acm_io_callbacks { |
| 53 | /* |
| 54 | * Read up to "n" items with "size" bytes each into the buf at "ptr", |
| 55 | * kinda like fread(). |
| 56 | * "datasrc" is the "io_arg" that was passed to acm_open_decoder() |
| 57 | * |
| 58 | * return the number of items read (not numbytes, but numbytes/size) |
| 59 | * or 0 on EOF (end of file) |
| 60 | * or a negative value on error |
| 61 | */ |
| 62 | int (*read_func)(void *ptr, int size, int n, void *datasrc); |
| 63 | /* optional, must support seeking into start*/ |
| 64 | int (*seek_func)(void *datasrc, int offset, int whence); |
| 65 | /* optional, called on acm_close */ |
| 66 | int (*close_func)(void *datasrc); |
| 67 | /* returns size in bytes*/ |
| 68 | int (*get_length_func)(void *datasrc); |
| 69 | }; |
| 70 | |
| 71 | struct ACMStream { |
| 72 | struct ACMInfo info; |
| 73 | unsigned total_values; /* number of sound samples in the ACM file */ |
| 74 | |
| 75 | /* acm data stream */ |
| 76 | void *io_arg; |
| 77 | struct acm_io_callbacks io; |
| 78 | unsigned data_len; |
| 79 | |
| 80 | /* acm stream buffer */ |
| 81 | uint8_t *buf; |
| 82 | unsigned buf_max, buf_size, buf_pos, bit_avail; |
| 83 | unsigned bit_data; |
| 84 | unsigned buf_start_ofs; |
| 85 | |
| 86 | /* block lengths (in samples) */ |
| 87 | unsigned block_len; |
| 88 | unsigned wrapbuf_len; |
| 89 | /* buffers */ |
| 90 | int *block; |
| 91 | int *wrapbuf; |
| 92 | int *ampbuf; |
| 93 | int *midbuf; /* pointer into ampbuf */ |
| 94 | /* result */ |
| 95 | unsigned block_ready:1; |
| 96 | unsigned file_eof:1; |
| 97 | unsigned wavc_file:1; |
| 98 | unsigned stream_pos; /* in words. absolute */ |
| 99 | unsigned block_pos; /* in words, relative */ |
| 100 | }; |
| 101 | typedef struct ACMStream ACMStream; |
| 102 | |
| 103 | /* decode.c */ |
| 104 | |
| 105 | /* |
| 106 | * Open ACMStream from acm_io_callbacks. |
| 107 | * - res: if opening the decoder succeeds, the new ACMStream will be assigned to *res |
| 108 | * - io_arg: will be passed to the acm_io_callbacks as "datasrc" argument |
| 109 | * - io: your custom callbacks to read the data |
| 110 | * - force_chans >= 1: force the file to be decoded and played with that |
| 111 | * many channels, regardless of number of channels in the file header |
| 112 | * - force_chans == 0: assume the ACM file's header contains the |
| 113 | * correct amount of channels (this is not always the case, stereo |
| 114 | * files tagged as mono exist in some games!) |
| 115 | * - force_chans == -1: quirk mode: for plain ACM files stereo |
| 116 | * is assumed, for WAVC files the header's value is used |
| 117 | * |
| 118 | * returns ACM_OK if opening was successful, otherwise an ACM_ERR_* code |
| 119 | */ |
| 120 | int acm_open_decoder(ACMStream **res, void *io_arg, struct acm_io_callbacks io, int force_chans); |
| 121 | |
| 122 | /* |
| 123 | * Read up to "nbytes" bytes of audio samples from ACMStream "acm" into buffer "buf". |
| 124 | * "bigendianp", "wordlen" and "sgned" specify the format you want the returned samples |
| 125 | * to have, for example 0, 2, 1 for "little endian, 16bit (2byte), signed" (s16le). |
| 126 | * - bigendianp: 0 for samples in little endian byteorder, 1 for big endian |
| 127 | * - wordlen: only 2 for 16bit (2byte) samples is currently supported |
| 128 | * - sgned: 1 for signed samples, 0 for unsigned samples |
| 129 | * |
| 130 | * returns the amount of bytes have been successfully read into buf |
| 131 | * or 0 on EOF (nothing left to read in file) |
| 132 | * or a value < 0 (ACM_ERR_*) on error |
| 133 | */ |
| 134 | int acm_read(ACMStream *acm, void *buf, unsigned nbytes, |
| 135 | int bigendianp, int wordlen, int sgned); |
| 136 | void acm_close(ACMStream *acm); |
| 137 | |
| 138 | /* util.c */ |
| 139 | |
| 140 | /* |
| 141 | * Open ACMStream from file. |
| 142 | * - acm: if opening the decoder succeeds, the new ACMStream will be assigned to *acm |
| 143 | * - filename: filename (incl. path) to the ACM file to be opened |
| 144 | * - force_chans >= 1: force the file to be decoded and played with that |
| 145 | * many channels, regardless of number of channels in the file header |
| 146 | * - force_chans == 0: assume the ACM file's header contains the |
| 147 | * correct amount of channels (this is not always the case, stereo |
| 148 | * files tagged as mono exist in some games!) |
| 149 | * - force_chans == -1: quirk mode: for plain ACM files stereo |
| 150 | * is assumed, for WAVC files the header's value is used |
| 151 | * |
| 152 | * returns ACM_OK if opening was successful, otherwise an ACM_ERR_* code |
| 153 | */ |
| 154 | int acm_open_file(ACMStream **acm, const char *filename, int force_chans); |
| 155 | const struct ACMInfo *acm_info(ACMStream *acm); |
| 156 | int acm_seekable(ACMStream *acm); |
| 157 | unsigned acm_bitrate(ACMStream *acm); |
| 158 | unsigned acm_rate(ACMStream *acm); |
| 159 | unsigned acm_channels(ACMStream *acm); |
| 160 | unsigned acm_raw_total(ACMStream *acm); |
| 161 | unsigned acm_raw_tell(ACMStream *acm); |
| 162 | unsigned acm_pcm_total(ACMStream *acm); |
| 163 | unsigned acm_pcm_tell(ACMStream *acm); |
| 164 | unsigned acm_time_total(ACMStream *acm); |
| 165 | unsigned acm_time_tell(ACMStream *acm); |
| 166 | int acm_read_loop(ACMStream *acm, void *dst, unsigned len, |
| 167 | int bigendianp, int wordlen, int sgned); |
| 168 | int acm_seek_pcm(ACMStream *acm, unsigned pcm_pos); |
| 169 | int acm_seek_time(ACMStream *acm, unsigned pos_ms); |
| 170 | const char *acm_strerror(int err); |
| 171 | |
| 172 | #ifdef __cplusplus |
| 173 | } // extern "C" |
| 174 | #endif |
| 175 | |
| 176 | #endif |
| 177 |