Skip to content

Commit

Permalink
Continued the tutorial to involve Matlab code AND C code in algorithm…
Browse files Browse the repository at this point in the history
… development
  • Loading branch information
hkbinaurics committed Feb 22, 2024
1 parent be65ae0 commit 83cb4af
Show file tree
Hide file tree
Showing 10 changed files with 96 additions and 79 deletions.
14 changes: 4 additions & 10 deletions cmake/macros.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -354,16 +354,10 @@ macro(JVX_ACTIVATE_VERSION_MATLAB project_name local_project_options)
set(LOCAL_COMPILE_DEFINITIONS "${LOCAL_COMPILE_DEFINITIONS};${local_project_options}")
include_directories(${JVX_BASE_LIBS_INCLUDE_PATH}/jvxLexternalCall/include)
if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/codeGen/export_project.mcg)
set(LOCAL_MCG_FILES
${CMAKE_CURRENT_SOURCE_DIR}/codeGen/export_project.mcg
)
set(LOCAL_LIBS ${LOCAL_LIBS}
jvxLexternalCall_static
)
set(LOCAL_SOURCES ${LOCAL_SOURCES}
${LOCAL_MCG_FILES}
)
endif()
set(LOCAL_MCG_FILES ${CMAKE_CURRENT_SOURCE_DIR}/codeGen/export_project.mcg)
set(LOCAL_SOURCES ${LOCAL_SOURCES} ${LOCAL_MCG_FILES})
endif()
set(LOCAL_LIBS ${LOCAL_LIBS} jvxLexternalCall_static)
endif()

include(${JVX_CMAKE_DIR}/common_local_lib_foot.cmake)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ class CjvxMexCallsProfileTpl : public CjvxMexCallsTpl<T>, public CjvxMexCallsPro
protected:
struct
{
jvxBool matlab_debug_enabled = false; // <- this means: allow matlab and c code operated in parallel - required for verification
jvxCBool skipInvolveCCode = c_false;
jvxBool matlab_profiling_enabled = false;
} config;
Expand All @@ -26,8 +25,8 @@ class CjvxMexCallsProfileTpl : public CjvxMexCallsTpl<T>, public CjvxMexCallsPro
CjvxMexCallsTpl<T>(JVX_CONSTRUCTOR_ARGUMENTS_MACRO_CALL) {};

// Some callbacks
virtual jvxErrorType local_allocate_profiling() = 0;
virtual jvxErrorType local_deallocate_profiling() = 0;
virtual jvxErrorType local_allocate_profiling() { return JVX_NO_ERROR; };
virtual jvxErrorType local_deallocate_profiling() { return JVX_NO_ERROR; };

