Converting the code to use the modes instead of global arrays.

This commit is contained in:
Jean-Marc Valin 2007-12-05 17:48:24 +11:00
parent ecb36a3323
commit 73e51b3e94
7 changed files with 40 additions and 23 deletions

View file

@ -31,6 +31,7 @@
#include <math.h> #include <math.h>
#include "bands.h" #include "bands.h"
#include "modes.h"
#include "vq.h" #include "vq.h"
#include "cwrs.h" #include "cwrs.h"
@ -45,31 +46,35 @@ const int qpulses[NBANDS ] = {7, 5, 4, 4, 3, 3, 3, 4, 4, 4, -2, -1, -1, -1
int pbank[] = {0, 4, 8, 12, 20, WAVEFORM_END, 128}; int pbank[] = {0, 4, 8, 12, 20, WAVEFORM_END, 128};
/* Compute the energy in each of the bands */ /* Compute the energy in each of the bands */
void compute_bands(float *X, int B, float *bank) void compute_band_energies(const CELTMode *m, float *X, float *bank)
{ {
int i; int i, B;
const int *eBands = m->eBands;
B = m->nbMdctBlocks;
for (i=0;i<NBANDS;i++) for (i=0;i<NBANDS;i++)
{ {
int j; int j;
bank[i] = 1e-10; bank[i] = 1e-10;
for (j=B*qbank[i];j<B*qbank[i+1];j++) for (j=B*eBands[i];j<B*eBands[i+1];j++)
bank[i] += X[j]*X[j]; bank[i] += X[j]*X[j];
bank[i] = sqrt(bank[i]); bank[i] = sqrt(bank[i]);
} }
} }
/* Normalise each band such that the energy is one. */ /* Normalise each band such that the energy is one. */
void normalise_bands(float *X, int B, float *bank) void normalise_bands(const CELTMode *m, float *X, float *bank)
{ {
int i; int i, B;
const int *eBands = m->eBands;
B = m->nbMdctBlocks;
for (i=0;i<NBANDS;i++) for (i=0;i<NBANDS;i++)
{ {
int j; int j;
float x = 1.f/(1e-10+bank[i]); float x = 1.f/(1e-10+bank[i]);
for (j=B*qbank[i];j<B*qbank[i+1];j++) for (j=B*eBands[i];j<B*eBands[i+1];j++)
X[j] *= x; X[j] *= x;
} }
for (i=B*qbank[NBANDS];i<B*qbank[NBANDS+1];i++) for (i=B*eBands[NBANDS];i<B*eBands[NBANDS+1];i++)
X[i] = 0; X[i] = 0;
} }

View file

@ -32,14 +32,16 @@
#ifndef BANDS_H #ifndef BANDS_H
#define BANDS_H #define BANDS_H
#include "modes.h"
/* Number of constant-energy bands */ /* Number of constant-energy bands */
#define NBANDS 15 #define NBANDS 15
/* Number of bands only for the pitch prediction */ /* Number of bands only for the pitch prediction */
#define PBANDS 5 #define PBANDS 5
void compute_bands(float *X, int B, float *bands); void compute_band_energies(const CELTMode *m, float *X, float *bands);
void normalise_bands(float *X, int B, float *bands); void normalise_bands(const CELTMode *m, float *X, float *bands);
void denormalise_bands(float *X, int B, float *bands); void denormalise_bands(float *X, int B, float *bands);

View file

@ -62,15 +62,17 @@ struct CELTState_ {
CELTState *celt_encoder_new(int blockSize, int blocksPerFrame) CELTState *celt_encoder_new(const CELTMode *mode)
{ {
int i, N; int i, N, B;
N = blockSize; N = mode->mdctSize;
B = mode->nbMdctBlocks;
CELTState *st = celt_alloc(sizeof(CELTState)); CELTState *st = celt_alloc(sizeof(CELTState));
st->frame_size = blockSize * blocksPerFrame; st->mode = mode;
st->block_size = blockSize; st->frame_size = B*N;
st->nb_blocks = blocksPerFrame; st->block_size = N;
st->nb_blocks = B;
mdct_init(&st->mdct_lookup, 2*N); mdct_init(&st->mdct_lookup, 2*N);
st->fft = spx_fft_init(MAX_PERIOD); st->fft = spx_fft_init(MAX_PERIOD);
@ -197,11 +199,12 @@ int celt_encode(CELTState *st, short *pcm)
//haar1(P, B*N); //haar1(P, B*N);
/* Band normalisation */ /* Band normalisation */
compute_bands(X, B, bandE); compute_band_energies(st->mode, X, bandE);
normalise_bands(X, B, bandE); normalise_bands(st->mode, X, bandE);
compute_bands(P, B, bandEp); //for (i=0;i<NBANDS;i++)printf("%f ", bandE[i]);printf("\n");
normalise_bands(P, B, bandEp); compute_band_energies(st->mode, P, bandEp);
normalise_bands(st->mode, P, bandEp);
/* Pitch prediction */ /* Pitch prediction */
compute_pitch_gain(X, B, P, gains, bandE); compute_pitch_gain(X, B, P, gains, bandE);

View file

@ -35,9 +35,12 @@
struct CELTState_; struct CELTState_;
typedef struct CELTState_ CELTState; typedef struct CELTState_ CELTState;
struct CELTMode_;
typedef struct CELTMode_ CELTMode;
extern const CELTMode const *celt_mode1;
CELTState *celt_encoder_new(int blockSize, int blocksPerFrame); CELTState *celt_encoder_new(const CELTMode *mode);
void celt_encoder_destroy(CELTState *st); void celt_encoder_destroy(CELTState *st);

View file

@ -55,3 +55,5 @@ const CELTMode mode1 = {
pbank1, /**< pBands*/ pbank1, /**< pBands*/
qpulses1 /**< pulses */ qpulses1 /**< pulses */
}; };
const CELTMode const *celt_mode1 = &mode1;

View file

@ -32,7 +32,9 @@
#ifndef MODES_H #ifndef MODES_H
#define MODES_H #define MODES_H
typedef struct { #include "celt.h"
struct CELTMode_ {
int frameSize; int frameSize;
int mdctSize; int mdctSize;
int nbMdctBlocks; int nbMdctBlocks;
@ -44,6 +46,6 @@ typedef struct {
const int *eBands; const int *eBands;
const int *pBands; const int *pBands;
const int *pulses; const int *pulses;
} CELTMode; };
#endif #endif

View file

@ -48,7 +48,7 @@ int main(int argc, char *argv[])
outFile = argv[2]; outFile = argv[2];
fout = fopen(outFile, "wb+"); fout = fopen(outFile, "wb+");
st = celt_encoder_new(FRAME_SIZE/NBLOCKS, NBLOCKS); st = celt_encoder_new(celt_mode1);
while (!feof(fin)) while (!feof(fin))
{ {