opussum/decoder

Source   Edit  

The decoder is used to decode opus frames into raw PCM bytes

Types

OpusDecoderRaw = object
  
The C struct of OpusDecoder. It is recommended to use OpusDecoder procs instead since they handle memory Source   Edit  

Procs

proc createDecoder(sampleRate: int32; channels: range[1 .. 2]; frameSize: int): OpusDecoder {.
    ...raises: [], tags: [], forbids: [].}
Creates a decoder. This is recommend over opusCreateEncoder since this has more helper procs and you don't need to manage its memory Source   Edit  
proc decode(decoder: OpusDecoder; encoded: OpusFrame;
            errorCorrection: bool = false): PCMData {....raises: [OpusError],
    tags: [], forbids: [].}
Decodes an opus frame

Example:

import std/streams
import opussum
let file = newFileStream("tests/test.raw")
let
  enc = createEncoder(48000, 2, 960, Voip)
  dec = createDecoder(48000, 2, 960)
while not file.atEnd:
  let
    pcmBytes = file.readStr(
      enc.frameSize * enc.channels * 2 # We want to encode two channels worth of frame data
    ).toPCMData(enc)
    encodedData = enc.encode(pcmBytes) # Encode PCM to opus frame
    decodedData = dec.decode(encodedData) # Decode opus frame
Source   Edit  
proc decode(st: ptr OpusDecoderRaw; data: ptr uint8; len: opusInt32;
            pcm: ptr opusInt16; frame_size, decodeFec: cint): cint {.
    importc: "opus_decode", cdecl, ...raises: [], tags: [], forbids: [].}
Decodes an opus packet
  • st: Decoder state
  • data: Opus encoded packet
  • len: Length of data
  • pcm: Where to store PCM bytes (length is frameSize * channels)
  • decodeFec: flag (0 or 1) to request that any in-band forward error correction data be decoded. If no such data is available, the frame is decoded as if it was lost
Source   Edit  
proc decodeFloat(st: ptr OpusDecoderRaw; data: ptr uint8; len: opusInt32;
                 pcm: ptr cfloat; frame_size, decodeFec: cint): cint {.
    importc: "opus_decode_float", cdecl, ...raises: [], tags: [], forbids: [].}
Decodes an opus packet with floating point input. See decode for details about parameters Source   Edit  
proc destroy(str: ptr OpusDecoderRaw) {.importc: "opus_decoder_destroy", cdecl,
                                        ...raises: [], tags: [], forbids: [].}
Frees an OpusEncoderRaw allocated by opusCreateDecoder Source   Edit  
proc getBandwidth(data: ptr uint8): cint {.importc: "opus_packet_get_bandwidth",
    cdecl, ...raises: [], tags: [], forbids: [].}
Gets the bandwidth of an Opus packet. Source   Edit  
proc getDecoderSize(channels: cint): cint {.importc: "opus_decoder_get_size",
    cdecl, ...raises: [], tags: [], forbids: [].}
Gets the size of an OpusDecoderRaw structu
  • channels: Number of channels. This must be 1 or 2
Source   Edit  
proc getNumChannels(data: ptr uint8): cint {.
    importc: "opus_packet_get_nb_channels", cdecl, ...raises: [], tags: [],
    forbids: [].}
Gets the number of channels from an Opus packet Source   Edit  
proc getNumFrames(packet: ptr uint8; len: opusInt32): cint {.
    importc: "opus_packet_get_nb_frames", cdecl, ...raises: [], tags: [],
    forbids: [].}
Gets the number of frames in an Opus packet Source   Edit  
proc getNumSamples(dec: ptr OpusDecoderRaw; packet: ptr uint8; len: opusInt32): cint {.
    importc: "opus_decoder_get_nb_packet", cdecl, ...raises: [], tags: [],
    forbids: [].}
Gets the number of samples of an Opus packet Source   Edit  
proc getNumSamples(decoder: OpusDecoder; packet: seq[uint8]): int {....raises: [],
    tags: [], forbids: [].}
Gets the number of samples of an Opus packet Source   Edit  
proc getNumSamples(packet: ptr uint8; len, fs: opusInt32): cint {.
    importc: "opus_packet_get_nb_samples", cdecl, ...raises: [], tags: [],
    forbids: [].}
Gets the number of samples of an Opus packet Source   Edit  
proc getSamplesPerFrame(data: ptr uint8; fs: opusInt32): cint {.
    importc: "opus_packet_get_samples_per_frame", cdecl, ...raises: [], tags: [],
    forbids: [].}
Gets the number of samples per frame from an Opus packet Source   Edit  
proc opusCreateDecoder(fs: opusInt32; channels: cint; error: ptr cint): ptr OpusDecoderRaw {.
    importc: "opus_decoder_create", cdecl, ...raises: [], tags: [], forbids: [].}
Allocates and initialises a decoder state
  • fs: Sample rate to decode at (Hz). This must be one of 8000, 12000, 16000, 24000, or 48000
  • channels: Number of channels (1 or 2) to decode
  • error Where to store error
Source   Edit  
proc pcmSoftClip(pcm: ptr cfloat; frameSize, channels: cint;
                 softclipMem: ptr float) {.importc: "opus_pcm_soft_clip", cdecl,
    ...raises: [], tags: [], forbids: [].}

Applies soft-clipping to bring a float signal within the -1,1 range.

If the signal is already in that range, nothing is done. If there are values outside of -1,1, then the signal is clipped as smoothly as possible to both fit in the range and avoid creating excessive distortion in the process.

Source   Edit  
proc performCTL(st: ptr OpusDecoderRaw; request: cint): cint {.
    importc: "opus_decoder_ctl", varargs, cdecl, ...raises: [], tags: [],
    forbids: [].}
Performs a CTL code. Returns error code Source   Edit  
proc performCTL(st: ptr OpusDecoderRaw; request: cint; param: ptr cint): cint {.
    importc: "opus_decoder_ctl", cdecl, ...raises: [], tags: [], forbids: [].}
Source   Edit