jvxErrorType prepare_connect_icon(JVX_CONNECTION_FEEDBACK_TYPE(fdb))
{
Expand All @@ -40,7 +39,7 @@ class CjvxMexCallsProfileTpl : public CjvxMexCallsTpl<T>, public CjvxMexCallsPro
{
// We need to run this BEFORE we run any prepare call to obtain the
// modified parameter settings in the Matlab startup
if (config.matlab_debug_enabled && config.matlab_profiling_enabled)
if (config.matlab_profiling_enabled)
{
res = CjvxMexCallsProfiler::profile_config_on_prepare();
}
Expand Down Expand Up @@ -68,7 +67,7 @@ class CjvxMexCallsProfileTpl : public CjvxMexCallsTpl<T>, public CjvxMexCallsPro
_common_set_icon.theData_in->con_params.number_channels,
_common_set_icon.theData_in->con_params.buffersize);

if (config.matlab_debug_enabled && config.matlab_profiling_enabled)
if (config.matlab_profiling_enabled)
{
res = local_allocate_profiling();
}
Expand All @@ -87,7 +86,7 @@ class CjvxMexCallsProfileTpl : public CjvxMexCallsTpl<T>, public CjvxMexCallsPro
jvxErrorType res = JVX_NO_ERROR;
jvxErrorType resL = JVX_NO_ERROR;

if (config.matlab_debug_enabled && config.matlab_profiling_enabled)
if (config.matlab_profiling_enabled)
{
// Stop the profiler
CjvxMexCallsProfiler::profile_stop_on_postprocess();
Expand Down Expand Up @@ -131,53 +130,46 @@ class CjvxMexCallsProfileTpl : public CjvxMexCallsTpl<T>, public CjvxMexCallsPro
jvxData** buffers_in = jvx_process_icon_extract_input_buffers<jvxData>(_common_set_icon.theData_in, idx_stage);
jvxData** buffers_out = jvx_process_icon_extract_output_buffers<jvxData>(_common_set_ocon.theData_out);

if (config.matlab_debug_enabled)
{
// ===================================================
// This case to run Matlab and C code in parallel
// ===================================================
// ===================================================
// This case to run Matlab and C code in parallel
// ===================================================

if (config.matlab_profiling_enabled)
{
// Run this command only once!! Need to put it here since processing information is not available in "prepare"!!
CjvxMexCallsProfiler::profile_start_in_process();
}
if (config.matlab_profiling_enabled)
{
// Run this command only once!! Need to put it here since processing information is not available in "prepare"!!
CjvxMexCallsProfiler::profile_start_in_process();
}

if (config.skipInvolveCCode)
{
skipCCode = true;
}
if (config.skipInvolveCCode)
{
skipCCode = true;
}

if (!skipCCode)
if (!skipCCode)
{
// Copy input data for later usage
for (i = 0; i < _common_set_icon.theData_in->con_params.number_channels; i++)
{
// Copy input data for later usage
for (i = 0; i < _common_set_icon.theData_in->con_params.number_channels; i++)
{
jvxData* ptrTo = dbgFldCopyInputs[i];
jvxData* ptrFrom = (jvxData*)buffers_in[i];
memcpy(ptrTo, ptrFrom, sizeof(jvxData) * _common_set_icon.theData_in->con_params.buffersize);
}
jvxData* ptrTo = dbgFldCopyInputs[i];
jvxData* ptrFrom = (jvxData*)buffers_in[i];
memcpy(ptrTo, ptrFrom, sizeof(jvxData) * _common_set_icon.theData_in->con_params.buffersize);
}
}

// This lets Matlab run one frame of processing
// ======================================================================================
res = CjvxMexCalls::process_buffers_icon(_common_set_icon.theData_in, &_common_set_ocon.theData_out);
// ======================================================================================
// This lets Matlab run one frame of processing
// ======================================================================================
res = CjvxMexCalls::process_buffers_icon(_common_set_icon.theData_in, &_common_set_ocon.theData_out);
// ======================================================================================

if (!skipCCode)
if (!skipCCode)
{
for (i = 0; i < _common_set_icon.theData_in->con_params.number_channels; i++)
{
for (i = 0; i < _common_set_icon.theData_in->con_params.number_channels; i++)
{
jvxData* ptrFrom = dbgFldCopyInputs[i];
jvxData* ptrTo = (jvxData*)buffers_in[i];
memcpy(ptrTo, ptrFrom, sizeof(jvxData) * _common_set_icon.theData_in->con_params.buffersize);
}
jvxData* ptrFrom = dbgFldCopyInputs[i];
jvxData* ptrTo = (jvxData*)buffers_in[i];
memcpy(ptrTo, ptrFrom, sizeof(jvxData) * _common_set_icon.theData_in->con_params.buffersize);
}
}
else
{
return res;
}
}

if (!skipCCode)
Expand All @@ -192,7 +184,7 @@ class CjvxMexCallsProfileTpl : public CjvxMexCallsTpl<T>, public CjvxMexCallsPro
}
}

if (engaged && config.matlab_debug_enabled && config.matlab_profiling_enabled)
if (engaged && config.matlab_profiling_enabled)
{
// Profiler step not necessary requires c code execution
CjvxMexCallsProfiler::profile_step_in_process();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,8 +170,8 @@ class CjvxMexCallsTpl : public CjvxMexCalls, public T
};

// ==========================================================================================
virtual void initExternalCall() = 0;
virtual void terminateExternalCall() = 0;
virtual void initExternalCall() {};
virtual void terminateExternalCall() {};
// virtual void onPrepareConnectMexCalls() = 0;
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ include_directories(
${JVX_BASE_ROOT}/sources/jvxLibraries/jvx-component-templates-base/include
${JVX_BASE_BINARY_DIR}/sources/jvxLibraries/jvx-component-templates-base/generated
${SUBPRODUCT_AYFSTARTER_LIBS_INCLUDE_PATH}/ayfstarterlib/include

# Step IV
${JVX_BASE_ROOT}/sources/jvxLibraries/jvx-component-templates-mex/include
)

