Skip to content

Commit

Permalink
message printing and offtim fix
Browse files Browse the repository at this point in the history
  • Loading branch information
vlazzarini committed Dec 14, 2017
1 parent aed0a91 commit cf10758
Show file tree
Hide file tree
Showing 5 changed files with 173 additions and 22 deletions.
84 changes: 80 additions & 4 deletions Engine/insert.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,20 +45,84 @@ static int insert_midi(CSOUND *csound, int insno, MCHNBLK *chn,
MEVENT *mep);
static int insert_event(CSOUND *csound, int insno, EVTBLK *newevtp);

static void print_messages(CSOUND *csound, int attr, const char *str){
#if defined(WIN32)
switch (attr & CSOUNDMSG_TYPE_MASK) {
case CSOUNDMSG_ERROR:
case CSOUNDMSG_WARNING:
case CSOUNDMSG_REALTIME:
fprintf(stderr, str);
break;
default:
fprintf(stdout, str);
}
#else
FILE *fp = stderr;
if ((attr & CSOUNDMSG_TYPE_MASK) == CSOUNDMSG_STDOUT)
fp = stdout;
if (!attr || !csound->enableMsgAttr) {
fprintf(fp, str);
return;
}
if ((attr & CSOUNDMSG_TYPE_MASK) == CSOUNDMSG_ORCH)
if (attr & CSOUNDMSG_BG_COLOR_MASK)
fprintf(fp, "\033[4%cm", ((attr & 0x70) >> 4) + '0');
if (attr & CSOUNDMSG_FG_ATTR_MASK) {
if (attr & CSOUNDMSG_FG_BOLD)
fprintf(fp, "\033[1m");
if (attr & CSOUNDMSG_FG_UNDERLINE)
fprintf(fp, "\033[4m");
}
if (attr & CSOUNDMSG_FG_COLOR_MASK)
fprintf(fp, "\033[3%cm", (attr & 7) + '0');
fprintf(fp, str);
fprintf(fp, "\033[m");
#endif
}

#define QUEUESIZ 64

void message_string_enqueue(CSOUND *csound, int attr,
const char *str) {
unsigned long wp = csound->message_string_queue_wp;
csound->message_string_queue[wp].attr = attr;
strncpy(csound->message_string_queue[wp].str, str, MAX_MESSAGE_STR);
csound->message_string_queue_wp = wp + 1 < QUEUESIZ ? wp + 1 : 0;
ATOMIC_INCR(csound->message_string_queue_items);
}


/*
* creates a thread to process instance allocations
*/
uintptr_t event_insert_thread(void *p) {
CSOUND *csound = (CSOUND *) p;
ALLOC_DATA *inst = csound->alloc_queue;
float wakeup = (1000*csound->ksmps/csound->esr);
unsigned long rp = 0, items;

unsigned long rp = 0, items, rpm = 0;
message_string_queue_t *mess;
void (*csoundMessageStringCallback)(CSOUND *csound,
int attr,
const char *str);
void (*csoundMessageCallback)(CSOUND *csound,
int attr,
const char *format,
va_list args)
= csound->csoundMessageCallback_;
if(csound->message_string_queue == NULL)
csound->message_string_queue = (message_string_queue_t *)
csound->Calloc(csound, QUEUESIZ*sizeof(message_string_queue_t));
mess = csound->message_string_queue;
if(csound->csoundMessageStringCallback)
csoundMessageStringCallback = csound->csoundMessageStringCallback;
else csoundMessageStringCallback = print_messages;
csoundSetMessageStringCallback(csound, message_string_enqueue);

while(csound->event_insert_loop) {
// get the value of items_to_alloc
items = ATOMIC_GET(csound->alloc_queue_items);
if(items == 0)
csoundSleep((int) ((int) wakeup > 0 ? wakeup : 1));
csoundSleep((int) ((int) wakeup > 0 ? wakeup : 1));
else while(items) {
if(inst[rp].isMidi) {
csoundSpinLock(&csound->alloc_spinlock);
Expand All @@ -75,7 +139,16 @@ uintptr_t event_insert_thread(void *p) {
items--;
rp = rp + 1 < MAX_ALLOC_QUEUE ? rp + 1 : 0;
}
items = ATOMIC_GET(csound->message_string_queue_items);
while(items) {
if(mess != NULL)
csoundMessageStringCallback(csound, mess[rpm].attr, mess[rpm].str);
ATOMIC_DECR(csound->message_string_queue_items);
items--;
rpm = rpm + 1 < QUEUESIZ ? rpm + 1 : 0;
}
}
csoundSetMessageCallback(csound, csoundMessageCallback);
return (uintptr_t) NULL;
}

Expand Down Expand Up @@ -389,7 +462,10 @@ int insert_event(CSOUND *csound, int insno, EVTBLK *newevtp)
__LINE__, ip->offtim, ip->offtim*csound->ekr);
#endif
if(csound->oparms->realtime) // compensate for possible late starts
ip->offtim += (csound->icurTime/csound->esr - ip->p2.value);
{
double p2 = (double) ip->p2.value + csound->timeOffs;
ip->offtim += (csound->icurTime/csound->esr - p2);
}
//printf("%lf \n", );
schedofftim(csound, ip); /* put in turnoff list */
}
Expand Down
39 changes: 27 additions & 12 deletions Engine/musmon.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,24 @@ extern void MidiClose(CSOUND *);
extern void RTclose(CSOUND *);
extern void remote_Cleanup(CSOUND *);
extern char **csoundGetSearchPathFromEnv(CSOUND *, const char *);

