[BACK]Return to headers.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / aucat

Annotation of src/usr.bin/aucat/headers.c, Revision 1.18

1.18    ! ratchov     1: /*     $OpenBSD: headers.c,v 1.17 2010/06/05 16:52:28 ratchov Exp $    */
1.1       ratchov     2: /*
                      3:  * Copyright (c) 2008 Alexandre Ratchov <alex@caoua.org>
                      4:  *
                      5:  * Permission to use, copy, modify, and distribute this software for any
                      6:  * purpose with or without fee is hereby granted, provided that the above
                      7:  * copyright notice and this permission notice appear in all copies.
                      8:  *
                      9:  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
                     10:  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
                     11:  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
                     12:  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
                     13:  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
                     14:  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
                     15:  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
                     16:  */
                     17:
                     18: #include <sys/param.h>
                     19:
                     20: #include <err.h>
                     21: #include <stdio.h>
1.16      ratchov    22: #include <stdint.h>
1.1       ratchov    23: #include <stdlib.h>
                     24: #include <string.h>
                     25: #include <unistd.h>
                     26:
1.7       ratchov    27: #include "aparams.h"
1.1       ratchov    28: #include "conf.h"
1.6       ratchov    29: #include "wav.h"
                     30:
                     31: /*
1.7       ratchov    32:  * Encoding IDs used in .wav headers.
1.6       ratchov    33:  */
                     34: #define WAV_ENC_PCM    1
                     35: #define WAV_ENC_ALAW   6
                     36: #define WAV_ENC_ULAW   7
1.15      ratchov    37: #define WAV_ENC_EXT    0xfffe
1.1       ratchov    38:
                     39: struct wavriff {
                     40:        char magic[4];
                     41:        uint32_t size;
                     42:        char type[4];
                     43: } __packed;
                     44:
                     45: struct wavchunk {
                     46:        char id[4];
                     47:        uint32_t size;
                     48: } __packed;
                     49:
                     50: struct wavfmt {
                     51:        uint16_t fmt;
                     52:        uint16_t nch;
                     53:        uint32_t rate;
                     54:        uint32_t byterate;
                     55:        uint16_t blkalign;
1.4       ratchov    56:        uint16_t bits;
1.15      ratchov    57: #define WAV_FMT_SIZE            16
                     58: #define WAV_FMT_SIZE2          (16 + 2)
                     59: #define WAV_FMT_EXT_SIZE       (16 + 24)
                     60:        uint16_t extsize;
                     61:        uint16_t valbits;
                     62:        uint32_t chanmask;
                     63:        uint16_t extfmt;
                     64:        char     guid[14];
1.1       ratchov    65: } __packed;
                     66:
                     67: char wav_id_riff[4] = { 'R', 'I', 'F', 'F' };
                     68: char wav_id_wave[4] = { 'W', 'A', 'V', 'E' };
                     69: char wav_id_data[4] = { 'd', 'a', 't', 'a' };
                     70: char wav_id_fmt[4] = { 'f', 'm', 't', ' ' };
1.15      ratchov    71: char wav_guid[14] = {
                     72:        0x00, 0x00, 0x00, 0x00,
                     73:        0x10, 0x00, 0x80, 0x00,
                     74:        0x00, 0xAA, 0x00, 0x38,
                     75:        0x9B, 0x71
                     76: };
1.1       ratchov    77:
                     78: int
