Upsample Audio

This article explains how to upsample an audio clip from 44.1 Khz to 48 KHz.

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

Sample Code

This code takes an MP3 file with 44.1 KHz audio, and converts it to an MP3 file with 48 KHz audio using polyphase resampling method. The input and output file paths are hardcoded in the source code, no command line parsing is used.

The snippets in this section are from the simple_audio_upsample macOS sample.

Linux and Windows samples are also available:

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 Library::initialize() method sets up the internal state and loads necessary codecs. Always remember to call Library::shutdown() at the end of your program to properly clean up resources and release any allocated memory.

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_48Khz.mp3");

    // ... upsampling code ...

    Library::shutdown();
    return 0;
}

Configure Transcoder and Upsample

We probe the input file with a MediaInfo object, and create an input socket from it. The output socket is created by cloning the input socket, so it starts out with an identical configuration, and then we set the output file path on it. The transcoder is the core component that performs the actual upsampling - it takes the 44.1 KHz audio from the input and resamples it to 48 KHz using polyphase resampling for the best upsampling quality.

Probe Input and Create Sockets

First, we probe the input file and create the input socket. Then we clone it to create the output socket and set the output file path.

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())
    );

    // Start with the same output as the input
    auto outputSocket = primo::make_ref(inputSocket->clone());
    outputSocket->setFile(outputFile);
} else {
    std::cerr << "inputInfo->open() failed: "
               << primo::ustring(inputInfo->error()->message())
               << std::endl;
}

Change Sampling Rate

Next, we get the output audio pin and change the sampling rate from 44.1 KHz to 48 KHz.

auto outAudioPin = outputSocket->pins()->at(0);
auto outAudioStream = (AudioStreamInfo*)outAudioPin->streamInfo();

// Set output sampling rate to 48 KHz
outAudioStream->setSampleRate(48000);

Create and Run Transcoder

Finally, we create the transcoder, add the input and output sockets, and run the operation. The setAllowDemoMode(true) call allows the transcoder to work even without a valid license (useful for testing, but not recommended for production).

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;
}

Complete C++ Code

Here’s the complete working example that demonstrates audio upsampling using AVBlocks. The input and output file paths are hardcoded, so no command line parsing is required.

#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_48Khz.mp3");

    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())
        );

        // Start with the same output as the input
        auto outputSocket = primo::make_ref(inputSocket->clone());
        outputSocket->setFile(outputFile);

        // Get the output audio pin
        auto outAudioPin = outputSocket->pins()->at(0);

        // Set output sampling rate to 48 KHz
        auto outAudioStream = (AudioStreamInfo*)outAudioPin->streamInfo();
        outAudioStream->setSampleRate(48000);

        // 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;
        }
    } else {
        std::cerr << "inputInfo->open() failed: "
                   << primo::ustring(inputInfo->error()->message())
                   << std::endl;
    }

    Library::shutdown();
    return 0;
}

How to Run

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

Download the sample audio, the kahvi011_kennybeltrey-hydrate.mp3 song from the Internet Archive (MPEG Audio Layer 3, 44.1 KHz, Joint Stereo):

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

Run the sample from the sample directory (the input/output file paths are relative to the working directory):

../../../bin/x64/simple_audio_upsample

The upsampled output file kahvi011_kennybeltrey-hydrate_48Khz.mp3 will be created in the samples/darwin/simple_audio_upsample directory.