kirill_782
@kirill_782
Днем я Маринетт

Как сжать аудиопоток через Opus?

Пишу некое подобие голосового чата. Есть вот такой код:

voip::voip(QWidget *parent)
	: QWidget(parent)
{
	ui.setupUi(this);

	QAudioFormat format;
	format.setSampleRate( 48000 );
	format.setChannelCount( 2 );
	format.setSampleSize( 8 );
	format.setCodec( "audio/pcm" );
	format.setByteOrder( QAudioFormat::LittleEndian );
	format.setSampleType( QAudioFormat::SignedInt );

	QAudioDeviceInfo info( QAudioDeviceInfo::defaultOutputDevice( ) );


	QAudioOutput *out = new QAudioOutput( format, this );
	QAudioInput *in = new QAudioInput( format, this );
	in->setNotifyInterval( 20 );

	QIODevice * inIO = in->start( );

	QIODevice * outIO = out->start( );

	int error = 0;

	OpusEncoder *encoder =  opus_encoder_create( 48000, 2, OPUS_APPLICATION_VOIP, &error );
	opus_encoder_ctl( encoder, OPUS_SET_BITRATE( 64000 ) );

	OpusDecoder *decoder = opus_decoder_create( 48000, 2, &error );
	opus_decoder_ctl( decoder, OPUS_SET_BITRATE( 64000 ) );

	connect( in, &QAudioInput::notify, this, [=] {
		QByteArray ba = inIO->readAll( );

		unsigned char *data = new unsigned char[1440];
		opus_int16 *pcm = new opus_int16[1440 * 2];

		int nbBytes = opus_encode( encoder, (opus_int16 *)inIO->readAll( ).data( ), 960, data, 1440 );
		
		// Тип приняли данные

		int nbBytes2 = opus_decode( decoder, data, nbBytes, pcm, 1440, 0);

		outIO->write( (char *)pcm, nbBytes2 * 2 );
		
	} );
}


Но после opus_decode получаю шум. Есть способ исправить?
  • Вопрос задан
  • 314 просмотров
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы