Ref T730, OPUS de/encoder namespace

This commit is contained in:
Klaus Basan
2019-09-19 01:44:28 +02:00
committed by Mat Sutcliffe
parent 9809acd98c
commit 34d1e8268c
7 changed files with 169 additions and 105 deletions

View File

@@ -27,7 +27,6 @@
#include <QTimer> #include <QTimer>
#include <QDateTime> #include <QDateTime>
namespace BlackCore namespace BlackCore
{ {
namespace Afv namespace Afv
@@ -91,7 +90,7 @@ namespace BlackCore
BufferedWaveProvider *audioInput; BufferedWaveProvider *audioInput;
QTimer m_timer; QTimer m_timer;
COpusDecoder m_decoder; BlackSound::Codecs::COpusDecoder m_decoder;
bool m_lastPacketLatch = false; bool m_lastPacketLatch = false;
QDateTime m_lastSamplesAddedUtc; QDateTime m_lastSamplesAddedUtc;
bool m_underflow = false; bool m_underflow = false;

View File

@@ -90,7 +90,7 @@ namespace BlackCore
static constexpr qint64 c_frameSize = 960; static constexpr qint64 c_frameSize = 960;
int m_sampleRate = 0; int m_sampleRate = 0;
COpusEncoder m_encoder; BlackSound::Codecs::COpusEncoder m_encoder;
QScopedPointer<QAudioInput> m_audioInput; QScopedPointer<QAudioInput> m_audioInput;
bool m_started = false; bool m_started = false;

View File

@@ -16,16 +16,16 @@ DEPENDPATH += . ..
DEFINES += LOG_IN_FILE BUILD_BLACKSOUND_LIB DEFINES += LOG_IN_FILE BUILD_BLACKSOUND_LIB
HEADERS += *.h HEADERS += *.h
HEADERS += wav/wavfile.h HEADERS += $$PWD/wav/wavfile.h
HEADERS += dsp/*.h HEADERS += $$PWD/dsp/*.h
HEADERS += codecs/*.h HEADERS += $$PWD/codecs/*.h
HEADERS += samplesprovider/*.h HEADERS += $$PWD/sampleprovider/*.h
SOURCES += *.cpp SOURCES += *.cpp
SOURCES += wav/wavfile.cpp SOURCES += $$PWD/wav/wavfile.cpp
SOURCES += dsp/*.cpp SOURCES += $$PWD/dsp/*.cpp
SOURCES += codecs/*.cpp SOURCES += $$PWD/codecs/*.cpp
SOURCES += samplesprovider/*.cpp SOURCES += $$PWD/sampleprovider/*.cpp
LIBS *= -lopus LIBS *= -lopus

View File

@@ -1,40 +1,54 @@
/* Copyright (C) 2019
* swift project Community / Contributors
*
* This file is part of swift project. It is subject to the license terms in the LICENSE file found in the top-level
* directory of this distribution. No part of swift project, including this file, may be copied, modified, propagated,
* or distributed except according to the terms contained in the LICENSE file.
*/
#include "opusdecoder.h" #include "opusdecoder.h"
COpusDecoder::COpusDecoder(int sampleRate, int channels) : namespace BlackSound
{
namespace Codecs
{
COpusDecoder::COpusDecoder(int sampleRate, int channels) :
m_sampleRate(sampleRate), m_sampleRate(sampleRate),
m_channels(channels) m_channels(channels)
{ {
int error; int error;
opusDecoder = opus_decoder_create(sampleRate, channels, &error); opusDecoder = opus_decoder_create(sampleRate, channels, &error);
} }
COpusDecoder::~COpusDecoder() COpusDecoder::~COpusDecoder()
{ {
opus_decoder_destroy(opusDecoder); opus_decoder_destroy(opusDecoder);
} }
int COpusDecoder::frameCount(int bufferSize) int COpusDecoder::frameCount(int bufferSize)
{ {
// seems like bitrate should be required // seems like bitrate should be required
int bitrate = 16; int bitrate = 16;
int bytesPerSample = (bitrate / 8) * m_channels; int bytesPerSample = (bitrate / 8) * m_channels;
return bufferSize / bytesPerSample; return bufferSize / bytesPerSample;
} }
QVector<qint16> COpusDecoder::decode(const QByteArray opusData, int dataLength, int *decodedLength) QVector<qint16> COpusDecoder::decode(const QByteArray opusData, int dataLength, int *decodedLength)
{ {
QVector<qint16> decoded(maxDataBytes, 0); QVector<qint16> decoded(maxDataBytes, 0);
int count = frameCount(maxDataBytes); int count = frameCount(maxDataBytes);
if (! opusData.isEmpty()) if (! opusData.isEmpty())
{ {
*decodedLength = opus_decode(opusDecoder, reinterpret_cast<const unsigned char*>(opusData.data()), dataLength, decoded.data(), count, 0); *decodedLength = opus_decode(opusDecoder, reinterpret_cast<const unsigned char *>(opusData.data()), dataLength, decoded.data(), count, 0);
} }
decoded.resize(*decodedLength); decoded.resize(*decodedLength);
return decoded; return decoded;
} }
void COpusDecoder::resetState() void COpusDecoder::resetState()
{ {
opus_decoder_ctl(opusDecoder, OPUS_RESET_STATE); opus_decoder_ctl(opusDecoder, OPUS_RESET_STATE);
} }
} // ns
} // ns

View File

@@ -1,16 +1,33 @@
#ifndef OPUSDECODER_H /* Copyright (C) 2019
#define OPUSDECODER_H * swift project Community / Contributors
*
* This file is part of swift project. It is subject to the license terms in the LICENSE file found in the top-level
* directory of this distribution. No part of swift project, including this file, may be copied, modified, propagated,
* or distributed except according to the terms contained in the LICENSE file.
*/
//! \file
#ifndef BLACKSOUND_OPUSDECODER_H
#define BLACKSOUND_OPUSDECODER_H
#include "blacksound/blacksoundexport.h" #include "blacksound/blacksoundexport.h"
#include "opus/opus.h" #include "opus/opus.h"
#include <QVector> #include <QVector>
class BLACKSOUND_EXPORT COpusDecoder namespace BlackSound
{ {
public: namespace Codecs
{
//! OPUS decoder
class BLACKSOUND_EXPORT COpusDecoder
{
public:
//! Ctor
COpusDecoder(int sampleRate, int channels); COpusDecoder(int sampleRate, int channels);
//! Dtor
~COpusDecoder(); ~COpusDecoder();
int frameCount(int bufferSize); int frameCount(int bufferSize);
@@ -19,12 +36,14 @@ public:
void resetState(); void resetState();
private: private:
OpusDecoder *opusDecoder; OpusDecoder *opusDecoder;
int m_sampleRate; int m_sampleRate;
int m_channels; int m_channels;
static constexpr int maxDataBytes = 4000; static constexpr int maxDataBytes = 4000;
}; };
} // ns
} // ns
#endif // OPUSDECODER_H #endif // guard

View File

@@ -1,30 +1,42 @@
/* Copyright (C) 2019
* swift project Community / Contributors
*
* This file is part of swift project. It is subject to the license terms in the LICENSE file found in the top-level
* directory of this distribution. No part of swift project, including this file, may be copied, modified, propagated,
* or distributed except according to the terms contained in the LICENSE file.
*/
#include "opusencoder.h" #include "opusencoder.h"
COpusEncoder::COpusEncoder(int sampleRate, int channels, int application) : namespace BlackSound
{
namespace Codecs
{
COpusEncoder::COpusEncoder(int sampleRate, int channels, int application) :
m_sampleRate(sampleRate), m_sampleRate(sampleRate),
m_channels(channels) m_channels(channels)
{ {
int error; int error;
opusEncoder = opus_encoder_create(sampleRate, channels, application, &error); opusEncoder = opus_encoder_create(sampleRate, channels, application, &error);
} }
COpusEncoder::~COpusEncoder() COpusEncoder::~COpusEncoder()
{ {
opus_encoder_destroy(opusEncoder); opus_encoder_destroy(opusEncoder);
} }
void COpusEncoder::setBitRate(int bitRate) void COpusEncoder::setBitRate(int bitRate)
{ {
opus_encoder_ctl(opusEncoder, OPUS_SET_BITRATE(bitRate)); opus_encoder_ctl(opusEncoder, OPUS_SET_BITRATE(bitRate));
} }
QByteArray COpusEncoder::encode(const QVector<qint16> pcmSamples, int samplesLength, int *encodedLength) QByteArray COpusEncoder::encode(const QVector<qint16> pcmSamples, int samplesLength, int *encodedLength)
{ {
QByteArray encoded(maxDataBytes, 0); QByteArray encoded(maxDataBytes, 0);
int length = opus_encode(opusEncoder, reinterpret_cast<const opus_int16 *>(pcmSamples.data()), samplesLength, reinterpret_cast<unsigned char*>(encoded.data()), maxDataBytes); int length = opus_encode(opusEncoder, reinterpret_cast<const opus_int16 *>(pcmSamples.data()), samplesLength, reinterpret_cast<unsigned char *>(encoded.data()), maxDataBytes);
*encodedLength = length; *encodedLength = length;
encoded.truncate(length); encoded.truncate(length);
return encoded; return encoded;
} }
} // ns
} // ns

View File

@@ -1,5 +1,15 @@
#ifndef OPUSENCODER_H /* Copyright (C) 2019
#define OPUSENCODER_H * swift project Community / Contributors
*
* This file is part of swift project. It is subject to the license terms in the LICENSE file found in the top-level
* directory of this distribution. No part of swift project, including this file, may be copied, modified, propagated,
* or distributed except according to the terms contained in the LICENSE file.
*/
//! \file
#ifndef BLACKSOUND_OPUSENCODER_H
#define BLACKSOUND_OPUSENCODER_H
#include "blacksound/blacksoundexport.h" #include "blacksound/blacksoundexport.h"
#include "opus/opus.h" #include "opus/opus.h"
@@ -7,10 +17,18 @@
#include <QByteArray> #include <QByteArray>
#include <QVector> #include <QVector>
class BLACKSOUND_EXPORT COpusEncoder namespace BlackSound
{ {
public: namespace Codecs
{
//! OPUS encoder
class BLACKSOUND_EXPORT COpusEncoder
{
public:
//! Ctor
COpusEncoder(int sampleRate, int channels, int application = OPUS_APPLICATION_VOIP); COpusEncoder(int sampleRate, int channels, int application = OPUS_APPLICATION_VOIP);
//! Dtor
~COpusEncoder(); ~COpusEncoder();
void setBitRate(int bitRate); void setBitRate(int bitRate);
@@ -23,12 +41,14 @@ public:
QByteArray encode(const QVector<qint16> pcmSamples, int samplesLength, int *encodedLength); QByteArray encode(const QVector<qint16> pcmSamples, int samplesLength, int *encodedLength);
private: private:
OpusEncoder *opusEncoder; OpusEncoder *opusEncoder;
int m_sampleRate; int m_sampleRate;
int m_channels; int m_channels;
static constexpr int maxDataBytes = 4000; static constexpr int maxDataBytes = 4000;
}; };
} // ns
} // ns
#endif // guard #endif // guard