- Add sustains support,

- Improve task wait management.
This commit is contained in:
Arnaud G. GIBERT 2022-04-29 20:48:54 +02:00
parent 5113c24f6a
commit 7cbad040ee
6 changed files with 161 additions and 21 deletions

View File

@ -2537,7 +2537,7 @@ static void *DR_Task_Thread( DRT_Base *Base_Ptr)
case DRD_TASK_CMD_KIT_LOAD: case DRD_TASK_CMD_KIT_LOAD:
{ {
Base_Ptr->Task_Tab[task_id].Status = DRD_TASK_STATUS_RUNNING; Base_Ptr->Task_Tab[task_id].Status = DRD_TASK_STATUS_RUNNING;
Base_Ptr->Task_Count_Waiting--; // Base_Ptr->Task_Count_Waiting--;
pthread_mutex_unlock( &( Base_Ptr->Task_Mutex)); pthread_mutex_unlock( &( Base_Ptr->Task_Mutex));
@ -2570,7 +2570,7 @@ static void *DR_Task_Thread( DRT_Base *Base_Ptr)
default: default:
{ {
printf( "DR_Task_Thrad called with an undefined command [%d] [%s]\n", Base_Ptr->Task_Tab[task_id].Command, DR_TASK_CMD_ASCII_GET( &(Base_Ptr->Task_Tab[task_id]))); printf( "DR_Task_Thread called with an undefined command [%d] [%s]\n", Base_Ptr->Task_Tab[task_id].Command, DR_TASK_CMD_ASCII_GET( &(Base_Ptr->Task_Tab[task_id])));
} }
} }
@ -2624,10 +2624,12 @@ DRT_Status DR_Task_Id_Run( DRT_Task_Id Task_Id, DRT_Base *Base_Ptr, DRT_Task_
{ {
Base_Ptr->Task_Tab[Task_Id].Command = Command; Base_Ptr->Task_Tab[Task_Id].Command = Command;
Base_Ptr->Task_Tab[Task_Id].Target_Ptr = Target_Ptr; Base_Ptr->Task_Tab[Task_Id].Target_Ptr = Target_Ptr;
Base_Ptr->Task_Count_Waiting--;
pthread_cond_signal( &( Base_Ptr->Task_Tab[Task_Id].Cond)); pthread_cond_signal( &( Base_Ptr->Task_Tab[Task_Id].Cond));
// pthread_mutex_unlock( &( Base_Ptr->Task_Mutex)); // pthread_mutex_unlock( &( Base_Ptr->Task_Mutex));
status = DRS_OK; status = DRS_OK;
} }
@ -2686,6 +2688,65 @@ DRT_Status DR_Task_Run( DRT_Base *Base_Ptr, DRT_Task_Command Command, void *
DRT_Boolean found = DRD_FALSE; DRT_Boolean found = DRD_FALSE;
// fprintf( stderr, "Task Run: Called!\n");
Base_Ptr->Task_Count_Run++;
while( found != DRD_TRUE)
{
pthread_mutex_lock( &( Base_Ptr->Task_Mutex));
if( Base_Ptr->Task_Count_Waiting > 0)
{
// while( ( task_id < Base_Ptr->Task_Number) && ( found != DRD_TRUE))
for( task_id = 0; ( task_id < Base_Ptr->Task_Number) && ( found != DRD_TRUE); task_id++)
{
if( DR_TASK_STATUS_WAITING_IS( &( Base_Ptr->Task_Tab[task_id])) && DR_TASK_CMD_NONE_IS( &( Base_Ptr->Task_Tab[task_id])))
{
// pthread_mutex_unlock( &( Base_Ptr->Task_Mutex));
// fprintf( stderr, "Task Run: Found (%d)!\n", task_id);
if( DR_Task_Id_Run( task_id, Base_Ptr, Command, Target_Ptr) != DRS_OK)
{
fprintf( stderr, "Can't run task !\n");
pthread_mutex_unlock( &( Base_Ptr->Task_Mutex));
return( DRS_KO);
}
pthread_mutex_unlock( &( Base_Ptr->Task_Mutex));
found = DRD_TRUE;
}
}
if( found != DRD_TRUE)
{
DR_Tasks_Dump( Base_Ptr);
Base_Ptr->Task_Count_FakeWU++;
}
}
if( found != DRD_TRUE)
{
// fprintf( stderr, "Task Run: Sleeping!\n");
Base_Ptr->Task_Count_Sleep++;
pthread_cond_wait( &( Base_Ptr->Task_Cond), &( Base_Ptr->Task_Mutex));
pthread_mutex_unlock( &( Base_Ptr->Task_Mutex));
}
}
return( DRS_OK);
}
/*
DRT_Status DR_Task_Run( DRT_Base *Base_Ptr, DRT_Task_Command Command, void *Target_Ptr)
{
DRT_Task_Id task_id;
DRT_Boolean found = DRD_FALSE;
// fprintf( stderr, "Task Run: Called!\n"); // fprintf( stderr, "Task Run: Called!\n");
Base_Ptr->Task_Count_Run++; Base_Ptr->Task_Count_Run++;
@ -2731,7 +2792,7 @@ DRT_Status DR_Task_Run( DRT_Base *Base_Ptr, DRT_Task_Command Command, void *
return( DRS_OK); return( DRS_OK);
} }
*/
@ -2786,7 +2847,7 @@ DRT_Status DR_Tasks_Dump( DRT_Base *Base_Ptr)
DRT_Task_Id task_id; DRT_Task_Id task_id;
pthread_mutex_lock( &( Base_Ptr->Task_Mutex)); // pthread_mutex_lock( &( Base_Ptr->Task_Mutex));
fprintf( stderr, "Task Number: (%d) Task Load Layer: [%s] Task Load Instrument: [%s] Task Load Kit: [%s] Task Sync Kit: [%s] Task Sync Kits: [%s]\n", fprintf( stderr, "Task Number: (%d) Task Load Layer: [%s] Task Load Instrument: [%s] Task Load Kit: [%s] Task Sync Kit: [%s] Task Sync Kits: [%s]\n",
Base_Ptr->Task_Number, DR_BOOLEAN_VALUE_ASCII_GET( Base_Ptr->Task_Load_Layer_Flag), DR_BOOLEAN_VALUE_ASCII_GET( Base_Ptr->Task_Load_Instrument_Flag), DR_BOOLEAN_VALUE_ASCII_GET(Base_Ptr->Task_Load_Kit_Flag), DR_BOOLEAN_VALUE_ASCII_GET(Base_Ptr->Task_Sync_Kit_Flag), DR_BOOLEAN_VALUE_ASCII_GET(Base_Ptr->Task_Sync_Kits_Flag)); Base_Ptr->Task_Number, DR_BOOLEAN_VALUE_ASCII_GET( Base_Ptr->Task_Load_Layer_Flag), DR_BOOLEAN_VALUE_ASCII_GET( Base_Ptr->Task_Load_Instrument_Flag), DR_BOOLEAN_VALUE_ASCII_GET(Base_Ptr->Task_Load_Kit_Flag), DR_BOOLEAN_VALUE_ASCII_GET(Base_Ptr->Task_Sync_Kit_Flag), DR_BOOLEAN_VALUE_ASCII_GET(Base_Ptr->Task_Sync_Kits_Flag));
@ -2798,9 +2859,9 @@ DRT_Status DR_Tasks_Dump( DRT_Base *Base_Ptr)
task_id, DR_TASK_STATUS_ASCII_GET( &(Base_Ptr->Task_Tab[task_id])), (long)( Base_Ptr->Task_Tab[task_id].Thread), DR_TASK_CMD_ASCII_GET( &( Base_Ptr->Task_Tab[task_id])), Base_Ptr->Task_Tab[task_id].Target_Ptr, Base_Ptr->Task_Tab[task_id].Count); task_id, DR_TASK_STATUS_ASCII_GET( &(Base_Ptr->Task_Tab[task_id])), (long)( Base_Ptr->Task_Tab[task_id].Thread), DR_TASK_CMD_ASCII_GET( &( Base_Ptr->Task_Tab[task_id])), Base_Ptr->Task_Tab[task_id].Target_Ptr, Base_Ptr->Task_Tab[task_id].Count);
} }
fprintf( stderr, "Task Count Waiting: (%ld) Task Count Run: (%ld) Task Count Sleep: (%ld)\n", Base_Ptr->Task_Count_Waiting, Base_Ptr->Task_Count_Run, Base_Ptr->Task_Count_Sleep); fprintf( stderr, "Task Count Waiting: (%ld) Task Count Run: (%ld) Task Count Sleep: (%ld) Task Count FakeWU: (%ld)\n", Base_Ptr->Task_Count_Waiting, Base_Ptr->Task_Count_Run, Base_Ptr->Task_Count_Sleep, Base_Ptr->Task_Count_FakeWU);
pthread_mutex_unlock( &( Base_Ptr->Task_Mutex)); // pthread_mutex_unlock( &( Base_Ptr->Task_Mutex));
return( DRS_OK); return( DRS_OK);
} }
@ -2832,6 +2893,8 @@ DRT_Status DR_DataStruct_Init( DRT_Base *Base_Ptr, DRT_SampleRate SampleRate,
} }
Base_Ptr->SampleRate = SampleRate; Base_Ptr->SampleRate = SampleRate;
Base_Ptr->Velocity_Ignore = DRD_VELOCITY_IGNORE_DEFAULT;
Base_Ptr->Note_Off_Ignore = DRD_NOTE_OFF_IGNORE_DEFAULT;
Base_Ptr->Task_Number = Task_Number; Base_Ptr->Task_Number = Task_Number;
Base_Ptr->Task_Load_Layer_Flag = DRD_TASK_LOAD_LAYER_DEFAULT; Base_Ptr->Task_Load_Layer_Flag = DRD_TASK_LOAD_LAYER_DEFAULT;
Base_Ptr->Task_Load_Instrument_Flag = DRD_TASK_LOAD_INSTRUMENT_DEFAULT; Base_Ptr->Task_Load_Instrument_Flag = DRD_TASK_LOAD_INSTRUMENT_DEFAULT;
@ -2841,6 +2904,7 @@ DRT_Status DR_DataStruct_Init( DRT_Base *Base_Ptr, DRT_SampleRate SampleRate,
Base_Ptr->Task_Count_Waiting = 0L; Base_Ptr->Task_Count_Waiting = 0L;
Base_Ptr->Task_Count_Run = 0L; Base_Ptr->Task_Count_Run = 0L;
Base_Ptr->Task_Count_Sleep = 0L; Base_Ptr->Task_Count_Sleep = 0L;
Base_Ptr->Task_Count_FakeWU = 0L;
if( ( Base_Ptr->Task_Tab = malloc( sizeof( DRT_Task) * Task_Number)) == NULL) if( ( Base_Ptr->Task_Tab = malloc( sizeof( DRT_Task) * Task_Number)) == NULL)
{ {

View File

@ -160,6 +160,10 @@ typedef struct DRT_Base
NDT_Root *Kit_DS_Ptr; NDT_Root *Kit_DS_Ptr;
DRT_SampleRate SampleRate; DRT_SampleRate SampleRate;
//Config
bool Velocity_Ignore;
bool Note_Off_Ignore;
DRT_Task_Id Task_Number; DRT_Task_Id Task_Number;
DRT_Boolean Task_Load_Layer_Flag; DRT_Boolean Task_Load_Layer_Flag;
DRT_Boolean Task_Load_Instrument_Flag; DRT_Boolean Task_Load_Instrument_Flag;
@ -172,6 +176,8 @@ typedef struct DRT_Base
DRT_Task_Id Task_Count_Waiting; DRT_Task_Id Task_Count_Waiting;
long Task_Count_Run; long Task_Count_Run;
long Task_Count_Sleep; long Task_Count_Sleep;
long Task_Count_FakeWU;
} DRT_Base; } DRT_Base;

View File

@ -113,7 +113,7 @@ int main( int argc, char **argv)
DR_Kits_Sample_Load( &base); DR_Kits_Sample_Load( &base);
/* /*
for( i = 0; i < 264; i++) for( i = 0; i < 1; i++)
{ {
if( ( status = DR_Kit_Logical_Id_Find( &kit_ptr, base.Kit_DS_Ptr, i)) == DRS_OK) if( ( status = DR_Kit_Logical_Id_Find( &kit_ptr, base.Kit_DS_Ptr, i)) == DRS_OK)
{ {
@ -121,7 +121,7 @@ int main( int argc, char **argv)
DR_Kit_Sample_Load( kit_ptr); DR_Kit_Sample_Load( kit_ptr);
} }
} }
*/ */
/* /*
if( DR_Task_Wait( &base) != DRS_OK) if( DR_Task_Wait( &base) != DRS_OK)
{ {

View File

@ -11,6 +11,7 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <stdarg.h> #include <stdarg.h>
#include <stdbool.h>
#include <ctype.h> #include <ctype.h>
#include <math.h> #include <math.h>
#include <unistd.h> #include <unistd.h>
@ -57,6 +58,10 @@ typedef short DRT_Boolean;
#define DRD_SAMPLE_RATE_DEFAULT 44100 #define DRD_SAMPLE_RATE_DEFAULT 44100
#define DRD_VELOCITY_IGNORE_DEFAULT false
#define DRD_NOTE_OFF_IGNORE_DEFAULT false
#define DRD_THREAD_NUMBER_DEFAULT 16L #define DRD_THREAD_NUMBER_DEFAULT 16L
#define DRD_TASK_LOAD_LAYER_DEFAULT DRD_TRUE #define DRD_TASK_LOAD_LAYER_DEFAULT DRD_TRUE
#define DRD_TASK_LOAD_INSTRUMENT_DEFAULT DRD_FALSE #define DRD_TASK_LOAD_INSTRUMENT_DEFAULT DRD_FALSE

View File

@ -49,6 +49,7 @@ DRT_Status DR_LV2_Kit_Sample_Load( DRT_LV2_Base *LV2_Base_Ptr, DRT_Kit *Kit_P
LV2_Base_Ptr->Samples[sample_id].Layer_Ptr = 0; LV2_Base_Ptr->Samples[sample_id].Layer_Ptr = 0;
LV2_Base_Ptr->Samples[sample_id].Data_Ptr = NULL; LV2_Base_Ptr->Samples[sample_id].Data_Ptr = NULL;
LV2_Base_Ptr->Samples[sample_id].Data_Offset = 0; LV2_Base_Ptr->Samples[sample_id].Data_Offset = 0;
LV2_Base_Ptr->Samples[sample_id].Sustained = false;
if( cur_node_ptr == NULL) if( cur_node_ptr == NULL)
{ {
@ -413,8 +414,9 @@ static inline void DR_Sample_Trigger( DRT_LV2_Base *LV2_Base_Ptr, int Sample_
LV2_Base_Ptr->Samples[Sample_Id].Active = 1; LV2_Base_Ptr->Samples[Sample_Id].Active = 1;
LV2_Base_Ptr->Samples[Sample_Id].Offset = 0; LV2_Base_Ptr->Samples[Sample_Id].Offset = 0;
LV2_Base_Ptr->Samples[Sample_Id].Velocity = LV2_Base_Ptr->Ignore_Velocity ? 1.0f : ( (float)Data[2]) / DRD_VELOCITY_MAX; LV2_Base_Ptr->Samples[Sample_Id].Velocity = LV2_Base_Ptr->Base.Velocity_Ignore ? 1.0f : ( (float)Data[2]) / DRD_VELOCITY_MAX;
LV2_Base_Ptr->Samples[Sample_Id].Data_Offset = Offset; LV2_Base_Ptr->Samples[Sample_Id].Data_Offset = Offset;
LV2_Base_Ptr->Samples[Sample_Id].Sustained = false;
if( LV2_Base_Ptr->Samples[Sample_Id].Instrument_Ptr->Layer_DS_Ptr->Index_Tab[NDD_INDEX_PRIMARY].Node_Number > 0) if( LV2_Base_Ptr->Samples[Sample_Id].Instrument_Ptr->Layer_DS_Ptr->Index_Tab[NDD_INDEX_PRIMARY].Node_Number > 0)
{ {
@ -445,7 +447,16 @@ static inline void DR_Sample_Untrigger( DRT_LV2_Base *LV2_Base_Ptr, int Sampl
{ {
pthread_mutex_lock( &( LV2_Base_Ptr->Load_Mutex)); pthread_mutex_lock( &( LV2_Base_Ptr->Load_Mutex));
fprintf( stderr, "UnTrigger Sample: Id: (%d) Offset: (%d)\n", Sample_Id, Offset); if( LV2_Base_Ptr->Sustain == true)
{
fprintf( stderr, "Sustains Sample: Id: (%d) Offset: (%d)\n", Sample_Id, Offset);
LV2_Base_Ptr->Samples[Sample_Id].Sustained = true;
}
else
{
fprintf( stderr, "UnTrigger Sample: Id: (%d) Offset: (%d)\n", Sample_Id, Offset);
/* /*
if (nn >= 0 && nn < drmr->num_samples) { if (nn >= 0 && nn < drmr->num_samples) {
@ -458,8 +469,10 @@ static inline void DR_Sample_Untrigger( DRT_LV2_Base *LV2_Base_Ptr, int Sampl
drmr->samples[nn].dataoffset = offset; drmr->samples[nn].dataoffset = offset;
} }
*/ */
LV2_Base_Ptr->Samples[Sample_Id].Active = 0; LV2_Base_Ptr->Samples[Sample_Id].Active = 0;
LV2_Base_Ptr->Samples[Sample_Id].Data_Offset = Offset; LV2_Base_Ptr->Samples[Sample_Id].Data_Offset = Offset;
LV2_Base_Ptr->Samples[Sample_Id].Sustained = false;
}
pthread_mutex_unlock( &( LV2_Base_Ptr->Load_Mutex)); pthread_mutex_unlock( &( LV2_Base_Ptr->Load_Mutex));
} }
@ -468,6 +481,28 @@ static inline void DR_Sample_Untrigger( DRT_LV2_Base *LV2_Base_Ptr, int Sampl
/*----------------------------------------------------------------------------*/
/* */
/*----------------------------------------------------------------------------*/
static inline void DR_Sample_UnSustain( DRT_LV2_Base *LV2_Base_Ptr, uint32_t Offset)
{
int sample_id;
for( sample_id =0; sample_id < DRD_PORT_NUMBER_MAX; sample_id++)
{
if( LV2_Base_Ptr->Samples[sample_id].Sustained)
{
DR_Sample_Untrigger( LV2_Base_Ptr, sample_id, Offset);
}
}
}
/*----------------------------------------------------------------------------*/ /*----------------------------------------------------------------------------*/
/* */ /* */
/*----------------------------------------------------------------------------*/ /*----------------------------------------------------------------------------*/
@ -502,12 +537,11 @@ static LV2_Handle DR_LV2_Instantiate( const LV2_Descriptor *LV2_Descriptor
lv2_base_ptr->Current_Path = NULL; lv2_base_ptr->Current_Path = NULL;
lv2_base_ptr->CurReq = -1; lv2_base_ptr->CurReq = -1;
lv2_base_ptr->Ignore_Velocity = false;
lv2_base_ptr->Ignore_Note_Off = true;
// lv2_base_ptr->Ignore_Note_Off = false;
lv2_base_ptr->Channel_Nb = 0; lv2_base_ptr->Channel_Nb = 0;
lv2_base_ptr->Zero_Position = 0; lv2_base_ptr->Zero_Position = 0;
lv2_base_ptr->Sustain = false;
lv2_base_ptr->Sample_Number = 0; lv2_base_ptr->Sample_Number = 0;
while( *LV2_Features_Ptr != NULL) while( *LV2_Features_Ptr != NULL)
@ -761,7 +795,7 @@ static void DR_LV2_Run( LV2_Handle Instance_Ptr, uint32_t N_Samples)
{ {
case 8: // Note Off case 8: // Note Off
{ {
if( !lv2_base_ptr->Ignore_Note_Off) if( !lv2_base_ptr->Base.Note_Off_Ignore)
{ {
nn = data[1]; nn = data[1];
nn -= basenote; nn -= basenote;
@ -805,6 +839,27 @@ static void DR_LV2_Run( LV2_Handle Instance_Ptr, uint32_t N_Samples)
break; break;
} }
case 64: // Sustain
{
fprintf( stderr, "Sustain: (%d)!\n", value);
if( value == 127)
{
lv2_base_ptr->Sustain = true;
}
else
{
lv2_base_ptr->Sustain = false;
}
if( !( lv2_base_ptr->Sustain))
{
DR_Sample_UnSustain( lv2_base_ptr, offset);
}
break;
}
default: default:
{ {
fprintf( stderr, "Unhandled controler: (%d) value: (%d)!\n", controler, value); fprintf( stderr, "Unhandled controler: (%d) value: (%d)!\n", controler, value);
@ -919,10 +974,10 @@ static void DR_LV2_Run( LV2_Handle Instance_Ptr, uint32_t N_Samples)
} }
if( ignvel) if( ignvel)
lv2_base_ptr->Ignore_Velocity = ((const LV2_Atom_Bool*)ignvel)->body; lv2_base_ptr->Base.Velocity_Ignore = ((const LV2_Atom_Bool*)ignvel)->body;
if( ignno) if( ignno)
lv2_base_ptr->Ignore_Note_Off = ((const LV2_Atom_Bool*)ignno)->body; lv2_base_ptr->Base.Note_Off_Ignore = ((const LV2_Atom_Bool*)ignno)->body;
if( channel_nb) if( channel_nb)
lv2_base_ptr->Channel_Nb = ((const LV2_Atom_Int*)channel_nb)->body; lv2_base_ptr->Channel_Nb = ((const LV2_Atom_Int*)channel_nb)->body;

View File

@ -233,9 +233,9 @@ typedef struct
char Active; char Active;
uint32_t Offset; uint32_t Offset;
float Velocity; float Velocity;
bool Sustained;
} DRT_Sample; } DRT_Sample;
typedef struct DRT_LV2_Base typedef struct DRT_LV2_Base
{ {
DRT_Base Base; DRT_Base Base;
@ -252,14 +252,14 @@ typedef struct DRT_LV2_Base
// params // params
int Channel; int Channel;
bool Ignore_Velocity;
bool Ignore_Note_Off;
int Channel_Nb; int Channel_Nb;
int Zero_Position; int Zero_Position;
float **Gains; float **Gains;
float **Pans; float **Pans;
float *BaseNote; float *BaseNote;
bool Sustain;
// URIs // URIs
LV2_URID_Map *Map_Ptr; LV2_URID_Map *Map_Ptr;
DRT_Drummer_URIS URIS; DRT_Drummer_URIS URIS;
@ -448,6 +448,16 @@ static inline void DR_Sample_Untrigger( DRT_LV2_Base *, int, uint32_t);
/*----------------------------------------------------------------------------*/
/* */
/*----------------------------------------------------------------------------*/
static inline void DR_Sample_UnSustain( DRT_LV2_Base *, uint32_t);
/*----------------------------------------------------------------------------*/ /*----------------------------------------------------------------------------*/
/* */ /* */
/*----------------------------------------------------------------------------*/ /*----------------------------------------------------------------------------*/