YobeSDK  0.2.0
BioListener_demo.cpp

This is an example of how to use the Yobe BioListener.

#include "util/demo_utils.hpp"
#include <fstream>
#include <iostream>
#include <vector>
constexpr auto ENV_VAR_LICENSE = "YOBE_LICENSE";
std::vector<double> YobeProcessing(const std::string& license, std::vector<double> input_buffer);
std::ofstream log_stream;
int main(int argc, char* argv[]) {
if (argc != 2) {
std::cout << "cpp demo requires a .wav file as input.\n";
} else {
// Just printing out the setting the Yobe engine expects
std::cout << "Just checking to see if the Yobe parameters match the audio file.\n";
std::cout << "Expected sampling rate: " << Yobe::Info::SamplingRate() << '\n';
std::cout << "Expected buffer size in seconds: " << Yobe::Info::AudioBufferTime() << '\n';
std::cout << "Number expected input channels: " << Yobe::Info::InputChannels() << '\n';
std::cout << "Number expected output channels: " << Yobe::Info::OutputChannels() << "\n\n";
const std::string file_path(argv[1]);
// Preparing input buffer by reading the audio file
const auto input_buffer = DemoUtil::ReadAudioFile(file_path);
std::cout << '\n';
// All the Yobe processing happens in this function
const auto processed_audio = YobeProcessing(std::getenv(ENV_VAR_LICENSE), input_buffer);
// Writing the processed data to a .wav file
DemoUtil::WriteAudioFile(file_path, processed_audio);
}
return 0;
}
std::vector<double> YobeProcessing(const std::string& license, std::vector<double> input_buffer) {
// Create a new BioListener instance.
auto bio_listener = Yobe::Create::NewBioListener();
// Set up a logging callback.
log_stream.open("yobe_bio_listener_demo.log");
Yobe::Info::RegisterCallback([](const char* mess) { log_stream << mess << '\n'; });
// Initialize the Yobe engine with the provided license.
auto init_status = bio_listener->Init(license.c_str());
if (init_status != Yobe::Status::YOBE_OK) {
std::cout << "YOBE_Init returned: " << Yobe::Info::StdError(init_status) << '\n';
}
// Determine the size of the input buffer required for YOBE_ProcessBuffer.
const auto input_size = Yobe::Info::InputBufferSize();
// Prepare an output buffer for collecting the output from the Yobe engine.
std::vector<double> output_buffer;
// Determine the size of the output buffer required for YOBE_ProcessBuffer.
const auto output_size = Yobe::Info::OutputBufferSize();
// Pre-allocate a buffer that will be returned with processed data in it.
std::vector<double> scratch_buffer(output_size);
uint32_t out_buffer_size = 0;
const auto total_input_samples = input_buffer.size();
std::cout << "Yobe engine has started processing.\n";
// Process the audio buffer one chunk at a time.
for (size_t input_index = 0; input_index < total_input_samples; input_index += input_size) {
// If the last buffer is too small, pad it with zeros to avoid losing information.
if (input_index + input_size > total_input_samples) {
std::vector<double> pad_buffer(input_index + input_size - total_input_samples, 0.0);
input_buffer.insert(input_buffer.end(), pad_buffer.begin(), pad_buffer.end());
}
// Reset the output buffer size.
out_buffer_size = output_size;
// Process the current chunk of audio.
status = bio_listener->ProcessBuffer(&input_buffer[input_index], scratch_buffer.data(), input_size,
&out_buffer_size);
log_stream << "Yobe::ProcessBuffer: " << Yobe::Info::StdError(status) << "\n";
// Check the status to ensure that the audio was processed.
std::cout << "ProcessBuffer returned: " << Yobe::Info::StdError(status) << '\n';
} else if (out_buffer_size != 0) {
// Collect the processed audio data into the output buffer.
output_buffer.insert(output_buffer.end(), scratch_buffer.begin(), scratch_buffer.end());
}
}
// Clean up and deinitialize the Yobe engine.
auto deinit_status = bio_listener->Deinit();
if (deinit_status != Yobe::Status::YOBE_STOPPED) {
std::cout << "There was an error when deinitializing the Yobe engine.\n";
}
std::cout << "Yobe engine has finished processing.\n";
// Close the log stream.
log_stream.close();
return output_buffer;
}
YOBE_SDK_API std::shared_ptr< BioListener > NewBioListener()
Creates a new instance of BioListener.
YOBE_SDK_API int32_t InputChannels()
Returns the number of input channels required for processing.
YOBE_SDK_API double AudioBufferTime()
Returns the processing audio buffer length in seconds.
YOBE_SDK_API void RegisterCallback(std::function< void(const char *)> log_callback)
Registers a callback function to receive Yobe logging information.
YOBE_SDK_API uint32_t OutputBufferSize()
Returns the output buffer size in samples.
YOBE_SDK_API uint32_t InputBufferSize()
Returns the input buffer size in samples.
YOBE_SDK_API const char * StdError(Status status)
Translates a Yobe Status code into a more readable string.
YOBE_SDK_API uint32_t SamplingRate(bool output_sampling_rate=true)
Returns the expected sampling rate of the input/output buffers.
YOBE_SDK_API int32_t OutputChannels()
Returns the number of processing output channels.
@ YOBE_STOPPED
This means that the engine successfully stopped.
@ YOBE_UNKNOWN
An unknown error has occurred.
@ YOBE_OK
The function executed successfully.
@ NEEDS_MORE_DATA
The algorithm needs more data before it can start processing the audio.