Skip to content

Commit

Permalink
Extended binaural HRTF dispenser to support multiple slots for differ…
Browse files Browse the repository at this point in the history
…ent HRTFs in different modules
  • Loading branch information
hkhauke committed May 3, 2024
1 parent 350d8b3 commit f740340
Show file tree
Hide file tree
Showing 6 changed files with 175 additions and 120 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ CjvxAuNBinauralRender::prepare_connect_icon(JVX_CONNECTION_FEEDBACK_TYPE(fdb))
// _common_set_icon.theData_in->con_params

jvxSize samplerate = _common_set_icon.theData_in->con_params.rate;
theHrtfDispenser->init(&samplerate); // Check result in the future
theHrtfDispenser->init(&samplerate, 1); // Check result in the future - number of slots provided here!

// Allocate renderer
render_pri = allocate_renderer(_common_set_icon.theData_in->con_params.buffersize,
Expand Down Expand Up @@ -280,12 +280,12 @@ CjvxAuNBinauralRender::update_hrirs(jvxOneRenderCore* renderer, jvxData azimuth_
this->source_direction_angles_deg_inuse[1] = inclination_deg;

jvxSize length_hrir;
this->theHrtfDispenser->get_length_hrir(length_hrir, &loadId);
this->theHrtfDispenser->get_length_hrir(length_hrir, &loadId, slotIdHrtfs);
if (renderer->loadId == loadId)
{
jvxErrorType res = this->theHrtfDispenser->copy_closest_hrir_pair(azimuth_deg, inclination_deg,
renderer->buffer_hrir_left, renderer->buffer_hrir_right, renderer->length_buffer_hrir,
renderer->loadId);
renderer->loadId, slotIdHrtfs);
if (res != JVX_NO_ERROR)
{
std::cout << __FUNCTION__ << " Warning: on update of position, the request to return hrir buffers failed with error <" <<
Expand Down Expand Up @@ -409,7 +409,7 @@ CjvxAuNBinauralRender::allocate_renderer(jvxSize bsize, jvxData startAz, jvxData
jvxOneRenderCore* render_inst = nullptr;
JVX_SAFE_ALLOCATE_OBJECT(render_inst, jvxOneRenderCore);

jvxErrorType res = theHrtfDispenser->get_length_hrir(render_inst->length_buffer_hrir, &render_inst->loadId);
jvxErrorType res = theHrtfDispenser->get_length_hrir(render_inst->length_buffer_hrir, &render_inst->loadId, slotIdHrtfs);
assert(res == JVX_NO_ERROR);

// Allocate the hrir buffers
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ class CjvxAuNBinauralRender : public CjvxBareNode1ioRearrange,
jvxRenderingUpdateStatus updateDBase = jvxRenderingUpdateStatus::JVX_RENDERING_UPDATE_OFF;

jvxSize missedUpdatesPosition = 0;

jvxSize slotIdHrtfs = 0;

public:

Expand Down Expand Up @@ -123,7 +123,7 @@ class CjvxAuNBinauralRender : public CjvxBareNode1ioRearrange,

// =========================================================================================================
// IjvxPropertyExtenderHrtfDispenser_report
virtual jvxErrorType report_database_changed() override;
virtual jvxErrorType report_database_changed(jvxSize slotId) override;
// =========================================================================================================
jvxErrorType supports_prop_extender_type(jvxPropertyExtenderType tp) override;
jvxErrorType prop_extender_specialization(jvxHandle** prop_extender, jvxPropertyExtenderType tp)override;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,37 +54,39 @@ JVX_PROPERTIES_FORWARD_C_CALLBACK_EXECUTE_FULL(CjvxAuNBinauralRender, set_extend
}

jvxErrorType
CjvxAuNBinauralRender::report_database_changed()
CjvxAuNBinauralRender::report_database_changed(jvxSize slotId)
{
// Here, we need to be careful: the fftw does not allow threaded creation of plans,
//
// https://www.fftw.org/fftw3_doc/Thread-safety.html
//
// The consequence is that we are not allowed to allocate fftws in parallel threads.
// Therefore, all fftw allocation must be in the main thread.
// The update of the position, in contrast, is relocated into another thread since this
// update may occur in the main thread but also in the processing thread for high speed
// modification.
// Only if our slot is in use
if (slotId == slotIdHrtfs)
{
// Here, we need to be careful: the fftw does not allow threaded creation of plans,
//
// https://www.fftw.org/fftw3_doc/Thread-safety.html
//
// The consequence is that we are not allowed to allocate fftws in parallel threads.
// Therefore, all fftw allocation must be in the main thread.
// The update of the position, in contrast, is relocated into another thread since this
// update may occur in the main thread but also in the processing thread for high speed
// modification.
jvxBool triggerThread = false;
JVX_LOCK_MUTEX(safeAccessUpdateBgrd);

jvxBool triggerThread = false;
JVX_LOCK_MUTEX(safeAccessUpdateBgrd);
// Cancel all current position updates
updateHRir = jvxRenderingUpdateStatus::JVX_RENDERING_UPDATE_OFF;

// Cancel all current position updates
updateHRir = jvxRenderingUpdateStatus::JVX_RENDERING_UPDATE_OFF;
// jvxOneRenderCore* removedThis = render_sec;

// jvxOneRenderCore* removedThis = render_sec;
if (render_sec)
{
deallocate_renderer(render_sec);
}

if (render_sec)
{
deallocate_renderer(render_sec);
// Allocate new renderer but as "secondary" renderer for now
render_sec = allocate_renderer(_common_set_icon.theData_in->con_params.buffersize,
this->source_direction_angles_deg_inuse[0], this->source_direction_angles_deg_inuse[1]);
updateDBase = jvxRenderingUpdateStatus::JVX_RENDERING_UPDATE_READY;
JVX_UNLOCK_MUTEX(safeAccessUpdateBgrd);
}

// Allocate new renderer but as "secondary" renderer for now
render_sec = allocate_renderer(_common_set_icon.theData_in->con_params.buffersize,
this->source_direction_angles_deg_inuse[0], this->source_direction_angles_deg_inuse[1]);
updateDBase = jvxRenderingUpdateStatus::JVX_RENDERING_UPDATE_READY;
JVX_UNLOCK_MUTEX(safeAccessUpdateBgrd);

return JVX_NO_ERROR;
}

Expand Down
30 changes: 17 additions & 13 deletions sources/jvxLibraries/ayf-hrtf-dispenser/include/ayf-hrtf.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,14 @@ class oneSofaDataBase
jvxErrorType close();
};

struct oneDBaslot
{
oneSofaDataBase* activeDatabase = nullptr;
float* buffer_hrir_left = nullptr;
float* buffer_hrir_right = nullptr;
jvxSize length_buffer_hrir = JVX_SIZE_UNSELECTED;
jvxSize idx = JVX_SIZE_UNSELECTED;
};

class ayfHrtfDispenser : public IjvxPropertyExtender, public IjvxPropertyExtenderHrtfDispenser
{
Expand All @@ -50,15 +58,11 @@ class ayfHrtfDispenser : public IjvxPropertyExtender, public IjvxPropertyExtende
std::string pathName = "./";
std::list<oneSofaDataBase*> sofaDataBases;

oneSofaDataBase* activeDatabase = nullptr;
jvxSize samplerate = JVX_SIZE_UNSELECTED;
JVX_MUTEX_HANDLE safeAccess;

float *buffer_hrir_left = nullptr;
float *buffer_hrir_right = nullptr;
jvxSize length_buffer_hrir = JVX_SIZE_UNSELECTED;

std::list<IjvxPropertyExtenderHrtfDispenser_report*> registeredListeners;
std::vector<oneDBaslot> dataBaseSlots;

public:
ayfHrtfDispenser();
Expand All @@ -71,11 +75,11 @@ class ayfHrtfDispenser : public IjvxPropertyExtender, public IjvxPropertyExtende

// ==============================================================================

virtual jvxErrorType JVX_CALLINGCONVENTION init(jvxSize* samplerate) override;
virtual jvxErrorType JVX_CALLINGCONVENTION get_length_hrir(jvxSize& length_hrir, jvxSize* loadId) override;
virtual jvxErrorType JVX_CALLINGCONVENTION init(jvxSize* samplerate, jvxSize numSlots) override;
virtual jvxErrorType JVX_CALLINGCONVENTION get_length_hrir(jvxSize& length_hrir, jvxSize* loadId, jvxSize slotId) override;
virtual jvxErrorType JVX_CALLINGCONVENTION copy_closest_hrir_pair(jvxData azimuth_deg, jvxData inclination_deg,
jvxData* hrir_left, jvxData* hrir_right, jvxSize length_hrir, jvxSize dataBaseId) override;
virtual jvxErrorType JVX_CALLINGCONVENTION get_closest_direction(jvxData& azimuth_deg, jvxData& inclination_deg) override;
jvxData* hrir_left, jvxData* hrir_right, jvxSize length_hrir, jvxSize dataBaseId, jvxSize slotId) override;
virtual jvxErrorType JVX_CALLINGCONVENTION get_closest_direction(jvxData& azimuth_deg, jvxData& inclination_deg, jvxSize slotId) override;

// ==============================================================================

Expand All @@ -93,15 +97,15 @@ class ayfHrtfDispenser : public IjvxPropertyExtender, public IjvxPropertyExtende
jvxErrorType name_sofa_file(jvxSize idx, std::string& name);

// Select SOFA database without loading it.
jvxErrorType select_database(jvxSize idx_database);
jvxErrorType select_database(jvxSize idx_database, jvxSize slotId);

// Load the selected SOFA database.
jvxErrorType load_selected_database(oneSofaDataBase* dbase);
jvxErrorType load_selected_database(oneSofaDataBase* dbase, jvxSize slotId);

// Return list index of selected SOFA database.
jvxSize selected_database();
jvxSize selected_database(jvxSize slotId);

void allocate_hrir_buffers(oneSofaDataBase* dbase);
void allocate_hrir_buffers(oneSofaDataBase* dbase, jvxSize slotId);

};

Expand Down
Loading

0 comments on commit f740340

Please sign in to comment.