swift
opusdecoder.cpp
1 // SPDX-FileCopyrightText: Copyright (C) 2019 swift Project Community / Contributors
2 // SPDX-License-Identifier: GPL-3.0-or-later OR LicenseRef-swift-pilot-client-1
3 
4 #include "opusdecoder.h"
5 
6 namespace swift::sound::codecs
7 {
8  COpusDecoder::COpusDecoder(int sampleRate, int channels) : m_channels(channels)
9  {
10  int error;
11  m_opusDecoder = opus_decoder_create(sampleRate, channels, &error);
12  }
13 
14  COpusDecoder::~COpusDecoder() { opus_decoder_destroy(m_opusDecoder); }
15 
16  int COpusDecoder::frameCount(int bufferSize)
17  {
18  // seems like bitrate should be required
19  int bitrate = 16;
20  int bytesPerSample = (bitrate / 8) * m_channels;
21  return bufferSize / bytesPerSample;
22  }
23 
24  QVector<qint16> COpusDecoder::decode(const QByteArray &opusData, int dataLength, int *decodedLength)
25  {
26  QVector<qint16> decoded(MaxDataBytes, 0);
27  int count = frameCount(MaxDataBytes);
28 
29  if (!opusData.isEmpty())
30  {
31  *decodedLength = opus_decode(m_opusDecoder, reinterpret_cast<const unsigned char *>(opusData.data()),
32  dataLength, decoded.data(), count, 0);
33  }
34  decoded.resize(*decodedLength);
35  return decoded;
36  }
37 
39  {
40  if (!m_opusDecoder) { return; }
41  opus_decoder_ctl(m_opusDecoder, OPUS_RESET_STATE);
42  }
43 } // namespace swift::sound::codecs
int frameCount(int bufferSize)
Frame count.
Definition: opusdecoder.cpp:16
COpusDecoder(int sampleRate, int channels)
Ctor.
Definition: opusdecoder.cpp:8
QVector< qint16 > decode(const QByteArray &opusData, int dataLength, int *decodedLength)
Decode.
Definition: opusdecoder.cpp:24