本文介绍了speex编解码器代码/解码wavein流仅获得背景噪音“嘶嘶声"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的所有人,

我正在使用speex/wavein和waveout API实现语音聊天,

但是speex无法对语音流进行编码/解码.
wavein-> speex编码-> speex解码-> waveout,这里我只能得到

背景噪音嘶嘶声" ...我不知道为什么.我已经检查过

www.speex-dev.org中的问题与解答",其中显示
``原因之一可能是输入语音的缩放. Speex期望

信号具有$ \ pm2 ^ {15} $(带符号的短)动态范围''.
http://www.speex.org/docs/manual/speex-

manual/node12.html#SECTION0012012000000000000000
动态范围是多少,我不知道. Wavein的格式是

配置为:
#define BITS_PER_SAMPLE(8)
#define CHANNEL_NB(1)
#define SAMPLE_RATE(8000)//(44100)
#define SAMPLES_SEC(SAMPLE_RATE *

(BITS_SAMPLE>>>>>>>>>>>>>>>>> ;; 3)* CHANNEL_NB)//Hz *字节*通道#define DURATION(0.08)

以下是我的src代码的代码段:
tInt32 SpeexConverter :: Init()
{
tInt32 sample_rate;
tInt32 tmp;
float ftmp;

_enc_state = speex_encoder_init(speex_lib_get_mode

(SPEEX_MODEID_NB));

如果(_enc_state == NULL)
{
LOGERROR("SpeexConverter :: Init:无法创建

编码器.);

返回-1;
}

_dec_state = speex_decoder_init(speex_lib_get_mode

(SPEEX_MODEID_NB));

如果(_dec_state == NULL)
{
LOGERROR("SpeexConverter :: Init:无法创建

解码器.);

返回-2;
}

tmp = 8000;
speex_encoder_ctl(_enc_state,SPEEX_SET_SAMPLING_RATE,& tmp);
speex_decoder_ctl(_dec_state,SPEEX_SET_SAMPLING_RATE,& tmp);

/**
*获取编码器和解码器的帧大小
*/
speex_encoder_ctl(_enc_state,SPEEX_GET_FRAME_SIZE,(void *)

& _encframe_size);
speex_decoder_ctl(_dec_state,SPEEX_GET_FRAME_SIZE,(void *)

& _decframe_size);

/**
*初始化位struct
*/
speex_bits_init(& _bits);
speex_bits_init(& _bits2);
返回1;
}

tInt32 SpeexConverter :: Code(tByte * src,tUInt32 len,tByte * out,

tUInt32 out_buffer_len,void * arg)
{
tInt32 nvBytes = 0;
tInt16 * in_short =(tInt16 *)src;
tUInt32 len_short = len/2;
tUInt32 coded_shorts = 0;
tUInt32 in_offset = 0;
tUInt32 out_offset = 0;

tUInt32 skip_group_delay = _skip_group_delay;


tUInt32 tmp = 0;

while(in_offset< = len-_encframe_size * 2)
{
speex_bits_reset(& _bits);

memcpy(_buf.ca,src + in_offset,_encframe_size * 2);

//speex_encode_int(_enc_state,(spx_int16_t *)

(src + in_offset),& _bits);
tmp = speex_encode_int(_enc_state,_buf.sa,& _bits);

nvBytes = speex_bits_nbytes(& _bits);

*(tUInt32 *)(out + out_offset)= nvBytes;

out_offset + = sizeof(tUInt32);

nvBytes = speex_bits_write(& _bits,(char *)

(out + out_offset),out_buffer_len-out_offset);

in_offset + = _encframe_size * 2;

out_offset + = nvBytes;
}

返回out_offset;
}

tInt32 SpeexConverter :: Decode(tByte * src,tUInt32 len,tByte * out,

tUInt32 out_buffer_len,void * arg)
{
tUInt32 nvByte = 0;
tUInt32 in_offset = 0;
tUInt32 out_offset = 0;

tInt32 tmp = 0;

while(in_offset< len)
{
nvByte = *(tUInt32 *)(src + in_offset);
in_offset + = sizeof(tUInt32);

speex_bits_reset(& _bits2);
speex_bits_set_bit_buffer(& _bits2,(void *)

(src + in_offset),nvByte);
tmp = speex_decode_int(_dec_state,& _bits2,_buf2.sa);

memcpy(out + out_offset,(void *)_ buf2.ca,(_ decframe_size)

* 2);

in_offset + = nvByte;

out_offset + = _decframe_size * 2;
}

返回out_offset;
}

/**和头文件中,有人说这可能是必要的.但是

仍然不起作用.
工会
{
char ca [FRAME_SIZE * 2];
短sa [FRAME_SIZE];
} _buf,_buf2;
*/

