cancel
Showing results for 
Search instead for 
Did you mean: 

Adding reverb to sound file with Native C++ Oculus Audio SDK

vishalchoudhari
Level 2

Dear Developers,

 

I am trying to generate spatialized sound though a C++ script by making use of the Native Oculus Audio SDK. In the end, I find that my audio is spatialized, but lacks any reverb or reflections. The only reference for devloping the script was found at the end of the file: AudioSDK/Include/OVR_Audio.h. Version of Oculus Audio SDK being used is v25.0.0.

 

I try to simulate a shoebox model with the parameters:

1. Width = 4 (metres) (x-axis) 

2. Height = 3 (metres) (y-axis)

3. Depth = 6 (metres) (z-axis)

 

I believe that the listener is originally placed at (0, 0, 0) in the cartesian coordinates, facing the positive z direction. And the room spans from -2 to +2 on the x-axis, -1.5 to +1.5 on the y-axis, -3 to +3 on the z-axis. I use a sampling rate of 24000 kHz, block size of 512 samples, and read the location of a sound source from a CSV file.

 

If my assumptions are correct, then the following code does not seem to be adding reverb:

 

 

 

#include <iostream>
#include <fstream>
#include <string>
#include <cstring>
#include <vector>
#include <sstream>
#include "OVR_Audio.h"
using namespace std;


