Vorbis Encoder (Run)

This article explains how to use TTranscoder::run to encode a WAV file to a Vorbis OGG file.

The code snippets in this article are from the enc_vorbis_file macOS sample.

Linux and Windows samples are also available:

Source Audio

For source we use the equinox-48KHz.wav file from the AVBlocks Assets repository. After downloading and unzipping you will find equinox-48KHz.wav in the aud subdirectory.

Code

This code takes a WAV file and encodes it to Vorbis audio in an OGG container.

Initialize AVBlocks

The first step in any AVBlocks application is to initialize the library. This must be done before using any other AVBlocks functionality. The TLibrary object shuts the library down and cleans up resources when it goes out of scope at the end of the program.

int main(int argc, char* argv[])
{
    Options opt;
    switch(prepareOptions(opt, argc, argv))
    {
        case Command: return 0;
        case Error: return 1;
        case Parsed: break;
    }

    TLibrary library;
    return encode(opt) ? 0 : 1;
}

Configure Input Socket

The input socket points directly to the WAV input file.

TMediaSocket()
    .file(opt.inputFile)

Configure Output Socket

The output socket writes an OGG container with a Vorbis audio stream.

TMediaSocket()
    .file(opt.outputFile)
    .streamType(StreamType::OGG)
    .addPin(
        TMediaPin()
            .streamInfo(TAudioStreamInfo()
                .streamType(StreamType::Vorbis)
            )
    )

Configure Transcoder and Encode

After creating the input and output sockets, the sample creates a transcoder, enables demo mode, adds the sockets, removes any existing output file, opens the transcoder, runs the encode, and closes it.

bool encode(const Options& opt)
{
    try {
        deleteFile(opt.outputFile.c_str());

        TTranscoder()
            .allowDemoMode(true)
            .addInput(
                TMediaSocket()
                    .file(opt.inputFile)
            )
            .addOutput(
                TMediaSocket()
                    .file(opt.outputFile)
                    .streamType(StreamType::OGG)
                    .addPin(
                        TMediaPin()
                            .streamInfo(TAudioStreamInfo()
                                .streamType(StreamType::Vorbis)
                            )
                    )
            )
            .open()
            .run()
            .close();

        std::println("Output: {}", opt.outputFile);
        return true;

    } catch (const TAVBlocksException& ex) {
        std::println(stderr, "AVBlocks error: {}", ex.what());
        return false;
    } catch (const std::exception& ex) {
        std::println(stderr, "Error: {}", ex.what());
        return false;
    }
}

Complete C++ Code

Here’s the complete working example from enc_vorbis_file.cpp:

#include <primo/avblocks/avb++.h>

#include <print>

#include "options.h"
#include "util.h"

using namespace primo::codecs;
using namespace primo::avblocks::modern;

bool encode(const Options& opt)
{
    try {
        deleteFile(opt.outputFile.c_str());

        TTranscoder()
            .allowDemoMode(true)
            .addInput(
                TMediaSocket()
                    .file(opt.inputFile)
            )
            .addOutput(
                TMediaSocket()
                    .file(opt.outputFile)
                    .streamType(StreamType::OGG)
                    .addPin(
                        TMediaPin()
                            .streamInfo(TAudioStreamInfo()
                                .streamType(StreamType::Vorbis)
                            )
                    )
            )
            .open()
            .run()
            .close();

        std::println("Output: {}", opt.outputFile);
        return true;

    } catch (const TAVBlocksException& ex) {
        std::println(stderr, "AVBlocks error: {}", ex.what());
        return false;
    } catch (const std::exception& ex) {
        std::println(stderr, "Error: {}", ex.what());
        return false;
    }
}

int main(int argc, char* argv[])
{
    Options opt;
    switch(prepareOptions(opt, argc, argv))
    {
        case Command: return 0;
        case Error: return 1;
        case Parsed: break;
    }

    TLibrary library;
    return encode(opt) ? 0 : 1;
}

How to Run

See the build instructions for macOS and the enc_vorbis_file example for details.

Command Line

enc_vorbis_file --input <wav file> --output <ogg file>

Examples

List options:

./bin/x64/enc_vorbis_file --help
Usage: enc_vorbis_file --input <wav file> --output <ogg file>
  -h,    --help
  -i,    --input    input WAV file
  -o,    --output   output OGG file

Encode the input file ./assets/aud/equinox-48KHz.wav into output file ./output/enc_vorbis_file/equinox-48KHz.ogg:

mkdir -p ./output/enc_vorbis_file

./bin/x64/enc_vorbis_file \
    --input ./assets/aud/equinox-48KHz.wav \
    --output ./output/enc_vorbis_file/equinox-48KHz.ogg