From 5113c24f6aabc4b1aab8ff1f799538b24a86e067 Mon Sep 17 00:00:00 2001 From: "Arnaud G. GIBERT" Date: Wed, 27 Apr 2022 23:13:33 +0200 Subject: [PATCH] - Add Multithread task support, - Add task layer loading support, - Add task kit loading support, - Add task kits loading support. --- datastruct.c | 1031 +++++++++++++++++++++++++++++++++++++++++--------- datastruct.h | 375 +++++++++++++++--- drummer.c | 79 +++- drummer.h | 37 +- go.sh | 2 +- lv2_plugin.c | 48 ++- 6 files changed, 1302 insertions(+), 270 deletions(-) diff --git a/datastruct.c b/datastruct.c index aa5afc8..2e11d21 100644 --- a/datastruct.c +++ b/datastruct.c @@ -90,13 +90,14 @@ NDT_Status DR_Layer_Manager( NDT_Root *Root_Ptr, NDT_Index_Id Index_Id, NDT_N strncpy( ( *Layer_Ptr_Ptr)->FileName, Layer_Template_Ptr->FileName, DRD_FILENAME_SIZE); - ( *Layer_Ptr_Ptr)->Sample_Ptr = Layer_Template_Ptr->Sample_Ptr; - ( *Layer_Ptr_Ptr)->Sample_Size = Layer_Template_Ptr->Sample_Size; - ( *Layer_Ptr_Ptr)->SF_Info_Ptr = Layer_Template_Ptr->SF_Info_Ptr; - ( *Layer_Ptr_Ptr)->Min = Layer_Template_Ptr->Min; - ( *Layer_Ptr_Ptr)->Max = Layer_Template_Ptr->Max; - ( *Layer_Ptr_Ptr)->Gain = Layer_Template_Ptr->Gain; - ( *Layer_Ptr_Ptr)->Pitch = Layer_Template_Ptr->Pitch; + ( *Layer_Ptr_Ptr)->Sample_Ptr = Layer_Template_Ptr->Sample_Ptr; + ( *Layer_Ptr_Ptr)->Sample_Size = Layer_Template_Ptr->Sample_Size; + ( *Layer_Ptr_Ptr)->SF_Info_Ptr = Layer_Template_Ptr->SF_Info_Ptr; + ( *Layer_Ptr_Ptr)->Min = Layer_Template_Ptr->Min; + ( *Layer_Ptr_Ptr)->Max = Layer_Template_Ptr->Max; + ( *Layer_Ptr_Ptr)->Gain = Layer_Template_Ptr->Gain; + ( *Layer_Ptr_Ptr)->Pitch = Layer_Template_Ptr->Pitch; + ( *Layer_Ptr_Ptr)->Instrument_Ptr = Layer_Template_Ptr->Instrument_Ptr; return( NDS_OK); } @@ -305,19 +306,22 @@ NDT_Status DR_Layer_Manager( NDT_Root *Root_Ptr, NDT_Index_Id Index_Id, NDT_N { ND_VA_ARG_GET( Next_Node_Ptr, *Args_Ptr, NDT_Node *); - ND_VA_LIST_OPEN( user_args, *Args_Ptr); + DRT_Layer *Layer_Ptr = Node_Ptr->Value; + DRT_Status status; - ND_VA_ARG_GET( Kit_Ptr, user_args, DRT_Kit *); - ND_VA_ARG_GET( SampleRate, user_args, DRT_SampleRate); - ND_VA_LIST_CLOSE( user_args); - - DRT_Layer *Layer_Ptr = Node_Ptr->Value; - - Command_Name = "NDD_CMD_SAMPLE_LOAD"; - if( DR_Layer_Sample_Load( Layer_Ptr, Kit_Ptr, SampleRate) != DRS_OK) + if( Layer_Ptr->Instrument_Ptr->Kit_Ptr->Base_Ptr->Task_Load_Layer_Flag == DRD_TRUE) + { + status = DR_Task_Run( Layer_Ptr->Instrument_Ptr->Kit_Ptr->Base_Ptr, DRD_TASK_CMD_LAYER_LOAD, (void *)Layer_Ptr); + } + else + { + status = DR_Layer_Sample_Load( Layer_Ptr); + } + + if( status != DRS_OK) { return( NDS_OK); // Continue even if sample loading fail... } @@ -330,7 +334,7 @@ NDT_Status DR_Layer_Manager( NDT_Root *Root_Ptr, NDT_Index_Id Index_Id, NDT_N case NDD_CMD_SAMPLE_UNLOAD: { ND_VA_ARG_GET( Next_Node_Ptr, *Args_Ptr, NDT_Node *); - + ND_VA_LIST_OPEN( user_args, *Args_Ptr); ND_VA_ARG_GET( Kit_Ptr, user_args, DRT_Kit *); @@ -451,6 +455,7 @@ NDT_Status DR_Instrument_Manager( NDT_Root *Root_Ptr, NDT_Index_Id Index_Id, ( *Instrument_Ptr_Ptr)->Gain = Instrument_Template_Ptr->Gain; ( *Instrument_Ptr_Ptr)->Pan_Left = Instrument_Template_Ptr->Pan_Left; ( *Instrument_Ptr_Ptr)->Pan_Right = Instrument_Template_Ptr->Pan_Right; + ( *Instrument_Ptr_Ptr)->Kit_Ptr = Instrument_Template_Ptr->Kit_Ptr; if( ( nd_status = ND_DataStruct_Open( &((*Instrument_Ptr_Ptr)->Layer_DS_Ptr), DRD_LAYER_DS_INDEX_NB, DRG_Layer_DS_Index_Tab_Initial, "Layer_Manager", DR_Layer_Manager, NULL, NULL, NULL, NULL, NDD_TRUE, NULL)) != NDS_OK) @@ -693,18 +698,29 @@ NDT_Status DR_Instrument_Manager( NDT_Root *Root_Ptr, NDT_Index_Id Index_Id, { ND_VA_ARG_GET( Next_Node_Ptr, *Args_Ptr, NDT_Node *); - ND_VA_LIST_OPEN( user_args, *Args_Ptr); - - ND_VA_ARG_GET( Kit_Ptr, user_args, DRT_Kit *); - ND_VA_ARG_GET( SampleRate, user_args, DRT_SampleRate); - - ND_VA_LIST_CLOSE( user_args); - DRT_Instrument *Instrument_Ptr = Node_Ptr->Value; + DRT_Status status; + Command_Name = "NDD_CMD_SAMPLE_LOAD"; - return( ND_DataStruct_Traverse( Instrument_Ptr->Layer_DS_Ptr, NDD_CMD_SAMPLE_LOAD, Kit_Ptr, SampleRate)); + if( Instrument_Ptr->Kit_Ptr->Base_Ptr->Task_Load_Instrument_Flag == DRD_TRUE) + { + status = DR_Task_Run( Instrument_Ptr->Kit_Ptr->Base_Ptr, DRD_TASK_CMD_INSTRUMENT_LOAD, (void *)Instrument_Ptr); + } + else + { + status = DR_Instrument_Sample_Load( Instrument_Ptr); + } + + if( status != DRS_OK) + { + return( NDS_OK); // Continue even if sample loading fail... + } + else + { + return( NDS_OK); + } } case NDD_CMD_SAMPLE_UNLOAD: @@ -719,9 +735,19 @@ NDT_Status DR_Instrument_Manager( NDT_Root *Root_Ptr, NDT_Index_Id Index_Id, DRT_Instrument *Instrument_Ptr = Node_Ptr->Value; + Command_Name = "NDD_CMD_SAMPLE_UNLOAD"; - return( ND_DataStruct_Traverse( Instrument_Ptr->Layer_DS_Ptr, NDD_CMD_SAMPLE_UNLOAD, Kit_Ptr)); + if( DR_Instrument_Sample_UnLoad( Instrument_Ptr) != DRS_OK) + { + return( NDS_KO); + } + else + { + return( NDS_OK); + } + +// return( ND_DataStruct_Traverse( Instrument_Ptr->Layer_DS_Ptr, NDD_CMD_SAMPLE_UNLOAD, Kit_Ptr)); } default: @@ -787,12 +813,13 @@ NDT_Status DR_Kit_Manager( NDT_Root *Root_Ptr, NDT_Index_Id Index_Id, NDT_Nod case NDD_CMD_VALUE_FIND: { - if( Kit_Ptr->Name[0] == 0) + if( Kit_Ptr->Id != DRD_ID_UNKNOWN) { *Reply_Index_Id_Ptr = 0; } else { +// fprintf( stderr, "IDX 1!\n"); *Reply_Index_Id_Ptr = 1; } @@ -809,6 +836,13 @@ NDT_Status DR_Kit_Manager( NDT_Root *Root_Ptr, NDT_Index_Id Index_Id, NDT_Nod case NDD_CMD_VALUE_PRINT: case NDD_CMD_INDEX1_PRINT: + { + *Reply_Index_Id_Ptr = 0; + *Reply_Command_Ptr = Cmd; + break; + } + + case NDD_CMD_ID_REFRESH: { *Reply_Index_Id_Ptr = 1; *Reply_Command_Ptr = Cmd; @@ -840,13 +874,16 @@ NDT_Status DR_Kit_Manager( NDT_Root *Root_Ptr, NDT_Index_Id Index_Id, NDT_Nod if( ( *Kit_Ptr_Ptr = (DRT_Kit *)malloc( sizeof( DRT_Kit))) != NULL) { - ( *Kit_Ptr_Ptr)->Id = Root_Ptr->Index_Tab[NDD_INDEX_PRIMARY].Node_Number; - ( *Kit_Ptr_Ptr)->Status = Kit_Template_Ptr->Status; + ( *Kit_Ptr_Ptr)->Id = Root_Ptr->Index_Tab[NDD_INDEX_PRIMARY].Node_Number; + ( *Kit_Ptr_Ptr)->Logical_Id = Kit_Template_Ptr->Logical_Id; + ( *Kit_Ptr_Ptr)->Status = Kit_Template_Ptr->Status; strncpy( ( *Kit_Ptr_Ptr)->Name, Kit_Template_Ptr->Name, DRD_NAME_SIZE); strncpy( ( *Kit_Ptr_Ptr)->Desc, Kit_Template_Ptr->Desc, DRD_DESC_SIZE); strncpy( ( *Kit_Ptr_Ptr)->DirName, Kit_Template_Ptr->DirName, DRD_DIRNAME_SIZE); + ( *Kit_Ptr_Ptr)->Base_Ptr = Kit_Template_Ptr->Base_Ptr; + if( ( nd_status = ND_DataStruct_Open( &((*Kit_Ptr_Ptr)->Instrument_DS_Ptr), DRD_INSTRUMENT_DS_INDEX_NB, DRG_Instrument_DS_Index_Tab_Initial, "Instrument_Manager", DR_Instrument_Manager, NULL, NULL, NULL, NULL, NDD_TRUE, NULL)) != NDS_OK) { printf( "DR_Kit_Manager: ND_DataStruct_Open() failed (%d)!\n", nd_status); @@ -901,7 +938,15 @@ NDT_Status DR_Kit_Manager( NDT_Root *Root_Ptr, NDT_Index_Id Index_Id, NDT_Nod case 1: { - rc = strcmp( Kit1_Ptr->Name, Kit2_Ptr->Name); + if( ( Kit1_Ptr->Name[0] != 0) && ( Kit2_Ptr->Name[0] != 0)) + { + rc = strcmp( Kit1_Ptr->Name, Kit2_Ptr->Name); + } + else + { + rc = Kit1_Ptr->Logical_Id - Kit2_Ptr->Logical_Id; +// fprintf( stderr, "Comp: (%ld) - (%ld) = (%ld)\n", Kit1_Ptr->Logical_Id, Kit2_Ptr->Logical_Id, rc); + } break; } @@ -989,7 +1034,45 @@ NDT_Status DR_Kit_Manager( NDT_Root *Root_Ptr, NDT_Index_Id Index_Id, NDT_Nod Command_Name = "NDD_CMD_VALUE_PRINT"; +/* + fprintf( stderr, "CurId: (%03ld)", Kit_Ptr->Id); + fprintf( stderr, " ParentId: "); + if( Node_Ptr->Parent == NULL) + { + fprintf( stderr, "NULL "); + } + else + { + fprintf( stderr, "(%03ld)", ((DRT_Kit *)Node_Ptr->Parent->Value)->Id); + } + + fprintf( stderr, " LeftId: "); + + if( Node_Ptr->Left == NULL) + { + fprintf( stderr, "NULL "); + } + else + { + fprintf( stderr, "(%03ld)", ((DRT_Kit *)Node_Ptr->Left->Value)->Id); + } + + fprintf( stderr, " RightId: "); + + if( Node_Ptr->Right == NULL) + { + fprintf( stderr, "NULL "); + } + else + { + fprintf( stderr, "(%03ld)", ((DRT_Kit *)Node_Ptr->Right->Value)->Id); + } + + fprintf( stderr, "\n"); + + return( NDS_OK); +*/ if( DR_Kit_Dump( Kit_Ptr, ++(*Count_Ptr)) != DRS_OK) { return( NDS_KO); @@ -1028,6 +1111,32 @@ NDT_Status DR_Kit_Manager( NDT_Root *Root_Ptr, NDT_Index_Id Index_Id, NDT_Nod return( NDS_OK); } + case NDD_CMD_GRAPH_PRINT: + { + ND_VA_ARG_GET( Next_Node_Ptr, *Args_Ptr, NDT_Node *); + + DRT_Kit *Kit_Ptr = Node_Ptr->Value; + + + Command_Name = "NDD_CMD_GRAPH_PRINT"; + + fprintf( stderr, " node%03ld [ label =\" | %03ld\\n%20s | \"];\n", Kit_Ptr->Id, Kit_Ptr->Id, Kit_Ptr->Name); + + if( Node_Ptr->Left != NULL) + { + fprintf( stderr, " node%03ld:f0 -> node%03ld:f1;\n", Kit_Ptr->Id, ((DRT_Kit *)Node_Ptr->Left->Value)->Id); + } + + if( Node_Ptr->Right != NULL) + { + fprintf( stderr, " node%03ld:f2 -> node%03ld:f1;\n", Kit_Ptr->Id, ((DRT_Kit *)Node_Ptr->Right->Value)->Id); + } + + fprintf( stderr, "\n"); + + return( NDS_OK); + } + case NDD_CMD_INDEX0_PRINT: case NDD_CMD_INDEX1_PRINT: { @@ -1055,6 +1164,26 @@ NDT_Status DR_Kit_Manager( NDT_Root *Root_Ptr, NDT_Index_Id Index_Id, NDT_Nod } } + case NDD_CMD_ID_REFRESH: + { + ND_VA_ARG_GET( Next_Node_Ptr, *Args_Ptr, NDT_Node *); + + ND_VA_LIST_OPEN( user_args, *Args_Ptr); + + ND_VA_ARG_GET( Count_Ptr, user_args, DRT_Kit_Id *); + + ND_VA_LIST_CLOSE( user_args); + + DRT_Kit *Kit_Ptr = Node_Ptr->Value; + + + Command_Name = "NDD_CMD_ID_REFRESH"; + + Kit_Ptr->Logical_Id = (*Count_Ptr)++; + + return( NDS_OK); + } + case NDD_CMD_STATS_GET: { ND_VA_ARG_GET( Next_Node_Ptr, *Args_Ptr, NDT_Node *); @@ -1082,18 +1211,22 @@ NDT_Status DR_Kit_Manager( NDT_Root *Root_Ptr, NDT_Index_Id Index_Id, NDT_Nod { ND_VA_ARG_GET( Next_Node_Ptr, *Args_Ptr, NDT_Node *); - ND_VA_LIST_OPEN( user_args, *Args_Ptr); + DRT_Kit *Kit_Ptr = Node_Ptr->Value; + DRT_Status status; - ND_VA_ARG_GET( SampleRate, user_args, DRT_SampleRate); - ND_VA_LIST_CLOSE( user_args); - - DRT_Kit *Kit_Ptr = Node_Ptr->Value; - - Command_Name = "NDD_CMD_SAMPLE_LOAD"; - if( DR_Kit_Sample_Load( Kit_Ptr, SampleRate) != DRS_OK) + if( Kit_Ptr->Base_Ptr->Task_Load_Kit_Flag == DRD_TRUE) + { + status = DR_Task_Run( Kit_Ptr->Base_Ptr, DRD_TASK_CMD_KIT_LOAD, (void *)Kit_Ptr); + } + else + { + status = DR_Kit_Sample_Load( Kit_Ptr); + } + + if( status != DRS_OK) { return( NDS_KO); } @@ -1201,13 +1334,14 @@ DRT_Status DR_Layer_Add( DRT_Layer **Layer_Ptr_Ptr, NDT_Root *DS_Ptr, DRT_Lay /*----------------------------------------------------------------------------*/ -/* DR_Layer_Sample_Load */ +/* DR_Layer_Sample_Load */ /*----------------------------------------------------------------------------*/ -DRT_Status DR_Layer_Sample_Load( DRT_Layer *Layer_Ptr, DRT_Kit *Kit_Ptr, DRT_SampleRate SampleRate) +DRT_Status DR_Layer_Sample_Load( DRT_Layer *Layer_Ptr) { - DRT_Status status; - SNDFILE *sndf_ptr; + DRT_Status status; + SNDFILE *sndf_ptr; + DRT_SampleRate samplerate = Layer_Ptr->Instrument_Ptr->Kit_Ptr->Base_Ptr->SampleRate; // fprintf( stderr, "Loading: [%s]\n", Layer_Ptr->FileName); @@ -1216,12 +1350,12 @@ DRT_Status DR_Layer_Sample_Load( DRT_Layer *Layer_Ptr, DRT_Kit *Kit_Ptr, DRT_ { if( Layer_Ptr->Sample_Ptr != NULL) { -// fprintf( stderr, "Skiping: [%s]\n", Layer_Ptr->FileName); + fprintf( stderr, "Skiping: [%s]\n", Layer_Ptr->FileName); return( DRS_OK); } else { -// fprintf( stderr, "Freeing SF_Info: [%s]\n", Layer_Ptr->FileName); + fprintf( stderr, "Freeing SF_Info: [%s]\n", Layer_Ptr->FileName); free( Layer_Ptr->SF_Info_Ptr ); } } @@ -1229,7 +1363,7 @@ DRT_Status DR_Layer_Sample_Load( DRT_Layer *Layer_Ptr, DRT_Kit *Kit_Ptr, DRT_ { if( Layer_Ptr->Sample_Ptr != NULL) { -// fprintf( stderr, "Freeing Sample: [%s]\n", Layer_Ptr->FileName); + fprintf( stderr, "Freeing Sample: [%s]\n", Layer_Ptr->FileName); free( Layer_Ptr->Sample_Ptr); } } @@ -1270,18 +1404,18 @@ DRT_Status DR_Layer_Sample_Load( DRT_Layer *Layer_Ptr, DRT_Kit *Kit_Ptr, DRT_ status = DRS_OK; // convert rate if needed - if( Layer_Ptr->SF_Info_Ptr->samplerate != SampleRate) + if( Layer_Ptr->SF_Info_Ptr->samplerate != samplerate) { int src_status; SRC_DATA src_data; long out_size; -// fprintf( stderr, "Resampling: [%s] (%d)->(%d)\n", Layer_Ptr->FileName, Layer_Ptr->SF_Info_Ptr->samplerate, SampleRate); +// fprintf( stderr, "Resampling: [%s] (%d)->(%d)\n", Layer_Ptr->FileName, Layer_Ptr->SF_Info_Ptr->samplerate, samplerate); src_data.data_in = Layer_Ptr->Sample_Ptr; src_data.input_frames = Layer_Ptr->SF_Info_Ptr->frames; - src_data.src_ratio = (double)SampleRate / Layer_Ptr->SF_Info_Ptr->samplerate; + src_data.src_ratio = (double)samplerate / Layer_Ptr->SF_Info_Ptr->samplerate; src_data.output_frames = (long)ceil( Layer_Ptr->SF_Info_Ptr->frames * src_data.src_ratio); out_size = src_data.output_frames * Layer_Ptr->SF_Info_Ptr->channels * sizeof( float); @@ -1312,7 +1446,7 @@ DRT_Status DR_Layer_Sample_Load( DRT_Layer *Layer_Ptr, DRT_Kit *Kit_Ptr, DRT_ Layer_Ptr->Sample_Ptr = src_data.data_out; Layer_Ptr->Sample_Size = src_data.output_frames_gen * Layer_Ptr->SF_Info_Ptr->channels; - Layer_Ptr->SF_Info_Ptr->samplerate = SampleRate; + Layer_Ptr->SF_Info_Ptr->samplerate = samplerate; Layer_Ptr->SF_Info_Ptr->frames = src_data.output_frames_gen; } } @@ -1368,8 +1502,8 @@ DRT_Status DR_Layer_Dump( DRT_Layer *Layer_Ptr, long count) fprintf( stderr, " %03ld) Layer: ", count); - fprintf( stderr, " Id: (% 3d) File Name: [%s] Sample: [%lx] Sample Size: (%ld) Min: (%f) Max: (%f) Gain: (%f) Pitch: (%f)", - Layer_Ptr->Id, Layer_Ptr->FileName, (long)Layer_Ptr->Sample_Ptr, Layer_Ptr->Sample_Size, Layer_Ptr->Min, Layer_Ptr->Max, Layer_Ptr->Gain, Layer_Ptr->Pitch); + fprintf( stderr, " Id: (% 3ld) File Name: [%s] Sample: [%lx] Sample Size: (%ld) Min: (%f) Max: (%f) Gain: (%f) Pitch: (%f) Intrument Id: (% 3ld)", + Layer_Ptr->Id, Layer_Ptr->FileName, (long)Layer_Ptr->Sample_Ptr, Layer_Ptr->Sample_Size, Layer_Ptr->Min, Layer_Ptr->Max, Layer_Ptr->Gain, Layer_Ptr->Pitch, Layer_Ptr->Instrument_Ptr->Id); if( Layer_Ptr->SF_Info_Ptr != NULL) { @@ -1428,8 +1562,8 @@ DRT_Status DR_Instrument_Dump( DRT_Instrument *Instrument_Ptr, long Count) fprintf( stderr, " %03ld) Instrument: ", Count); - fprintf( stderr, " Id: (% 3d) Name: [%s] Gain: (%f) Pan Left: (%f) Pan Right: (%f)\n", - Instrument_Ptr->Id, Instrument_Ptr->Name, Instrument_Ptr->Gain, Instrument_Ptr->Pan_Left, Instrument_Ptr->Pan_Right); + fprintf( stderr, " Id: (% 3ld) Name: [%s] Gain: (%f) Pan Left: (%f) Pan Right: (%f) Kit Id: (% 3ld)\n", + Instrument_Ptr->Id, Instrument_Ptr->Name, Instrument_Ptr->Gain, Instrument_Ptr->Pan_Left, Instrument_Ptr->Pan_Right, Instrument_Ptr->Kit_Ptr->Id); // fprintf( stderr, "\n"); fprintf( stderr, " Layer List:\n"); @@ -1457,6 +1591,50 @@ DRT_Status DR_Instrument_Dump( DRT_Instrument *Instrument_Ptr, long Count) +/*----------------------------------------------------------------------------*/ +/* DR_Instrument_Sample_Load */ +/*----------------------------------------------------------------------------*/ + +DRT_Status DR_Instrument_Sample_Load( DRT_Instrument *Instrument_Ptr) +{ + NDT_Status nd_status; + + + if( ( nd_status = ND_DataStruct_Traverse( Instrument_Ptr->Layer_DS_Ptr, NDD_CMD_SAMPLE_LOAD)) != NDS_OK) + { + printf( "Cant't load instrument samples: ND_DataStruct_Traverse() failed (%d)!\n", nd_status); + return( DRS_KO); + } + + return( DRS_OK); +} + + + + + +/*----------------------------------------------------------------------------*/ +/* DR_Instrument_Sample_UnLoad */ +/*----------------------------------------------------------------------------*/ + +DRT_Status DR_Instrument_Sample_UnLoad( DRT_Instrument *Instrument_Ptr) +{ + NDT_Status nd_status; + + + if( ( nd_status = ND_DataStruct_Traverse( Instrument_Ptr->Layer_DS_Ptr, NDD_CMD_SAMPLE_UNLOAD)) != NDS_OK) + { + printf( "Cant't unload instrument samples: ND_DataStruct_Traverse() failed (%d)\n", nd_status); + return( DRS_KO); + } + + return( DRS_OK); +} + + + + + /*----------------------------------------------------------------------------*/ /* DR_Kit_Add */ /*----------------------------------------------------------------------------*/ @@ -1498,8 +1676,9 @@ DRT_Status DR_Kit_Id_Find( DRT_Kit **Kit_Ptr_Ptr, NDT_Root *DS_Ptr, DRT_Kit_I DRT_Kit kit_template; - kit_template.Id = Kit_Id; - kit_template.Name[0] = '\0'; + kit_template.Id = Kit_Id; + kit_template.Logical_Id = DRD_ID_UNKNOWN; + kit_template.Name[0] = '\0'; if( ( nd_status = ND_DataStruct_Value_Find( (void **)Kit_Ptr_Ptr, DS_Ptr, &kit_template)) != NDS_OK) { @@ -1527,17 +1706,58 @@ DRT_Status DR_Kit_Id_Find( DRT_Kit **Kit_Ptr_Ptr, NDT_Root *DS_Ptr, DRT_Kit_I /*----------------------------------------------------------------------------*/ -/* DR_Kit_Name_Find */ +/* DR_Kit_Logical_Id_Find */ /*----------------------------------------------------------------------------*/ -extern DRT_Status DR_Kit_Name_Find( DRT_Kit **Kit_Ptr_Ptr, NDT_Root *DS_Ptr, char *Kit_Name) +DRT_Status DR_Kit_Logical_Id_Find( DRT_Kit **Kit_Ptr_Ptr, NDT_Root *DS_Ptr, DRT_Kit_Id Kit_Logical_Id) { DRT_Status status; NDT_Status nd_status; DRT_Kit kit_template; - kit_template.Id = 1; + kit_template.Id = DRD_ID_UNKNOWN; + kit_template.Logical_Id = Kit_Logical_Id; + kit_template.Name[0] = '\0'; + + if( ( nd_status = ND_DataStruct_Value_Find( (void **)Kit_Ptr_Ptr, DS_Ptr, &kit_template)) != NDS_OK) + { + fprintf( stderr, "DR_Kit_Logical_Id_Find: Find error: (%d)!\n", nd_status); + status = DRS_KO; + } + else + { + if( *Kit_Ptr_Ptr == NULL) + { + fprintf( stderr, "Can't find: kit with logical id: [%d]!\n", Kit_Logical_Id); + status = DRS_KO; + } + else + { + status = DRS_OK; + } + } + + return( status); +} + + + + + +/*----------------------------------------------------------------------------*/ +/* DR_Kit_Name_Find */ +/*----------------------------------------------------------------------------*/ + +DRT_Status DR_Kit_Name_Find( DRT_Kit **Kit_Ptr_Ptr, NDT_Root *DS_Ptr, char *Kit_Name) +{ + DRT_Status status; + NDT_Status nd_status; + DRT_Kit kit_template; + + + kit_template.Id = DRD_ID_UNKNOWN; + kit_template.Logical_Id = DRD_ID_UNKNOWN; strncpy( kit_template.Name, Kit_Name, DRD_NAME_LEN); if( ( nd_status = ND_DataStruct_Value_Find( (void **)Kit_Ptr_Ptr, DS_Ptr, &kit_template)) != NDS_OK) @@ -1577,21 +1797,29 @@ DRT_Status DR_Kit_Dump( DRT_Kit *Kit_Ptr, long Count) fprintf( stderr, " %03ld) DrumKit:", Count); - fprintf( stderr, " Id: (% 3d) Status: [%s] Name: [%s] Descrition: [%.32s] Dir Name: [%s]\n", Kit_Ptr->Id, DR_LOAD_STATUS_ASCII_GET( Kit_Ptr), Kit_Ptr->Name, Kit_Ptr->Desc, Kit_Ptr->DirName); + fprintf( stderr, " Id: (% 3ld) Logical Id: (% 3ld) Status: [%s] Name: [%s] Descrition: [%.32s] Dir Name: [%s] Sample Rate: (%d)\n", + Kit_Ptr->Id, Kit_Ptr->Logical_Id, DR_LOAD_STATUS_ASCII_GET( Kit_Ptr), Kit_Ptr->Name, Kit_Ptr->Desc, Kit_Ptr->DirName, Kit_Ptr->Base_Ptr->SampleRate); fprintf( stderr, " Instruments List:\n"); - if( Kit_Ptr->Instrument_DS_Ptr->Index_Tab[NDD_INDEX_PRIMARY].Node_Number == 0) + if( Kit_Ptr->Instrument_DS_Ptr == NULL) { - fprintf( stderr, " Empty list!\n"); + fprintf( stderr, " Uninitialised list!\n"); } else { - count2 = 0; - - if( ( nd_status = ND_DataStruct_Value_Print( stderr, Kit_Ptr->Instrument_DS_Ptr, NDD_RECURSIVE_MODE_PARENT_CHILD, 0, 0, &count2)) != NDS_OK) + if( Kit_Ptr->Instrument_DS_Ptr->Index_Tab[NDD_INDEX_PRIMARY].Node_Number == 0) { - printf( "Cant't dump intruments: ND_DataStruct_Traverse() failed (%d)!\n", nd_status); - return( DRS_KO); + fprintf( stderr, " Empty list!\n"); + } + else + { + count2 = 0; + + if( ( nd_status = ND_DataStruct_Value_Print( stderr, Kit_Ptr->Instrument_DS_Ptr, NDD_RECURSIVE_MODE_PARENT_CHILD, 0, 0, &count2)) != NDS_OK) + { + printf( "Cant't dump intruments: ND_DataStruct_Traverse() failed (%d)!\n", nd_status); + return( DRS_KO); + } } } @@ -1816,7 +2044,10 @@ DRT_Status DR_Kit_Load( DRT_Base *Base_Ptr, char *FileName) short i; - kit_template.Status = DRD_LOAD_STATUS_UNLOADED; + kit_template.Id = DRD_ID_UNKNOWN; + kit_template.Logical_Id = DRD_ID_UNKNOWN; + kit_template.Status = DRD_LOAD_STATUS_UNLOADED; + kit_template.Base_Ptr = Base_Ptr; layer_template.Sample_Ptr = NULL; layer_template.Sample_Size = 0; layer_template.SF_Info_Ptr = NULL; @@ -1872,6 +2103,8 @@ DRT_Status DR_Kit_Load( DRT_Base *Base_Ptr, char *FileName) status = DRS_OK; instrument = 1; + instrument_template.Kit_Ptr = kit_ptr; + while( ( status == DRS_OK) && ( sprintf( xpath_head, DRD_XML_XPATH_DRUMKIT_INSTRUMENT_FULL, instrument) > 0) && ( DR_XML_Node_Exist( xml_doc, xml_xpathCtx, xpath_head) == DRS_OK)) @@ -1896,6 +2129,8 @@ DRT_Status DR_Kit_Load( DRT_Base *Base_Ptr, char *FileName) } layer = 1; + layer_template.Instrument_Ptr = instrument_ptr; + while( ( status == DRS_OK) && ( sprintf( xpath_head, DRD_XML_XPATH_DRUMKIT_LAYER_FULL, instrument, layer) > 0) && ( DR_XML_Node_Exist( xml_doc, xml_xpathCtx, xpath_head) == DRS_OK)) @@ -1944,11 +2179,11 @@ DRT_Status DR_Kit_Load( DRT_Base *Base_Ptr, char *FileName) /* DR_Kit_Sample_Load */ /*----------------------------------------------------------------------------*/ -DRT_Status DR_Kit_Sample_Load( DRT_Kit *Kit_Ptr, DRT_SampleRate SampleRate) +DRT_Status DR_Kit_Sample_Load( DRT_Kit *Kit_Ptr) { NDT_Status nd_status; - + if( DR_LOAD_STATUS_LOADED_IS( Kit_Ptr)) { fprintf( stderr, "Loading kit samples: [%s] Skiped!\n", Kit_Ptr->Name); @@ -1957,12 +2192,21 @@ DRT_Status DR_Kit_Sample_Load( DRT_Kit *Kit_Ptr, DRT_SampleRate SampleRate) { fprintf( stderr, "Loading kit samples: [%s]!\n", Kit_Ptr->Name); - if( ( nd_status = ND_DataStruct_Traverse( Kit_Ptr->Instrument_DS_Ptr, NDD_CMD_SAMPLE_LOAD, Kit_Ptr, SampleRate)) != NDS_OK) + if( ( nd_status = ND_DataStruct_Traverse( Kit_Ptr->Instrument_DS_Ptr, NDD_CMD_SAMPLE_LOAD)) != NDS_OK) { printf( "Cant't load kit samples: ND_DataStruct_Traverse() failed (%d)!\n", nd_status); return( DRS_KO); } + if( Kit_Ptr->Base_Ptr->Task_Sync_Kit_Flag == DRD_TRUE) + { + if( DR_Task_Wait( Kit_Ptr->Base_Ptr) != DRS_OK) + { + printf( "DR_Task_Wait failed (%d)!\n"); + return( DRS_KO); + } + } + Kit_Ptr->Status = DRD_LOAD_STATUS_LOADED; } @@ -2006,64 +2250,6 @@ DRT_Status DR_Kit_Sample_UnLoad( DRT_Kit *Kit_Ptr) -/*----------------------------------------------------------------------------*/ -/* DR_DataStruct_Init */ -/*----------------------------------------------------------------------------*/ - -DRT_Status DR_DataStruct_Init( DRT_Base *Base_Ptr, DRT_SampleRate SampleRate) -{ - NDT_Status nd_status; - - - if( ( nd_status = ND_Library_Open( NDD_TRUE)) != NDS_OK) - { - fprintf( stderr, "Can't open node library (%d)!\n", nd_status); - return( DRS_KO); - } - - if( ( nd_status = ND_DataStruct_Open( &(Base_Ptr->Kit_DS_Ptr), DRD_KIT_DS_INDEX_NB, DRG_Kit_DS_Index_Tab_Initial, "Kit_Manager", DR_Kit_Manager, NULL, NULL, NULL, NULL, NDD_TRUE, NULL)) != NDS_OK) - { - printf( "Kit_DS: ND_DataStruct_Open() failed (%d)!\n", nd_status); - return( DRS_KO); - } - - Base_Ptr->SampleRate = SampleRate; - - return( DRS_OK); -} - - - - - -/*----------------------------------------------------------------------------*/ -/* DR_DataStruct_DeInit */ -/*----------------------------------------------------------------------------*/ - -DRT_Status DR_DataStruct_DeInit( DRT_Base *Base_Ptr) -{ - NDT_Status nd_status; - - - if( ( nd_status = ND_DataStruct_Close( Base_Ptr->Kit_DS_Ptr)) != NDS_OK) - { - printf( "Can't close Kit_DS (%d)!\n", nd_status); - return( DRS_KO); - } - - if( ( nd_status = ND_Library_Close()) != NDS_OK) - { - fprintf( stderr, "Can't close node library (%d)!\n", nd_status); - return( DRS_KO); - } - - return( DRS_OK); -} - - - - - /*----------------------------------------------------------------------------*/ /* DR_Kits_Sample_Load */ /*----------------------------------------------------------------------------*/ @@ -2075,12 +2261,25 @@ DRT_Status DR_Kits_Sample_Load( DRT_Base *Base_Ptr) fprintf( stderr, "Loading kits samples!\n"); - if( ( nd_status = ND_DataStruct_Traverse( Base_Ptr->Kit_DS_Ptr, NDD_CMD_SAMPLE_LOAD, Base_Ptr->SampleRate)) != NDS_OK) + if( ( nd_status = ND_DataStruct_Traverse( Base_Ptr->Kit_DS_Ptr, NDD_CMD_SAMPLE_LOAD)) != NDS_OK) { printf( "Cant't load kit samples: ND_DataStruct_Traverse() failed (%d)!\n", nd_status); return( DRS_KO); } + if( Base_Ptr->Task_Sync_Kits_Flag == DRD_TRUE) + { + fprintf( stderr, "Synchronising...\n"); + + if( DR_Task_Wait( Base_Ptr) != DRS_OK) + { + fprintf( stderr, "DR_Task_Wait failed (%d)!\n"); + return( DRS_KO); + } + + fprintf( stderr, "Sync done!\n"); + } + return( DRS_OK); } @@ -2126,6 +2325,7 @@ DRT_Status DR_Kits_Load( DRT_Base *Base_Ptr) // char glob_pattern[] = "~/.hydrogen/data/drumkits/Mars Mod */drumkit.xml"; glob_t glob_struct; int i; + DRT_Kit_Id count = 0L; LIBXML_TEST_VERSION; @@ -2147,19 +2347,29 @@ DRT_Status DR_Kits_Load( DRT_Base *Base_Ptr) } } - if( ( nd_status = ND_DataStruct_Convert( Base_Ptr->Kit_DS_Ptr, DRG_Kit_DS_Index_Tab_Final)) != NDS_OK) + if( ( nd_status = ND_DataStruct_Traverse( Base_Ptr->Kit_DS_Ptr, NDD_CMD_ID_REFRESH, &count)) != NDS_OK) { - fprintf( stderr, "ND_DataStruct_Convert() failed (%d)!\n", status); + printf( "Can't refresh kit id: ND_DataStruct_Traverse() failed (%d)!\n", nd_status); status = DRS_KO; } else { - status = DRS_OK; + if( ( nd_status = ND_DataStruct_Convert( Base_Ptr->Kit_DS_Ptr, DRG_Kit_DS_Index_Tab_Final)) != NDS_OK) + { + fprintf( stderr, "ND_DataStruct_Convert() failed (%d)!\n", status); + status = DRS_KO; + } + else + { + status = DRS_OK; + } } } globfree( &glob_struct); +// DR_Kits_Dump( Base_Ptr); + // Base_Ptr->SampleRate=44100; /* @@ -2185,62 +2395,22 @@ DRT_Status DR_Kits_Load( DRT_Base *Base_Ptr) } - + /*----------------------------------------------------------------------------*/ -/* DR_Kits_Dump */ +/* DR_Kits_Stats_Dump */ /*----------------------------------------------------------------------------*/ -DRT_Status DR_Kits_Dump( DRT_Base *Base_Ptr) +DRT_Status DR_Kits_Stats_Dump( DRT_Base *Base_Ptr) { NDT_Status nd_status; - short index; - long count = 0; - long kit_count = 0; - long instrument_count = 0; - long layer_count = 0; - long sample_count = 0; + DRT_Id kit_count = 0; + DRT_Id instrument_count = 0; + DRT_Id layer_count = 0; + DRT_Id sample_count = 0; - fprintf( stderr, "\n"); - fprintf( stderr, "Sample Rate: (%d)\n", Base_Ptr->SampleRate); - fprintf( stderr, "DrumKit List:\n"); -/* - if( ( nd_status = ND_DataStruct_Info_Print( stderr, Base_Ptr->Kit_DS_Ptr, NDD_RECURSIVE_MODE_PARENT_CHILD, 0, 0)) != NDS_OK) - { - printf( "ND_DataStruct_Info_Print() failed (%d)!\n", nd_status); - return( DRS_KO); - } - - for( index = 0; index < Base_Ptr->Kit_DS_Ptr->Index_Nb; index++) - { - count = 0; - fprintf( stderr, "Dump Index: (%d)\n", index); - - if( Base_Ptr-> Kit_DS_Ptr->Index_Tab[index].Node_Number == 0) - { - fprintf( stderr, " Empty list!\n"); - } - else - { - if( ( nd_status = ND_DataStruct_Traverse( Base_Ptr->Kit_DS_Ptr, (NDD_CMD_INDEX0_PRINT + index), stderr, &count)) != NDS_OK) - { - printf( "Can't dump kits: ND_DataStruct_Traverse() failed (%d)!\n", nd_status); - return( DRS_KO); - } - } - - fprintf( stderr, "\n"); - } -*/ - - if( ( nd_status = ND_DataStruct_Value_Print( stderr, Base_Ptr->Kit_DS_Ptr, NDD_RECURSIVE_MODE_PARENT_CHILD, 0, 0, &count)) != NDS_OK) - { - printf( "Can't dump kits: ND_DataStruct_Value_Print() failed (%d)!\n", nd_status); - return( DRS_KO); - } - if( ( nd_status = ND_DataStruct_Traverse( Base_Ptr->Kit_DS_Ptr, NDD_CMD_STATS_GET, &kit_count, &instrument_count, &layer_count, &sample_count)) != NDS_OK) { printf( "Can't compute stats: ND_DataStruct_Traverse() failed (%d)!\n", nd_status); @@ -2251,3 +2421,506 @@ DRT_Status DR_Kits_Dump( DRT_Base *Base_Ptr) return( DRS_OK); } + + + + + +/*----------------------------------------------------------------------------*/ +/* DR_Kits_Dump */ +/*----------------------------------------------------------------------------*/ + +DRT_Status DR_Kits_Dump( DRT_Base *Base_Ptr) +{ + NDT_Status nd_status; + DRT_Kit_Id count = 0; + + + fprintf( stderr, "\n"); + fprintf( stderr, "Sample Rate: (%d)\n", Base_Ptr->SampleRate); + fprintf( stderr, "DrumKit List:\n"); + + if( ( nd_status = ND_DataStruct_Value_Print( stderr, Base_Ptr->Kit_DS_Ptr, NDD_RECURSIVE_MODE_PARENT_CHILD, 0, 0, &count)) != NDS_OK) + { + printf( "Can't dump kits: ND_DataStruct_Value_Print() failed (%d)!\n", nd_status); + return( DRS_KO); + } + + + if( DR_Kits_Stats_Dump( Base_Ptr) != DRS_OK) + { + return( DRS_KO); + } + + return( DRS_OK); +} + + + + + +/*----------------------------------------------------------------------------*/ +/* DR_Kits_Graph_Dump */ +/*----------------------------------------------------------------------------*/ + +DRT_Status DR_Kits_Graph_Dump( DRT_Base *Base_Ptr) +{ + NDT_Status nd_status; + + + fprintf( stderr, "digraph G\n{\n node [shape = record];\n\n"); + + if( ( nd_status = ND_DataStruct_Traverse( Base_Ptr->Kit_DS_Ptr, NDD_CMD_GRAPH_PRINT)) != NDS_OK) + { + printf( "Can't dump graph kits: ND_DataStruct_Value_Print() failed (%d)!\n", nd_status); + return( DRS_KO); + } + + fprintf( stderr, "}\n"); + + return( DRS_OK); +} + + + + + +/*----------------------------------------------------------------------------*/ +/* DR_Task_Tread */ +/*----------------------------------------------------------------------------*/ + +static void *DR_Task_Thread( DRT_Base *Base_Ptr) +{ + pthread_t my_thread_id = pthread_self(); + short task_id; + + +// fprintf( stderr, "Start Task Thread (%d)!\n", my_thread_id); + + for( task_id = 0; ( Base_Ptr->Task_Tab[task_id].Thread != my_thread_id) && ( task_id < Base_Ptr->Task_Number); task_id++); + + if( task_id == Base_Ptr->Task_Number) + { + fprintf( stderr, "Can't find my id: (%d)!\n", my_thread_id); + } + else + { +// fprintf( stderr, "I'm: (%02d)!\n", task_id); + + pthread_mutex_lock( &( Base_Ptr->Task_Mutex)); + + do + { + switch( Base_Ptr->Task_Tab[task_id].Command) + { + case DRD_TASK_CMD_TASK_SPAWN: + { +// fprintf( stderr, "I'm: (%02d) Spawning!\n", task_id); + + Base_Ptr->Task_Tab[task_id].Status = DRD_TASK_STATUS_RUNNING; + Base_Ptr->Task_Tab[task_id].Count = 0L; + break; + } + + case DRD_TASK_CMD_TASK_KILL: + { +// fprintf( stderr, "I'm: (%02d) Killing!\n", task_id); + + Base_Ptr->Task_Tab[task_id].Status = DRD_TASK_STATUS_KILLED; + + pthread_mutex_unlock( &( Base_Ptr->Task_Mutex)); + break; + } + + case DRD_TASK_CMD_INSTRUMENT_LOAD: + case DRD_TASK_CMD_LAYER_LOAD: + case DRD_TASK_CMD_KIT_LOAD: + { + Base_Ptr->Task_Tab[task_id].Status = DRD_TASK_STATUS_RUNNING; + Base_Ptr->Task_Count_Waiting--; + + pthread_mutex_unlock( &( Base_Ptr->Task_Mutex)); + + switch( Base_Ptr->Task_Tab[task_id].Command) + { + case DRD_TASK_CMD_LAYER_LOAD: + { + DR_Layer_Sample_Load( (DRT_Layer *)( Base_Ptr->Task_Tab[task_id].Target_Ptr)); + break; + } + + case DRD_TASK_CMD_INSTRUMENT_LOAD: + { + DR_Instrument_Sample_Load( (DRT_Instrument *)( Base_Ptr->Task_Tab[task_id].Target_Ptr)); + break; + } + + case DRD_TASK_CMD_KIT_LOAD: + { + DR_Kit_Sample_Load( (DRT_Kit *)( Base_Ptr->Task_Tab[task_id].Target_Ptr)); + break; + } + } + + Base_Ptr->Task_Tab[task_id].Count++; + + pthread_mutex_lock( &( Base_Ptr->Task_Mutex)); + break; + } + + 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]))); + + } + } + + if( DR_TASK_STATUS_RUNNING_IS( &( Base_Ptr->Task_Tab[task_id]))) + { +// fprintf( stderr, "I'm: (%02d) Waiting!\n", task_id); + + Base_Ptr->Task_Tab[task_id].Status = DRD_TASK_STATUS_WAITING; + Base_Ptr->Task_Tab[task_id].Command = DRD_TASK_CMD_NONE; + Base_Ptr->Task_Tab[task_id].Target_Ptr = NULL; + Base_Ptr->Task_Count_Waiting++; + + pthread_cond_signal( &( Base_Ptr->Task_Cond)); + pthread_cond_wait( &( Base_Ptr->Task_Tab[task_id].Cond), &( Base_Ptr->Task_Mutex)); + } + } + while( ! DR_TASK_STATUS_KILLED_IS( &( Base_Ptr->Task_Tab[task_id]))); + + pthread_mutex_unlock( &( Base_Ptr->Task_Mutex)); + +// fprintf( stderr, "I'm: (%02d) and killed!\n", task_id); + } + + return(0); +} + + + + + +/*----------------------------------------------------------------------------*/ +/* DR_Task_Id_Run */ +/*----------------------------------------------------------------------------*/ + +DRT_Status DR_Task_Id_Run( DRT_Task_Id Task_Id, DRT_Base *Base_Ptr, DRT_Task_Command Command, void *Target_Ptr) +{ + DRT_Status status; + + +// pthread_mutex_lock( &( Base_Ptr->Task_Mutex)); + + if( !DR_TASK_STATUS_WAITING_IS( &( Base_Ptr->Task_Tab[Task_Id]))) + { + fprintf( stderr, "Task_Id_Run: Inconsistent task status: [%s]!\n", DR_TASK_STATUS_ASCII_GET( &( Base_Ptr->Task_Tab[Task_Id]))); +// pthread_mutex_unlock( &( Base_Ptr->Task_Mutex)); + + status = DRS_KO; + } + else + { + Base_Ptr->Task_Tab[Task_Id].Command = Command; + Base_Ptr->Task_Tab[Task_Id].Target_Ptr = Target_Ptr; + + pthread_cond_signal( &( Base_Ptr->Task_Tab[Task_Id].Cond)); +// pthread_mutex_unlock( &( Base_Ptr->Task_Mutex)); + + status = DRS_OK; + } + + return( status); +} + + + + + +/*----------------------------------------------------------------------------*/ +/* DR_Task_Kill */ +/*----------------------------------------------------------------------------*/ + +DRT_Status DR_Task_Id_Kill( DRT_Task_Id Task_Id, DRT_Base *Base_Ptr) +{ + DRT_Status status; + + + pthread_mutex_lock( &( Base_Ptr->Task_Mutex)); + + if( !DR_TASK_STATUS_WAITING_IS( &( Base_Ptr->Task_Tab[Task_Id]))) + { + fprintf( stderr, "Task_Kill: Inconsistent task status: [%s]!\n", DR_TASK_STATUS_ASCII_GET( &( Base_Ptr->Task_Tab[Task_Id]))); + pthread_mutex_unlock( &( Base_Ptr->Task_Mutex)); + + status = DRS_KO; + } + else + { +// fprintf( stderr, "Killing task id: (%d)!\n", Task_Id); + + Base_Ptr->Task_Tab[Task_Id].Command = DRD_TASK_CMD_TASK_KILL; + Base_Ptr->Task_Tab[Task_Id].Target_Ptr = NULL; + + pthread_cond_signal( &( Base_Ptr->Task_Tab[Task_Id].Cond)); + pthread_mutex_unlock( &( Base_Ptr->Task_Mutex)); + + status = DRS_OK; + } + + return( status); +} + + + + + +/*----------------------------------------------------------------------------*/ +/* DR_Task_Run */ +/*----------------------------------------------------------------------------*/ + +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"); + + Base_Ptr->Task_Count_Run++; + + while( found != DRD_TRUE) + { + task_id = 0; + + pthread_mutex_lock( &( Base_Ptr->Task_Mutex)); + + while( ( task_id < Base_Ptr->Task_Number) && ( found != DRD_TRUE)) + { + 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; + } + + task_id++; + } + + 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); +} + + + + + +/*----------------------------------------------------------------------------*/ +/* DR_Task_Wait */ +/*----------------------------------------------------------------------------*/ + +DRT_Status DR_Task_Wait( DRT_Base *Base_Ptr) +{ + DRT_Task_Id task_id, count = 0; + + + pthread_mutex_lock( &( Base_Ptr->Task_Mutex)); + + while( count < Base_Ptr->Task_Number) + { + task_id = 0; + count = 0; + + for( task_id = 0; task_id < Base_Ptr->Task_Number; 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]))) + { + count++; + } + } + + if( count < Base_Ptr->Task_Number) + { +// DR_Tasks_Dump( Base_Ptr); + + pthread_cond_wait( &( Base_Ptr->Task_Cond), &( Base_Ptr->Task_Mutex)); + } + } + + pthread_mutex_unlock( &( Base_Ptr->Task_Mutex)); + + return( DRS_OK); +} + + + + + +/*----------------------------------------------------------------------------*/ +/* DR_Tasks_Dump */ +/*----------------------------------------------------------------------------*/ + +DRT_Status DR_Tasks_Dump( DRT_Base *Base_Ptr) +{ + DRT_Task_Id task_id; + + + 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", + 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)); + fprintf( stderr, "Task Tab:\n"); + + for( task_id = 0; task_id < Base_Ptr->Task_Number; task_id++) + { + fprintf( stderr, " Id: (%02d) Status: [%s] Thread: (%ld) Command: [%s] Target Ptr: [%lx] Count: (%ld)\n", + 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); + + pthread_mutex_unlock( &( Base_Ptr->Task_Mutex)); + + return( DRS_OK); +} + + + + + +/*----------------------------------------------------------------------------*/ +/* DR_DataStruct_Init */ +/*----------------------------------------------------------------------------*/ + +DRT_Status DR_DataStruct_Init( DRT_Base *Base_Ptr, DRT_SampleRate SampleRate, DRT_Task_Id Task_Number) +{ + NDT_Status nd_status; + DRT_Task_Id task_id; + + + if( ( nd_status = ND_Library_Open( NDD_TRUE)) != NDS_OK) + { + fprintf( stderr, "Can't open node library (%d)!\n", nd_status); + return( DRS_KO); + } + + if( ( nd_status = ND_DataStruct_Open( &(Base_Ptr->Kit_DS_Ptr), DRD_KIT_DS_INDEX_NB, DRG_Kit_DS_Index_Tab_Initial, "Kit_Manager", DR_Kit_Manager, NULL, NULL, NULL, NULL, NDD_TRUE, NULL)) != NDS_OK) + { + fprintf( stderr, "Kit_DS: ND_DataStruct_Open() failed (%d)!\n", nd_status); + return( DRS_KO); + } + + Base_Ptr->SampleRate = SampleRate; + Base_Ptr->Task_Number = Task_Number; + 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_Kit_Flag = DRD_TASK_LOAD_KIT_DEFAULT; + Base_Ptr->Task_Sync_Kit_Flag = DRD_TASK_SYNC_KIT_DEFAULT; + Base_Ptr->Task_Sync_Kits_Flag = DRD_TASK_SYNC_KITS_DEFAULT; + Base_Ptr->Task_Count_Waiting = 0L; + Base_Ptr->Task_Count_Run = 0L; + Base_Ptr->Task_Count_Sleep = 0L; + + if( ( Base_Ptr->Task_Tab = malloc( sizeof( DRT_Task) * Task_Number)) == NULL) + { + fprintf( stderr, "Can't allocate task tab!\n"); + return( NDS_KO); + } + else + { + if( pthread_mutex_init( &( Base_Ptr->Task_Mutex), 0)) + { + fprintf( stderr, "Could not initialize global task mutex!\n"); + return( NDS_KO); + } + + if( pthread_cond_init( &( Base_Ptr->Task_Cond), 0)) + { + fprintf( stderr, "Could not initialize global task cond!\n"); + return( NDS_KO); + } + + for( task_id = 0; task_id < Task_Number; task_id++) + { + Base_Ptr->Task_Tab[task_id].Status = DRD_TASK_STATUS_UNSPAWNED; + Base_Ptr->Task_Tab[task_id].Command = DRD_TASK_CMD_TASK_SPAWN; + Base_Ptr->Task_Tab[task_id].Target_Ptr = NULL; + Base_Ptr->Task_Tab[task_id].Count = 0L; + + if( pthread_cond_init( &( Base_Ptr->Task_Tab[task_id].Cond), 0)) + { + fprintf( stderr, "Could not initialize task cond!\n"); + return( NDS_KO); + } + + if( pthread_create( &(Base_Ptr->Task_Tab[task_id].Thread), 0, (void * (*)(void *))DR_Task_Thread, Base_Ptr)) + { + fprintf( stderr, "Could not initialize task thread!\n"); + return( NDS_KO); + } + } + } + + return( DRS_OK); +} + + + + + +/*----------------------------------------------------------------------------*/ +/* DR_DataStruct_DeInit */ +/*----------------------------------------------------------------------------*/ + +DRT_Status DR_DataStruct_DeInit( DRT_Base *Base_Ptr) +{ + NDT_Status nd_status; + DRT_Task_Id task_id; + + + for( task_id = 0; task_id < Base_Ptr->Task_Number; task_id++) + { + if( DR_Task_Id_Kill( task_id, Base_Ptr) != DRS_OK) + { + printf( "Can't kill task: (%d)!\n", task_id); + return( NDS_KO); + } + } + +// sleep(5); + + if( ( nd_status = ND_DataStruct_Close( Base_Ptr->Kit_DS_Ptr)) != NDS_OK) + { + printf( "Can't close Kit_DS (%d)!\n", nd_status); + return( DRS_KO); + } + + if( ( nd_status = ND_Library_Close()) != NDS_OK) + { + fprintf( stderr, "Can't close node library (%d)!\n", nd_status); + return( DRS_KO); + } + + return( DRS_OK); +} diff --git a/datastruct.h b/datastruct.h index dfa2dc5..0c4a8bd 100644 --- a/datastruct.h +++ b/datastruct.h @@ -11,15 +11,28 @@ +/*----------------------------------------------------------------------------*/ +/* Pre definitions */ +/*----------------------------------------------------------------------------*/ + +typedef struct DRT_Instrument DRT_Instrument; +typedef struct DRT_Kit DRT_Kit; + + + /*----------------------------------------------------------------------------*/ /* Global definitions */ /*----------------------------------------------------------------------------*/ -#define NDD_CMD_STATS_GET ( NDT_Command) 65 -#define NDD_CMD_SAMPLE_LOAD ( NDT_Command) 66 -#define NDD_CMD_SAMPLE_UNLOAD ( NDT_Command) 67 -#define NDD_CMD_INDEX0_PRINT ( NDT_Command) 68 -#define NDD_CMD_INDEX1_PRINT ( NDT_Command) 69 +#define NDD_CMD_ID_REFRESH ( NDT_Command) 65 +#define NDD_CMD_STATS_GET ( NDT_Command) 66 +#define NDD_CMD_GRAPH_PRINT ( NDT_Command) 67 +#define NDD_CMD_SAMPLE_LOAD ( NDT_Command) 68 +#define NDD_CMD_SAMPLE_UNLOAD ( NDT_Command) 69 +#define NDD_CMD_INDEX0_PRINT ( NDT_Command) 70 +#define NDD_CMD_INDEX1_PRINT ( NDT_Command) 71 + + #define DRD_NAME_LEN (short) 256 #define DRD_NAME_SIZE (DRD_NAME_LEN + 1) @@ -30,17 +43,24 @@ #define DRD_DESC_LEN (short) 4096 #define DRD_DESC_SIZE (DRD_DESC_LEN + 1) -typedef long DRT_Layer_Id; -typedef long DRT_Instrument_Id; -typedef long DRT_Kit_Id; -typedef short DRT_Load_Status; +typedef long DRT_Id; +typedef DRT_Id DRT_Layer_Id; +typedef DRT_Id DRT_Instrument_Id; +typedef DRT_Id DRT_Kit_Id; +typedef DRT_Id DRT_Task_Id; -#define DRD_LOAD_STATUS_UNKNOWN ( DRT_Load_Status) 0 -#define DRD_LOAD_STATUS_UNLOADED ( DRT_Load_Status) 1 -#define DRD_LOAD_STATUS_LOADED ( DRT_Load_Status) 2 -#define DRD_LOAD_STATUS_TRANSIENT ( DRT_Load_Status) 3 +#define DRD_ID_UNKNOWN (DRT_Id) -1 + + + +typedef short DRT_Load_Status; + +#define DRD_LOAD_STATUS_UNKNOWN ( DRT_Load_Status) -1 +#define DRD_LOAD_STATUS_UNLOADED ( DRT_Load_Status) 0 +#define DRD_LOAD_STATUS_LOADED ( DRT_Load_Status) 1 +#define DRD_LOAD_STATUS_TRANSIENT ( DRT_Load_Status) 2 #define DR_LOAD_STATUS_VALUE_UNKNOWN_IS( v) ( (v) == DRD_LOAD_STATUS_UNKNOWN) #define DR_LOAD_STATUS_VALUE_UNLOADED_IS( v) ( (v) == DRD_LOAD_STATUS_UNLOADED) @@ -57,6 +77,105 @@ typedef short DRT_Load_Status; +/*---------------------------------------------------------------------------------*/ +/* DRT_Task */ +/*---------------------------------------------------------------------------------*/ + +typedef short DRT_Task_Status; + +#define DRD_TASK_STATUS_UNKNOWN ( DRT_Task_Status) -1 +#define DRD_TASK_STATUS_UNSPAWNED ( DRT_Task_Status) 0 +#define DRD_TASK_STATUS_WAITING ( DRT_Task_Status) 1 +#define DRD_TASK_STATUS_RUNNING ( DRT_Task_Status) 2 +#define DRD_TASK_STATUS_KILLED ( DRT_Task_Status) 3 + +#define DR_TASK_STATUS_VALUE_UNKNOWN_IS( v) ( (v) == DRD_TASK_STATUS_UNKNOWN) +#define DR_TASK_STATUS_VALUE_UNSPAWNED_IS( v) ( (v) == DRD_TASK_STATUS_UNSPAWNED) +#define DR_TASK_STATUS_VALUE_WAITING_IS( v) ( (v) == DRD_TASK_STATUS_WAITING) +#define DR_TASK_STATUS_VALUE_RUNNING_IS( v) ( (v) == DRD_TASK_STATUS_RUNNING) +#define DR_TASK_STATUS_VALUE_KILLED_IS( v) ( (v) == DRD_TASK_STATUS_KILLED) + +#define DR_TASK_STATUS_UNKNOWN_IS( s) DR_TASK_STATUS_VALUE_UNKNOWN_IS( (s)->Status) +#define DR_TASK_STATUS_UNSPAWNED_IS( s) DR_TASK_STATUS_VALUE_UNSPAWNED_IS( (s)->Status) +#define DR_TASK_STATUS_WAITING_IS( s) DR_TASK_STATUS_VALUE_WAITING_IS( (s)->Status) +#define DR_TASK_STATUS_RUNNING_IS( s) DR_TASK_STATUS_VALUE_RUNNING_IS( (s)->Status) +#define DR_TASK_STATUS_KILLED_IS( s) DR_TASK_STATUS_VALUE_KILLED_IS( (s)->Status) + +#define DR_TASK_STATUS_VALUE_ASCII_GET( v) ( DR_TASK_STATUS_VALUE_UNKNOWN_IS( (v)) ? "UNKNOWN" : ( DR_TASK_STATUS_VALUE_UNSPAWNED_IS( (v)) ? "UNUNSPAWNED" : ( DR_TASK_STATUS_VALUE_WAITING_IS( (v)) ? "WAITING" : ( DR_TASK_STATUS_VALUE_RUNNING_IS( (v)) ? "RUNNING" : ( DR_TASK_STATUS_VALUE_KILLED_IS( (v)) ? "KILLED" : "???"))))) +#define DR_TASK_STATUS_ASCII_GET( s) DR_TASK_STATUS_VALUE_ASCII_GET( (s)->Status) + + + +typedef short DRT_Task_Command; + +#define DRD_TASK_CMD_UNKNOWN ( DRT_Task_Command) -1 +#define DRD_TASK_CMD_NONE ( DRT_Task_Command) 0 +#define DRD_TASK_CMD_TASK_SPAWN ( DRT_Task_Command) 1 +#define DRD_TASK_CMD_TASK_KILL ( DRT_Task_Command) 2 +#define DRD_TASK_CMD_LAYER_LOAD ( DRT_Task_Command) 3 +#define DRD_TASK_CMD_INSTRUMENT_LOAD ( DRT_Task_Command) 4 +#define DRD_TASK_CMD_KIT_LOAD ( DRT_Task_Command) 5 + +#define DR_TASK_CMD_VALUE_UNKNOWN_IS( v) ( (v) == DRD_TASK_CMD_UNKNOWN) +#define DR_TASK_CMD_VALUE_NONE_IS( v) ( (v) == DRD_TASK_CMD_NONE) +#define DR_TASK_CMD_VALUE_TASK_SPAWN_IS( v) ( (v) == DRD_TASK_CMD_TASK_SPAWN) +#define DR_TASK_CMD_VALUE_TASK_KILL_IS( v) ( (v) == DRD_TASK_CMD_TASK_KILL) +#define DR_TASK_CMD_VALUE_LAYER_LOAD_IS( v) ( (v) == DRD_TASK_CMD_LAYER_LOAD) +#define DR_TASK_CMD_VALUE_INSTRUMENT_LOAD_IS( v) ( (v) == DRD_TASK_CMD_INSTRUMENT_LOAD) +#define DR_TASK_CMD_VALUE_KIT_LOAD_IS( v) ( (v) == DRD_TASK_CMD_KIT_LOAD) + +#define DR_TASK_CMD_UNKNOWN_IS( s) DR_TASK_CMD_VALUE_UNKNOWN_IS( (s)->Command) +#define DR_TASK_CMD_NONE_IS( s) DR_TASK_CMD_VALUE_NONE_IS( (s)->Command) +#define DR_TASK_CMD_TASK_SPAWN_IS( s) DR_TASK_CMD_VALUE_TASK_SPAWN_IS( (s)->Command) +#define DR_TASK_CMD_TASK_KILL_IS( s) DR_TASK_CMD_VALUE_TASK_KILL_IS( (s)->Command) +#define DR_TASK_CMD_LAYER_LOAD_IS( s) DR_TASK_CMD_VALUE_LAYER_LOAD_IS( (s)->Command) +#define DR_TASK_CMD_INSTRUMENT_LOAD_IS( s) DR_TASK_CMD_VALUE_INSTRUMENT_LOAD_IS( (s)->Command) +#define DR_TASK_CMD_KIT_LOAD_IS( s) DR_TASK_CMD_VALUE_KIT_LOAD_IS( (s)->Command) + +#define DR_TASK_CMD_VALUE_ASCII_GET( v) ( DR_TASK_CMD_VALUE_UNKNOWN_IS( (v)) ? "UNKNOWN" : ( DR_TASK_CMD_VALUE_NONE_IS( (v)) ? "None" : ( DR_TASK_CMD_VALUE_TASK_SPAWN_IS( (v)) ? "Task Spawn" : ( DR_TASK_CMD_VALUE_TASK_KILL_IS( (v)) ? "Task Kill" : ( DR_TASK_CMD_VALUE_LAYER_LOAD_IS( (v)) ? "Layer Load" : ( DR_TASK_CMD_VALUE_INSTRUMENT_LOAD_IS( (v)) ? "Instrument Load" : ( DR_TASK_CMD_VALUE_KIT_LOAD_IS( (v)) ? "Kit Load" : "???"))))))) +#define DR_TASK_CMD_ASCII_GET( s) DR_TASK_CMD_VALUE_ASCII_GET( (s)->Command) + +typedef struct DRT_Task +{ + DRT_Task_Status Status; + pthread_t Thread; + pthread_cond_t Cond; + DRT_Task_Command Command; + void *Target_Ptr; + long Count; +} DRT_Task; + + + + + +/*---------------------------------------------------------------------------------*/ +/* DRT_Base */ +/*---------------------------------------------------------------------------------*/ + +typedef int DRT_SampleRate; + +typedef struct DRT_Base +{ + NDT_Root *Kit_DS_Ptr; + DRT_SampleRate SampleRate; + + DRT_Task_Id Task_Number; + DRT_Boolean Task_Load_Layer_Flag; + DRT_Boolean Task_Load_Instrument_Flag; + DRT_Boolean Task_Load_Kit_Flag; + DRT_Boolean Task_Sync_Kit_Flag; + DRT_Boolean Task_Sync_Kits_Flag; + DRT_Task *Task_Tab; + pthread_mutex_t Task_Mutex; + pthread_cond_t Task_Cond; + DRT_Task_Id Task_Count_Waiting; + long Task_Count_Run; + long Task_Count_Sleep; +} DRT_Base; + + + /*---------------------------------------------------------------------------------*/ @@ -66,7 +185,6 @@ typedef short DRT_Load_Status; typedef struct DRT_Layer { DRT_Layer_Id Id; - DRT_Load_Status Status; char FileName[ DRD_FILENAME_SIZE]; float *Sample_Ptr; long Sample_Size; @@ -75,6 +193,7 @@ typedef struct DRT_Layer double Max; double Gain; double Pitch; + DRT_Instrument *Instrument_Ptr; } DRT_Layer; @@ -82,11 +201,11 @@ typedef struct DRT_Layer typedef struct DRT_Instrument { DRT_Instrument_Id Id; - DRT_Load_Status Status; char Name[ DRD_NAME_SIZE]; double Gain; double Pan_Left; double Pan_Right; + DRT_Kit *Kit_Ptr; NDT_Root *Layer_DS_Ptr; } DRT_Instrument; @@ -95,10 +214,12 @@ typedef struct DRT_Instrument typedef struct DRT_Kit { DRT_Kit_Id Id; + DRT_Kit_Id Logical_Id; DRT_Load_Status Status; char Name[ DRD_NAME_SIZE]; char Desc[ DRD_DESC_SIZE]; char DirName[ DRD_DIRNAME_SIZE]; + DRT_Base *Base_Ptr; NDT_Root *Instrument_DS_Ptr; } DRT_Kit; @@ -313,6 +434,14 @@ extern DRT_Status DR_Kit_Id_Find( DRT_Kit **, NDT_Root *, DRT_Kit_Id); +/*----------------------------------------------------------------------------*/ +/* DR_Kit_Logical_Id_Find */ +/*----------------------------------------------------------------------------*/ + +extern DRT_Status DR_Kit_Logical_Id_Find( DRT_Kit **, NDT_Root *, DRT_Kit_Id); + + + /*---------------------------------------------------------------------------------*/ /* DR_Kit_Name_Find */ /*---------------------------------------------------------------------------------*/ @@ -341,7 +470,7 @@ extern DRT_Status DR_Kit_Load( DRT_Base *, char *); /* DR_Kit_Sample_Load */ /*---------------------------------------------------------------------------------*/ -extern DRT_Status DR_Kit_Sample_Load( DRT_Kit *, DRT_SampleRate); +extern DRT_Status DR_Kit_Sample_Load( DRT_Kit *); @@ -352,22 +481,6 @@ extern DRT_Status DR_Kit_Sample_Load( DRT_Kit *, DRT_SampleRate); extern DRT_Status DR_Kit_Sample_UnLoad( DRT_Kit *); - -/*---------------------------------------------------------------------------------*/ -/* DR_DataStruct_Init */ -/*---------------------------------------------------------------------------------*/ - -extern DRT_Status DR_DataStruct_Init( DRT_Base *, DRT_SampleRate); - - - -/*---------------------------------------------------------------------------------*/ -/* DR_DataStruct_DeInit */ -/*---------------------------------------------------------------------------------*/ - -extern DRT_Status DR_DataStruct_DeInit( DRT_Base *); - - /*----------------------------------------------------------------------------*/ /* DR_Kits_Sample_Load */ @@ -393,6 +506,14 @@ extern DRT_Status DR_Kits_Load( DRT_Base *); +/*----------------------------------------------------------------------------*/ +/* DR_Kits_Stats_Dump */ +/*----------------------------------------------------------------------------*/ + +extern DRT_Status DR_Kits_Stats_Dump( DRT_Base *); + + + /*----------------------------------------------------------------------------*/ /* DR_Kits_Dump */ /*----------------------------------------------------------------------------*/ @@ -401,6 +522,70 @@ extern DRT_Status DR_Kits_Dump( DRT_Base *); +/*----------------------------------------------------------------------------*/ +/* DR_Kits_Graph_Dump */ +/*----------------------------------------------------------------------------*/ + +extern DRT_Status DR_Kits_Graph_Dump( DRT_Base *); + + + +/*----------------------------------------------------------------------------*/ +/* DR_Task_Id_Run */ +/*----------------------------------------------------------------------------*/ + +extern DRT_Status DR_Task_Id_Run( DRT_Task_Id, DRT_Base *, DRT_Task_Command, void *); + + + +/*----------------------------------------------------------------------------*/ +/* DR_Task_Id_Kill */ +/*----------------------------------------------------------------------------*/ + +extern DRT_Status DR_Task_Id_Kill( DRT_Task_Id, DRT_Base *); + + + +/*----------------------------------------------------------------------------*/ +/* DR_Task_Run */ +/*----------------------------------------------------------------------------*/ + +extern DRT_Status DR_Task_Run( DRT_Base *, void *); + + + +/*----------------------------------------------------------------------------*/ +/* DR_Task_Wait */ +/*----------------------------------------------------------------------------*/ + +extern DRT_Status DR_Task_Wait( DRT_Base *); + + + +/*----------------------------------------------------------------------------*/ +/* DR_Tasks_Dump */ +/*----------------------------------------------------------------------------*/ + +extern DRT_Status DR_Tasks_Dump( DRT_Base *); + + + +/*---------------------------------------------------------------------------------*/ +/* DR_DataStruct_Init */ +/*---------------------------------------------------------------------------------*/ + +extern DRT_Status DR_DataStruct_Init( DRT_Base *, DRT_SampleRate, DRT_Task_Id); + + + +/*---------------------------------------------------------------------------------*/ +/* DR_DataStruct_DeInit */ +/*---------------------------------------------------------------------------------*/ + +extern DRT_Status DR_DataStruct_DeInit( DRT_Base *); + + + # else // ifndef _DATA_STRUCT_C_ @@ -431,7 +616,7 @@ DRT_Status DR_Layer_Add( DRT_Layer **, NDT_Root *, DRT_Layer *); /* DR_Layer_Sample_Load */ /*---------------------------------------------------------------------------------*/ -DRT_Status DR_Layer_Sample_Load( DRT_Layer *, DRT_Kit *, DRT_SampleRate); +DRT_Status DR_Layer_Sample_Load( DRT_Layer *); @@ -467,6 +652,22 @@ DRT_Status DR_Instrument_Dump( DRT_Instrument *, long); +/*----------------------------------------------------------------------------*/ +/* DR_Instrument_Sample_Load */ +/*----------------------------------------------------------------------------*/ + +DRT_Status DR_Instrument_Sample_Load( DRT_Instrument *); + + + +/*----------------------------------------------------------------------------*/ +/* DR_Instrument_Sample_Unload */ +/*----------------------------------------------------------------------------*/ + +DRT_Status DR_Instrument_Sample_UnLoad( DRT_Instrument *); + + + /*----------------------------------------------------------------------------*/ /* DR_Kit_Add */ /*----------------------------------------------------------------------------*/ @@ -483,6 +684,14 @@ DRT_Status DR_Kit_Id_Find( DRT_Kit **, NDT_Root *, DRT_Kit_Id); +/*----------------------------------------------------------------------------*/ +/* DR_Kit_Logical_Id_Find */ +/*----------------------------------------------------------------------------*/ + +DRT_Status DR_Kit_Logical_Id_Find( DRT_Kit **, NDT_Root *, DRT_Kit_Id); + + + /*---------------------------------------------------------------------------------*/ /* DR_Kit_Name_Find */ /*---------------------------------------------------------------------------------*/ @@ -535,34 +744,18 @@ DRT_Status DR_Kit_Load( DRT_Base *, char *); /* DR_Kit_Sample_Load */ /*---------------------------------------------------------------------------------*/ -DRT_Status DR_Kit_Sample_Load( DRT_Kit *, DRT_SampleRate); +DRT_Status DR_Kit_Sample_Load( DRT_Kit *); /*---------------------------------------------------------------------------------*/ -/* DR_Kit_Sample_UnLoad */ +/* DR_Kit_Sample_UnLoad */ /*---------------------------------------------------------------------------------*/ DRT_Status DR_Kit_Sample_UnLoad( DRT_Kit *); -/*---------------------------------------------------------------------------------*/ -/* DR_DataStruct_Init */ -/*---------------------------------------------------------------------------------*/ - -DRT_Status DR_DataStruct_Init( DRT_Base *, DRT_SampleRate); - - - -/*---------------------------------------------------------------------------------*/ -/* DR_DataStruct_DeInit */ -/*---------------------------------------------------------------------------------*/ - -DRT_Status DR_DataStruct_DeInit( DRT_Base *); - - - /*----------------------------------------------------------------------------*/ /* DR_Kits_Sample_Load */ /*----------------------------------------------------------------------------*/ @@ -587,6 +780,14 @@ DRT_Status DR_Kits_Load( DRT_Base *); +/*----------------------------------------------------------------------------*/ +/* DR_Kits_Stats_Dump */ +/*----------------------------------------------------------------------------*/ + +DRT_Status DR_Kits_Stats_Dump( DRT_Base *); + + + /*----------------------------------------------------------------------------*/ /* DR_Kits_Dump */ /*----------------------------------------------------------------------------*/ @@ -595,6 +796,78 @@ DRT_Status DR_Kits_Dump( DRT_Base *); +/*----------------------------------------------------------------------------*/ +/* DR_Kits_Graph_Dump */ +/*----------------------------------------------------------------------------*/ + +DRT_Status DR_Kits_Graph_Dump( DRT_Base *); + + + +/*----------------------------------------------------------------------------*/ +/* DR_Task_Tread */ +/*----------------------------------------------------------------------------*/ + +static void *DR_Task_Thread( DRT_Base *); + + + +/*----------------------------------------------------------------------------*/ +/* DR_Task_Id_Run */ +/*----------------------------------------------------------------------------*/ + +DRT_Status DR_Task_Id_Run( DRT_Task_Id, DRT_Base *, DRT_Task_Command, void *); + + + +/*----------------------------------------------------------------------------*/ +/* DR_Task_Id_Kill */ +/*----------------------------------------------------------------------------*/ + +DRT_Status DR_Task_Id_Kill( DRT_Task_Id, DRT_Base *); + + + +/*----------------------------------------------------------------------------*/ +/* DR_Task_Run */ +/*----------------------------------------------------------------------------*/ + +DRT_Status DR_Task_Run( DRT_Base *, DRT_Task_Command, void *); + + + +/*----------------------------------------------------------------------------*/ +/* DR_Task_Wait */ +/*----------------------------------------------------------------------------*/ + +DRT_Status DR_Task_Wait( DRT_Base *); + + + +/*----------------------------------------------------------------------------*/ +/* DR_Tasks_Dump */ +/*----------------------------------------------------------------------------*/ + +DRT_Status DR_Tasks_Dump( DRT_Base *); + + + +/*---------------------------------------------------------------------------------*/ +/* DR_DataStruct_Init */ +/*---------------------------------------------------------------------------------*/ + +DRT_Status DR_DataStruct_Init( DRT_Base *, DRT_SampleRate, DRT_Task_Id); + + + +/*---------------------------------------------------------------------------------*/ +/* DR_DataStruct_DeInit */ +/*---------------------------------------------------------------------------------*/ + +DRT_Status DR_DataStruct_DeInit( DRT_Base *); + + + # endif // ifndef _DATASTRUCT_C_ diff --git a/drummer.c b/drummer.c index 441d835..527f4bf 100644 --- a/drummer.c +++ b/drummer.c @@ -21,7 +21,7 @@ DRT_Status DR_Init( DRT_Base *Base_Ptr) DRT_Status status; - if( ( status = DR_DataStruct_Init( Base_Ptr, DRD_SAMPLE_RATE_DEFAULT)) != DRS_OK) + if( ( status = DR_DataStruct_Init( Base_Ptr, DRD_SAMPLE_RATE_DEFAULT, DRD_THREAD_NUMBER_DEFAULT)) != DRS_OK) { fprintf( stderr, "Can't init data structures (%d) !", status); return( status); @@ -66,6 +66,7 @@ int main( int argc, char **argv) DRT_Base base; DRT_Status status; DRT_Kit *kit_ptr; + DRT_Id i; if( ( status = DR_Init( &base)) != DRS_OK) @@ -77,16 +78,16 @@ int main( int argc, char **argv) { exit( -1); } - - - if( ( status = DR_Kit_Id_Find( &kit_ptr, base.Kit_DS_Ptr, 0)) != DRS_OK) -// if( ( status = DR_Kit_Name_Find( &kit_ptr, base.Kit_DS_Ptr, "Mars LM1: 01-777 Clean Kit")) != DRS_OK) +/* + if( ( status = DR_Kit_Id_Find( &kit_ptr, base.Kit_DS_Ptr, 0)) != DRS_OK) +// if( ( status = DR_Kit_Logical_Id_Find( &kit_ptr, base.Kit_DS_Ptr, 31)) != DRS_OK) +// if( ( status = DR_Kit_Name_Find( &kit_ptr, base.Kit_DS_Ptr, "HardElectro1")) != DRS_OK) { exit( -1); } else { - if( ( status = DR_Kit_Sample_Load( kit_ptr, base.SampleRate)) != DRS_OK) + if( ( status = DR_Kit_Sample_Load( kit_ptr)) != DRS_OK) { exit( -1); } @@ -97,18 +98,74 @@ int main( int argc, char **argv) { exit( -1); } -*/ +*/ +/* fprintf( stderr, "After!\n"); - if( ( status = DR_Kit_Sample_Load( kit_ptr, base.SampleRate)) != DRS_OK) + if( ( status = DR_Kit_Sample_Load( kit_ptr)) != DRS_OK) { exit( -1); } +*/ + +// } + + DR_Kits_Sample_Load( &base); + +/* + for( i = 0; i < 264; i++) + { + if( ( status = DR_Kit_Logical_Id_Find( &kit_ptr, base.Kit_DS_Ptr, i)) == DRS_OK) + { + printf( "Loading Logical Id: (%d)\n", i); + DR_Kit_Sample_Load( kit_ptr); + } + } +*/ +/* + if( DR_Task_Wait( &base) != DRS_OK) + { + printf( "DR_Task_Wait failed (%d)!\n"); + exit(-1); + } +*/ + + fprintf( stderr, "hello world!\n"); + + + +// DR_Kits_Graph_Dump( &base); + +/* + if( ( status = DR_Kits_Dump( &base)) != DRS_OK) + { + exit( -1); + } +*/ +/* + DR_Task_Run( &base, (DRT_Instrument *)1); + DR_Task_Run( &base, (DRT_Instrument *)2); + DR_Task_Run( &base, (DRT_Instrument *)3); +*/ +// sleep( 10); + +/* + if( ( status = DR_Tasks_Dump( &base)) != DRS_OK) + { + exit( -1); } - fprintf( stderr, "hello world!\n"); - - if( ( status = DR_Kits_Dump( &base)) != DRS_OK) + if( ( status = DR_Task_Wait( &base)) != DRS_OK) + { + exit( -1); + } +*/ + if( ( status = DR_Tasks_Dump( &base)) != DRS_OK) + { + exit( -1); + } + + if( ( status = DR_Kits_Stats_Dump( &base)) != DRS_OK) { exit( -1); } diff --git a/drummer.h b/drummer.h index 6fd1cb5..51d865a 100644 --- a/drummer.h +++ b/drummer.h @@ -13,6 +13,7 @@ #include #include #include +#include #include #include @@ -31,8 +32,6 @@ - - /*----------------------------------------------------------------------------*/ /* Global definitions */ /*----------------------------------------------------------------------------*/ @@ -42,13 +41,28 @@ typedef short DRT_Boolean; #define DRD_FALSE ( DRT_Boolean) 0 #define DRD_TRUE ( DRT_Boolean) 1 +#define DR_BOOLEAN_VALUE_FALSE_IS( v) ( (v) == DRD_FALSE) +#define DR_BOOLEAN_VALUE_TRUE_IS( v) ( (v) == DRD_TRUE) + +#define DR_BOOLEAN_VALUE_ASCII_GET( v) ( DR_BOOLEAN_VALUE_FALSE_IS( (v)) ? "FALSE" : ( DR_BOOLEAN_VALUE_TRUE_IS( (v)) ? "TRUE" : "???")) + + + #define DRD_NO 'n' #define DRD_YES 'y' #define DR_MAX(A,B) (((A) < (B)) ? (B) : (A)) #define DR_MIN(A,B) (((A) > (B)) ? (B) : (A)) -#define DRD_SAMPLE_RATE_DEFAULT 44100 + + +#define DRD_SAMPLE_RATE_DEFAULT 44100 +#define DRD_THREAD_NUMBER_DEFAULT 16L +#define DRD_TASK_LOAD_LAYER_DEFAULT DRD_TRUE +#define DRD_TASK_LOAD_INSTRUMENT_DEFAULT DRD_FALSE +#define DRD_TASK_LOAD_KIT_DEFAULT DRD_FALSE +#define DRD_TASK_SYNC_KIT_DEFAULT DRD_TRUE +#define DRD_TASK_SYNC_KITS_DEFAULT DRD_TRUE @@ -74,17 +88,12 @@ typedef short DRT_Status; -/*---------------------------------------------------------------------------------*/ -/* DRT_Base */ -/*---------------------------------------------------------------------------------*/ +/*----------------------------------------------------------------------------*/ +/* Drummer Includes */ +/*----------------------------------------------------------------------------*/ -typedef int DRT_SampleRate; - -typedef struct DRT_Base -{ - NDT_Root *Kit_DS_Ptr; - DRT_SampleRate SampleRate; -} DRT_Base; +#include +#include @@ -125,7 +134,5 @@ int main( int, char **); /*----------------------------------------------------------------------------*/ -#include -#include #endif diff --git a/go.sh b/go.sh index d9ca4ac..bb26f27 100755 --- a/go.sh +++ b/go.sh @@ -13,7 +13,7 @@ done -cmd="gcc -g -o drummer ${module_list//.c/.o} drummer.o -fPIC -ldl -lxml2 -lz -llzma -lm -lsndfile -lsamplerate" +cmd="gcc -g -o drummer ${module_list//.c/.o} drummer.o -fPIC -ldl -lpthread -lxml2 -lz -llzma -lm -lsndfile -lsamplerate" echo ${cmd} eval ${cmd} diff --git a/lv2_plugin.c b/lv2_plugin.c index 0c0d1f8..0271153 100644 --- a/lv2_plugin.c +++ b/lv2_plugin.c @@ -32,7 +32,7 @@ DRT_Status DR_LV2_Kit_Sample_Load( DRT_LV2_Base *LV2_Base_Ptr, DRT_Kit *Kit_P { // fprintf( stderr, "Sample Rate: (%d)\n", LV2_Base_Ptr->Base.SampleRate); - if( ( status = DR_Kit_Sample_Load( Kit_Ptr, LV2_Base_Ptr->Base.SampleRate)) == DRS_OK) + if( ( status = DR_Kit_Sample_Load( Kit_Ptr)) == DRS_OK) { LV2_Base_Ptr->Kit_Cur_Ptr = Kit_Ptr; @@ -67,7 +67,7 @@ DRT_Status DR_LV2_Kit_Sample_Load( DRT_LV2_Base *LV2_Base_Ptr, DRT_Kit *Kit_P pthread_mutex_unlock( &( LV2_Base_Ptr->Load_Mutex)); LV2_Base_Ptr->Sample_Number = Kit_Ptr->Instrument_DS_Ptr->Index_Tab[NDD_INDEX_PRIMARY].Node_Number; - fprintf( stderr, "Loaded: (%d) samples!\n", LV2_Base_Ptr->Sample_Number); + fprintf( stderr, "Loaded: (%d) instruments!\n", LV2_Base_Ptr->Sample_Number); // DR_Kit_Dump( Kit_Ptr, 0); } @@ -95,14 +95,32 @@ DRT_Status DR_LV2_CurKit_Sample_Load( DRT_LV2_Base *LV2_Base_Ptr) if( kit_id == LV2_Base_Ptr->Kit_Id) { - fprintf( stderr, "Same Kit id: (%d) Bank/Program: (%d/%d/%d)!\n", kit_id, LV2_Base_Ptr->Bank_Id_LSB_New, LV2_Base_Ptr->Bank_Id_MSB_New, LV2_Base_Ptr->Program_Id_New); + fprintf( stderr, "Same kit id: (%d) Bank/Program: (%d/%d/%d) Name: [%s]!\n", + kit_id, LV2_Base_Ptr->Bank_Id_LSB_New, LV2_Base_Ptr->Bank_Id_MSB_New, LV2_Base_Ptr->Program_Id_New, LV2_Base_Ptr->Kit_Cur_Ptr->Name); status = DRS_OK; } else { - if( ( status = DR_Kit_Id_Find( &Kit_Ptr, LV2_Base_Ptr->Base.Kit_DS_Ptr, kit_id)) == DRS_OK) + if( ( status = DR_Kit_Logical_Id_Find( &Kit_Ptr, LV2_Base_Ptr->Base.Kit_DS_Ptr, kit_id)) != DRS_OK) { - fprintf( stderr, "New Kit id: (%d) Bank/Program: (%d/%d/%d)!\n", kit_id, LV2_Base_Ptr->Bank_Id_LSB_New, LV2_Base_Ptr->Bank_Id_MSB_New, LV2_Base_Ptr->Program_Id_New); + fprintf( stderr, "Can't find kit id: (%d) Bank/Program: (%d/%d/%d)!\n", + kit_id, LV2_Base_Ptr->Bank_Id_LSB_New, LV2_Base_Ptr->Bank_Id_MSB_New, LV2_Base_Ptr->Program_Id_New); + + if( LV2_Base_Ptr->Kit_Id == DRD_ID_UNKNOWN) + { + fprintf( stderr, "No Kit available!\n"); + status=DRS_KO; + } + else + { + fprintf( stderr, "Keep kit id: (%d) Bank/Program: (%d/%d/%d) Name: [%s]!\n", + LV2_Base_Ptr->Kit_Id, LV2_Base_Ptr->Bank_Id_LSB, LV2_Base_Ptr->Bank_Id_MSB, LV2_Base_Ptr->Program_Id, LV2_Base_Ptr->Kit_Cur_Ptr->Name); + } + } + else + { + fprintf( stderr, "New kit id: (%d) Bank/Program: (%d/%d/%d) Name: [%s]!\n", + kit_id, LV2_Base_Ptr->Bank_Id_LSB_New, LV2_Base_Ptr->Bank_Id_MSB_New, LV2_Base_Ptr->Program_Id_New, Kit_Ptr->Name); LV2_Base_Ptr->Bank_Id_LSB = LV2_Base_Ptr->Bank_Id_LSB_New; LV2_Base_Ptr->Bank_Id_MSB = LV2_Base_Ptr->Bank_Id_MSB_New; @@ -480,7 +498,7 @@ static LV2_Handle DR_LV2_Instantiate( const LV2_Descriptor *LV2_Descriptor lv2_base_ptr->Bank_Id_LSB_New = 0; lv2_base_ptr->Bank_Id_MSB_New = 0; lv2_base_ptr->Program_Id_New = 0; - lv2_base_ptr->Kit_Id = -1; + lv2_base_ptr->Kit_Id = DRD_ID_UNKNOWN; lv2_base_ptr->Current_Path = NULL; lv2_base_ptr->CurReq = -1; @@ -569,21 +587,21 @@ static LV2_Handle DR_LV2_Instantiate( const LV2_Descriptor *LV2_Descriptor lv2_base_ptr->Pans[i] = NULL; } - if( ( status = DR_DataStruct_Init( &( lv2_base_ptr->Base), (DRT_SampleRate)SampleRate)) != DRS_OK) + if( ( status = DR_DataStruct_Init( &( lv2_base_ptr->Base), (DRT_SampleRate)SampleRate, DRD_THREAD_NUMBER_DEFAULT)) != DRS_OK) { - fprintf( stderr, "Can't init data structures (%d) !", status); + fprintf( stderr, "Can't init data structures (%d)!\n", status); } else { if( ( status = DR_Kits_Load( & ( lv2_base_ptr->Base))) != DRS_OK) { - fprintf( stderr, "Can't load kits (%d) !", status); + fprintf( stderr, "Can't load kits (%d)!\n", status); } else { if( ( status = DR_LV2_CurKit_Sample_Load( lv2_base_ptr)) != DRS_OK) { - fprintf( stderr, "Can't load kit sample (%d) !", status); + fprintf( stderr, "Can't load kit sample (%d)!\n", status); } else { @@ -723,7 +741,7 @@ static void DR_LV2_Run( LV2_Handle Instance_Ptr, uint32_t N_Samples) LV2_ATOM_SEQUENCE_FOREACH( lv2_base_ptr->Control_Port, ev_ptr) { - fprintf( stderr, "LV2 Event!\n"); +// fprintf( stderr, "LV2 Event!\n"); if( ev_ptr->body.type == lv2_base_ptr->URIS.midi_event) { @@ -735,7 +753,7 @@ static void DR_LV2_Run( LV2_Handle Instance_Ptr, uint32_t N_Samples) uint8_t value; - fprintf( stderr, " Midi Event!\n"); +// fprintf( stderr, " Midi Event!\n"); if( ( lv2_base_ptr->Channel_Nb == 0) || ( channel == ( lv2_base_ptr->Channel_Nb - 1))) { @@ -834,7 +852,7 @@ static void DR_LV2_Run( LV2_Handle Instance_Ptr, uint32_t N_Samples) default: { - fprintf( stderr, "Unhandeled status: (%i) Data 1: (%i) Data 2: (%d)\n", ( *data) >> 4, data[1], data[2]); +// fprintf( stderr, "Unhandeled status: (%i) Data 1: (%i) Data 2: (%d)\n", ( *data) >> 4, data[1], data[2]); break; } } @@ -1072,6 +1090,10 @@ static void DR_LV2_Cleanup( LV2_Handle Instance_Ptr) fprintf( stderr, "LV2 Cleanup!\n"); + + DR_Tasks_Dump( &( lv2_base_ptr->Base)); + DR_Kits_Stats_Dump( &( lv2_base_ptr->Base)); + if( ( status = DR_DataStruct_DeInit( &( lv2_base_ptr->Base))) != DRS_OK) { printf( "Can't deinit data structures (%d)!\n", status);