How to record audio with Superpowered SDK on Android Q?

570 views Asked by At

I want to record audio with Superpowered SDK on my rooted Android Q device.

As Google has put some restrictions on recording Audio on Android Q so I am trying to port Superpowered SDK Android Example into a Linux executable that I can run on system space bypassing the Android Apps restriction. Please let me know if I am wrong at this point.

For this I have created a simple project with the main class as the following:

#include <string>
#include <OpenSource/SuperpoweredAndroidAudioIO.h>
#include <Superpowered.h>
#include <SuperpoweredSimple.h>
#include <SuperpoweredRecorder.h>
#include <unistd.h>
#include <iostream>
#include <chrono>
#include <thread>

static SuperpoweredAndroidAudioIO *audioIO;
static Superpowered::Recorder *recorder;

// This is called periodically by the audio I/O.
static bool audioProcessing(
    void *clientdata,   // custom pointer
    short int *audio,   // buffer of interleaved samples
    int numberOfFrames, // number of frames to process
    int samplerate      // current sample rate in Hz
)
{
    std::cout << "------- Processing Audio...numberOfFrames:" << numberOfFrames << " audio:" << *audio << std::endl;
    float floatBuffer[numberOfFrames * 2];
    Superpowered::ShortIntToFloat(audio, floatBuffer, (unsigned int)numberOfFrames);
    recorder->recordInterleaved(floatBuffer, (unsigned int)numberOfFrames);
    return false;
}

// StartAudio - Start audio engine.
void startAudio(
    unsigned int samplerate,
    int buffersize,
    const char *temp,
    const char *dest)
{
    Superpowered::Initialize(
        "ExampleLicenseKey-WillExpire-OnNextUpdate",
        false, // enableAudioAnalysis (using SuperpoweredAnalyzer, SuperpoweredLiveAnalyzer, SuperpoweredWaveform or SuperpoweredBandpassFilterbank)
        false, // enableFFTAndFrequencyDomain (using SuperpoweredFrequencyDomain, SuperpoweredFFTComplex, SuperpoweredFFTReal or SuperpoweredPolarFFT)
        false, // enableAudioTimeStretching (using SuperpoweredTimeStretching)
        false, // enableAudioEffects (using any SuperpoweredFX class)
        false, // enableAudioPlayerAndDecoder (using SuperpoweredAdvancedAudioPlayer or SuperpoweredDecoder)
        false, // enableCryptographics (using Superpowered::RSAPublicKey, Superpowered::RSAPrivateKey, Superpowered::hasher or Superpowered::AES)
        false  // enableNetworking (using Superpowered::httpRequest)
    );

    // Initialize the recorder with a temporary file path.
    recorder = new Superpowered::Recorder(temp);
    // Start a new recording.
    recorder->prepare(
        dest,       // destination path
        samplerate, // sample rate in Hz
        true,       // apply fade in/fade out
        1           // minimum length of the recording in seconds
    );

    std::cout << "Initializing audioIO..\n";
    // Initialize audio engine with audio callback function.
    audioIO = new SuperpoweredAndroidAudioIO(
        samplerate,      // native sampe rate
        buffersize,      // native buffer size
        true,            // enableInput
        false,           // enableOutput
        audioProcessing, // process callback function
        nullptr          // clientData
    );
}

// StopAudio - Stop audio engine and free audio buffer.
void stopRecording()
{

    std::this_thread::sleep_for(std::chrono::seconds(60));

    recorder->stop();
    std::cout << "Deleting IO\n";
    delete audioIO;

    // Wait until the recorder finished writing everything to disk.
    // It's better to do this asynchronously, but we're just blocking (sleeping) now.
    std::cout << "Finishing..\n";
    while (!recorder->isFinished())
        usleep(100000);

    //    __android_log_print(ANDROID_LOG_DEBUG, "Recorder", "Finished recording.");
    std::cout << "Deleting Recorder\n";
    delete recorder;
}

// onBackground - Put audio processing to sleep if no audio is playing.
void onBackground()
{
    audioIO->onBackground();
}

// onForeground - Resume audio processing.
void onForeground()
{
    audioIO->onForeground();
}

int main(int, char **)
{
    std::cout << "Hello, AudioRecorder!\n";

    unsigned int samplerate = 48000;
    int buffersize = 192;
    const char *temp = "/system/recording/temp.wav";
    const char *dest = "/system/recording/final.wav";

    std::cout << "Starting recording::"
              << "\n\tsamplerate:" << samplerate
              << "\n\tbuffersize:" << buffersize
              << "\n\ttemp:" << temp
              << "\n\tdest:" << dest
              << std::endl;
    startAudio(samplerate, buffersize, temp, dest);
    onForeground();

    std::cout << "Starting thread to stop recording...\n";
    std::thread stopThread(&stopRecording); // thread starts running

    std::cout << "Stoping recording...\n";
    stopThread.join(); // main thread waits for the thread t to finish

    std::cout << "Returning from main\n";
    return 0;
}

I am trying to record a 1 minute audio with the above code. But I can't get any call to audioProcessing function. When I run the above code then I get the following output:

OnePlus7Pro:/system # ./xbin/audio_recorder
Hello, AudioRecorder!
Starting recording::
        samplerate:48000
        buffersize:192
        temp:/system/recording/temp.wav
        dest:/system/recording/final.wav
Initializing audioIO..
Starting thread to stop recording...
Stoping recording...
Deleting IO
Finishing..
^C
130|OnePlus7Pro:/system # cd recording 
OnePlus7Pro:/system/recording # ls -l
total 0
-rw-r--r-- 1 root root 0 2020-01-08 12:34 temp.wav
-rw-r--r-- 1 root root 0 2020-01-08 12:34 temp.wav.txt

After "Finishing.." it doesn't print "Deleting Recorder" so there is some issue in the code but I am not getting it.

I am an Android Developer and new to c++. Is there anyone who can guide me?

0

There are 0 answers