Vorbis Decoder (Run)¶
This article explains how to use TTranscoder::run to decode a Vorbis OGG file to a WAV file.
The code snippets in this article are from the dec_vorbis_file macOS sample.
Linux and Windows samples are also available:
Source Audio¶
For source we use the Hydrate-Kenny_Beltrey.ogg file from the AVBlocks Assets repository. After downloading and unzipping you will find Hydrate-Kenny_Beltrey.ogg in the aud subdirectory.
Code¶
This code takes a Vorbis OGG file and decodes it to uncompressed WAV output.
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 decode(opt) ? 0 : 1;
}
Configure Input Socket¶
The input socket points directly to the Vorbis OGG input file.
// Create input socket from file.
TMediaSocket inputSocket = TMediaSocket().file(opt.inputFile);
Configure Output Socket¶
The output socket writes a WAV file. The sample configures the output socket as StreamType::WAVE and uses an LPCM audio stream pin.
// Create output socket with WAV format and LPCM audio
TMediaSocket outputSocket = TMediaSocket()
.file(opt.outputFile)
.streamType(StreamType::WAVE)
.addPin(TMediaPin()
.streamInfo(TAudioStreamInfo()
.streamType(StreamType::LPCM)));
Configure Transcoder and Decode¶
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 decode, and closes it.
bool decode(const Options& opt)
{
try {
// Delete output file if it exists
deleteFile(opt.outputFile.c_str());
// Create input socket from file.
TMediaSocket inputSocket = TMediaSocket().file(opt.inputFile);
// Create output socket with WAV format and LPCM audio
TMediaSocket outputSocket = TMediaSocket()
.file(opt.outputFile)
.streamType(StreamType::WAVE)
.addPin(TMediaPin()
.streamInfo(TAudioStreamInfo()
.streamType(StreamType::LPCM)));
// Create and run transcoder
TTranscoder()
.allowDemoMode(true)
.addInput(inputSocket)
.addOutput(outputSocket)
.open()
.run()
.close();
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 dec_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 decode(const Options& opt)
{
try {
// Delete output file if it exists
deleteFile(opt.outputFile.c_str());
// Create input socket from file.
TMediaSocket inputSocket = TMediaSocket().file(opt.inputFile);
// Create output socket with WAV format and LPCM audio
TMediaSocket outputSocket = TMediaSocket()
.file(opt.outputFile)
.streamType(StreamType::WAVE)
.addPin(TMediaPin()
.streamInfo(TAudioStreamInfo()
.streamType(StreamType::LPCM)));
// Create and run transcoder
TTranscoder()
.allowDemoMode(true)
.addInput(inputSocket)
.addOutput(outputSocket)
.open()
.run()
.close();
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 decode(opt) ? 0 : 1;
}
How to Run¶
See the build instructions for macOS and the dec_vorbis_file example for details.
Command Line¶
./dec_vorbis_file --input <ogg file> --output <wav file>
Examples¶
List options:
./bin/x64/dec_vorbis_file --help
dec_vorbis_file --input <ogg file> --output <wav file>
-h, --help
-i, --input input OGG file
-o, --output output WAV file
The following example decodes input file ./assets/aud/Hydrate-Kenny_Beltrey.ogg into output file Hydrate-Kenny_Beltrey.wav:
mkdir -p ./output/dec_vorbis_file
./bin/x64/dec_vorbis_file \
--input ./assets/aud/Hydrate-Kenny_Beltrey.ogg \
--output ./output/dec_vorbis_file/Hydrate-Kenny_Beltrey.wav