vadpcm.h 3.77 KB
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <libaudio.h>

#define VERSION                 1

#define FRAMEBYTES              9
#define FRAMESIZE               16
#define VECTORSIZE              8
#define HEADERBITS              8
#define SAMPLEBITS              4
#define MAXSCALE                12

#define MAXCLIP                 1
#define MAXITER                 2

#define MINLOOPLENGTH           800

#define DEFAULT_NPREDICTORS     4
#define DEFAULT_ORDER           2

#define VSCALING                2048

#define IFF_ID_FORM             'FORM'
#define IFF_ID_AIFC             'AIFC'
#define IFF_ID_AIFF             'AIFF'
#define IFF_ID_COMM             'COMM'
#define IFF_ID_SSND             'SSND'
#define IFF_ID_INST             'INST'
#define IFF_ID_APPL             'APPL'
#define IFF_ID_MARK             'MARK'

#define CODE_NAME               "VADPCMCODES"
#define LOOP_NAME               "VADPCMLOOPS"

#define NoLooping               0
#define ForwardLooping          1
#define ForwardBackwardLooping  2

typedef struct {
    unsigned long       ckID;
    long                ckSize;
} ChunkHeader;

typedef struct {
    unsigned long       ckID;
    long                ckSize;
    unsigned long       formType;
} Chunk;

typedef struct {
    short               numChannels;
    unsigned short      numFramesH;
    unsigned short      numFramesL;             /* To prevent 4 byte alignment */
    short               sampleSize;
    char                sampleRate[10];
    unsigned short      compressionTypeH;
    unsigned short      compressionTypeL;        /* note: string follows */
} CommonChunk;

typedef short MarkerID;

typedef struct {
    MarkerID            id;
    unsigned short      positionH;
    unsigned short      positionL;              /* note: pstring follows */
} Marker;

typedef struct {
    short               playMode;
    MarkerID            beginLoop;
    MarkerID            endLoop;
} Loop;

/* typedef struct {
    long                start;
    long                end;
    long                count;
    short               state[FRAMESIZE];
} Aloop;
*/
typedef struct {
    char                baseNote;
    char                detune;
    char                lowNote;
    char                highNote;
    char                lowVelocity;
    char                highVelocity;
    short               gain;
    Loop                sustainLoop;
    Loop                releaseLoop;
} InstrumentChunk;

typedef struct {
    unsigned long       offset;
    unsigned long       blockSize;
} SoundDataChunk;

typedef struct {
    short       version;
    short       order;
    short       nEntries;
} CodeChunk;

typedef struct {
    unsigned long   hi;
    unsigned long   lo;
} IEEE_DBL;

typedef struct {
    unsigned long   l1;
    unsigned long   l2;
    unsigned short  s1;
} SANE_EXT;

/* vpredictor.c */
int inner_product(int size, int *v1, int *v2);
int readaifccodebook(FILE *fhandle, int ****table, short *order, short *npredictors);
int readcodebook(FILE *fhandle, int ****table, int *order, int *npredictors);

/* vencode.c */
int vencodeframe(FILE *ofile, short *inBuffer, int *state, int ***coefTable, int order, int npredictors, int nsam);

/* vdecode.c */
void vdecodeframe(FILE *ifile, int *outp, int order, int ***coefTable);

/* vquant.c */
short qsample(float x, int scale);
void clamp(int fs, float *e, int *ie, int bits);
int clip(int ix, int l, int u);

/* In sampleio.c */
void writeout(FILE *outfd, int size, int *l_out, int *r_out, int chans);

/* In util.c */

uint readbits(uint nbits, FILE *ifile);
char *ReadPString(FILE *ifile);
int lookupMarker(unsigned long *sample, MarkerID loopPoint, Marker *markers, int nmarkers);
ALADPCMloop *readlooppoints(FILE *ifile, short *nloops);