set(LOCAL_SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/target/componentEntry.cpp
Expand Down Expand Up @@ -36,6 +39,9 @@ include(${JVX_CMAKE_DIR}/common_local_lib_foot.cmake)
#================================= Step IV =================================
if(JVX_USE_PART_MATLAB)

# Add all Matlab specific extensions
set(LOCAL_SOURCES ${LOCAL_SOURCES} ${CMAKE_CURRENT_SOURCE_DIR}/src/CayfAuNStarter_mex.cpp)

# Setup namespaces
set(LOCAL_COMPILE_DEFINITIONS "${LOCAL_COMPILE_DEFINITIONS_COPY};JVX_PROJECT_NAMESPACE=AYFSTARTERM")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@
global jvx_host_call_global; % <- access to the underlying AudYoFlo host

% Realize talkthrough
jvx_out_frame = jvx_in_frame;
jvx_out_frame = jvx_in_frame * jvx_handle.volume;
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,6 @@

jvx_handle.jvx_add_hints = jvx_add_hints;

% Restore private data in global data struct
inProcessing.jvx_handle = jvx_handle;
% Initialize volume level
jvx_handle.volume = 0.1;

Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ REM ================================================================
REM ================================================================
set SYSTEM_JVX_MEX_PATH='%CD%/matlab/'
set SYSTEM_JVX_MATLAB_PATH={'%CD%/matlab/m-files' '%CD%/matlab/m-files/subprojects-audionode' '%CD%/matlab/m-files/audioProperties' '%CD%/matlab/m-files/profiling' '@JVX_MATLAB_ORIGIN_PATH@'}
set SYSTEM_JVX_MATLAB_DEMO={'Realtime Processing: jvxHost(''rt-audio'', ''--config'', ''%CD%/ayfstarter-mat.jvx'', ''--jvxdir'', ''%CD%/jvxComponents'');';'Offline Processing: jvxHost(''off-audio'', ''--config'', ''%CD%/ayfstarter-mat.jvx'', ''--jvxdir'', ''%CD%/jvxComponents'')'}
set SYSTEM_JVX_MATLAB_DEMO={'Realtime Processing: jvxHost(''rt-audio'', ''--config'', ''%CD%/ayfstarter-mat.jvx'', ''--jvxdir'', ''%CD%/jvxComponents'');';'Offline Processing: jvxHost(''off-audio'', ''--config'', ''%CD%/ayfstarter-mat.jvx'', ''--jvxdir'', ''%CD%/jvxComponents'');'}
set COMMAND_SCRIPTING='jvxHost(''off-audio'', ''--config'', ''%CD%/ayfstarter-mat.jvx'', ''--jvxdir'', ''%CD%/jvxComponents'');'
set PATH=%PATH%;%CD%/bin

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,12 @@ CayfAuNStarter::deactivate()
* during processing until postprocessing is run.
*/
jvxErrorType
CayfAuNStarter::prepare_connect_icon(JVX_CONNECTION_FEEDBACK_TYPE(fdb))
CayfAuNStarter::local_prepare_connect_icon(JVX_CONNECTION_FEEDBACK_TYPE(fdb))
{
jvxErrorType res = AYF_AUDIO_NODE_BASE_CLASS::prepare_connect_icon(JVX_CONNECTION_FEEDBACK_CALL(fdb));
if (res != JVX_NO_ERROR) return res;
ayf_starter_prepare(&processing_lib);
return res;
// jvxErrorType res = AYF_AUDIO_NODE_NODE_CLASS::prepare_connect_icon(JVX_CONNECTION_FEEDBACK_CALL(fdb));
// if (res != JVX_NO_ERROR) return res;
// <- no longer in use as this call is in template class already
return ayf_starter_prepare(&processing_lib);
}

/**
Expand All @@ -78,30 +78,30 @@ CayfAuNStarter::prepare_connect_icon(JVX_CONNECTION_FEEDBACK_TYPE(fdb))
* producing the output data.
*/
jvxErrorType
CayfAuNStarter::process_buffers_icon(jvxSize mt_mask, jvxSize idx_stage)
CayfAuNStarter::local_process_buffers_icon(jvxSize mt_mask, jvxSize idx_stage)
{
jvxData** buffers_in = jvx_process_icon_extract_input_buffers<jvxData>(_common_set_icon.theData_in, idx_stage);
jvxData** buffers_out = jvx_process_icon_extract_output_buffers<jvxData>(_common_set_ocon.theData_out);

// Involve the c-library for processing
ayf_starter_process(&processing_lib, buffers_in, buffers_out,
return ayf_starter_process(&processing_lib, buffers_in, buffers_out,
_common_set_icon.theData_in->con_params.number_channels,
_common_set_ocon.theData_out.con_params.number_channels,
_common_set_icon.theData_in->con_params.buffersize);

return fwd_process_buffers_icon(mt_mask, idx_stage);
// return fwd_process_buffers_icon(mt_mask, idx_stage); <- no longer in use as this call is in template class already
}

/**
* Run the post processing, that is, deallocate all buffers and required structures which we allocated on prepare.
*/
jvxErrorType
CayfAuNStarter::postprocess_connect_icon(JVX_CONNECTION_FEEDBACK_TYPE(fdb))
CayfAuNStarter::local_postprocess_connect_icon(JVX_CONNECTION_FEEDBACK_TYPE(fdb))
{
jvxErrorType res = AYF_AUDIO_NODE_BASE_CLASS::postprocess_connect_icon(JVX_CONNECTION_FEEDBACK_CALL(fdb));
if (res != JVX_NO_ERROR) return res;
ayf_starter_postprocess(&processing_lib);
return res;
// jvxErrorType res = AYF_AUDIO_NODE_NODE_CLASS::postprocess_connect_icon(JVX_CONNECTION_FEEDBACK_CALL(fdb));
// if (res != JVX_NO_ERROR) return res;
// <-no longer in use as this call is in template class already
return ayf_starter_postprocess(&processing_lib);
}

JVX_PROPERTIES_FORWARD_C_CALLBACK_EXECUTE_FULL(CayfAuNStarter, cb_async_set)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,25 @@

#ifdef AYF_INVOLVE_CHANNEL_REARRANGE
#include "jvxNodes/CjvxBareNode1ioRearrange.h"
#define AYF_AUDIO_NODE_BASE_CLASS CjvxBareNode1ioRearrange
#else
#include "jvxNodes/CjvxBareNode1io.h"
#define AYF_AUDIO_NODE_BASE_CLASS CjvxBareNode1io
#endif

#ifdef JVX_EXTERNAL_CALL_ENABLED
#include "CjvxMexCallsProfileTpl.h"
#define AYF_TEMPLATE_PARENT_CLASS CjvxMexCallsProfileTpl
#else
#include "CjvxMexCallsProfileNoMexTpl.h"
#define AYF_TEMPLATE_PARENT_CLASS CjvxMexCallsProfileNoMexTpl
#endif

#ifdef AYF_INVOLVE_CHANNEL_REARRANGE
#define AYF_AUDIO_NODE_NODE_CLASS CjvxBareNode1ioRearrange
#else
#define AYF_AUDIO_NODE_NODE_CLASS CjvxBareNode1io
#endif
#define AYF_AUDIO_NODE_BASE_CLASS AYF_TEMPLATE_PARENT_CLASS<AYF_AUDIO_NODE_NODE_CLASS>

extern "C"
{
#include "ayfstarterlib.h"
Expand All @@ -36,9 +49,10 @@ class CayfAuNStarter: public AYF_AUDIO_NODE_BASE_CLASS, public genStarter_node
jvxErrorType deactivate() override;

// Step II: Add prepare, postprocess and process function
jvxErrorType process_buffers_icon(jvxSize mt_mask, jvxSize idx_stage)override;
jvxErrorType prepare_connect_icon(JVX_CONNECTION_FEEDBACK_TYPE(fdb)) override;
jvxErrorType postprocess_connect_icon(JVX_CONNECTION_FEEDBACK_TYPE(fdb))override;
// STep IV: rename the main processing functions
jvxErrorType local_process_buffers_icon(jvxSize mt_mask, jvxSize idx_stage)override;
jvxErrorType local_prepare_connect_icon(JVX_CONNECTION_FEEDBACK_TYPE(fdb)) override;
jvxErrorType local_postprocess_connect_icon(JVX_CONNECTION_FEEDBACK_TYPE(fdb))override;

// Step III: Define a property-set callback
JVX_PROPERTIES_FORWARD_C_CALLBACK_DECLARE(cb_async_set);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#include "CayfAuNStarter.h"

#ifdef JVX_PROJECT_NAMESPACE
namespace JVX_PROJECT_NAMESPACE {
#endif

#ifdef JVX_PROJECT_NAMESPACE
}
#endif

0 comments on commit 83cb4af

Please sign in to comment.