/**
 * Audio processing blocks for channel simulation.
 *
 * @copyright 2008, Research In Motion Ltd.
 *
 * @version $Id: //depot/main/dsp/ecall/c/testlab/audio.h#11 $
 */

#ifndef __AUDIO_H__
#define __AUDIO_H__

#include <stdio.h>
#include "process.h"

/*
 * All processing is done in 20ms (160 sample) frames
 */
#define PCM_LENGTH      160

/*
 * 30ms @ 8kHz = 240 samples 
 */
#define MAX_DELAY       240

#define DELAY_BUF_SIZE  (MAX_DELAY + PCM_LENGTH)

typedef struct {
    float inHistory[2]; 
    float outHistory[2];  
    float inCoef[3];
    float outCoef[3];
} BiquadState;

typedef struct {
    int read; 
    int write;  
    Int16 buffer[DELAY_BUF_SIZE];
} DelayState;

typedef enum {
    CodecTypeFr,
    CodecTypeAmr,
    CodecTypeNone,
    CodecTypeBypass
} CodecType;

typedef enum {
    CodecModeNa,
    CodecModeAmr12_2,
    CodecModeAmr10_2,
    CodecModeAmr7_95,
    CodecModeAmr7_4,
    CodecModeAmr6_7,
    CodecModeAmr5_9,
    CodecModeAmr5_15,
    CodecModeAmr4_75
} CodecMode;

typedef enum {
    VadTypeNone,
    VadType1,
    VadType2,
} VadType;

typedef struct {
    CodecType codecType;
    CodecMode codecMode;
    Process e;       /* encoder process */
    Process d;       /* decoder process */
    FILE *errorFile;
} CodecState;


void DelayInit(DelayState *state, Ord16 nSamples);
void DelayProcess(DelayState *state, Int16 *samples);

void BiquadInit(BiquadState *state, const float a[3], const float b[3]);
void BiquadProcess(BiquadState *state, Int16 *samples);

/* Wraps encoder, error pattern XOR, and decoder */
BOOL CodecInit(
    CodecState *state, 
    CodecType type,
    CodecMode mode,
    VadType vad,
    const char *errorPattern,
    Ord32 errorPatternIndex);
void CodecProcess(CodecState *state, Int16 *samples);
void CodecFinal(CodecState *channelState);

#endif
