C++ Example

CMakeLists.txt

 1cmake_minimum_required(VERSION 3.26)
 2project(iris-sdk-sample)
 3
 4# use c++ 17
 5set(CMAKE_CXX_STANDARD 17)
 6set(CMAKE_POSITION_INDEPENDENT_CODE ON)
 7
 8# set the path to the iris sdk directory
 9set(IRIS_SDK_PATH ${CMAKE_SOURCE_DIR}/../iris-sdk)
10
11# add the sample app executable
12add_executable(iris-sdk-sample main.cpp)
13
14# link the sample app executable with the iris sdk. To enable wav support link against libsndfile here
15target_link_libraries(iris-sdk-sample PRIVATE iris-sdk)
16
17# let cmake know where to find the iris sdk headers and lib
18target_include_directories(iris-sdk-sample PRIVATE ${IRIS_SDK_PATH}/include)
19target_link_directories(iris-sdk-sample PRIVATE ${IRIS_SDK_PATH}/lib)
20
21# set a define for the path to our sample input and output files
22add_definitions(-DSAMPLE_INPUT_FILE="${CMAKE_SOURCE_DIR}/res/breaking_point_f32_48k.raw")
23add_definitions(-DSAMPLE_OUTPUT_FILE="${CMAKE_SOURCE_DIR}/res/breaking_point_f32_48k_processed.raw")
24
25if (MSVC)
26  # copy the dll to the build directory on windows
27  add_custom_command(TARGET iris-sdk-sample POST_BUILD
28    COMMAND ${CMAKE_COMMAND} -E copy ${IRIS_SDK_PATH}/lib/iris-sdk.dll ${PROJECT_BINARY_DIR}
29  )
30elseif (APPLE)
31  # remove the quarantine attribute that mac os sets on the downloaded sdk dylib if present
32  add_custom_command(TARGET iris-sdk-sample POST_BUILD
33    COMMAND xattr -d com.apple.quarantine ${IRIS_SDK_PATH}/lib/libiris-sdk.dylib || (exit 0)
34  )
35endif ()

main.cpp

 1#include <Iris/Iris.h>
 2
 3#include <iostream>
 4#include <iomanip>
 5
 6#include "util.h"
 7
 8void progressCallback(Iris::ProgressEvent event) {
 9  switch (event.type) {
10  case Iris::ProgressEventType::start:
11    std::cout << "Processing: started" << std::endl;
12    break;
13  case Iris::ProgressEventType::process:
14    std::cout << "\rProcessing: " << std::fixed << std::setprecision(2) << event.percentage << "%";
15    break;
16  case Iris::ProgressEventType::end:
17    std::cout << "\nProcessing: ended" << std::endl;
18    break;
19  default:
20    break;
21  }
22}
23
24int main() {
25  // initialise iris sdk. either pass in the licence and key as strings or let the sdk discover
26  // them from its default data location. See readme.md for default locations
27  // const std::string licence = fileToString("/full/path/to/iris.lic");
28  // const std::string key = fileToString("/full/path/to/iris.key");
29  // auto sdkInfo = Iris::init(licence, key);
30  auto licenceInfo = Iris::init();
31  std::cout << "Initialised iris-sdk " << licenceInfo.version << " for " << licenceInfo.name << std::endl;
32  checkLicenceInfo(licenceInfo);
33
34  // Load some source audio and create somewhere to store the result.
35  // In a real world application this will be an incoming audio stream
36  // adjust sample rate and channels to match your audio
37  int samplerate = 48000;
38  int channels = 1;
39  std::vector<float> inData = rawFloatsFromFile(SAMPLE_INPUT_FILE);
40  // std::vector<float> inData = floatsFromWavFile(SAMPLE_INPUT_FILE, &samplerate, &channels); // load from a file using libsndfile
41
42  // prepare anb output buffer to hold the processed audio
43  std::vector<float> outData(inData.size());
44
45  // Create an audio processing context request
46  Iris::AudioContextRequest contextRequest;
47  contextRequest.processors = {"clarity"}; // set the processor chain to use
48  contextRequest.sampleRate = samplerate; // set the sample rate of the input audio
49  contextRequest.channelCount = channels; // set the channel count of the input audio
50  contextRequest.resamplerMode = Iris::ResamplerMode::hq; // set the resampler quality
51  checkContextRequest(contextRequest);
52
53  // Create an audio processing context using the context request
54  Iris::AudioContextConfig config = Iris::createAudioContext(contextRequest);
55  checkContextConfig(config);
56
57  // Process the entire buffer in one go, accounting for any transport delay
58  // If processing a stream, call iris.sdk.process when each new buffer of stream data arrives instead
59  // In the streaming case, frames should be set to the size of the stream buffer size.
60  // If each buffer is the same size, set fixed_frame_count to true in the context request
61  // If the stream buffer size is known up front, set the buffer size in the context request
62  // streaming pseudocode
63  // while (newStreamDataAvailable) {
64  //  std::vector<float> processInBuffer = get_stream_source()
65  //  processOutBuffer = std::vector<float>(processInBuffer.size(), 0.f);
66  //  Iris::process(config.contextId, processInBuffer.data(), processOutBuffer.data(), processInBuffer.size());
67  //  put_stream_sink(processOutBuffer)
68  std::cout << "Processing " << SAMPLE_INPUT_FILE << std::endl;
69  Iris::processOffline(config.contextId, inData.data(), outData.data(), inData.size(), &progressCallback);
70
71  // save the processed output alongside the input file
72  std::cout << "Saving " << SAMPLE_OUTPUT_FILE << std::endl;
73  rawFloatsToFile(SAMPLE_OUTPUT_FILE, outData);
74  // floatsToWavFile(SAMPLE_OUTPUT_FILE, outData, samplerate, channels);
75
76  // release the context
77  std::cout << "Cleaning up..." << std::endl;
78  Iris::releaseAudioContext(config.contextId);
79  Iris::cleanup();
80  return 0;
81}

File format support

This sample demonstrates processing of raw audio files. To convert other formats to float32 input we recommend using libsndfile.

In CMakeLists.txt:

  • add sndfile to the target_link_libraries

  • add the include and lib paths for libsndfile to target_include_directories and target_link_directories

In util.h:

  • uncomment the two wav functions

In main.cpp:

  • switch the call to rawFloatsFromFile to floatsFromWavFile

  • switch the call to rawFloatsToFile to floatsToWavFile

Install libsndfile:

apt install libsndfile1-dev