任何人都可以帮忙,真的很感激.

Dear all,

I am using speex/wavein and waveout api to implement voice chatter,

but speex failed to encode/decode the voice stream.
wavein-->speex encode-->speex decode->waveout, here I can only get

background noise ''hiss''... I do not know why. I have checked with the

Q&A in the www.speex-dev.org, which says
''One of the causes could be scaling of the input speech. Speex expects

signals to have a $ \pm2^{15}$ (signed short) dynamic range''.
http://www.speex.org/docs/manual/speex-

manual/node12.html#SECTION0012012000000000000000
what''s the dynamic range, I do not know. the format for wavein are

configured as:
#define BITS_PER_SAMPLE(8)
#defineCHANNEL_NB(1)
#define SAMPLE_RATE(8000) //(44100)
#define SAMPLES_SEC(SAMPLE_RATE *

(BITS_SAMPLE>>3) * CHANNEL_NB)// Hz * Bytes * Channels
#define DURATION(0.08)

follow is the snippet of my src code:
tInt32SpeexConverter::Init()
{
tInt32sample_rate;
tInt32tmp;
floatftmp;

_enc_state = speex_encoder_init(speex_lib_get_mode

(SPEEX_MODEID_NB));

if (_enc_state == NULL)
{
LOGERROR("SpeexConverter::Init:Failed to create a

encoder.");

return -1;
}

_dec_state = speex_decoder_init(speex_lib_get_mode

(SPEEX_MODEID_NB));

if (_dec_state == NULL)
{
LOGERROR("SpeexConverter::Init:Failed to create a

decoder.");

return -2;
}

tmp = 8000;
speex_encoder_ctl(_enc_state, SPEEX_SET_SAMPLING_RATE, &tmp);
speex_decoder_ctl(_dec_state, SPEEX_SET_SAMPLING_RATE, &tmp);

/**
* Get the frame size of the encoder and decoder
*/
speex_encoder_ctl(_enc_state,SPEEX_GET_FRAME_SIZE,(void*)

&_encframe_size);
speex_decoder_ctl(_dec_state,SPEEX_GET_FRAME_SIZE,(void*)

&_decframe_size);

/**
* Initialization of the bits struct
*/
speex_bits_init(&_bits);
speex_bits_init(&_bits2);
return 1;
}

tInt32SpeexConverter::Code(tByte* src, tUInt32 len, tByte* out,

tUInt32 out_buffer_len,void*arg)
{
tInt32nvBytes= 0;
tInt16*in_short= (tInt16*)src;
tUInt32len_short= len/2;
tUInt32coded_shorts= 0;
tUInt32in_offset= 0;
tUInt32out_offset= 0;

tUInt32skip_group_delay = _skip_group_delay;


tUInt32 tmp = 0;

while (in_offset <= len-_encframe_size*2)
{
speex_bits_reset(&_bits);

memcpy(_buf.ca,src+in_offset,_encframe_size*2);

//speex_encode_int(_enc_state, (spx_int16_t*)

(src+in_offset), &_bits);
tmp = speex_encode_int(_enc_state, _buf.sa, &_bits);

nvBytes = speex_bits_nbytes(&_bits);

*(tUInt32*)(out+out_offset) = nvBytes;

out_offset += sizeof(tUInt32);

nvBytes = speex_bits_write(&_bits, (char*)

(out+out_offset), out_buffer_len-out_offset);

in_offset += _encframe_size*2;

out_offset += nvBytes;
}

return out_offset;
}

tInt32SpeexConverter::Decode(tByte* src, tUInt32 len, tByte* out,

tUInt32 out_buffer_len,void*arg)
{
tUInt32 nvByte= 0;
tUInt32in_offset= 0;
tUInt32out_offset= 0;

tInt32 tmp = 0;

while (in_offset < len)
{
nvByte = *(tUInt32*)(src+in_offset);
in_offset += sizeof(tUInt32);

speex_bits_reset(&_bits2);
speex_bits_set_bit_buffer(&_bits2, (void*)

(src+in_offset), nvByte);
tmp = speex_decode_int(_dec_state, &_bits2, _buf2.sa);

memcpy(out+out_offset,(void*)_buf2.ca,(_decframe_size)

*2);

in_offset += nvByte;

out_offset+= _decframe_size*2;
}

return out_offset;
}

/** and in the header file, some people said it maybe necessary. but

still does not work.
union
{
char ca[FRAME_SIZE*2];
short sa[FRAME_SIZE];
} _buf,_buf2;
*/

can anybody help, really appreciate it.

推荐答案




这篇关于speex编解码器代码/解码wavein流仅获得背景噪音“嘶嘶声"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-18 22:39