int main(){
    
    cout << "Booting up Oculus Audio SDK ..." << endl;
    
//  Declaring a context
    ovrAudioContext c1;

//  Configuring AudioContext Parameters
    ovrAudioContextConfiguration config = {};
    
    config.acc_Size = sizeof( config );
    config.acc_SampleRate = 24000;
    config.acc_BufferLength = 512;
    config.acc_MaxNumSources = 16;
    
//  Creating a context with mentioned configuration and checking for success.
    int result = ovrAudio_CreateContext(&c1, &config);
    if (result == ovrSuccess){
        cout << "Context in main function has been created successfully!" << endl;
    } else{
        cout << "Oops! Context could not be created." << endl;
    }

    //  Turning on room paramters /hint:change ordering here?
    int rs1 = ovrAudio_Enable(c1, ovrAudioEnable_SimpleRoomModeling, 1);
    int rs2 = ovrAudio_Enable(c1, ovrAudioEnable_LateReverberation, 1);
    int rs3 = ovrAudio_Enable(c1, ovrAudioEnable_RandomizeReverb, 1);
    
//  Check if enabled successfully
    if( (rs1 == ovrSuccess) && (rs2 == ovrSuccess) && (rs3 == ovrSuccess)  ){
        cout<<"Room settings have been set!" << endl;
    } else{
        cout << "Oops! Room setting parameters have not been set." << endl;
    }

//  Setting simple room parameters, passing to the context and checking
    int rsN = ovrAudio_SetReflectionModel(c1, ovrAudioReflectionModel_StaticShoeBox);
    
    ovrAudioBoxRoomParameters brp = {};

    brp.brp_Size = sizeof( brp );
    brp.brp_ReflectLeft = brp.brp_ReflectRight = 0.8;
    brp.brp_ReflectUp = brp.brp_ReflectDown = 0.8;
    brp.brp_ReflectFront = brp.brp_ReflectBehind = 0.8;

    brp.brp_Width = 4;
    brp.brp_Height = 3;
    brp.brp_Depth = 6;

    int brp1 = ovrAudio_SetSimpleBoxRoomParameters(c1, &brp );
    
    if( brp1 == ovrSuccess && rsN == ovrSuccess){
        cout<<"Box room params have been set!" << endl;
    } else{
        cout << "Oops! Box room params have not been set." << endl;
    }
    
    // ovrAudio_SetSharedReverbWetLevel(c1, 0.5);
    
//  Managing sounds
    
    int N = 1;
    string sound_file_names[N] = {"Trial_1_Conv1"};
    
    string sound[N];
    string posns[N];
    string input_path = "ToCPP/";
    
    for (int i = 0; i < N; i ++){
        sound[i] = input_path + sound_file_names[i] + "_" + to_string(config.acc_SampleRate) + ".csv";
        posns[i] = input_path + sound_file_names[i] + "_" + "xyz.csv";
    }

    //  Open sound and posn CSV files
    
    ifstream sound_files[N];
    ifstream posns_files[N];
    
    for (int i = 0; i < N; i++){
        
        sound_files[i].open(sound[i]);
        posns_files[i].open(posns[i]);
        
    }
    
    //  Opening an output file
    string write_name;
    for (int i = 0; i<N; i++){
        write_name = write_name + sound_file_names[i];
        if (i < N-1){
            write_name = write_name + "+";
        }
    }
    
    
    ofstream opfile;
    opfile.open("FromCPP/" + write_name + ".csv");
    

    
//  Declaring variables
    
    string posn_string;
    int proceed = 1;
    int buffer_size = 512;
    int records1 = 0;
    int records2 = 0;
    
//  Determine how many blocks and set HINTs
    
    int blocks[N];
    
    for (int i = 0; i < N; i ++){
        
        // Set hints
        int prop1 = ovrAudio_SetAudioSourceFlags( c1, i, ovrAudioSourceFlag_WideBand_HINT);
        int prop2 = ovrAudio_SetAudioSourceAttenuationMode(c1, i, ovrAudioSourceAttenuationMode_InverseSquare, 1);  // 1 is actually don't care for the chosen AttenuationMode

        if (prop1 == ovrSuccess && prop2 == ovrSuccess){
            cout<<"Sound properties (wideband, attenuation) have been set!" << endl;
        } else {
            cout<<"Oops! Sound properties have not been set.";
        }
        
        int blk_count  = 0;
        while(posns_files[i].good()){
            getline(posns_files[i], posn_string, '\n');
            blk_count  = blk_count + 1;
        }
        
        cout<<"File: "<<i<<" has "<<blk_count<<" blocks"<<endl;
        blocks[i]  = blk_count;
    }
    
//  Close and re-open posn files for further processing
    
    for(int i = 0; i < N; i++)
    {
        posns_files[i].close();
        posns_files[i].open(posns[i]);
    }
    
// Finding the least number of blocks in the array of files [this is the time-limiter!]
    
    int least = blocks[0];
    
    for(int i = 0; i < N; i++){
        if (blocks[i] <= least){
            least = blocks[i];
        }
    }
    
    cout<<"Least number of blocks: "<<least<<endl;
    
    
//  Applying spatialisation
    
    for(int blk = 1; blk <= least - 2; blk++){
    
        cout<<"Processing block "<<blk<<" of "<<least-2<<"."<<endl;
        
//      Read and apply block position
        
        for(int i = 0; i < N; i++){


            vector<string> v;

            getline(posns_files[i], posn_string, '\n');

//          Parsing

            stringstream ss(posn_string);

            while (ss.good()) {
                string substr;
                getline(ss, substr, ',');
                v.push_back(substr);
            }

            float x, y, z;

            x = stof(v[0]);
            y = stof(v[1]);
            z = stof(v[2]);

            cout<<"Positions successfully updated!" << endl;

            ovrAudio_SetAudioSourcePos(c1, i, x, y, z);

        }
        
        
//      Read blocks
        
        uint32_t Flags = 0, Status = 0;
        
        float inbuffer[512];
        float outbuffer[1024];
        float mixbuffer[1024];
        string sample_string;
        
        cout<<"Reading block: " << blk << " from sound .csv"<<endl;
        
        for(int i = 0; i < N; i++){
            
            for(int sample_no = 0; sample_no < 512; sample_no++){
                getline(sound_files[i], sample_string, '\n');
                inbuffer[sample_no] = stof(sample_string);
            }
        
            ovrAudio_SpatializeMonoSourceInterleaved(c1, i, &Status, outbuffer, inbuffer);
            
            for(int j = 0; j < 1024; j ++){
                if (i==0){
                    mixbuffer[j] = outbuffer[j];
                } else {
                    mixbuffer[j] = mixbuffer[j] + outbuffer[j];
                }
            }
            
        }
        
        cout<<"Finished processing block: " << blk << " from sound .csv"<<endl;
        
//      Write output files
        
        for(int j = 0; j < 1024; j++){
            opfile<<mixbuffer[j]<<endl;
        }
    }
    
    
    
   ovrAudio_DestroyContext(c1);
   ovrAudio_Shutdown();
    
//  Closing the files
    
    for (int i = 0; i < N; i++){
        
        sound_files[i].close();
        posns_files[i].close();
        
    }
    
    opfile.close();
    
    cout << "Spatializer script ends." << endl;
   	return 0;
}

 

 

 

Help would be much appreciated. 

 

Thank you,

Vishal

0 REPLIES 0