金牌会员
 
- 积分
- 1243
- 金钱
- 1243
- 注册时间
- 2016-1-23
- 在线时间
- 487 小时
|
发表于 2016-1-27 17:31:20
|
显示全部楼层
FUNCTION....: codec2_encode_3200
AUTHOR......: David Rowe
DATE CREATED: 13 Sep 2012
Encodes 160 speech samples (20ms of speech) into 64 bits.
The codec2 algorithm actually operates internally on 10ms (80
sample) frames, so we run the encoding algorithm twice. On the
first frame we just send the voicing bits. On the second frame we
send all model parameters. Compared to 2400 we use a larger number
of bits for the LSPs and non-VQ pitch and energy.
The bit allocation is:
Parameter bits/frame
--------------------------------------
Harmonic magnitudes (LSPs) 50
Pitch (Wo) 7
Energy 5
Voicing (10ms update) 2
TOTAL 64
\*---------------------------------------------------------------------------*/
void codec2_encode_3200(struct CODEC2 *c2, unsigned char * bits, short speech[])
{
MODEL model;
float ak[LPC_ORD+1];
float lsps[LPC_ORD];
float e;
int Wo_index, e_index;
int lspd_indexes[LPC_ORD];
int i;
unsigned int nbit = 0;
assert(c2 != NULL);
memset(bits, '\0', ((codec2_bits_per_frame(c2) + 7) / 8));
/* first 10ms analysis frame - we just want voicing */
analyse_one_frame(c2, &model, speech);
pack(bits, &nbit, model.voiced, 1);
/* second 10ms analysis frame */
analyse_one_frame(c2, &model, &speech[N]);
pack(bits, &nbit, model.voiced, 1);
Wo_index = encode_Wo(model.Wo, WO_BITS);
pack(bits, &nbit, Wo_index, WO_BITS);
e = speech_to_uq_lsps(lsps, ak, c2->Sn, c2->w, LPC_ORD);
e_index = encode_energy(e, E_BITS);
pack(bits, &nbit, e_index, E_BITS);
encode_lspds_scalar(lspd_indexes, lsps, LPC_ORD);
for(i=0; i<LSPD_SCALAR_INDEXES; i++) {
pack(bits, &nbit, lspd_indexes[i], lspd_bits(i));
}
assert(nbit == (unsigned)codec2_bits_per_frame(c2));
}
/*---------------------------------------------------------------------------*\
FUNCTION....: codec2_decode_3200
AUTHOR......: David Rowe
DATE CREATED: 13 Sep 2012
Decodes a frame of 64 bits into 160 samples (20ms) of speech.
\*---------------------------------------------------------------------------*/
void codec2_decode_3200(struct CODEC2 *c2, short speech[], const unsigned char * bits)
{
MODEL model[2];
int lspd_indexes[LPC_ORD];
float lsps[2][LPC_ORD];
int Wo_index, e_index;
float e[2];
float snr;
float ak[2][LPC_ORD+1];
int i,j;
unsigned int nbit = 0;
COMP Aw[FFT_ENC];
assert(c2 != NULL);
/* only need to zero these out due to (unused) snr calculation */
for(i=0; i<2; i++)
for(j=1; j<=MAX_AMP; j++)
model[i].A[j] = 0.0;
/* unpack bits from channel ------------------------------------*/
/* this will partially fill the model params for the 2 x 10ms
frames */
model[0].voiced = unpack(bits, &nbit, 1);
model[1].voiced = unpack(bits, &nbit, 1);
Wo_index = unpack(bits, &nbit, WO_BITS);
model[1].Wo = decode_Wo(Wo_index, WO_BITS);
model[1].L = PI/model[1].Wo;
e_index = unpack(bits, &nbit, E_BITS);
e[1] = decode_energy(e_index, E_BITS);
for(i=0; i<LSPD_SCALAR_INDEXES; i++) {
lspd_indexes[i] = unpack(bits, &nbit, lspd_bits(i));
}
decode_lspds_scalar(&lsps[1][0], lspd_indexes, LPC_ORD);
/* interpolate ------------------------------------------------*/
/* Wo and energy are sampled every 20ms, so we interpolate just 1
10ms frame between 20ms samples */
interp_Wo(&model[0], &c2->prev_model_dec, &model[1]);
e[0] = interp_energy(c2->prev_e_dec, e[1]);
/* LSPs are sampled every 20ms so we interpolate the frame in
between, then recover spectral amplitudes */
interpolate_lsp_ver2(&lsps[0][0], c2->prev_lsps_dec, &lsps[1][0], 0.5, LPC_ORD);
for(i=0; i<2; i++) {
lsp_to_lpc(&lsps[i][0], &ak[i][0], LPC_ORD);
aks_to_M2(c2->fft_fwd_cfg, &ak[i][0], LPC_ORD, &model[i], e[i], &snr, 0, 0,
c2->lpc_pf, c2->bass_boost, c2->beta, c2->gamma, Aw);
apply_lpc_correction(&model[i]);
synthesise_one_frame(c2, &speech[N*i], &model[i], Aw);
}
/* update memories for next frame ----------------------------*/
c2->prev_model_dec = model[1];
c2->prev_e_dec = e[1];
for(i=0; i<LPC_ORD; i++)
c2->prev_lsps_dec[i] = lsps[1][i];
}
/*---------------------------------------------------------------------------*\
FUNCTION....: codec2_encode_2400
AUTHOR......: David Rowe
DATE CREATED: 21/8/2010
Encodes 160 speech samples (20ms of speech) into 48 bits.
The codec2 algorithm actually operates internally on 10ms (80
sample) frames, so we run the encoding algorithm twice. On the
first frame we just send the voicing bit. On the second frame we
send all model parameters.
The bit allocation is:
Parameter bits/frame
--------------------------------------
Harmonic magnitudes (LSPs) 36
Joint VQ of Energy and Wo 8
Voicing (10ms update) 2
Spare 2
TOTAL 48
\*---------------------------------------------------------------------------*/
void codec2_encode_2400(struct CODEC2 *c2, unsigned char * bits, short speech[])
{
MODEL model;
float ak[LPC_ORD+1];
float lsps[LPC_ORD];
float e;
int WoE_index;
int lsp_indexes[LPC_ORD];
int i;
int spare = 0;
unsigned int nbit = 0;
assert(c2 != NULL);
memset(bits, '\0', ((codec2_bits_per_frame(c2) + 7) / 8));
/* first 10ms analysis frame - we just want voicing */
analyse_one_frame(c2, &model, speech);
pack(bits, &nbit, model.voiced, 1);
/* second 10ms analysis frame */
analyse_one_frame(c2, &model, &speech[N]);
pack(bits, &nbit, model.voiced, 1);
e = speech_to_uq_lsps(lsps, ak, c2->Sn, c2->w, LPC_ORD);
WoE_index = encode_WoE(&model, e, c2->xq_enc);
pack(bits, &nbit, WoE_index, WO_E_BITS);
encode_lsps_scalar(lsp_indexes, lsps, LPC_ORD);
for(i=0; i<LSP_SCALAR_INDEXES; i++) {
pack(bits, &nbit, lsp_indexes[i], lsp_bits(i));
}
pack(bits, &nbit, spare, 2);
assert(nbit == (unsigned)codec2_bits_per_frame(c2));
}
/*---------------------------------------------------------------------------*\
FUNCTION....: codec2_decode_2400
AUTHOR......: David Rowe
DATE CREATED: 21/8/2010
Decodes frames of 48 bits into 160 samples (20ms) of speech.
\*---------------------------------------------------------------------------*/ |
|