#ifdef HAVE_PTHREAD_SPIN_LOCK
#define RT_SPIN_TRYLOCK { if(csound->oparms->realtime) \
int trylock = pthread_spin_trylock(&csound->allo_spinlock); \
if(trylock = 0) {
#else
#define RT_SPIN_TRYLOCK csoundSpinLock(&csound->alloc_spinlock);
#endif

#ifdef HAVE_PTHREAD_SPIN_LOCK
#define RT_SPIN_UNLOCK \
if(csound->oparms->realtime) \
csoundSpinUnLock(&csound->alloc_spinlock); \
trylock = 0; } }
#else
#define RT_SPIN_UNLOCK csoundSpinUnLock(&csound->alloc_spinlock);
#endif

/* extern void initialize_instrument0(CSOUND *); */

typedef struct evt_cb_func {
Expand Down Expand Up @@ -423,6 +441,8 @@ PUBLIC int csoundCleanup(CSOUND *csound)
if (csound->QueryGlobalVariable(csound,"::UDPCOM")
!= NULL) csoundUDPServerClose(csound);



while (csound->evtFuncChain != NULL) {
p = (void*) csound->evtFuncChain;
csound->evtFuncChain = ((EVT_CB_FUNC*) p)->nxt;
Expand Down Expand Up @@ -910,14 +930,12 @@ extern int sensMidi(CSOUND *);
/* 0: continue performance */
/* 1: terminate (e.g. end of MIDI file) */
/* 2: normal end of score */

int sensevents(CSOUND *csound)
{
EVTBLK *e;
OPARMS *O = csound->oparms;
int retval = 0, sensType;
int conn, *sinp, end_check=1;
int trylock = 0;

csdebug_data_t *data = (csdebug_data_t *) csound->csdebug_data;
if (UNLIKELY(data && data->status == CSDEBUG_STATUS_STOPPED)) {
Expand All @@ -929,13 +947,7 @@ int sensevents(CSOUND *csound)
return 1; /* abort with perf incomplete */
}
/* if turnoffs pending, remove any expired instrs */
if(csound->oparms->realtime)
#ifdef HAVE_PTHREAD_SPIN_LOCK
trylock = pthread_spin_trylock(&csound->alloc_spinlock);
#else
csoundSpinLock(&csound->alloc_spinlock);
#endif
if(trylock == 0) {
RT_SPIN_TRYLOCK
if (UNLIKELY(csound->frstoff != NULL)) {
double tval;
/* the following comparisons must match those in schedofftim() */
Expand All @@ -949,9 +961,8 @@ int sensevents(CSOUND *csound)
timexpire(csound, tval);
}
}
if(csound->oparms->realtime)
csoundSpinUnLock(&csound->alloc_spinlock);
}
RT_SPIN_UNLOCK

e = &(csound->evt);
if (--(csound->cyclesRemaining) <= 0) { /* if done performing score segment: */
if (!csound->cyclesRemaining) {
Expand All @@ -975,8 +986,10 @@ int sensevents(CSOUND *csound)
if (csound->frstoff != NULL) { /* if still have notes
with finite length, wait
until all are turned off */
RT_SPIN_TRYLOCK
csound->nxtim = csound->frstoff->offtim;
csound->nxtbt = csound->frstoff->offbet;
RT_SPIN_UNLOCK
break;
}
/* end of: 1: section, 2: score, 3: lplay list */
Expand Down Expand Up @@ -1164,10 +1177,12 @@ int sensevents(CSOUND *csound)
section_amps(csound, 0);
}
if (retval == 1) { /* if s code, */
RT_SPIN_TRYLOCK
orcompact(csound); /* rtn inactiv spc */
if (csound->actanchor.nxtact == NULL) /* if no indef ins */
rlsmemfiles(csound); /* purge memfiles */
csound->Message(csound, Str("SECTION %d:\n"), ++STA(sectno));
RT_SPIN_UNLOCK
goto retest; /* & back for more */
}
return 2; /* done with entire score */
Expand Down
48 changes: 42 additions & 6 deletions Top/csound.c
Original file line number Diff line number Diff line change
Expand Up @@ -912,7 +912,12 @@ static const CSOUND cenviron_ = {
NULL, /* alloc_queue */
0, /* alloc_queue_items */
0, /* alloc_queue_wp */
0 /* alloc_spinlock */
0, /* alloc_spinlock */
NULL, /* message string callback */
NULL, /* message_string */
0, /* message_string_queue_items */
0, /* message_string_queue_wp */
NULL /* message_string_queue */
/*, NULL */ /* self-reference */
};

Expand Down Expand Up @@ -2417,6 +2422,22 @@ PUBLIC void csoundSetDefaultMessageCallback(
}
}



PUBLIC void csoundSetMessageStringCallback(CSOUND *csound,
void (*csoundMessageStrCallback)(CSOUND *csound,
int attr,
const char *str)) {

if (csoundMessageStrCallback) {
if(csound->message_string == NULL)
csound->message_string = (char *) csound->Calloc(csound, MAX_MESSAGE_STR);
csound->csoundMessageStringCallback = csoundMessageStrCallback;
csound->csoundMessageCallback_ = NULL;
}

}

PUBLIC void csoundSetMessageCallback(CSOUND *csound,
void (*csoundMessageCallback)(CSOUND *csound,
int attr,
Expand All @@ -2434,22 +2455,37 @@ PUBLIC void csoundSetMessageCallback(CSOUND *csound,
PUBLIC void csoundMessageV(CSOUND *csound,
int attr, const char *format, va_list args)
{
if(csound->csoundMessageCallback_) {
csound->csoundMessageCallback_(csound, attr, format, args);
} else {
vsnprintf(csound->message_string, MAX_MESSAGE_STR, format, args);
csound->csoundMessageStringCallback(csound, attr, csound->message_string);
}
}

PUBLIC void csoundMessage(CSOUND *csound, const char *format, ...)
{
va_list args;
va_start(args, format);
if(csound->csoundMessageCallback_)
csound->csoundMessageCallback_(csound, 0, format, args);
else {
vsnprintf(csound->message_string, MAX_MESSAGE_STR, format, args);
csound->csoundMessageStringCallback(csound, 0, csound->message_string);
}
va_end(args);
}

PUBLIC void csoundMessageS(CSOUND *csound, int attr, const char *format, ...)
{
va_list args;
va_start(args, format);
csound->csoundMessageCallback_(csound, attr, format, args);
if(csound->csoundMessageCallback_)
csound->csoundMessageCallback_(csound, 0, format, args);
else {
vsnprintf(csound->message_string, MAX_MESSAGE_STR, format, args);
csound->csoundMessageStringCallback(csound, attr, csound->message_string);
}
va_end(args);
}

Expand All @@ -2470,7 +2506,7 @@ void csoundWarning(CSOUND *csound, const char *msg, ...)
return;
csoundMessageS(csound, CSOUNDMSG_WARNING, Str("WARNING: "));
va_start(args, msg);
csound->csoundMessageCallback_(csound, CSOUNDMSG_WARNING, msg, args);
csoundMessageV(csound, CSOUNDMSG_WARNING, msg, args);
va_end(args);
csoundMessageS(csound, CSOUNDMSG_WARNING, "\n");
}
Expand All @@ -2481,7 +2517,7 @@ void csoundDebugMsg(CSOUND *csound, const char *msg, ...)
if (!(csound->oparms_.odebug))
return;
va_start(args, msg);
csound->csoundMessageCallback_(csound, 0, msg, args);
csoundMessageV(csound, 0, msg, args);
va_end(args);
csoundMessage(csound, "\n");
}
Expand All @@ -2490,7 +2526,7 @@ void csoundErrorMsg(CSOUND *csound, const char *msg, ...)
{
va_list args;
va_start(args, msg);
csound->csoundMessageCallback_(csound, CSOUNDMSG_ERROR, msg, args);
csoundMessageV(csound, CSOUNDMSG_ERROR, msg, args);
va_end(args);
csound->MessageS(csound, CSOUNDMSG_ERROR, "\n");
}
Expand All @@ -2500,7 +2536,7 @@ void csoundErrMsgV(CSOUND *csound,
{
if (hdr != NULL)
csound->MessageS(csound, CSOUNDMSG_ERROR, "%s", hdr);
csound->csoundMessageCallback_(csound, CSOUNDMSG_ERROR, msg, args);
csoundMessageV(csound, CSOUNDMSG_ERROR, msg, args);
csound->MessageS(csound, CSOUNDMSG_ERROR, "\n");
}

Expand Down
11 changes: 11 additions & 0 deletions include/csound.h
Original file line number Diff line number Diff line change
Expand Up @@ -1528,13 +1528,24 @@ extern "C" {

/**
* Sets a function to be called by Csound to print an informational message.
* This callback is never called on --realtime mode
*/
PUBLIC void csoundSetMessageCallback(CSOUND *,
void (*csoundMessageCallback_)(CSOUND *,
int attr,
const char *format,
va_list valist));

/**
* Sets an alternative function to be called by Csound to print an
* informational message, using a less granular signature.
* This callback can be set for --realtime mode
*/
PUBLIC void csoundSetMessageStringCallback(CSOUND *csound,
void (*csoundMessageStrCallback)(CSOUND *csound,
int attr,
const char *str));

/**
* Returns the Csound message level (from 0 to 231).
*/
Expand Down
13 changes: 13 additions & 0 deletions include/csoundCore.h
Original file line number Diff line number Diff line change
Expand Up @@ -970,6 +970,12 @@ typedef struct _alloc_data_ {
MEVENT mep;
} ALLOC_DATA;

#define MAX_MESSAGE_STR 1024
typedef struct _message_queue_t_ {
int attr;
char str[MAX_MESSAGE_STR];
} message_string_queue_t;


/**
* Contains all function pointers, data, and data pointers required
Expand Down Expand Up @@ -1781,6 +1787,13 @@ typedef struct _alloc_data_ {
unsigned long alloc_queue_wp;
int alloc_spinlock;
EVTBLK *init_event;
void (*csoundMessageStringCallback)(CSOUND *csound,
int attr,
const char *str);
char* message_string;
volatile unsigned long message_string_queue_items;
unsigned long message_string_queue_wp;
message_string_queue_t *message_string_queue;
/*struct CSOUND_ **self;*/
/**@}*/
#endif /* __BUILDING_LIBCSOUND */
Expand Down

0 comments on commit cf10758

Please sign in to comment.