/*-
 * Copyright (c) 2008 Ariff Abdullah <ariff@FreeBSD.org>
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 * $FreeBSD$
 */

#ifndef _SND_WAVEUTIL_H_
#define _SND_WAVEUTIL_H_

struct wave_info {
        uint32_t format;
        uint32_t bit;
        uint32_t channels;
        uint32_t rate;
        uint32_t endian;
        uint32_t header_size;
        uint32_t data_size;
        FILE *fp;
        int seekable;
};

#define WAVE_HEADER_SIZE        44
#define WAVE_HEADER_SIZE_G711   58
#define WAVE_HEADER_SIZE_EXT    80
#define WAVE_DATA_SIZE_BIG      0x7ffff000U
#define WAVE_BLOCK_ALIGN(w)     (((w)->bit >> 3) * (w)->channels)

#define WAVE_FORMAT_UNKNOWN     0x0000U
#define WAVE_FORMAT_PCM         0x0001U
#define WAVE_FORMAT_ALAW        0x0006U
#define WAVE_FORMAT_ULAW        0x0007U
#define WAVE_FORMAT_EXT         0xfffeU

#define WAVE_MKMAGIC(a, b, c, d)        ((uint32_t)(a) << 24 |         \
                                            (b) << 16 | (c) << 8 | (d))

#define WAVE_MAGIC_RIFF         WAVE_MKMAGIC('R', 'I', 'F', 'F')
#define WAVE_MAGIC_RIFX         WAVE_MKMAGIC('R', 'I', 'F', 'X')
#define WAVE_MAGIC_WAVE         WAVE_MKMAGIC('W', 'A', 'V', 'E')
#define WAVE_MAGIC_FMT          WAVE_MKMAGIC('f', 'm', 't', ' ')
#define WAVE_MAGIC_DATA         WAVE_MKMAGIC('d', 'a', 't', 'a')
#define WAVE_MAGIC_FACT         WAVE_MKMAGIC('f', 'a', 'c', 't')

enum {
        WAVE_UNKNOWN_ENDIAN,
        WAVE_LITTLE_ENDIAN,
        WAVE_BIG_ENDIAN
};

#define WAVE_READ_32(i, b)      ((uint32_t)                          \
                                    (((i)->endian ==                    \
                                    WAVE_LITTLE_ENDIAN) ?               \
                                    _PCM_READ_S32_LE(b) :               \
                                    _PCM_READ_S32_BE(b)))

#define WAVE_WRITE_32(i, b, v)  do {                                     \
        if ((i)->endian == WAVE_LITTLE_ENDIAN)                         \
                _PCM_WRITE_S32_LE(b, (uint32_t)v);                    \
        else                                                           \
                _PCM_WRITE_S32_BE(b, (uint32_t)v);                    \
} while (0)

#define WAVE_MAGIC_READ(b)      ((uint32_t)_PCM_READ_S32_BE(b))
#define WAVE_MAGIC_WRITE(b, v)  _PCM_WRITE_S32_BE(b, (uint32_t)v)


#define WAVE_READ_16(i, b)      ((uint32_t)                          \
                                    (((i)->endian ==                    \
                                    WAVE_LITTLE_ENDIAN) ?               \
                                    _PCM_READ_S16_LE(b) :               \
                                    _PCM_READ_S16_BE(b)) & 0x0000ffff)

#define WAVE_WRITE_16(i, b, v)  do {                                     \
        if ((i)->endian == WAVE_LITTLE_ENDIAN)                         \
                _PCM_WRITE_S16_LE(b, (uint32_t)(v) & 0x0000ffff);     \
        else                                                           \
                _PCM_WRITE_S16_BE(b, (uint32_t)(v) & 0x0000ffff);     \
} while (0)

int wave_header_read(struct wave_info *);
int wave_header_write(struct wave_info *);

#endif  /* !_SND_WAVEUTIL_H_ */