YobeSDK 0.3.2
 
Loading...
Searching...
No Matches
BioListener_demo.cpp

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

#include "util/demo_utils.hpp"
#include "util/client_license.h"
#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';
std::vector<double> processed_audio;
try {
// All the Yobe processing happens in this function
processed_audio = YobeProcessing(getLicense(ENV_VAR_LICENSE), input_buffer);
} catch (const std::exception& e) {
std::cerr << e.what() << '\n';
return 1;
}
// 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.
#if NEAR_VOICE
auto init_status = bio_listener->Init(license.c_str(), Yobe::MicOrientation::BROAD_SIDE, Yobe::VoiceTarget::NEAR_FIELD);
#else
auto init_status = bio_listener->Init(license.c_str(), Yobe::MicOrientation::BROAD_SIDE, Yobe::VoiceTarget::FAR_FIELD);
#endif
if (init_status != Yobe::Status::YOBE_OK) {
std::cout << "Init returned: " << Yobe::Info::StdError(init_status) << '\n';
throw std::runtime_error("Initialization error");
}
// 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";
throw std::runtime_error("Deinit error");
}
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 uint32_t SamplingRate(bool output_sampling_rate=true)
Returns the expected sampling rate of the input/output buffers.
YOBE_SDK_API const char * StdError(Status status)
Translates a Yobe Status code into a more readable string.
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.
@ BROAD_SIDE
This orientation has the target voice perpendicular to the line connecting the two microphones.
@ NEAR_FIELD
The target voice is near field (less than 2 feet).
@ FAR_FIELD
The target voice is far field (3 feet or more).