diff --git a/src/blackcore/afv/audio/callsignsampleprovider.h b/src/blackcore/afv/audio/callsignsampleprovider.h index 029fd3344..a3e6fec9b 100644 --- a/src/blackcore/afv/audio/callsignsampleprovider.h +++ b/src/blackcore/afv/audio/callsignsampleprovider.h @@ -27,7 +27,6 @@ #include #include - namespace BlackCore { namespace Afv @@ -91,7 +90,7 @@ namespace BlackCore BufferedWaveProvider *audioInput; QTimer m_timer; - COpusDecoder m_decoder; + BlackSound::Codecs::COpusDecoder m_decoder; bool m_lastPacketLatch = false; QDateTime m_lastSamplesAddedUtc; bool m_underflow = false; diff --git a/src/blackcore/afv/audio/input.h b/src/blackcore/afv/audio/input.h index 5d4f8e30f..e8b25188a 100644 --- a/src/blackcore/afv/audio/input.h +++ b/src/blackcore/afv/audio/input.h @@ -90,7 +90,7 @@ namespace BlackCore static constexpr qint64 c_frameSize = 960; int m_sampleRate = 0; - COpusEncoder m_encoder; + BlackSound::Codecs::COpusEncoder m_encoder; QScopedPointer m_audioInput; bool m_started = false; diff --git a/src/blacksound/blacksound.pro b/src/blacksound/blacksound.pro index d19cdd45a..c028e4a3f 100644 --- a/src/blacksound/blacksound.pro +++ b/src/blacksound/blacksound.pro @@ -16,16 +16,16 @@ DEPENDPATH += . .. DEFINES += LOG_IN_FILE BUILD_BLACKSOUND_LIB HEADERS += *.h -HEADERS += wav/wavfile.h -HEADERS += dsp/*.h -HEADERS += codecs/*.h -HEADERS += samplesprovider/*.h +HEADERS += $$PWD/wav/wavfile.h +HEADERS += $$PWD/dsp/*.h +HEADERS += $$PWD/codecs/*.h +HEADERS += $$PWD/sampleprovider/*.h SOURCES += *.cpp -SOURCES += wav/wavfile.cpp -SOURCES += dsp/*.cpp -SOURCES += codecs/*.cpp -SOURCES += samplesprovider/*.cpp +SOURCES += $$PWD/wav/wavfile.cpp +SOURCES += $$PWD/dsp/*.cpp +SOURCES += $$PWD/codecs/*.cpp +SOURCES += $$PWD/sampleprovider/*.cpp LIBS *= -lopus diff --git a/src/blacksound/codecs/opusdecoder.cpp b/src/blacksound/codecs/opusdecoder.cpp index 7b61cb325..4215f661b 100644 --- a/src/blacksound/codecs/opusdecoder.cpp +++ b/src/blacksound/codecs/opusdecoder.cpp @@ -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" -COpusDecoder::COpusDecoder(int sampleRate, int channels) : - m_sampleRate(sampleRate), - m_channels(channels) +namespace BlackSound { - int error; - opusDecoder = opus_decoder_create(sampleRate, channels, &error); -} - -COpusDecoder::~COpusDecoder() -{ - opus_decoder_destroy(opusDecoder); -} - -int COpusDecoder::frameCount(int bufferSize) -{ - // seems like bitrate should be required - int bitrate = 16; - int bytesPerSample = (bitrate / 8) * m_channels; - return bufferSize / bytesPerSample; -} - -QVector COpusDecoder::decode(const QByteArray opusData, int dataLength, int *decodedLength) -{ - QVector decoded(maxDataBytes, 0); - int count = frameCount(maxDataBytes); - - if (! opusData.isEmpty()) + namespace Codecs { - *decodedLength = opus_decode(opusDecoder, reinterpret_cast(opusData.data()), dataLength, decoded.data(), count, 0); - } - decoded.resize(*decodedLength); - return decoded; -} + COpusDecoder::COpusDecoder(int sampleRate, int channels) : + m_sampleRate(sampleRate), + m_channels(channels) + { + int error; + opusDecoder = opus_decoder_create(sampleRate, channels, &error); + } -void COpusDecoder::resetState() -{ - opus_decoder_ctl(opusDecoder, OPUS_RESET_STATE); -} + COpusDecoder::~COpusDecoder() + { + opus_decoder_destroy(opusDecoder); + } + + int COpusDecoder::frameCount(int bufferSize) + { + // seems like bitrate should be required + int bitrate = 16; + int bytesPerSample = (bitrate / 8) * m_channels; + return bufferSize / bytesPerSample; + } + + QVector COpusDecoder::decode(const QByteArray opusData, int dataLength, int *decodedLength) + { + QVector decoded(maxDataBytes, 0); + int count = frameCount(maxDataBytes); + + if (! opusData.isEmpty()) + { + *decodedLength = opus_decode(opusDecoder, reinterpret_cast(opusData.data()), dataLength, decoded.data(), count, 0); + } + decoded.resize(*decodedLength); + return decoded; + } + + void COpusDecoder::resetState() + { + opus_decoder_ctl(opusDecoder, OPUS_RESET_STATE); + } + } // ns +} // ns diff --git a/src/blacksound/codecs/opusdecoder.h b/src/blacksound/codecs/opusdecoder.h index 0c6be0626..7f862adbc 100644 --- a/src/blacksound/codecs/opusdecoder.h +++ b/src/blacksound/codecs/opusdecoder.h @@ -1,30 +1,49 @@ -#ifndef OPUSDECODER_H -#define OPUSDECODER_H +/* 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. + */ + +//! \file + +#ifndef BLACKSOUND_OPUSDECODER_H +#define BLACKSOUND_OPUSDECODER_H #include "blacksound/blacksoundexport.h" - #include "opus/opus.h" #include -class BLACKSOUND_EXPORT COpusDecoder +namespace BlackSound { -public: - COpusDecoder(int sampleRate, int channels); - ~COpusDecoder(); + namespace Codecs + { + //! OPUS decoder + class BLACKSOUND_EXPORT COpusDecoder + { + public: + //! Ctor + COpusDecoder(int sampleRate, int channels); - int frameCount(int bufferSize); + //! Dtor + ~COpusDecoder(); - QVector decode(const QByteArray opusData, int dataLength, int *decodedLength); + int frameCount(int bufferSize); - void resetState(); + QVector decode(const QByteArray opusData, int dataLength, int *decodedLength); -private: - OpusDecoder *opusDecoder; - int m_sampleRate; - int m_channels; + void resetState(); - static constexpr int maxDataBytes = 4000; -}; + private: + OpusDecoder *opusDecoder; + int m_sampleRate; + int m_channels; -#endif // OPUSDECODER_H + static constexpr int maxDataBytes = 4000; + }; + } // ns +} // ns + +#endif // guard diff --git a/src/blacksound/codecs/opusencoder.cpp b/src/blacksound/codecs/opusencoder.cpp index ef6a290e0..5efbdfb6f 100644 --- a/src/blacksound/codecs/opusencoder.cpp +++ b/src/blacksound/codecs/opusencoder.cpp @@ -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" -COpusEncoder::COpusEncoder(int sampleRate, int channels, int application) : - m_sampleRate(sampleRate), - m_channels(channels) +namespace BlackSound { - int error; - opusEncoder = opus_encoder_create(sampleRate, channels, application, &error); -} + namespace Codecs + { + COpusEncoder::COpusEncoder(int sampleRate, int channels, int application) : + m_sampleRate(sampleRate), + m_channels(channels) + { + int error; + opusEncoder = opus_encoder_create(sampleRate, channels, application, &error); + } -COpusEncoder::~COpusEncoder() -{ - opus_encoder_destroy(opusEncoder); -} - -void COpusEncoder::setBitRate(int bitRate) -{ - opus_encoder_ctl(opusEncoder, OPUS_SET_BITRATE(bitRate)); -} - -QByteArray COpusEncoder::encode(const QVector pcmSamples, int samplesLength, int *encodedLength) -{ - QByteArray encoded(maxDataBytes, 0); - int length = opus_encode(opusEncoder, reinterpret_cast(pcmSamples.data()), samplesLength, reinterpret_cast(encoded.data()), maxDataBytes); - *encodedLength = length; - encoded.truncate(length); - return encoded; -} + COpusEncoder::~COpusEncoder() + { + opus_encoder_destroy(opusEncoder); + } + void COpusEncoder::setBitRate(int bitRate) + { + opus_encoder_ctl(opusEncoder, OPUS_SET_BITRATE(bitRate)); + } + QByteArray COpusEncoder::encode(const QVector pcmSamples, int samplesLength, int *encodedLength) + { + QByteArray encoded(maxDataBytes, 0); + int length = opus_encode(opusEncoder, reinterpret_cast(pcmSamples.data()), samplesLength, reinterpret_cast(encoded.data()), maxDataBytes); + *encodedLength = length; + encoded.truncate(length); + return encoded; + } + } // ns +} // ns diff --git a/src/blacksound/codecs/opusencoder.h b/src/blacksound/codecs/opusencoder.h index 379a1fd64..f3f67e5e8 100644 --- a/src/blacksound/codecs/opusencoder.h +++ b/src/blacksound/codecs/opusencoder.h @@ -1,5 +1,15 @@ -#ifndef OPUSENCODER_H -#define OPUSENCODER_H +/* 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. + */ + +//! \file + +#ifndef BLACKSOUND_OPUSENCODER_H +#define BLACKSOUND_OPUSENCODER_H #include "blacksound/blacksoundexport.h" #include "opus/opus.h" @@ -7,28 +17,38 @@ #include #include -class BLACKSOUND_EXPORT COpusEncoder +namespace BlackSound { -public: - COpusEncoder(int sampleRate, int channels, int application = OPUS_APPLICATION_VOIP); - ~COpusEncoder(); + namespace Codecs + { + //! OPUS encoder + class BLACKSOUND_EXPORT COpusEncoder + { + public: + //! Ctor + COpusEncoder(int sampleRate, int channels, int application = OPUS_APPLICATION_VOIP); - void setBitRate(int bitRate); + //! Dtor + ~COpusEncoder(); - //! \param frameCount Number of audio samples per frame - //! \returns the size of an audio frame in bytes - int frameByteCount(int frameCount); + void setBitRate(int bitRate); - int frameCount(const QVector pcmSamples); + //! \param frameCount Number of audio samples per frame + //! \returns the size of an audio frame in bytes + int frameByteCount(int frameCount); - QByteArray encode(const QVector pcmSamples, int samplesLength, int *encodedLength); + int frameCount(const QVector pcmSamples); -private: - OpusEncoder *opusEncoder; - int m_sampleRate; - int m_channels; + QByteArray encode(const QVector pcmSamples, int samplesLength, int *encodedLength); - static constexpr int maxDataBytes = 4000; -}; + private: + OpusEncoder *opusEncoder; + int m_sampleRate; + int m_channels; + + static constexpr int maxDataBytes = 4000; + }; + } // ns +} // ns #endif // guard