Simple Audio Converter

Building a simple audio converter using C++ and the AVBlocks Transcoder.

Source Audio

For an audio source we use the kahvi011_kennybeltrey-hydrate.mp3 file from the Internet Archive. The original audio format is MPEG Audio Layer 3, 44.1 KHz, Joint Stereo, 136 Kbps, Variable Bit Rate

Overview

These snippets are fully functional audio converters. They take kahvi011_kennybeltrey-hydrate.mp3 as input and convert it to AAC, saved as kahvi011_kennybeltrey-hydrate.aac.

macOS

Code

#include <primo/avblocks/avb.h>
#include <primo/platform/reference++.h>
#include <primo/platform/ustring.h>

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

int main(int argc, const char *argv[]) {
    Library::initialize();

    auto inputFile = primo::ustring(L"kahvi011_kennybeltrey-hydrate.mp3");
    auto outputFile = primo::ustring(L"kahvi011_kennybeltrey-hydrate.aac");

    auto inputInfo = primo::make_ref(Library::createMediaInfo());
    inputInfo->inputs()->at(0)->setFile(inputFile);

    if (inputInfo->open()) {
        auto inputSocket = primo::make_ref(
            Library::createMediaSocket(inputInfo.get())
        );

        // Configure the output from the AAC preset
        auto outputSocket = primo::make_ref(
            Library::createMediaSocket(Preset::Audio::Generic::AAC)
        );
        outputSocket->setFile(outputFile);

        // Create Transcoder and configure it with
        // the input and output sockets
        auto transcoder = primo::make_ref(Library::createTranscoder());
        transcoder->inputs()->add(inputSocket.get());
        transcoder->outputs()->add(outputSocket.get());

        // Allow demo mode for the transcoder when
        // using the demo version of the library
        transcoder->setAllowDemoMode(true);

        // Run the transcoder
        if (transcoder->open()) {
            transcoder->run();
            transcoder->close();
        } else {
            std::cerr << "transcoder->open() failed: "
                      << primo::ustring(transcoder->error()->message())
                      << std::endl;
        }
    }

    Library::shutdown();
    return 0;
}

How to run

Follow the steps in Create a Command Line Tool using CMake on macOS, but use the code from this article.

Download the kahvi011_kennybeltrey-hydrate.mp3 song from the Internet Archive and save it in the project directory:

curl \
  -L -o kahvi011_kennybeltrey-hydrate.mp3 \
  https://archive.org/download/kahvi011/kahvi011_kennybeltrey-hydrate.mp3

Run the application. Wait a few seconds for the Transcoder to finish. The converted file kahvi011_kennybeltrey-hydrate.aac will be in the project directory.

Linux

Code

#include <primo/avblocks/avb.h>
#include <primo/platform/reference++.h>
#include <primo/platform/ustring.h>

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

int main(int argc, const char *argv[]) {
    Library::initialize();

    auto inputFile = primo::ustring(L"kahvi011_kennybeltrey-hydrate.mp3");
    auto outputFile = primo::ustring(L"kahvi011_kennybeltrey-hydrate.aac");

    auto inputInfo = primo::make_ref(Library::createMediaInfo());
    inputInfo->inputs()->at(0)->setFile(inputFile);

    if (inputInfo->open()) {
        auto inputSocket = primo::make_ref(
            Library::createMediaSocket(inputInfo.get())
        );

        // Configure the output from the AAC preset
        auto outputSocket = primo::make_ref(
            Library::createMediaSocket(Preset::Audio::Generic::AAC)
        );
        outputSocket->setFile(outputFile);

        // Create Transcoder and configure it with
        // the input and output sockets
        auto transcoder = primo::make_ref(Library::createTranscoder());
        transcoder->inputs()->add(inputSocket.get());
        transcoder->outputs()->add(outputSocket.get());

        // Allow demo mode for the transcoder when
        // using the demo version of the library
        transcoder->setAllowDemoMode(true);

        // Run the transcoder
        if (transcoder->open()) {
            transcoder->run();
            transcoder->close();
        } else {
            std::cerr << "transcoder->open() failed: "
                      << primo::ustring(transcoder->error()->message())
                      << std::endl;
        }
    }

    Library::shutdown();
    return 0;
}

How to run

Follow the steps in Create a Command Line Tool using CMake on Ubuntu, but use the code from this article.

Download the kahvi011_kennybeltrey-hydrate.mp3 song from the Internet Archive and save it in the project directory:

curl \
  -L -o kahvi011_kennybeltrey-hydrate.mp3 \
  https://archive.org/download/kahvi011/kahvi011_kennybeltrey-hydrate.mp3

Run the application. Wait a few seconds for the Transcoder to finish. The converted file kahvi011_kennybeltrey-hydrate.aac will be in the project directory.

Windows

Code

#include <primo/avblocks/avb.h>
#include <primo/platform/reference++.h>
#include <primo/platform/ustring.h>

#include <windows.h>

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

int wmain(int argc, wchar_t* argv[]) {
    // needed for Windows Media Codecs
    CoInitializeEx(nullptr, COINITBASE_MULTITHREADED);

    Library::initialize();

    auto inputFile = primo::ustring(L"kahvi011_kennybeltrey-hydrate.mp3");
    auto outputFile = primo::ustring(L"kahvi011_kennybeltrey-hydrate.aac");

    auto inputInfo = primo::make_ref(Library::createMediaInfo());
    inputInfo->inputs()->at(0)->setFile(inputFile);

    if (inputInfo->open()) {
        auto inputSocket = primo::make_ref(
            Library::createMediaSocket(inputInfo.get())
        );

        // Configure the output from the AAC preset
        auto outputSocket = primo::make_ref(
            Library::createMediaSocket(Preset::Audio::Generic::AAC)
        );
        outputSocket->setFile(outputFile);

        // Create Transcoder and configure it with
        // the input and output sockets
        auto transcoder = primo::make_ref(Library::createTranscoder());
        transcoder->inputs()->add(inputSocket.get());
        transcoder->outputs()->add(outputSocket.get());

        // Allow demo mode for the transcoder when
        // using the demo version of the library
        transcoder->setAllowDemoMode(true);

        // Run the transcoder
        if (transcoder->open()) {
            transcoder->run();
            transcoder->close();
        } else {
            std::wcerr << L"transcoder->open() failed: "
                       << primo::ustring(transcoder->error()->message())
                       << std::endl;
        }
    }

    Library::shutdown();

    CoUninitialize();

    return 0;
}

How to run

Follow the steps in Create a Command Line Tool using CMake on Windows, but use the code from this article.

Download the kahvi011_kennybeltrey-hydrate.mp3 song from the Internet Archive and save it in the project directory:

Invoke-WebRequest `
  -Uri https://archive.org/download/kahvi011/kahvi011_kennybeltrey-hydrate.mp3 `
  -OutFile kahvi011_kennybeltrey-hydrate.mp3

Run the application. Wait a few seconds for the Transcoder to finish. The converted file kahvi011_kennybeltrey-hydrate.aac will be in the project directory.