1.6       ratchov    79: wav_readfmt(int fd, unsigned csize, struct aparams *par, short **map)
1.1       ratchov    80: {
                     81:        struct wavfmt fmt;
1.15      ratchov    82:        unsigned nch, cmax, rate, bits, bps, enc;
1.1       ratchov    83:
1.15      ratchov    84:        if (csize < WAV_FMT_SIZE) {
                     85:                warnx("%u: bugus format chunk size", csize);
1.1       ratchov    86:                return 0;
                     87:        }
1.15      ratchov    88:        if (csize > WAV_FMT_EXT_SIZE)
                     89:                csize = WAV_FMT_EXT_SIZE;
                     90:        if (read(fd, &fmt, csize) != csize) {
1.1       ratchov    91:                warn("riff_read: chunk");
                     92:                return 0;
                     93:        }
                     94:        enc = letoh16(fmt.fmt);
1.15      ratchov    95:        bits = letoh16(fmt.bits);
                     96:        if (enc == WAV_ENC_EXT) {
                     97:                if (csize != WAV_FMT_EXT_SIZE) {
                     98:                        warnx("missing extended format chunk in .wav file");
                     99:                        return 0;
                    100:                }
                    101:                if (memcmp(fmt.guid, wav_guid, sizeof(wav_guid)) != 0) {
                    102:                        warnx("unknown format (GUID) in .wav file");
                    103:                        return 0;
                    104:                }
                    105:                bps = (bits + 7) / 8;
                    106:                bits = letoh16(fmt.valbits);
                    107:                enc = letoh16(fmt.extfmt);
                    108:        } else
                    109:                bps = (bits + 7) / 8;
1.6       ratchov   110:        switch (enc) {
                    111:        case WAV_ENC_PCM:
                    112:                *map = NULL;
                    113:                break;
                    114:        case WAV_ENC_ALAW:
                    115:                *map = wav_alawmap;
                    116:                break;
                    117:        case WAV_ENC_ULAW:
                    118:                *map = wav_ulawmap;
                    119:                break;
                    120:        default:
                    121:                errx(1, "%u: unsupported encoding in .wav file", enc);
1.1       ratchov   122:        }
                    123:        nch = letoh16(fmt.nch);
                    124:        if (nch == 0) {
                    125:                warnx("zero number of channels");
                    126:                return 0;
                    127:        }
                    128:        cmax = par->cmin + nch - 1;
1.2       ratchov   129:        if (cmax >= NCHAN_MAX) {
1.1       ratchov   130:                warnx("%u:%u: bad range", par->cmin, cmax);
                    131:                return 0;
                    132:        }
                    133:        rate = letoh32(fmt.rate);
1.9       ratchov   134:        if (rate < RATE_MIN || rate > RATE_MAX) {
1.1       ratchov   135:                warnx("%u: bad sample rate", rate);
                    136:                return 0;
                    137:        }
1.9       ratchov   138:        if (bits == 0 || bits > 32) {
1.1       ratchov   139:                warnx("%u: bad number of bits", bits);
                    140:                return 0;
                    141:        }
1.15      ratchov   142:        if (bits > bps * 8) {
                    143:                warnx("%u: bits larger than bytes-per-sample", bps);
                    144:                return 0;
                    145:        }
1.6       ratchov   146:        if (enc == WAV_ENC_PCM) {
1.15      ratchov   147:                par->bps = bps;
1.6       ratchov   148:                par->bits = bits;
                    149:                par->le = 1;
                    150:                par->sig = (bits <= 8) ? 0 : 1; /* ask microsoft why... */
                    151:        } else {
                    152:                if (bits != 8) {
                    153:                        warnx("%u: mulaw/alaw encoding not 8-bit", bits);
                    154:                        return 0;
                    155:                }
                    156:                par->bits = 8 * sizeof(short);
                    157:                par->bps = sizeof(short);
                    158:                par->le = NATIVE_LE;
                    159:                par->sig = 1;
                    160:        }
1.1       ratchov   161:        par->msb = 1;
                    162:        par->cmax = cmax;
                    163:        par->rate = rate;
                    164:        return 1;
                    165: }
                    166:
                    167: int
1.13      ratchov   168: wav_readhdr(int fd, struct aparams *par, off_t *startpos, off_t *datasz, short **map)
1.1       ratchov   169: {
                    170:        struct wavriff riff;
                    171:        struct wavchunk chunk;
                    172:        unsigned csize, rsize, pos = 0;
                    173:        int fmt_done = 0;
                    174:
                    175:        if (lseek(fd, 0, SEEK_SET) < 0) {
                    176:                warn("lseek: 0");
                    177:                return 0;
                    178:        }
                    179:        if (read(fd, &riff, sizeof(riff)) != sizeof(riff)) {
1.8       ratchov   180:                warn("wav_readhdr: header");
1.1       ratchov   181:                return 0;
                    182:        }
                    183:        if (memcmp(&riff.magic, &wav_id_riff, 4) != 0 ||
                    184:            memcmp(&riff.type, &wav_id_wave, 4)) {
                    185:                warnx("not a wave file");
                    186:                return 0;
                    187:        }
                    188:        rsize = letoh32(riff.size);
1.17      ratchov   189:        for (;;) {
                    190:                if (pos + sizeof(struct wavchunk) > rsize) {
                    191:                        warnx("missing data chunk");
                    192:                        return 0;
                    193:                }
1.1       ratchov   194:                if (read(fd, &chunk, sizeof(chunk)) != sizeof(chunk)) {
1.8       ratchov   195:                        warn("wav_readhdr: chunk");
1.1       ratchov   196:                        return 0;
                    197:                }
                    198:                csize = letoh32(chunk.size);
                    199:                if (memcmp(chunk.id, wav_id_fmt, 4) == 0) {
1.6       ratchov   200:                        if (!wav_readfmt(fd, csize, par, map))
1.1       ratchov   201:                                return 0;
                    202:                        fmt_done = 1;
                    203:                } else if (memcmp(chunk.id, wav_id_data, 4) == 0) {
1.18    ! ratchov   204:                        *startpos = pos + sizeof(riff) + sizeof(chunk);
1.1       ratchov   205:                        *datasz = csize;
                    206:                        break;
                    207:                } else {
1.10      ratchov   208: #ifdef DEBUG
1.14      ratchov   209:                        if (debug_level >= 2)
1.10      ratchov   210:                                warnx("ignoring chuck <%.4s>\n", chunk.id);
                    211: #endif
1.1       ratchov   212:                }
                    213:
                    214:                /*
                    215:                 * next chunk
                    216:                 */
                    217:                pos += sizeof(struct wavchunk) + csize;
                    218:                if (lseek(fd, sizeof(riff) + pos, SEEK_SET) < 0) {
                    219:                        warn("lseek");
                    220:                        return 0;
                    221:                }
                    222:        }
                    223:        if (!fmt_done) {
                    224:                warnx("missing format chunk");
                    225:                return 0;
                    226:        }
                    227:        return 1;
                    228: }
                    229:
1.13      ratchov   230: /*
                    231:  * Write header and seek to start position
                    232:  */
1.1       ratchov   233: int
1.13      ratchov   234: wav_writehdr(int fd, struct aparams *par, off_t *startpos, off_t datasz)
1.1       ratchov   235: {
                    236:        unsigned nch = par->cmax - par->cmin + 1;
                    237:        struct {
                    238:                struct wavriff riff;
                    239:                struct wavchunk fmt_hdr;
                    240:                struct wavfmt fmt;
                    241:                struct wavchunk data_hdr;
                    242:        } hdr;
                    243:
                    244:        /*
1.7       ratchov   245:         * Check that encoding is supported by .wav file format.
1.1       ratchov   246:         */
                    247:        if (par->bits > 8 && !par->le) {
                    248:                warnx("samples must be little endian");
                    249:                return 0;
                    250:        }
                    251:        if (8 * par->bps - par->bits >= 8) {
                    252:                warnx("padding must be less than 8 bits");
                    253:                return 0;
                    254:        }
                    255:        if ((par->bits <= 8 && par->sig) || (par->bits > 8 && !par->sig)) {
1.7       ratchov   256:                warnx("samples with more (less) than 8 bits must be signed "
                    257:                    "(unsigned)");
1.1       ratchov   258:                return 0;
                    259:        }
                    260:        if (8 * par->bps != par->bits && !par->msb) {
                    261:                warnx("samples must be MSB justified");
                    262:                return 0;
                    263:        }
                    264:
                    265:        memcpy(hdr.riff.magic, wav_id_riff, 4);
                    266:        memcpy(hdr.riff.type, wav_id_wave, 4);
                    267:        hdr.riff.size = htole32(datasz + sizeof(hdr) - sizeof(hdr.riff));
                    268:
                    269:        memcpy(hdr.fmt_hdr.id, wav_id_fmt, 4);
                    270:        hdr.fmt_hdr.size = htole32(sizeof(hdr.fmt));
                    271:        hdr.fmt.fmt = htole16(1);
                    272:        hdr.fmt.nch = htole16(nch);
                    273:        hdr.fmt.rate = htole32(par->rate);
                    274:        hdr.fmt.byterate = htole32(par->rate * par->bps * nch);
                    275:        hdr.fmt.bits = htole16(par->bits);
1.15      ratchov   276:        hdr.fmt.blkalign = par->bps * nch;
1.1       ratchov   277:
                    278:        memcpy(hdr.data_hdr.id, wav_id_data, 4);
                    279:        hdr.data_hdr.size = htole32(datasz);
1.4       ratchov   280:
1.1       ratchov   281:        if (lseek(fd, 0, SEEK_SET) < 0) {
                    282:                warn("wav_writehdr: lseek");
                    283:                return 0;
                    284:        }
                    285:        if (write(fd, &hdr, sizeof(hdr)) != sizeof(hdr)) {
                    286:                warn("wav_writehdr: write");
                    287:                return 0;
                    288:        }
1.13      ratchov   289:        *startpos = sizeof(hdr);
1.1       ratchov   290:        return 1;
                    291: }