SDRAngel  4.11.5
Developer docs for <a href="https://github.com/f4exb/sdrangel">SDRangel<\a>, an Open Source Qt5 / OpenGL 3.0+ SDR and signal analyzer frontend to various hardware.
audioopus.cpp
Go to the documentation of this file.
1 // Copyright (C) 2019 F4EXB //
3 // written by Edouard Griffiths //
4 // //
5 // In this version we will use a fixed constant bit rate of 64kbit/s. //
6 // With a frame time of 20ms the encoder output size is always 160 bytes. //
7 // //
8 // This program is free software; you can redistribute it and/or modify //
9 // it under the terms of the GNU General Public License as published by //
10 // the Free Software Foundation as version 3 of the License, or //
11 // (at your option) any later version. //
12 // //
13 // This program is distributed in the hope that it will be useful, //
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of //
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
16 // GNU General Public License V3 for more details. //
17 // //
18 // You should have received a copy of the GNU General Public License //
19 // along with this program. If not, see <http://www.gnu.org/licenses/>. //
21 
22 #include "opus/opus.h"
23 #include <QDebug>
24 #include <QMutexLocker>
25 
26 #include "audioopus.h"
27 
29  m_encoderState(0),
30  m_encoderOK(false),
31  m_mutex(QMutex::Recursive)
32 {
33  qDebug("AudioOpus::AudioOpus: libopus version %s", opus_get_version_string());
34 }
35 
37 {
38  if (m_encoderState) {
39  opus_encoder_destroy(m_encoderState);
40  }
41 }
42 
43 void AudioOpus::setEncoder(int32_t fs, int nChannels)
44 {
45  int error;
46  bool newInstance = true;
47  QMutexLocker mutexLocker(&m_mutex);
48 
49  if (m_encoderState) {
50  opus_encoder_destroy(m_encoderState);
51  }
52 
53  m_encoderState = opus_encoder_create(fs, nChannels, OPUS_APPLICATION_AUDIO, &error);
54 
55  if (error != OPUS_OK)
56  {
57  qWarning("AudioOpus::setEncoder: error: %s", opus_strerror(error));
58  m_encoderOK = false;
59  return;
60  }
61  else
62  {
63  qDebug("AudioOpus::setEncoder: fs: %d, nChannels: %d", fs, nChannels);
64  m_encoderOK = true;
65  }
66 
67  error = opus_encoder_ctl(m_encoderState, OPUS_SET_BITRATE(m_bitrate));
68 
69  if (error != OPUS_OK)
70  {
71  qWarning("AudioOpus::setEncoder: set bitrate error: %s", opus_strerror(error));
72  m_encoderOK = false;
73  return;
74  }
75 
76  error = opus_encoder_ctl(m_encoderState, OPUS_SET_VBR(0)); // force constant bit rate
77 
78  if (error != OPUS_OK)
79  {
80  qWarning("AudioOpus::setEncoder: set constant bitrate error: %s", opus_strerror(error));
81  m_encoderOK = false;
82  return;
83  }
84 }
85 
86 int AudioOpus::encode(int frameSize, int16_t *in, uint8_t *out)
87 {
88  QMutexLocker mutexLocker(&m_mutex);
89 
90  if (!m_encoderOK)
91  {
92  qWarning("AudioOpus::encode: encoder not initialized");
93  return 0;
94  }
95 
96  int nbBytes = opus_encode(m_encoderState, in, frameSize, out, m_maxPacketSize);
97 
98  if (nbBytes < 0)
99  {
100  qWarning("AudioOpus::encode failed: %s", opus_strerror(nbBytes));
101  return 0;
102  }
103  else
104  {
105  return nbBytes;
106  }
107 }
short int16_t
Definition: rtptypes_win.h:43
static const int m_bitrate
Fixed 64kb/s bitrate (8kB/s)
Definition: audioopus.h:37
static const int m_maxPacketSize
Definition: audioopus.h:38
int encode(int frameSize, int16_t *in, uint8_t *out)
Definition: audioopus.cpp:86
bool m_encoderOK
Definition: audioopus.h:42
unsigned char uint8_t
Definition: rtptypes_win.h:42
QMutex m_mutex
Definition: audioopus.h:43
int int32_t
Definition: rtptypes_win.h:45
void setEncoder(int32_t fs, int nChannels)
Definition: audioopus.cpp:43
OpusEncoder * m_encoderState
Definition: audioopus.h:41