aiff: handle id3 tags
[sox.git] / src / avr.c
blobab403a1f8fb0dbb999b727161ac1f09fd4cda254
1 /* AVR file format handler for SoX
2 * Copyright (C) 1999 Jan Paul Schmidt <jps@fundament.org>
4 * This library is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU Lesser General Public License as published by
6 * the Free Software Foundation; either version 2.1 of the License, or (at
7 * your option) any later version.
9 * This library is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser
12 * General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public License
15 * along with this library; if not, write to the Free Software Foundation,
16 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 #include "sox_i.h"
21 #include <stdio.h>
22 #include <string.h>
24 #define AVR_MAGIC "2BIT"
26 /* Taken from the Audio File Formats FAQ */
28 typedef struct {
29 char magic [5]; /* 2BIT */
30 char name [8]; /* null-padded sample name */
31 unsigned short mono; /* 0 = mono, 0xffff = stereo */
32 unsigned short rez; /* 8 = 8 bit, 16 = 16 bit */
33 unsigned short sign; /* 0 = unsigned, 0xffff = signed */
34 unsigned short loop; /* 0 = no loop, 0xffff = looping sample */
35 unsigned short midi; /* 0xffff = no MIDI note assigned,
36 0xffXX = single key note assignment
37 0xLLHH = key split, low/hi note */
38 uint32_t rate; /* sample frequency in hertz */
39 uint32_t size; /* sample length in bytes or words (see rez) */
40 uint32_t lbeg; /* offset to start of loop in bytes or words.
41 set to zero if unused. */
42 uint32_t lend; /* offset to end of loop in bytes or words.
43 set to sample length if unused. */
44 unsigned short res1; /* Reserved, MIDI keyboard split */
45 unsigned short res2; /* Reserved, sample compression */
46 unsigned short res3; /* Reserved */
47 char ext[20]; /* Additional filename space, used
48 if (name[7] != 0) */
49 char user[64]; /* User defined. Typically ASCII message. */
50 } priv_t;
55 * Do anything required before you start reading samples.
56 * Read file header.
57 * Find out sampling rate,
58 * size and encoding of samples,
59 * mono/stereo/quad.
63 static int startread(sox_format_t * ft)
65 priv_t * avr = (priv_t *)ft->priv;
66 int rc;
68 lsx_reads(ft, avr->magic, (size_t)4);
70 if (strncmp (avr->magic, AVR_MAGIC, (size_t)4)) {
71 lsx_fail_errno(ft,SOX_EHDR,"AVR: unknown header");
72 return(SOX_EOF);
75 lsx_readbuf(ft, avr->name, sizeof(avr->name));
77 lsx_readw (ft, &(avr->mono));
78 if (avr->mono) {
79 ft->signal.channels = 2;
81 else {
82 ft->signal.channels = 1;
85 lsx_readw (ft, &(avr->rez));
86 if (avr->rez == 8) {
87 ft->encoding.bits_per_sample = 8;
89 else if (avr->rez == 16) {
90 ft->encoding.bits_per_sample = 16;
92 else {
93 lsx_fail_errno(ft,SOX_EFMT,"AVR: unsupported sample resolution");
94 return(SOX_EOF);
97 lsx_readw (ft, &(avr->sign));
98 if (avr->sign) {
99 ft->encoding.encoding = SOX_ENCODING_SIGN2;
101 else {
102 ft->encoding.encoding = SOX_ENCODING_UNSIGNED;
105 lsx_readw (ft, &(avr->loop));
107 lsx_readw (ft, &(avr->midi));
109 lsx_readdw (ft, &(avr->rate));
111 * No support for AVRs created by ST-Replay,
112 * Replay Proffesional and PRO-Series 12.
114 * Just masking the upper byte out.
116 ft->signal.rate = (avr->rate & 0x00ffffff);
118 lsx_readdw (ft, &(avr->size));
120 lsx_readdw (ft, &(avr->lbeg));
122 lsx_readdw (ft, &(avr->lend));
124 lsx_readw (ft, &(avr->res1));
126 lsx_readw (ft, &(avr->res2));
128 lsx_readw (ft, &(avr->res3));
130 lsx_readbuf(ft, avr->ext, sizeof(avr->ext));
132 lsx_readbuf(ft, avr->user, sizeof(avr->user));
134 rc = lsx_rawstartread (ft);
135 if (rc)
136 return rc;
138 return(SOX_SUCCESS);
141 static int startwrite(sox_format_t * ft)
143 priv_t * avr = (priv_t *)ft->priv;
144 int rc;
146 if (!ft->seekable) {
147 lsx_fail_errno(ft,SOX_EOF,"AVR: file is not seekable");
148 return(SOX_EOF);
151 rc = lsx_rawstartwrite (ft);
152 if (rc)
153 return rc;
155 /* magic */
156 lsx_writes(ft, AVR_MAGIC);
158 /* name */
159 lsx_writeb(ft, 0);
160 lsx_writeb(ft, 0);
161 lsx_writeb(ft, 0);
162 lsx_writeb(ft, 0);
163 lsx_writeb(ft, 0);
164 lsx_writeb(ft, 0);
165 lsx_writeb(ft, 0);
166 lsx_writeb(ft, 0);
168 /* mono */
169 if (ft->signal.channels == 1) {
170 lsx_writew (ft, 0);
172 else if (ft->signal.channels == 2) {
173 lsx_writew (ft, 0xffff);
175 else {
176 lsx_fail_errno(ft,SOX_EFMT,"AVR: number of channels not supported");
177 return(0);
180 /* rez */
181 if (ft->encoding.bits_per_sample == 8) {
182 lsx_writew (ft, 8);
184 else if (ft->encoding.bits_per_sample == 16) {
185 lsx_writew (ft, 16);
187 else {
188 lsx_fail_errno(ft,SOX_EFMT,"AVR: unsupported sample resolution");
189 return(SOX_EOF);
192 /* sign */
193 if (ft->encoding.encoding == SOX_ENCODING_SIGN2) {
194 lsx_writew (ft, 0xffff);
196 else if (ft->encoding.encoding == SOX_ENCODING_UNSIGNED) {
197 lsx_writew (ft, 0);
199 else {
200 lsx_fail_errno(ft,SOX_EFMT,"AVR: unsupported encoding");
201 return(SOX_EOF);
204 /* loop */
205 lsx_writew (ft, 0xffff);
207 /* midi */
208 lsx_writew (ft, 0xffff);
210 /* rate */
211 lsx_writedw(ft, (unsigned)(ft->signal.rate + .5));
213 /* size */
214 /* Don't know the size yet. */
215 lsx_writedw (ft, 0);
217 /* lbeg */
218 lsx_writedw (ft, 0);
220 /* lend */
221 /* Don't know the size yet, so we can't set lend, either. */
222 lsx_writedw (ft, 0);
224 /* res1 */
225 lsx_writew (ft, 0);
227 /* res2 */
228 lsx_writew (ft, 0);
230 /* res3 */
231 lsx_writew (ft, 0);
233 /* ext */
234 lsx_writebuf(ft, "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0", sizeof(avr->ext));
236 /* user */
237 lsx_writebuf(ft,
238 "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
239 "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
240 "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
241 "\0\0\0\0", sizeof (avr->user));
243 return(SOX_SUCCESS);
246 static size_t write_samples(sox_format_t * ft, const sox_sample_t *buf, size_t nsamp)
248 priv_t * avr = (priv_t *)ft->priv;
250 avr->size += nsamp;
252 return (lsx_rawwrite (ft, buf, nsamp));
255 static int stopwrite(sox_format_t * ft)
257 priv_t * avr = (priv_t *)ft->priv;
259 unsigned size = avr->size / ft->signal.channels;
261 /* Fix size */
262 lsx_seeki(ft, (off_t)26, SEEK_SET);
263 lsx_writedw (ft, size);
265 /* Fix lend */
266 lsx_seeki(ft, (off_t)34, SEEK_SET);
267 lsx_writedw (ft, size);
269 return(SOX_SUCCESS);
272 LSX_FORMAT_HANDLER(avr)
274 static char const * const names[] = { "avr", NULL };
275 static unsigned const write_encodings[] = {
276 SOX_ENCODING_SIGN2, 16, 8, 0,
277 SOX_ENCODING_UNSIGNED, 16, 8, 0,
279 static sox_format_handler_t handler = {SOX_LIB_VERSION_CODE,
280 "Audio Visual Research format; used on the Mac",
281 names, SOX_FILE_BIG_END | SOX_FILE_MONO | SOX_FILE_STEREO,
282 startread, lsx_rawread, NULL,
283 startwrite, write_samples, stopwrite,
284 NULL, write_encodings, NULL, sizeof(priv_t)
286 return &handler;