2003-03-11 16:56:45 +00:00
|
|
|
/* FluidSynth - A Software Synthesizer
|
|
|
|
*
|
|
|
|
* Copyright (C) 2003 Peter Hanappe and others.
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Library General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2 of
|
|
|
|
* the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful, but
|
|
|
|
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Library General Public License for more details.
|
2007-03-04 16:45:05 +00:00
|
|
|
*
|
2003-03-11 16:56:45 +00:00
|
|
|
* You should have received a copy of the GNU Library General Public
|
|
|
|
* License along with this library; if not, write to the Free
|
|
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
|
|
|
|
* 02111-1307, USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "fluid_sys.h"
|
|
|
|
|
2009-04-27 19:13:49 +00:00
|
|
|
|
|
|
|
#if WITH_READLINE
|
|
|
|
#include <readline/readline.h>
|
|
|
|
#include <readline/history.h>
|
|
|
|
#endif
|
|
|
|
|
2010-06-11 08:23:36 +00:00
|
|
|
#ifdef DBUS_SUPPORT
|
|
|
|
#include "fluid_rtkit.h"
|
|
|
|
#endif
|
|
|
|
|
2009-10-12 19:18:06 +00:00
|
|
|
/* WIN32 HACK - Flag used to differentiate between a file descriptor and a socket.
|
|
|
|
* Should work, so long as no SOCKET or file descriptor ends up with this bit set. - JG */
|
|
|
|
#define WIN32_SOCKET_FLAG 0x40000000
|
2009-04-27 19:13:49 +00:00
|
|
|
|
2009-10-23 06:52:29 +00:00
|
|
|
/* SCHED_FIFO priority for high priority timer threads */
|
|
|
|
#define FLUID_SYS_TIMER_HIGH_PRIO_LEVEL 10
|
|
|
|
|
|
|
|
|
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
fluid_thread_func_t func;
|
|
|
|
void *data;
|
|
|
|
int prio_level;
|
|
|
|
} fluid_thread_info_t;
|
|
|
|
|
|
|
|
struct _fluid_timer_t
|
|
|
|
{
|
|
|
|
long msec;
|
|
|
|
fluid_timer_callback_t callback;
|
|
|
|
void *data;
|
|
|
|
fluid_thread_t *thread;
|
|
|
|
int cont;
|
|
|
|
int auto_destroy;
|
|
|
|
};
|
|
|
|
|
2009-04-27 19:13:49 +00:00
|
|
|
struct _fluid_server_socket_t
|
|
|
|
{
|
|
|
|
fluid_socket_t socket;
|
|
|
|
fluid_thread_t *thread;
|
|
|
|
int cont;
|
|
|
|
fluid_server_func_t func;
|
|
|
|
void *data;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
static int fluid_istream_gets(fluid_istream_t in, char* buf, int len);
|
|
|
|
|
|
|
|
|
2003-03-11 16:56:45 +00:00
|
|
|
static char fluid_errbuf[512]; /* buffer for error message */
|
|
|
|
|
|
|
|
static fluid_log_function_t fluid_log_function[LAST_LOG_LEVEL];
|
|
|
|
static void* fluid_log_user_data[LAST_LOG_LEVEL];
|
|
|
|
static int fluid_log_initialized = 0;
|
|
|
|
|
|
|
|
static char* fluid_libname = "fluidsynth";
|
|
|
|
|
|
|
|
|
|
|
|
void fluid_sys_config()
|
|
|
|
{
|
|
|
|
fluid_log_config();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
unsigned int fluid_debug_flags = 0;
|
|
|
|
|
|
|
|
#if DEBUG
|
|
|
|
/*
|
|
|
|
* fluid_debug
|
|
|
|
*/
|
|
|
|
int fluid_debug(int level, char * fmt, ...)
|
|
|
|
{
|
|
|
|
if (fluid_debug_flags & level) {
|
|
|
|
fluid_log_function_t fun;
|
2007-03-04 16:45:05 +00:00
|
|
|
va_list args;
|
2003-03-11 16:56:45 +00:00
|
|
|
|
2007-03-04 16:45:05 +00:00
|
|
|
va_start (args, fmt);
|
2005-06-12 01:19:37 +00:00
|
|
|
vsnprintf(fluid_errbuf, sizeof (fluid_errbuf), fmt, args);
|
2007-03-04 16:45:05 +00:00
|
|
|
va_end (args);
|
2003-03-11 16:56:45 +00:00
|
|
|
|
|
|
|
fun = fluid_log_function[FLUID_DBG];
|
|
|
|
if (fun != NULL) {
|
|
|
|
(*fun)(level, fluid_errbuf, fluid_log_user_data[FLUID_DBG]);
|
|
|
|
}
|
2007-03-04 16:45:05 +00:00
|
|
|
}
|
2003-03-11 16:56:45 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2006-12-10 16:02:04 +00:00
|
|
|
/**
|
|
|
|
* Installs a new log function for a specified log level.
|
|
|
|
* @param level Log level to install handler for.
|
|
|
|
* @param fun Callback function handler to call for logged messages
|
|
|
|
* @param data User supplied data pointer to pass to log function
|
|
|
|
* @return The previously installed function.
|
2003-03-11 16:56:45 +00:00
|
|
|
*/
|
2007-03-04 16:45:05 +00:00
|
|
|
fluid_log_function_t
|
2003-03-11 16:56:45 +00:00
|
|
|
fluid_set_log_function(int level, fluid_log_function_t fun, void* data)
|
|
|
|
{
|
|
|
|
fluid_log_function_t old = NULL;
|
|
|
|
|
|
|
|
if ((level >= 0) && (level < LAST_LOG_LEVEL)) {
|
|
|
|
old = fluid_log_function[level];
|
|
|
|
fluid_log_function[level] = fun;
|
|
|
|
fluid_log_user_data[level] = data;
|
|
|
|
}
|
|
|
|
return old;
|
|
|
|
}
|
|
|
|
|
2006-12-10 16:02:04 +00:00
|
|
|
/**
|
|
|
|
* Default log function which prints to the stderr.
|
|
|
|
* @param level Log level
|
|
|
|
* @param message Log message
|
|
|
|
* @param data User supplied data (not used)
|
2003-03-11 16:56:45 +00:00
|
|
|
*/
|
2007-03-04 16:45:05 +00:00
|
|
|
void
|
2009-11-05 04:37:51 +00:00
|
|
|
fluid_default_log_function(int level, char* message, void* data)
|
2003-03-11 16:56:45 +00:00
|
|
|
{
|
|
|
|
FILE* out;
|
|
|
|
|
|
|
|
#if defined(WIN32)
|
|
|
|
out = stdout;
|
|
|
|
#else
|
|
|
|
out = stderr;
|
|
|
|
#endif
|
2007-03-04 16:45:05 +00:00
|
|
|
|
2003-03-11 16:56:45 +00:00
|
|
|
if (fluid_log_initialized == 0) {
|
|
|
|
fluid_log_config();
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (level) {
|
2007-03-04 16:45:05 +00:00
|
|
|
case FLUID_PANIC:
|
2003-03-11 16:56:45 +00:00
|
|
|
FLUID_FPRINTF(out, "%s: panic: %s\n", fluid_libname, message);
|
|
|
|
break;
|
2007-03-04 16:45:05 +00:00
|
|
|
case FLUID_ERR:
|
2003-03-11 16:56:45 +00:00
|
|
|
FLUID_FPRINTF(out, "%s: error: %s\n", fluid_libname, message);
|
|
|
|
break;
|
2007-03-04 16:45:05 +00:00
|
|
|
case FLUID_WARN:
|
2003-03-11 16:56:45 +00:00
|
|
|
FLUID_FPRINTF(out, "%s: warning: %s\n", fluid_libname, message);
|
|
|
|
break;
|
2007-03-04 16:45:05 +00:00
|
|
|
case FLUID_INFO:
|
2003-03-11 16:56:45 +00:00
|
|
|
FLUID_FPRINTF(out, "%s: %s\n", fluid_libname, message);
|
|
|
|
break;
|
2007-03-04 16:45:05 +00:00
|
|
|
case FLUID_DBG:
|
2003-03-11 16:56:45 +00:00
|
|
|
#if DEBUG
|
|
|
|
FLUID_FPRINTF(out, "%s: debug: %s\n", fluid_libname, message);
|
|
|
|
#endif
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
FLUID_FPRINTF(out, "%s: %s\n", fluid_libname, message);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
fflush(out);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* fluid_init_log
|
|
|
|
*/
|
2007-03-04 16:45:05 +00:00
|
|
|
void
|
2003-03-11 16:56:45 +00:00
|
|
|
fluid_log_config(void)
|
|
|
|
{
|
|
|
|
if (fluid_log_initialized == 0) {
|
|
|
|
|
|
|
|
fluid_log_initialized = 1;
|
|
|
|
|
|
|
|
if (fluid_log_function[FLUID_PANIC] == NULL) {
|
|
|
|
fluid_set_log_function(FLUID_PANIC, fluid_default_log_function, NULL);
|
|
|
|
}
|
2007-03-04 16:45:05 +00:00
|
|
|
|
2003-03-11 16:56:45 +00:00
|
|
|
if (fluid_log_function[FLUID_ERR] == NULL) {
|
|
|
|
fluid_set_log_function(FLUID_ERR, fluid_default_log_function, NULL);
|
|
|
|
}
|
2007-03-04 16:45:05 +00:00
|
|
|
|
2003-03-11 16:56:45 +00:00
|
|
|
if (fluid_log_function[FLUID_WARN] == NULL) {
|
|
|
|
fluid_set_log_function(FLUID_WARN, fluid_default_log_function, NULL);
|
|
|
|
}
|
2007-03-04 16:45:05 +00:00
|
|
|
|
2003-03-11 16:56:45 +00:00
|
|
|
if (fluid_log_function[FLUID_INFO] == NULL) {
|
|
|
|
fluid_set_log_function(FLUID_INFO, fluid_default_log_function, NULL);
|
|
|
|
}
|
2007-03-04 16:45:05 +00:00
|
|
|
|
2003-03-11 16:56:45 +00:00
|
|
|
if (fluid_log_function[FLUID_DBG] == NULL) {
|
|
|
|
fluid_set_log_function(FLUID_DBG, fluid_default_log_function, NULL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-12-10 16:02:04 +00:00
|
|
|
/**
|
|
|
|
* Print a message to the log.
|
|
|
|
* @param level Log level (#fluid_log_level).
|
|
|
|
* @param fmt Printf style format string for log message
|
|
|
|
* @param ... Arguments for printf 'fmt' message string
|
2009-10-15 07:00:30 +00:00
|
|
|
* @return Always returns #FLUID_FAILED
|
2003-03-11 16:56:45 +00:00
|
|
|
*/
|
2007-03-04 16:45:05 +00:00
|
|
|
int
|
2009-10-28 19:38:10 +00:00
|
|
|
fluid_log(int level, const char* fmt, ...)
|
2003-03-11 16:56:45 +00:00
|
|
|
{
|
|
|
|
fluid_log_function_t fun = NULL;
|
|
|
|
|
2007-03-04 16:45:05 +00:00
|
|
|
va_list args;
|
|
|
|
va_start (args, fmt);
|
2005-06-12 01:19:37 +00:00
|
|
|
vsnprintf(fluid_errbuf, sizeof (fluid_errbuf), fmt, args);
|
2007-03-04 16:45:05 +00:00
|
|
|
va_end (args);
|
2003-03-11 16:56:45 +00:00
|
|
|
|
|
|
|
if ((level >= 0) && (level < LAST_LOG_LEVEL)) {
|
|
|
|
fun = fluid_log_function[level];
|
|
|
|
if (fun != NULL) {
|
|
|
|
(*fun)(level, fluid_errbuf, fluid_log_user_data[level]);
|
|
|
|
}
|
|
|
|
}
|
2007-03-04 16:45:05 +00:00
|
|
|
return FLUID_FAILED;
|
2003-03-11 16:56:45 +00:00
|
|
|
}
|
|
|
|
|
2006-12-10 16:02:04 +00:00
|
|
|
/**
|
|
|
|
* An improved strtok, still trashes the input string, but is portable and
|
|
|
|
* thread safe. Also skips token chars at beginning of token string and never
|
|
|
|
* returns an empty token (will return NULL if source ends in token chars though).
|
|
|
|
* NOTE: NOT part of public API
|
|
|
|
* @internal
|
|
|
|
* @param str Pointer to a string pointer of source to tokenize. Pointer gets
|
|
|
|
* updated on each invocation to point to beginning of next token. Note that
|
|
|
|
* token char get's overwritten with a 0 byte. String pointer is set to NULL
|
|
|
|
* when final token is returned.
|
|
|
|
* @param delim String of delimiter chars.
|
|
|
|
* @return Pointer to the next token or NULL if no more tokens.
|
|
|
|
*/
|
|
|
|
char *fluid_strtok (char **str, char *delim)
|
|
|
|
{
|
|
|
|
char *s, *d, *token;
|
|
|
|
char c;
|
|
|
|
|
|
|
|
if (str == NULL || delim == NULL || !*delim)
|
|
|
|
{
|
|
|
|
FLUID_LOG(FLUID_ERR, "Null pointer");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
s = *str;
|
|
|
|
if (!s) return NULL; /* str points to a NULL pointer? (tokenize already ended) */
|
|
|
|
|
|
|
|
/* skip delimiter chars at beginning of token */
|
|
|
|
do
|
|
|
|
{
|
|
|
|
c = *s;
|
|
|
|
if (!c) /* end of source string? */
|
|
|
|
{
|
|
|
|
*str = NULL;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (d = delim; *d; d++) /* is source char a token char? */
|
|
|
|
{
|
|
|
|
if (c == *d) /* token char match? */
|
|
|
|
{
|
|
|
|
s++; /* advance to next source char */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} while (*d); /* while token char match */
|
|
|
|
|
|
|
|
token = s; /* start of token found */
|
|
|
|
|
|
|
|
/* search for next token char or end of source string */
|
|
|
|
for (s = s+1; *s; s++)
|
|
|
|
{
|
|
|
|
c = *s;
|
|
|
|
|
|
|
|
for (d = delim; *d; d++) /* is source char a token char? */
|
|
|
|
{
|
|
|
|
if (c == *d) /* token char match? */
|
|
|
|
{
|
|
|
|
*s = '\0'; /* overwrite token char with zero byte to terminate token */
|
|
|
|
*str = s+1; /* update str to point to beginning of next token */
|
|
|
|
return token;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* we get here only if source string ended */
|
|
|
|
*str = NULL;
|
|
|
|
return token;
|
|
|
|
}
|
2003-03-11 16:56:45 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* fluid_error
|
|
|
|
*/
|
2007-03-04 16:45:05 +00:00
|
|
|
char*
|
2003-03-11 16:56:45 +00:00
|
|
|
fluid_error()
|
|
|
|
{
|
|
|
|
return fluid_errbuf;
|
|
|
|
}
|
|
|
|
|
2009-10-15 07:00:30 +00:00
|
|
|
/**
|
|
|
|
* Check if a file is a MIDI file.
|
|
|
|
* @param filename Path to the file to check
|
|
|
|
* @return TRUE if it could be a MIDI file, FALSE otherwise
|
2007-03-04 16:45:05 +00:00
|
|
|
*
|
2009-10-15 07:00:30 +00:00
|
|
|
* The current implementation only checks for the "MThd" header in the file.
|
|
|
|
* It is useful only to distinguish between SoundFont and MIDI files.
|
2007-03-04 16:45:05 +00:00
|
|
|
*/
|
2003-03-11 16:56:45 +00:00
|
|
|
int
|
2009-10-28 19:38:10 +00:00
|
|
|
fluid_is_midifile(const char *filename)
|
2003-03-11 16:56:45 +00:00
|
|
|
{
|
|
|
|
FILE* fp = fopen(filename, "rb");
|
|
|
|
char id[4];
|
|
|
|
|
|
|
|
if (fp == NULL) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (fread((void*) id, 1, 4, fp) != 4) {
|
|
|
|
fclose(fp);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
fclose(fp);
|
2007-03-04 16:45:05 +00:00
|
|
|
|
2003-03-11 16:56:45 +00:00
|
|
|
return strncmp(id, "MThd", 4) == 0;
|
|
|
|
}
|
|
|
|
|
2009-10-15 07:00:30 +00:00
|
|
|
/**
|
|
|
|
* Check if a file is a SoundFont file.
|
|
|
|
* @param filename Path to the file to check
|
|
|
|
* @return TRUE if it could be a SoundFont, FALSE otherwise
|
2007-03-04 16:45:05 +00:00
|
|
|
*
|
2009-10-15 07:00:30 +00:00
|
|
|
* The current implementation only checks for the "RIFF" header in the file.
|
|
|
|
* It is useful only to distinguish between SoundFont and MIDI files.
|
2007-03-04 16:45:05 +00:00
|
|
|
*/
|
2003-03-11 16:56:45 +00:00
|
|
|
int
|
2009-10-28 19:38:10 +00:00
|
|
|
fluid_is_soundfont(const char *filename)
|
2003-03-11 16:56:45 +00:00
|
|
|
{
|
|
|
|
FILE* fp = fopen(filename, "rb");
|
|
|
|
char id[4];
|
|
|
|
|
|
|
|
if (fp == NULL) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (fread((void*) id, 1, 4, fp) != 4) {
|
|
|
|
fclose(fp);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
fclose(fp);
|
2007-03-04 16:45:05 +00:00
|
|
|
|
2003-03-11 16:56:45 +00:00
|
|
|
return strncmp(id, "RIFF", 4) == 0;
|
|
|
|
}
|
|
|
|
|
2009-04-27 19:13:49 +00:00
|
|
|
/**
|
|
|
|
* Get time in milliseconds to be used in relative timing operations.
|
|
|
|
* @return Unix time in milliseconds.
|
|
|
|
*/
|
|
|
|
unsigned int fluid_curtime(void)
|
|
|
|
{
|
2010-03-17 15:40:07 +00:00
|
|
|
static glong initial_seconds = 0;
|
2009-04-27 19:13:49 +00:00
|
|
|
GTimeVal timeval;
|
|
|
|
|
2010-03-17 15:40:07 +00:00
|
|
|
if (initial_seconds == 0) {
|
|
|
|
g_get_current_time (&timeval);
|
|
|
|
initial_seconds = timeval.tv_sec;
|
|
|
|
}
|
|
|
|
|
2009-04-27 19:13:49 +00:00
|
|
|
g_get_current_time (&timeval);
|
|
|
|
|
2010-03-17 15:40:07 +00:00
|
|
|
return (unsigned int)((timeval.tv_sec - initial_seconds) * 1000.0 + timeval.tv_usec / 1000.0);
|
2009-04-27 19:13:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get time in microseconds to be used in relative timing operations.
|
|
|
|
* @return Unix time in microseconds.
|
|
|
|
*/
|
|
|
|
double
|
|
|
|
fluid_utime (void)
|
|
|
|
{
|
|
|
|
GTimeVal timeval;
|
|
|
|
|
|
|
|
g_get_current_time (&timeval);
|
|
|
|
|
|
|
|
return (timeval.tv_sec * 1000000.0 + timeval.tv_usec);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-10-23 06:52:29 +00:00
|
|
|
#if defined(WIN32) /* Windoze specific stuff */
|
2003-03-11 16:56:45 +00:00
|
|
|
|
Added public API functions: fluid_synth_sysex, fluid_synth_activate_key_tuning, fluid_synth_activate_octave_tuning, fluid_synth_tune_notes, fluid_synth_activate_tuning and fluid_synth_deactivate_tuning.
Added audio.realtime, audio.realtime-prio, midi.realtime and midi.realtime-prio (only ALSA updated to use them at this point).
Fixed bug in new_fluid_channel() where tuning field was not initialized.
fluid_settings.h had wrong field order in prototypes for fluid_settings_register_num and fluid_settings_register_int.
Added multi core support: "synth.cpu-cores" setting, fluid_synth_core_thread_func() and many core_ variables to fluid_synth_t.
Switched fluid_mutex_t back to a normal non-recursive mutex and added fluid_rec_mutex_t.
Added fluid_cond_t thread synchronization stuff.
Added fluid_cond_mutex_t which is a dynamically allocated regular mutex for use with fluid_cond_t.
fluid_settings_t and fluid_synth_t are now using fluid_rec_mutex_t (same as before, just name change).
Added platform specific fluid_thread_self_set_prio() functions to fluid_sys.c for activating high priority for the calling thread.
Modified new_fluid_thread() to take a prio and prio_level parameters.
Added missing fluid_atomic_pointer_set().
fluid_voice_write() changed to only take a voice audio buffer to render to, mixing is done separately with new fluid_voice_mix().
fluid_voice_write() now returns the count of samples rendered.
fluid_voice_effects() split into fluid_voice_filter() and fluid_voice_mix().
Added dsp_buf_count field to fluid_voice_t to keep track of last count of samples rendered to dsp_buf.
fluid_voice_get_channel() converted to a macro.
Added FLUID_DEFAULT_AUDIO_RT_PRIO and FLUID_DEFAULT_MIDI_RT_PRIO in fluidsynth_priv.h, set to 90 and 80 respectively which get used for audio.realtime-prio and midi.realtime-prio (was 90 and 90 before).
2009-09-29 21:40:28 +00:00
|
|
|
void
|
2009-10-30 21:49:54 +00:00
|
|
|
fluid_thread_self_set_prio (int prio_level)
|
Added public API functions: fluid_synth_sysex, fluid_synth_activate_key_tuning, fluid_synth_activate_octave_tuning, fluid_synth_tune_notes, fluid_synth_activate_tuning and fluid_synth_deactivate_tuning.
Added audio.realtime, audio.realtime-prio, midi.realtime and midi.realtime-prio (only ALSA updated to use them at this point).
Fixed bug in new_fluid_channel() where tuning field was not initialized.
fluid_settings.h had wrong field order in prototypes for fluid_settings_register_num and fluid_settings_register_int.
Added multi core support: "synth.cpu-cores" setting, fluid_synth_core_thread_func() and many core_ variables to fluid_synth_t.
Switched fluid_mutex_t back to a normal non-recursive mutex and added fluid_rec_mutex_t.
Added fluid_cond_t thread synchronization stuff.
Added fluid_cond_mutex_t which is a dynamically allocated regular mutex for use with fluid_cond_t.
fluid_settings_t and fluid_synth_t are now using fluid_rec_mutex_t (same as before, just name change).
Added platform specific fluid_thread_self_set_prio() functions to fluid_sys.c for activating high priority for the calling thread.
Modified new_fluid_thread() to take a prio and prio_level parameters.
Added missing fluid_atomic_pointer_set().
fluid_voice_write() changed to only take a voice audio buffer to render to, mixing is done separately with new fluid_voice_mix().
fluid_voice_write() now returns the count of samples rendered.
fluid_voice_effects() split into fluid_voice_filter() and fluid_voice_mix().
Added dsp_buf_count field to fluid_voice_t to keep track of last count of samples rendered to dsp_buf.
fluid_voice_get_channel() converted to a macro.
Added FLUID_DEFAULT_AUDIO_RT_PRIO and FLUID_DEFAULT_MIDI_RT_PRIO in fluidsynth_priv.h, set to 90 and 80 respectively which get used for audio.realtime-prio and midi.realtime-prio (was 90 and 90 before).
2009-09-29 21:40:28 +00:00
|
|
|
{
|
2009-10-30 21:49:54 +00:00
|
|
|
if (prio_level > 0)
|
Added public API functions: fluid_synth_sysex, fluid_synth_activate_key_tuning, fluid_synth_activate_octave_tuning, fluid_synth_tune_notes, fluid_synth_activate_tuning and fluid_synth_deactivate_tuning.
Added audio.realtime, audio.realtime-prio, midi.realtime and midi.realtime-prio (only ALSA updated to use them at this point).
Fixed bug in new_fluid_channel() where tuning field was not initialized.
fluid_settings.h had wrong field order in prototypes for fluid_settings_register_num and fluid_settings_register_int.
Added multi core support: "synth.cpu-cores" setting, fluid_synth_core_thread_func() and many core_ variables to fluid_synth_t.
Switched fluid_mutex_t back to a normal non-recursive mutex and added fluid_rec_mutex_t.
Added fluid_cond_t thread synchronization stuff.
Added fluid_cond_mutex_t which is a dynamically allocated regular mutex for use with fluid_cond_t.
fluid_settings_t and fluid_synth_t are now using fluid_rec_mutex_t (same as before, just name change).
Added platform specific fluid_thread_self_set_prio() functions to fluid_sys.c for activating high priority for the calling thread.
Modified new_fluid_thread() to take a prio and prio_level parameters.
Added missing fluid_atomic_pointer_set().
fluid_voice_write() changed to only take a voice audio buffer to render to, mixing is done separately with new fluid_voice_mix().
fluid_voice_write() now returns the count of samples rendered.
fluid_voice_effects() split into fluid_voice_filter() and fluid_voice_mix().
Added dsp_buf_count field to fluid_voice_t to keep track of last count of samples rendered to dsp_buf.
fluid_voice_get_channel() converted to a macro.
Added FLUID_DEFAULT_AUDIO_RT_PRIO and FLUID_DEFAULT_MIDI_RT_PRIO in fluidsynth_priv.h, set to 90 and 80 respectively which get used for audio.realtime-prio and midi.realtime-prio (was 90 and 90 before).
2009-09-29 21:40:28 +00:00
|
|
|
SetThreadPriority (GetCurrentThread(), THREAD_PRIORITY_HIGHEST);
|
|
|
|
}
|
|
|
|
|
2003-03-11 16:56:45 +00:00
|
|
|
|
2009-10-23 06:52:29 +00:00
|
|
|
#elif defined(__OS2__) /* OS/2 specific stuff */
|
2009-02-04 07:45:44 +00:00
|
|
|
|
Added public API functions: fluid_synth_sysex, fluid_synth_activate_key_tuning, fluid_synth_activate_octave_tuning, fluid_synth_tune_notes, fluid_synth_activate_tuning and fluid_synth_deactivate_tuning.
Added audio.realtime, audio.realtime-prio, midi.realtime and midi.realtime-prio (only ALSA updated to use them at this point).
Fixed bug in new_fluid_channel() where tuning field was not initialized.
fluid_settings.h had wrong field order in prototypes for fluid_settings_register_num and fluid_settings_register_int.
Added multi core support: "synth.cpu-cores" setting, fluid_synth_core_thread_func() and many core_ variables to fluid_synth_t.
Switched fluid_mutex_t back to a normal non-recursive mutex and added fluid_rec_mutex_t.
Added fluid_cond_t thread synchronization stuff.
Added fluid_cond_mutex_t which is a dynamically allocated regular mutex for use with fluid_cond_t.
fluid_settings_t and fluid_synth_t are now using fluid_rec_mutex_t (same as before, just name change).
Added platform specific fluid_thread_self_set_prio() functions to fluid_sys.c for activating high priority for the calling thread.
Modified new_fluid_thread() to take a prio and prio_level parameters.
Added missing fluid_atomic_pointer_set().
fluid_voice_write() changed to only take a voice audio buffer to render to, mixing is done separately with new fluid_voice_mix().
fluid_voice_write() now returns the count of samples rendered.
fluid_voice_effects() split into fluid_voice_filter() and fluid_voice_mix().
Added dsp_buf_count field to fluid_voice_t to keep track of last count of samples rendered to dsp_buf.
fluid_voice_get_channel() converted to a macro.
Added FLUID_DEFAULT_AUDIO_RT_PRIO and FLUID_DEFAULT_MIDI_RT_PRIO in fluidsynth_priv.h, set to 90 and 80 respectively which get used for audio.realtime-prio and midi.realtime-prio (was 90 and 90 before).
2009-09-29 21:40:28 +00:00
|
|
|
void
|
2009-10-30 21:49:54 +00:00
|
|
|
fluid_thread_self_set_prio (int prio_level)
|
Added public API functions: fluid_synth_sysex, fluid_synth_activate_key_tuning, fluid_synth_activate_octave_tuning, fluid_synth_tune_notes, fluid_synth_activate_tuning and fluid_synth_deactivate_tuning.
Added audio.realtime, audio.realtime-prio, midi.realtime and midi.realtime-prio (only ALSA updated to use them at this point).
Fixed bug in new_fluid_channel() where tuning field was not initialized.
fluid_settings.h had wrong field order in prototypes for fluid_settings_register_num and fluid_settings_register_int.
Added multi core support: "synth.cpu-cores" setting, fluid_synth_core_thread_func() and many core_ variables to fluid_synth_t.
Switched fluid_mutex_t back to a normal non-recursive mutex and added fluid_rec_mutex_t.
Added fluid_cond_t thread synchronization stuff.
Added fluid_cond_mutex_t which is a dynamically allocated regular mutex for use with fluid_cond_t.
fluid_settings_t and fluid_synth_t are now using fluid_rec_mutex_t (same as before, just name change).
Added platform specific fluid_thread_self_set_prio() functions to fluid_sys.c for activating high priority for the calling thread.
Modified new_fluid_thread() to take a prio and prio_level parameters.
Added missing fluid_atomic_pointer_set().
fluid_voice_write() changed to only take a voice audio buffer to render to, mixing is done separately with new fluid_voice_mix().
fluid_voice_write() now returns the count of samples rendered.
fluid_voice_effects() split into fluid_voice_filter() and fluid_voice_mix().
Added dsp_buf_count field to fluid_voice_t to keep track of last count of samples rendered to dsp_buf.
fluid_voice_get_channel() converted to a macro.
Added FLUID_DEFAULT_AUDIO_RT_PRIO and FLUID_DEFAULT_MIDI_RT_PRIO in fluidsynth_priv.h, set to 90 and 80 respectively which get used for audio.realtime-prio and midi.realtime-prio (was 90 and 90 before).
2009-09-29 21:40:28 +00:00
|
|
|
{
|
2009-10-30 21:49:54 +00:00
|
|
|
if (prio_level > 0)
|
Added public API functions: fluid_synth_sysex, fluid_synth_activate_key_tuning, fluid_synth_activate_octave_tuning, fluid_synth_tune_notes, fluid_synth_activate_tuning and fluid_synth_deactivate_tuning.
Added audio.realtime, audio.realtime-prio, midi.realtime and midi.realtime-prio (only ALSA updated to use them at this point).
Fixed bug in new_fluid_channel() where tuning field was not initialized.
fluid_settings.h had wrong field order in prototypes for fluid_settings_register_num and fluid_settings_register_int.
Added multi core support: "synth.cpu-cores" setting, fluid_synth_core_thread_func() and many core_ variables to fluid_synth_t.
Switched fluid_mutex_t back to a normal non-recursive mutex and added fluid_rec_mutex_t.
Added fluid_cond_t thread synchronization stuff.
Added fluid_cond_mutex_t which is a dynamically allocated regular mutex for use with fluid_cond_t.
fluid_settings_t and fluid_synth_t are now using fluid_rec_mutex_t (same as before, just name change).
Added platform specific fluid_thread_self_set_prio() functions to fluid_sys.c for activating high priority for the calling thread.
Modified new_fluid_thread() to take a prio and prio_level parameters.
Added missing fluid_atomic_pointer_set().
fluid_voice_write() changed to only take a voice audio buffer to render to, mixing is done separately with new fluid_voice_mix().
fluid_voice_write() now returns the count of samples rendered.
fluid_voice_effects() split into fluid_voice_filter() and fluid_voice_mix().
Added dsp_buf_count field to fluid_voice_t to keep track of last count of samples rendered to dsp_buf.
fluid_voice_get_channel() converted to a macro.
Added FLUID_DEFAULT_AUDIO_RT_PRIO and FLUID_DEFAULT_MIDI_RT_PRIO in fluidsynth_priv.h, set to 90 and 80 respectively which get used for audio.realtime-prio and midi.realtime-prio (was 90 and 90 before).
2009-09-29 21:40:28 +00:00
|
|
|
DosSetPriority (PRTYS_THREAD, PRTYC_REGULAR, PRTYD_MAXIMUM, 0);
|
|
|
|
}
|
|
|
|
|
2009-10-23 06:52:29 +00:00
|
|
|
#else /* POSIX stuff.. Nice POSIX.. Good POSIX. */
|
2003-03-11 16:56:45 +00:00
|
|
|
|
Added public API functions: fluid_synth_sysex, fluid_synth_activate_key_tuning, fluid_synth_activate_octave_tuning, fluid_synth_tune_notes, fluid_synth_activate_tuning and fluid_synth_deactivate_tuning.
Added audio.realtime, audio.realtime-prio, midi.realtime and midi.realtime-prio (only ALSA updated to use them at this point).
Fixed bug in new_fluid_channel() where tuning field was not initialized.
fluid_settings.h had wrong field order in prototypes for fluid_settings_register_num and fluid_settings_register_int.
Added multi core support: "synth.cpu-cores" setting, fluid_synth_core_thread_func() and many core_ variables to fluid_synth_t.
Switched fluid_mutex_t back to a normal non-recursive mutex and added fluid_rec_mutex_t.
Added fluid_cond_t thread synchronization stuff.
Added fluid_cond_mutex_t which is a dynamically allocated regular mutex for use with fluid_cond_t.
fluid_settings_t and fluid_synth_t are now using fluid_rec_mutex_t (same as before, just name change).
Added platform specific fluid_thread_self_set_prio() functions to fluid_sys.c for activating high priority for the calling thread.
Modified new_fluid_thread() to take a prio and prio_level parameters.
Added missing fluid_atomic_pointer_set().
fluid_voice_write() changed to only take a voice audio buffer to render to, mixing is done separately with new fluid_voice_mix().
fluid_voice_write() now returns the count of samples rendered.
fluid_voice_effects() split into fluid_voice_filter() and fluid_voice_mix().
Added dsp_buf_count field to fluid_voice_t to keep track of last count of samples rendered to dsp_buf.
fluid_voice_get_channel() converted to a macro.
Added FLUID_DEFAULT_AUDIO_RT_PRIO and FLUID_DEFAULT_MIDI_RT_PRIO in fluidsynth_priv.h, set to 90 and 80 respectively which get used for audio.realtime-prio and midi.realtime-prio (was 90 and 90 before).
2009-09-29 21:40:28 +00:00
|
|
|
void
|
2009-10-30 21:49:54 +00:00
|
|
|
fluid_thread_self_set_prio (int prio_level)
|
Added public API functions: fluid_synth_sysex, fluid_synth_activate_key_tuning, fluid_synth_activate_octave_tuning, fluid_synth_tune_notes, fluid_synth_activate_tuning and fluid_synth_deactivate_tuning.
Added audio.realtime, audio.realtime-prio, midi.realtime and midi.realtime-prio (only ALSA updated to use them at this point).
Fixed bug in new_fluid_channel() where tuning field was not initialized.
fluid_settings.h had wrong field order in prototypes for fluid_settings_register_num and fluid_settings_register_int.
Added multi core support: "synth.cpu-cores" setting, fluid_synth_core_thread_func() and many core_ variables to fluid_synth_t.
Switched fluid_mutex_t back to a normal non-recursive mutex and added fluid_rec_mutex_t.
Added fluid_cond_t thread synchronization stuff.
Added fluid_cond_mutex_t which is a dynamically allocated regular mutex for use with fluid_cond_t.
fluid_settings_t and fluid_synth_t are now using fluid_rec_mutex_t (same as before, just name change).
Added platform specific fluid_thread_self_set_prio() functions to fluid_sys.c for activating high priority for the calling thread.
Modified new_fluid_thread() to take a prio and prio_level parameters.
Added missing fluid_atomic_pointer_set().
fluid_voice_write() changed to only take a voice audio buffer to render to, mixing is done separately with new fluid_voice_mix().
fluid_voice_write() now returns the count of samples rendered.
fluid_voice_effects() split into fluid_voice_filter() and fluid_voice_mix().
Added dsp_buf_count field to fluid_voice_t to keep track of last count of samples rendered to dsp_buf.
fluid_voice_get_channel() converted to a macro.
Added FLUID_DEFAULT_AUDIO_RT_PRIO and FLUID_DEFAULT_MIDI_RT_PRIO in fluidsynth_priv.h, set to 90 and 80 respectively which get used for audio.realtime-prio and midi.realtime-prio (was 90 and 90 before).
2009-09-29 21:40:28 +00:00
|
|
|
{
|
|
|
|
struct sched_param priority;
|
|
|
|
|
2009-10-30 21:49:54 +00:00
|
|
|
if (prio_level > 0)
|
Added public API functions: fluid_synth_sysex, fluid_synth_activate_key_tuning, fluid_synth_activate_octave_tuning, fluid_synth_tune_notes, fluid_synth_activate_tuning and fluid_synth_deactivate_tuning.
Added audio.realtime, audio.realtime-prio, midi.realtime and midi.realtime-prio (only ALSA updated to use them at this point).
Fixed bug in new_fluid_channel() where tuning field was not initialized.
fluid_settings.h had wrong field order in prototypes for fluid_settings_register_num and fluid_settings_register_int.
Added multi core support: "synth.cpu-cores" setting, fluid_synth_core_thread_func() and many core_ variables to fluid_synth_t.
Switched fluid_mutex_t back to a normal non-recursive mutex and added fluid_rec_mutex_t.
Added fluid_cond_t thread synchronization stuff.
Added fluid_cond_mutex_t which is a dynamically allocated regular mutex for use with fluid_cond_t.
fluid_settings_t and fluid_synth_t are now using fluid_rec_mutex_t (same as before, just name change).
Added platform specific fluid_thread_self_set_prio() functions to fluid_sys.c for activating high priority for the calling thread.
Modified new_fluid_thread() to take a prio and prio_level parameters.
Added missing fluid_atomic_pointer_set().
fluid_voice_write() changed to only take a voice audio buffer to render to, mixing is done separately with new fluid_voice_mix().
fluid_voice_write() now returns the count of samples rendered.
fluid_voice_effects() split into fluid_voice_filter() and fluid_voice_mix().
Added dsp_buf_count field to fluid_voice_t to keep track of last count of samples rendered to dsp_buf.
fluid_voice_get_channel() converted to a macro.
Added FLUID_DEFAULT_AUDIO_RT_PRIO and FLUID_DEFAULT_MIDI_RT_PRIO in fluidsynth_priv.h, set to 90 and 80 respectively which get used for audio.realtime-prio and midi.realtime-prio (was 90 and 90 before).
2009-09-29 21:40:28 +00:00
|
|
|
{
|
2010-06-11 08:23:36 +00:00
|
|
|
|
|
|
|
memset(&priority, 0, sizeof(priority));
|
Added public API functions: fluid_synth_sysex, fluid_synth_activate_key_tuning, fluid_synth_activate_octave_tuning, fluid_synth_tune_notes, fluid_synth_activate_tuning and fluid_synth_deactivate_tuning.
Added audio.realtime, audio.realtime-prio, midi.realtime and midi.realtime-prio (only ALSA updated to use them at this point).
Fixed bug in new_fluid_channel() where tuning field was not initialized.
fluid_settings.h had wrong field order in prototypes for fluid_settings_register_num and fluid_settings_register_int.
Added multi core support: "synth.cpu-cores" setting, fluid_synth_core_thread_func() and many core_ variables to fluid_synth_t.
Switched fluid_mutex_t back to a normal non-recursive mutex and added fluid_rec_mutex_t.
Added fluid_cond_t thread synchronization stuff.
Added fluid_cond_mutex_t which is a dynamically allocated regular mutex for use with fluid_cond_t.
fluid_settings_t and fluid_synth_t are now using fluid_rec_mutex_t (same as before, just name change).
Added platform specific fluid_thread_self_set_prio() functions to fluid_sys.c for activating high priority for the calling thread.
Modified new_fluid_thread() to take a prio and prio_level parameters.
Added missing fluid_atomic_pointer_set().
fluid_voice_write() changed to only take a voice audio buffer to render to, mixing is done separately with new fluid_voice_mix().
fluid_voice_write() now returns the count of samples rendered.
fluid_voice_effects() split into fluid_voice_filter() and fluid_voice_mix().
Added dsp_buf_count field to fluid_voice_t to keep track of last count of samples rendered to dsp_buf.
fluid_voice_get_channel() converted to a macro.
Added FLUID_DEFAULT_AUDIO_RT_PRIO and FLUID_DEFAULT_MIDI_RT_PRIO in fluidsynth_priv.h, set to 90 and 80 respectively which get used for audio.realtime-prio and midi.realtime-prio (was 90 and 90 before).
2009-09-29 21:40:28 +00:00
|
|
|
priority.sched_priority = prio_level;
|
|
|
|
|
2010-06-11 08:23:36 +00:00
|
|
|
if (pthread_setschedparam (pthread_self (), SCHED_FIFO, &priority) == 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
#ifdef DBUS_SUPPORT
|
|
|
|
/* Try to gain high priority via rtkit */
|
|
|
|
|
|
|
|
if (fluid_rtkit_make_realtime(0, prio_level) == 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
FLUID_LOG(FLUID_WARN, "Failed to set thread to high priority");
|
Added public API functions: fluid_synth_sysex, fluid_synth_activate_key_tuning, fluid_synth_activate_octave_tuning, fluid_synth_tune_notes, fluid_synth_activate_tuning and fluid_synth_deactivate_tuning.
Added audio.realtime, audio.realtime-prio, midi.realtime and midi.realtime-prio (only ALSA updated to use them at this point).
Fixed bug in new_fluid_channel() where tuning field was not initialized.
fluid_settings.h had wrong field order in prototypes for fluid_settings_register_num and fluid_settings_register_int.
Added multi core support: "synth.cpu-cores" setting, fluid_synth_core_thread_func() and many core_ variables to fluid_synth_t.
Switched fluid_mutex_t back to a normal non-recursive mutex and added fluid_rec_mutex_t.
Added fluid_cond_t thread synchronization stuff.
Added fluid_cond_mutex_t which is a dynamically allocated regular mutex for use with fluid_cond_t.
fluid_settings_t and fluid_synth_t are now using fluid_rec_mutex_t (same as before, just name change).
Added platform specific fluid_thread_self_set_prio() functions to fluid_sys.c for activating high priority for the calling thread.
Modified new_fluid_thread() to take a prio and prio_level parameters.
Added missing fluid_atomic_pointer_set().
fluid_voice_write() changed to only take a voice audio buffer to render to, mixing is done separately with new fluid_voice_mix().
fluid_voice_write() now returns the count of samples rendered.
fluid_voice_effects() split into fluid_voice_filter() and fluid_voice_mix().
Added dsp_buf_count field to fluid_voice_t to keep track of last count of samples rendered to dsp_buf.
fluid_voice_get_channel() converted to a macro.
Added FLUID_DEFAULT_AUDIO_RT_PRIO and FLUID_DEFAULT_MIDI_RT_PRIO in fluidsynth_priv.h, set to 90 and 80 respectively which get used for audio.realtime-prio and midi.realtime-prio (was 90 and 90 before).
2009-09-29 21:40:28 +00:00
|
|
|
}
|
|
|
|
}
|
2003-03-11 16:56:45 +00:00
|
|
|
|
2007-11-11 20:52:56 +00:00
|
|
|
#ifdef FPE_CHECK
|
2003-03-11 16:56:45 +00:00
|
|
|
|
|
|
|
/***************************************************************
|
|
|
|
*
|
|
|
|
* Floating point exceptions
|
|
|
|
*
|
|
|
|
* The floating point exception functions were taken from Ircam's
|
|
|
|
* jMax source code. http://www.ircam.fr/jmax
|
|
|
|
*
|
2007-03-04 16:45:05 +00:00
|
|
|
* FIXME: check in config for i386 machine
|
2003-03-11 16:56:45 +00:00
|
|
|
*
|
|
|
|
* Currently not used. I leave the code here in case we want to pick
|
|
|
|
* this up again some time later.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* Exception flags */
|
|
|
|
#define _FPU_STATUS_IE 0x001 /* Invalid Operation */
|
|
|
|
#define _FPU_STATUS_DE 0x002 /* Denormalized Operand */
|
|
|
|
#define _FPU_STATUS_ZE 0x004 /* Zero Divide */
|
|
|
|
#define _FPU_STATUS_OE 0x008 /* Overflow */
|
|
|
|
#define _FPU_STATUS_UE 0x010 /* Underflow */
|
|
|
|
#define _FPU_STATUS_PE 0x020 /* Precision */
|
|
|
|
#define _FPU_STATUS_SF 0x040 /* Stack Fault */
|
|
|
|
#define _FPU_STATUS_ES 0x080 /* Error Summary Status */
|
|
|
|
|
|
|
|
/* Macros for accessing the FPU status word. */
|
|
|
|
|
|
|
|
/* get the FPU status */
|
|
|
|
#define _FPU_GET_SW(sw) __asm__ ("fnstsw %0" : "=m" (*&sw))
|
|
|
|
|
|
|
|
/* clear the FPU status */
|
|
|
|
#define _FPU_CLR_SW() __asm__ ("fnclex" : : )
|
|
|
|
|
|
|
|
/* Purpose:
|
2007-11-11 20:52:56 +00:00
|
|
|
* Checks, if the floating point unit has produced an exception, print a message
|
|
|
|
* if so and clear the exception.
|
2003-03-11 16:56:45 +00:00
|
|
|
*/
|
|
|
|
unsigned int fluid_check_fpe_i386(char* explanation)
|
|
|
|
{
|
|
|
|
unsigned int s;
|
|
|
|
|
|
|
|
_FPU_GET_SW(s);
|
|
|
|
_FPU_CLR_SW();
|
|
|
|
|
2007-11-11 20:52:56 +00:00
|
|
|
s &= _FPU_STATUS_IE | _FPU_STATUS_DE | _FPU_STATUS_ZE | _FPU_STATUS_OE | _FPU_STATUS_UE;
|
|
|
|
|
|
|
|
if (s)
|
|
|
|
{
|
2003-03-11 16:56:45 +00:00
|
|
|
FLUID_LOG(FLUID_WARN, "FPE exception (before or in %s): %s%s%s%s%s", explanation,
|
|
|
|
(s & _FPU_STATUS_IE) ? "Invalid operation " : "",
|
|
|
|
(s & _FPU_STATUS_DE) ? "Denormal number " : "",
|
|
|
|
(s & _FPU_STATUS_ZE) ? "Zero divide " : "",
|
|
|
|
(s & _FPU_STATUS_OE) ? "Overflow " : "",
|
|
|
|
(s & _FPU_STATUS_UE) ? "Underflow " : "");
|
|
|
|
}
|
2007-03-04 16:45:05 +00:00
|
|
|
|
2003-03-11 16:56:45 +00:00
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2007-11-11 20:52:56 +00:00
|
|
|
/* Purpose:
|
|
|
|
* Clear floating point exception.
|
|
|
|
*/
|
|
|
|
void fluid_clear_fpe_i386 (void)
|
|
|
|
{
|
|
|
|
_FPU_CLR_SW();
|
|
|
|
}
|
2003-03-11 16:56:45 +00:00
|
|
|
|
2007-11-11 20:52:56 +00:00
|
|
|
#endif // ifdef FPE_CHECK
|
2003-03-11 16:56:45 +00:00
|
|
|
|
2007-11-11 20:52:56 +00:00
|
|
|
|
|
|
|
#endif // #else (its POSIX)
|
2003-03-11 16:56:45 +00:00
|
|
|
|
|
|
|
|
|
|
|
/***************************************************************
|
|
|
|
*
|
|
|
|
* Profiling (Linux, i586 only)
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#if WITH_PROFILING
|
|
|
|
|
2007-03-04 16:45:05 +00:00
|
|
|
fluid_profile_data_t fluid_profile_data[] =
|
2003-03-11 16:56:45 +00:00
|
|
|
{
|
2010-07-04 09:05:32 +00:00
|
|
|
{ FLUID_PROF_WRITE, "fluid_synth_write_* ", 1e10, 0.0, 0.0, 0},
|
2003-03-11 16:56:45 +00:00
|
|
|
{ FLUID_PROF_ONE_BLOCK, "fluid_synth_one_block ", 1e10, 0.0, 0.0, 0},
|
|
|
|
{ FLUID_PROF_ONE_BLOCK_CLEAR, "fluid_synth_one_block:clear ", 1e10, 0.0, 0.0, 0},
|
|
|
|
{ FLUID_PROF_ONE_BLOCK_VOICE, "fluid_synth_one_block:one voice ", 1e10, 0.0, 0.0, 0},
|
|
|
|
{ FLUID_PROF_ONE_BLOCK_VOICES, "fluid_synth_one_block:all voices", 1e10, 0.0, 0.0, 0},
|
|
|
|
{ FLUID_PROF_ONE_BLOCK_REVERB, "fluid_synth_one_block:reverb ", 1e10, 0.0, 0.0, 0},
|
|
|
|
{ FLUID_PROF_ONE_BLOCK_CHORUS, "fluid_synth_one_block:chorus ", 1e10, 0.0, 0.0, 0},
|
|
|
|
{ FLUID_PROF_VOICE_NOTE, "fluid_voice:note ", 1e10, 0.0, 0.0, 0},
|
|
|
|
{ FLUID_PROF_VOICE_RELEASE, "fluid_voice:release ", 1e10, 0.0, 0.0, 0},
|
2007-03-04 16:45:05 +00:00
|
|
|
{ FLUID_PROF_LAST, "last", 1e100, 0.0, 0.0, 0}
|
2003-03-11 16:56:45 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
void fluid_profiling_print(void)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
printf("fluid_profiling_print\n");
|
2007-03-04 16:45:05 +00:00
|
|
|
|
|
|
|
FLUID_LOG(FLUID_INFO, "Estimated times: min/avg/max (micro seconds)");
|
2003-03-11 16:56:45 +00:00
|
|
|
|
|
|
|
for (i = 0; i < FLUID_PROF_LAST; i++) {
|
|
|
|
if (fluid_profile_data[i].count > 0) {
|
2007-03-04 16:45:05 +00:00
|
|
|
FLUID_LOG(FLUID_INFO, "%s: %.3f/%.3f/%.3f",
|
|
|
|
fluid_profile_data[i].description,
|
|
|
|
fluid_profile_data[i].min,
|
|
|
|
fluid_profile_data[i].total / fluid_profile_data[i].count,
|
2003-03-11 16:56:45 +00:00
|
|
|
fluid_profile_data[i].max);
|
|
|
|
} else {
|
2006-02-19 10:01:58 +00:00
|
|
|
FLUID_LOG(FLUID_DBG, "%s: no profiling available", fluid_profile_data[i].description);
|
2003-03-11 16:56:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#endif /* WITH_PROFILING */
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/***************************************************************
|
|
|
|
*
|
|
|
|
* Threads
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2009-10-28 19:38:10 +00:00
|
|
|
/* Rather than inline this one, we just declare it as a function, to prevent
|
|
|
|
* GCC warning about inline failure. */
|
|
|
|
fluid_cond_t *
|
|
|
|
new_fluid_cond (void)
|
|
|
|
{
|
|
|
|
if (!g_thread_supported ()) g_thread_init (NULL);
|
|
|
|
return g_cond_new ();
|
|
|
|
}
|
|
|
|
|
Added public API functions: fluid_synth_sysex, fluid_synth_activate_key_tuning, fluid_synth_activate_octave_tuning, fluid_synth_tune_notes, fluid_synth_activate_tuning and fluid_synth_deactivate_tuning.
Added audio.realtime, audio.realtime-prio, midi.realtime and midi.realtime-prio (only ALSA updated to use them at this point).
Fixed bug in new_fluid_channel() where tuning field was not initialized.
fluid_settings.h had wrong field order in prototypes for fluid_settings_register_num and fluid_settings_register_int.
Added multi core support: "synth.cpu-cores" setting, fluid_synth_core_thread_func() and many core_ variables to fluid_synth_t.
Switched fluid_mutex_t back to a normal non-recursive mutex and added fluid_rec_mutex_t.
Added fluid_cond_t thread synchronization stuff.
Added fluid_cond_mutex_t which is a dynamically allocated regular mutex for use with fluid_cond_t.
fluid_settings_t and fluid_synth_t are now using fluid_rec_mutex_t (same as before, just name change).
Added platform specific fluid_thread_self_set_prio() functions to fluid_sys.c for activating high priority for the calling thread.
Modified new_fluid_thread() to take a prio and prio_level parameters.
Added missing fluid_atomic_pointer_set().
fluid_voice_write() changed to only take a voice audio buffer to render to, mixing is done separately with new fluid_voice_mix().
fluid_voice_write() now returns the count of samples rendered.
fluid_voice_effects() split into fluid_voice_filter() and fluid_voice_mix().
Added dsp_buf_count field to fluid_voice_t to keep track of last count of samples rendered to dsp_buf.
fluid_voice_get_channel() converted to a macro.
Added FLUID_DEFAULT_AUDIO_RT_PRIO and FLUID_DEFAULT_MIDI_RT_PRIO in fluidsynth_priv.h, set to 90 and 80 respectively which get used for audio.realtime-prio and midi.realtime-prio (was 90 and 90 before).
2009-09-29 21:40:28 +00:00
|
|
|
static gpointer
|
|
|
|
fluid_thread_high_prio (gpointer data)
|
|
|
|
{
|
|
|
|
fluid_thread_info_t *info = data;
|
|
|
|
|
2009-10-30 21:49:54 +00:00
|
|
|
fluid_thread_self_set_prio (info->prio_level);
|
2003-03-11 16:56:45 +00:00
|
|
|
|
Added public API functions: fluid_synth_sysex, fluid_synth_activate_key_tuning, fluid_synth_activate_octave_tuning, fluid_synth_tune_notes, fluid_synth_activate_tuning and fluid_synth_deactivate_tuning.
Added audio.realtime, audio.realtime-prio, midi.realtime and midi.realtime-prio (only ALSA updated to use them at this point).
Fixed bug in new_fluid_channel() where tuning field was not initialized.
fluid_settings.h had wrong field order in prototypes for fluid_settings_register_num and fluid_settings_register_int.
Added multi core support: "synth.cpu-cores" setting, fluid_synth_core_thread_func() and many core_ variables to fluid_synth_t.
Switched fluid_mutex_t back to a normal non-recursive mutex and added fluid_rec_mutex_t.
Added fluid_cond_t thread synchronization stuff.
Added fluid_cond_mutex_t which is a dynamically allocated regular mutex for use with fluid_cond_t.
fluid_settings_t and fluid_synth_t are now using fluid_rec_mutex_t (same as before, just name change).
Added platform specific fluid_thread_self_set_prio() functions to fluid_sys.c for activating high priority for the calling thread.
Modified new_fluid_thread() to take a prio and prio_level parameters.
Added missing fluid_atomic_pointer_set().
fluid_voice_write() changed to only take a voice audio buffer to render to, mixing is done separately with new fluid_voice_mix().
fluid_voice_write() now returns the count of samples rendered.
fluid_voice_effects() split into fluid_voice_filter() and fluid_voice_mix().
Added dsp_buf_count field to fluid_voice_t to keep track of last count of samples rendered to dsp_buf.
fluid_voice_get_channel() converted to a macro.
Added FLUID_DEFAULT_AUDIO_RT_PRIO and FLUID_DEFAULT_MIDI_RT_PRIO in fluidsynth_priv.h, set to 90 and 80 respectively which get used for audio.realtime-prio and midi.realtime-prio (was 90 and 90 before).
2009-09-29 21:40:28 +00:00
|
|
|
info->func (info->data);
|
|
|
|
FLUID_FREE (info);
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a new thread.
|
|
|
|
* @param func Function to execute in new thread context
|
|
|
|
* @param data User defined data to pass to func
|
2009-10-30 21:49:54 +00:00
|
|
|
* @param prio_level Priority level. If greater than 0 then high priority scheduling will
|
|
|
|
* be used, with the given priority level (used by pthreads only). 0 uses normal scheduling.
|
Added public API functions: fluid_synth_sysex, fluid_synth_activate_key_tuning, fluid_synth_activate_octave_tuning, fluid_synth_tune_notes, fluid_synth_activate_tuning and fluid_synth_deactivate_tuning.
Added audio.realtime, audio.realtime-prio, midi.realtime and midi.realtime-prio (only ALSA updated to use them at this point).
Fixed bug in new_fluid_channel() where tuning field was not initialized.
fluid_settings.h had wrong field order in prototypes for fluid_settings_register_num and fluid_settings_register_int.
Added multi core support: "synth.cpu-cores" setting, fluid_synth_core_thread_func() and many core_ variables to fluid_synth_t.
Switched fluid_mutex_t back to a normal non-recursive mutex and added fluid_rec_mutex_t.
Added fluid_cond_t thread synchronization stuff.
Added fluid_cond_mutex_t which is a dynamically allocated regular mutex for use with fluid_cond_t.
fluid_settings_t and fluid_synth_t are now using fluid_rec_mutex_t (same as before, just name change).
Added platform specific fluid_thread_self_set_prio() functions to fluid_sys.c for activating high priority for the calling thread.
Modified new_fluid_thread() to take a prio and prio_level parameters.
Added missing fluid_atomic_pointer_set().
fluid_voice_write() changed to only take a voice audio buffer to render to, mixing is done separately with new fluid_voice_mix().
fluid_voice_write() now returns the count of samples rendered.
fluid_voice_effects() split into fluid_voice_filter() and fluid_voice_mix().
Added dsp_buf_count field to fluid_voice_t to keep track of last count of samples rendered to dsp_buf.
fluid_voice_get_channel() converted to a macro.
Added FLUID_DEFAULT_AUDIO_RT_PRIO and FLUID_DEFAULT_MIDI_RT_PRIO in fluidsynth_priv.h, set to 90 and 80 respectively which get used for audio.realtime-prio and midi.realtime-prio (was 90 and 90 before).
2009-09-29 21:40:28 +00:00
|
|
|
* @param detach If TRUE, 'join' does not work and the thread destroys itself when finished.
|
|
|
|
* @return New thread pointer or NULL on error
|
|
|
|
*/
|
2009-04-27 19:13:49 +00:00
|
|
|
fluid_thread_t *
|
2009-10-30 21:49:54 +00:00
|
|
|
new_fluid_thread (fluid_thread_func_t func, void *data, int prio_level, int detach)
|
2003-03-11 16:56:45 +00:00
|
|
|
{
|
2009-04-27 19:13:49 +00:00
|
|
|
GThread *thread;
|
Added public API functions: fluid_synth_sysex, fluid_synth_activate_key_tuning, fluid_synth_activate_octave_tuning, fluid_synth_tune_notes, fluid_synth_activate_tuning and fluid_synth_deactivate_tuning.
Added audio.realtime, audio.realtime-prio, midi.realtime and midi.realtime-prio (only ALSA updated to use them at this point).
Fixed bug in new_fluid_channel() where tuning field was not initialized.
fluid_settings.h had wrong field order in prototypes for fluid_settings_register_num and fluid_settings_register_int.
Added multi core support: "synth.cpu-cores" setting, fluid_synth_core_thread_func() and many core_ variables to fluid_synth_t.
Switched fluid_mutex_t back to a normal non-recursive mutex and added fluid_rec_mutex_t.
Added fluid_cond_t thread synchronization stuff.
Added fluid_cond_mutex_t which is a dynamically allocated regular mutex for use with fluid_cond_t.
fluid_settings_t and fluid_synth_t are now using fluid_rec_mutex_t (same as before, just name change).
Added platform specific fluid_thread_self_set_prio() functions to fluid_sys.c for activating high priority for the calling thread.
Modified new_fluid_thread() to take a prio and prio_level parameters.
Added missing fluid_atomic_pointer_set().
fluid_voice_write() changed to only take a voice audio buffer to render to, mixing is done separately with new fluid_voice_mix().
fluid_voice_write() now returns the count of samples rendered.
fluid_voice_effects() split into fluid_voice_filter() and fluid_voice_mix().
Added dsp_buf_count field to fluid_voice_t to keep track of last count of samples rendered to dsp_buf.
fluid_voice_get_channel() converted to a macro.
Added FLUID_DEFAULT_AUDIO_RT_PRIO and FLUID_DEFAULT_MIDI_RT_PRIO in fluidsynth_priv.h, set to 90 and 80 respectively which get used for audio.realtime-prio and midi.realtime-prio (was 90 and 90 before).
2009-09-29 21:40:28 +00:00
|
|
|
fluid_thread_info_t *info;
|
2009-04-27 19:13:49 +00:00
|
|
|
GError *err = NULL;
|
2003-03-11 16:56:45 +00:00
|
|
|
|
2009-04-27 19:13:49 +00:00
|
|
|
g_return_val_if_fail (func != NULL, NULL);
|
2003-03-11 16:56:45 +00:00
|
|
|
|
2009-10-23 06:52:29 +00:00
|
|
|
/* Make sure g_thread_init has been called.
|
|
|
|
* FIXME - Probably not a good idea in a shared library,
|
|
|
|
* but what can we do *and* remain backwards compatible? */
|
|
|
|
if (!g_thread_supported ()) g_thread_init (NULL);
|
|
|
|
|
2009-10-30 21:49:54 +00:00
|
|
|
if (prio_level > 0)
|
Added public API functions: fluid_synth_sysex, fluid_synth_activate_key_tuning, fluid_synth_activate_octave_tuning, fluid_synth_tune_notes, fluid_synth_activate_tuning and fluid_synth_deactivate_tuning.
Added audio.realtime, audio.realtime-prio, midi.realtime and midi.realtime-prio (only ALSA updated to use them at this point).
Fixed bug in new_fluid_channel() where tuning field was not initialized.
fluid_settings.h had wrong field order in prototypes for fluid_settings_register_num and fluid_settings_register_int.
Added multi core support: "synth.cpu-cores" setting, fluid_synth_core_thread_func() and many core_ variables to fluid_synth_t.
Switched fluid_mutex_t back to a normal non-recursive mutex and added fluid_rec_mutex_t.
Added fluid_cond_t thread synchronization stuff.
Added fluid_cond_mutex_t which is a dynamically allocated regular mutex for use with fluid_cond_t.
fluid_settings_t and fluid_synth_t are now using fluid_rec_mutex_t (same as before, just name change).
Added platform specific fluid_thread_self_set_prio() functions to fluid_sys.c for activating high priority for the calling thread.
Modified new_fluid_thread() to take a prio and prio_level parameters.
Added missing fluid_atomic_pointer_set().
fluid_voice_write() changed to only take a voice audio buffer to render to, mixing is done separately with new fluid_voice_mix().
fluid_voice_write() now returns the count of samples rendered.
fluid_voice_effects() split into fluid_voice_filter() and fluid_voice_mix().
Added dsp_buf_count field to fluid_voice_t to keep track of last count of samples rendered to dsp_buf.
fluid_voice_get_channel() converted to a macro.
Added FLUID_DEFAULT_AUDIO_RT_PRIO and FLUID_DEFAULT_MIDI_RT_PRIO in fluidsynth_priv.h, set to 90 and 80 respectively which get used for audio.realtime-prio and midi.realtime-prio (was 90 and 90 before).
2009-09-29 21:40:28 +00:00
|
|
|
{
|
|
|
|
info = FLUID_NEW (fluid_thread_info_t);
|
|
|
|
|
|
|
|
if (!info)
|
|
|
|
{
|
|
|
|
FLUID_LOG(FLUID_ERR, "Out of memory");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
info->func = func;
|
|
|
|
info->data = data;
|
|
|
|
info->prio_level = prio_level;
|
|
|
|
thread = g_thread_create (fluid_thread_high_prio, info, detach == FALSE, &err);
|
|
|
|
}
|
|
|
|
else thread = g_thread_create ((GThreadFunc)func, data, detach == FALSE, &err);
|
2003-03-11 16:56:45 +00:00
|
|
|
|
2009-04-27 19:13:49 +00:00
|
|
|
if (!thread)
|
|
|
|
{
|
|
|
|
FLUID_LOG(FLUID_ERR, "Failed to create the thread: %s",
|
|
|
|
fluid_gerror_message (err));
|
|
|
|
g_clear_error (&err);
|
2003-03-11 16:56:45 +00:00
|
|
|
}
|
2007-03-04 16:45:05 +00:00
|
|
|
|
2003-03-11 16:56:45 +00:00
|
|
|
return thread;
|
|
|
|
}
|
|
|
|
|
Added public API functions: fluid_synth_sysex, fluid_synth_activate_key_tuning, fluid_synth_activate_octave_tuning, fluid_synth_tune_notes, fluid_synth_activate_tuning and fluid_synth_deactivate_tuning.
Added audio.realtime, audio.realtime-prio, midi.realtime and midi.realtime-prio (only ALSA updated to use them at this point).
Fixed bug in new_fluid_channel() where tuning field was not initialized.
fluid_settings.h had wrong field order in prototypes for fluid_settings_register_num and fluid_settings_register_int.
Added multi core support: "synth.cpu-cores" setting, fluid_synth_core_thread_func() and many core_ variables to fluid_synth_t.
Switched fluid_mutex_t back to a normal non-recursive mutex and added fluid_rec_mutex_t.
Added fluid_cond_t thread synchronization stuff.
Added fluid_cond_mutex_t which is a dynamically allocated regular mutex for use with fluid_cond_t.
fluid_settings_t and fluid_synth_t are now using fluid_rec_mutex_t (same as before, just name change).
Added platform specific fluid_thread_self_set_prio() functions to fluid_sys.c for activating high priority for the calling thread.
Modified new_fluid_thread() to take a prio and prio_level parameters.
Added missing fluid_atomic_pointer_set().
fluid_voice_write() changed to only take a voice audio buffer to render to, mixing is done separately with new fluid_voice_mix().
fluid_voice_write() now returns the count of samples rendered.
fluid_voice_effects() split into fluid_voice_filter() and fluid_voice_mix().
Added dsp_buf_count field to fluid_voice_t to keep track of last count of samples rendered to dsp_buf.
fluid_voice_get_channel() converted to a macro.
Added FLUID_DEFAULT_AUDIO_RT_PRIO and FLUID_DEFAULT_MIDI_RT_PRIO in fluidsynth_priv.h, set to 90 and 80 respectively which get used for audio.realtime-prio and midi.realtime-prio (was 90 and 90 before).
2009-09-29 21:40:28 +00:00
|
|
|
/**
|
|
|
|
* Frees data associated with a thread (does not actually stop thread).
|
|
|
|
* @param thread Thread to free
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
delete_fluid_thread(fluid_thread_t* thread)
|
2003-03-11 16:56:45 +00:00
|
|
|
{
|
Added public API functions: fluid_synth_sysex, fluid_synth_activate_key_tuning, fluid_synth_activate_octave_tuning, fluid_synth_tune_notes, fluid_synth_activate_tuning and fluid_synth_deactivate_tuning.
Added audio.realtime, audio.realtime-prio, midi.realtime and midi.realtime-prio (only ALSA updated to use them at this point).
Fixed bug in new_fluid_channel() where tuning field was not initialized.
fluid_settings.h had wrong field order in prototypes for fluid_settings_register_num and fluid_settings_register_int.
Added multi core support: "synth.cpu-cores" setting, fluid_synth_core_thread_func() and many core_ variables to fluid_synth_t.
Switched fluid_mutex_t back to a normal non-recursive mutex and added fluid_rec_mutex_t.
Added fluid_cond_t thread synchronization stuff.
Added fluid_cond_mutex_t which is a dynamically allocated regular mutex for use with fluid_cond_t.
fluid_settings_t and fluid_synth_t are now using fluid_rec_mutex_t (same as before, just name change).
Added platform specific fluid_thread_self_set_prio() functions to fluid_sys.c for activating high priority for the calling thread.
Modified new_fluid_thread() to take a prio and prio_level parameters.
Added missing fluid_atomic_pointer_set().
fluid_voice_write() changed to only take a voice audio buffer to render to, mixing is done separately with new fluid_voice_mix().
fluid_voice_write() now returns the count of samples rendered.
fluid_voice_effects() split into fluid_voice_filter() and fluid_voice_mix().
Added dsp_buf_count field to fluid_voice_t to keep track of last count of samples rendered to dsp_buf.
fluid_voice_get_channel() converted to a macro.
Added FLUID_DEFAULT_AUDIO_RT_PRIO and FLUID_DEFAULT_MIDI_RT_PRIO in fluidsynth_priv.h, set to 90 and 80 respectively which get used for audio.realtime-prio and midi.realtime-prio (was 90 and 90 before).
2009-09-29 21:40:28 +00:00
|
|
|
/* Threads free themselves when they quit, nothing to do */
|
2003-03-11 16:56:45 +00:00
|
|
|
}
|
|
|
|
|
Added public API functions: fluid_synth_sysex, fluid_synth_activate_key_tuning, fluid_synth_activate_octave_tuning, fluid_synth_tune_notes, fluid_synth_activate_tuning and fluid_synth_deactivate_tuning.
Added audio.realtime, audio.realtime-prio, midi.realtime and midi.realtime-prio (only ALSA updated to use them at this point).
Fixed bug in new_fluid_channel() where tuning field was not initialized.
fluid_settings.h had wrong field order in prototypes for fluid_settings_register_num and fluid_settings_register_int.
Added multi core support: "synth.cpu-cores" setting, fluid_synth_core_thread_func() and many core_ variables to fluid_synth_t.
Switched fluid_mutex_t back to a normal non-recursive mutex and added fluid_rec_mutex_t.
Added fluid_cond_t thread synchronization stuff.
Added fluid_cond_mutex_t which is a dynamically allocated regular mutex for use with fluid_cond_t.
fluid_settings_t and fluid_synth_t are now using fluid_rec_mutex_t (same as before, just name change).
Added platform specific fluid_thread_self_set_prio() functions to fluid_sys.c for activating high priority for the calling thread.
Modified new_fluid_thread() to take a prio and prio_level parameters.
Added missing fluid_atomic_pointer_set().
fluid_voice_write() changed to only take a voice audio buffer to render to, mixing is done separately with new fluid_voice_mix().
fluid_voice_write() now returns the count of samples rendered.
fluid_voice_effects() split into fluid_voice_filter() and fluid_voice_mix().
Added dsp_buf_count field to fluid_voice_t to keep track of last count of samples rendered to dsp_buf.
fluid_voice_get_channel() converted to a macro.
Added FLUID_DEFAULT_AUDIO_RT_PRIO and FLUID_DEFAULT_MIDI_RT_PRIO in fluidsynth_priv.h, set to 90 and 80 respectively which get used for audio.realtime-prio and midi.realtime-prio (was 90 and 90 before).
2009-09-29 21:40:28 +00:00
|
|
|
/**
|
|
|
|
* Join a thread (wait for it to terminate).
|
|
|
|
* @param thread Thread to join
|
|
|
|
* @return FLUID_OK
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
fluid_thread_join(fluid_thread_t* thread)
|
2003-03-11 16:56:45 +00:00
|
|
|
{
|
2009-04-27 19:13:49 +00:00
|
|
|
g_thread_join (thread);
|
|
|
|
|
|
|
|
return FLUID_OK;
|
2003-03-11 16:56:45 +00:00
|
|
|
}
|
|
|
|
|
2009-02-04 07:45:44 +00:00
|
|
|
|
2009-10-23 06:52:29 +00:00
|
|
|
void
|
|
|
|
fluid_timer_run (void *data)
|
|
|
|
{
|
|
|
|
fluid_timer_t *timer;
|
|
|
|
int count = 0;
|
|
|
|
int cont;
|
|
|
|
long start;
|
|
|
|
long delay;
|
|
|
|
|
|
|
|
timer = (fluid_timer_t *)data;
|
|
|
|
|
|
|
|
/* keep track of the start time for absolute positioning */
|
|
|
|
start = fluid_curtime ();
|
|
|
|
|
|
|
|
while (timer->cont)
|
|
|
|
{
|
|
|
|
cont = (*timer->callback)(timer->data, fluid_curtime() - start);
|
|
|
|
|
|
|
|
count++;
|
|
|
|
if (!cont) break;
|
|
|
|
|
|
|
|
/* to avoid incremental time errors, calculate the delay between
|
|
|
|
two callbacks bringing in the "absolute" time (count *
|
|
|
|
timer->msec) */
|
|
|
|
delay = (count * timer->msec) - (fluid_curtime() - start);
|
|
|
|
if (delay > 0) g_usleep (delay * 1000);
|
|
|
|
}
|
|
|
|
|
|
|
|
FLUID_LOG (FLUID_DBG, "Timer thread finished");
|
|
|
|
|
|
|
|
if (timer->auto_destroy)
|
|
|
|
FLUID_FREE (timer);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
fluid_timer_t*
|
|
|
|
new_fluid_timer (int msec, fluid_timer_callback_t callback, void* data,
|
|
|
|
int new_thread, int auto_destroy, int high_priority)
|
|
|
|
{
|
|
|
|
fluid_timer_t *timer;
|
|
|
|
|
|
|
|
timer = FLUID_NEW (fluid_timer_t);
|
|
|
|
|
|
|
|
if (timer == NULL)
|
|
|
|
{
|
|
|
|
FLUID_LOG (FLUID_ERR, "Out of memory");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
timer->msec = msec;
|
|
|
|
timer->callback = callback;
|
|
|
|
timer->data = data;
|
|
|
|
timer->cont = TRUE ;
|
|
|
|
timer->thread = NULL;
|
|
|
|
timer->auto_destroy = auto_destroy;
|
|
|
|
|
|
|
|
if (new_thread)
|
|
|
|
{
|
|
|
|
timer->thread = new_fluid_thread (fluid_timer_run, timer, high_priority
|
2009-10-30 21:49:54 +00:00
|
|
|
? FLUID_SYS_TIMER_HIGH_PRIO_LEVEL : 0, FALSE);
|
2009-10-23 06:52:29 +00:00
|
|
|
if (!timer->thread)
|
|
|
|
{
|
|
|
|
FLUID_FREE (timer);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else fluid_timer_run (timer); /* Run directly, instead of as a separate thread */
|
|
|
|
|
|
|
|
return timer;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
delete_fluid_timer (fluid_timer_t *timer)
|
|
|
|
{
|
2009-10-26 03:17:01 +00:00
|
|
|
int auto_destroy = timer->auto_destroy;
|
|
|
|
|
2009-10-23 06:52:29 +00:00
|
|
|
timer->cont = 0;
|
|
|
|
fluid_timer_join (timer);
|
2009-10-26 03:17:01 +00:00
|
|
|
|
|
|
|
/* Shouldn't access timer now if auto_destroy enabled, since it has been destroyed */
|
|
|
|
|
|
|
|
if (!auto_destroy) FLUID_FREE (timer);
|
2009-10-23 06:52:29 +00:00
|
|
|
|
|
|
|
return FLUID_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
fluid_timer_join (fluid_timer_t *timer)
|
|
|
|
{
|
2009-11-15 02:34:24 +00:00
|
|
|
int auto_destroy;
|
|
|
|
|
2009-10-23 06:52:29 +00:00
|
|
|
if (timer->thread)
|
|
|
|
{
|
2009-11-15 02:34:24 +00:00
|
|
|
auto_destroy = timer->auto_destroy;
|
2009-10-23 06:52:29 +00:00
|
|
|
fluid_thread_join (timer->thread);
|
2009-11-15 02:34:24 +00:00
|
|
|
|
|
|
|
if (!auto_destroy) timer->thread = NULL;
|
2009-10-23 06:52:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return FLUID_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-04-27 19:13:49 +00:00
|
|
|
/***************************************************************
|
|
|
|
*
|
|
|
|
* Sockets and I/O
|
|
|
|
*
|
|
|
|
*/
|
2009-02-04 07:45:44 +00:00
|
|
|
|
2009-10-28 19:38:10 +00:00
|
|
|
/**
|
|
|
|
* Get standard in stream handle.
|
|
|
|
* @return Standard in stream.
|
|
|
|
*/
|
2009-04-27 19:13:49 +00:00
|
|
|
fluid_istream_t
|
|
|
|
fluid_get_stdin (void)
|
2009-02-04 07:45:44 +00:00
|
|
|
{
|
2009-04-27 19:13:49 +00:00
|
|
|
return STDIN_FILENO;
|
2009-02-04 07:45:44 +00:00
|
|
|
}
|
|
|
|
|
2009-10-28 19:38:10 +00:00
|
|
|
/**
|
|
|
|
* Get standard output stream handle.
|
|
|
|
* @return Standard out stream.
|
|
|
|
*/
|
2009-04-27 19:13:49 +00:00
|
|
|
fluid_ostream_t
|
|
|
|
fluid_get_stdout (void)
|
|
|
|
{
|
|
|
|
return STDOUT_FILENO;
|
|
|
|
}
|
2009-02-04 07:45:44 +00:00
|
|
|
|
2009-04-27 19:13:49 +00:00
|
|
|
/**
|
|
|
|
* Read a line from an input stream.
|
|
|
|
* @return 0 if end-of-stream, -1 if error, non zero otherwise
|
|
|
|
*/
|
|
|
|
int
|
2009-10-12 19:18:06 +00:00
|
|
|
fluid_istream_readline (fluid_istream_t in, fluid_ostream_t out, char* prompt,
|
|
|
|
char* buf, int len)
|
2009-02-04 07:45:44 +00:00
|
|
|
{
|
2009-04-27 19:13:49 +00:00
|
|
|
#if WITH_READLINE
|
|
|
|
if (in == fluid_get_stdin ())
|
|
|
|
{
|
|
|
|
char *line;
|
2009-02-04 07:45:44 +00:00
|
|
|
|
2009-04-27 19:13:49 +00:00
|
|
|
line = readline (prompt);
|
2009-02-04 07:45:44 +00:00
|
|
|
|
2009-04-27 19:13:49 +00:00
|
|
|
if (line == NULL)
|
|
|
|
return -1;
|
2009-02-04 07:45:44 +00:00
|
|
|
|
2009-04-27 19:13:49 +00:00
|
|
|
snprintf(buf, len, "%s", line);
|
|
|
|
buf[len - 1] = 0;
|
2009-02-04 07:45:44 +00:00
|
|
|
|
2009-04-27 19:13:49 +00:00
|
|
|
free(line);
|
|
|
|
return 1;
|
2009-02-04 07:45:44 +00:00
|
|
|
}
|
2009-04-27 19:13:49 +00:00
|
|
|
else
|
|
|
|
#endif
|
2009-10-12 19:18:06 +00:00
|
|
|
{
|
|
|
|
fluid_ostream_printf (out, "%s", prompt);
|
|
|
|
return fluid_istream_gets (in, buf, len);
|
|
|
|
}
|
2009-02-04 07:45:44 +00:00
|
|
|
}
|
|
|
|
|
2009-04-27 19:13:49 +00:00
|
|
|
/**
|
|
|
|
* Reads a line from an input stream (socket).
|
|
|
|
* @param in The input socket
|
|
|
|
* @param buf Buffer to store data to
|
|
|
|
* @param len Maximum length to store to buf
|
|
|
|
* @return 1 if a line was read, 0 on end of stream, -1 on error
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
fluid_istream_gets (fluid_istream_t in, char* buf, int len)
|
2009-02-04 07:45:44 +00:00
|
|
|
{
|
2009-04-27 19:13:49 +00:00
|
|
|
char c;
|
|
|
|
int n;
|
2009-02-04 07:45:44 +00:00
|
|
|
|
2009-04-27 19:13:49 +00:00
|
|
|
buf[len - 1] = 0;
|
2009-02-04 07:45:44 +00:00
|
|
|
|
2009-04-27 19:13:49 +00:00
|
|
|
while (--len > 0)
|
|
|
|
{
|
2009-10-12 19:18:06 +00:00
|
|
|
#ifndef WIN32
|
2009-04-27 19:13:49 +00:00
|
|
|
n = read(in, &c, 1);
|
|
|
|
if (n == -1) return -1;
|
2009-10-12 19:18:06 +00:00
|
|
|
#else
|
|
|
|
/* Handle read differently depending on if its a socket or file descriptor */
|
|
|
|
if (!(in & WIN32_SOCKET_FLAG))
|
|
|
|
{
|
|
|
|
n = read(in, &c, 1);
|
|
|
|
if (n == -1) return -1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
n = recv(in & ~WIN32_SOCKET_FLAG, &c, 1, 0);
|
|
|
|
if (n == SOCKET_ERROR) return -1;
|
|
|
|
}
|
|
|
|
#endif
|
2003-03-11 16:56:45 +00:00
|
|
|
|
2009-04-27 19:13:49 +00:00
|
|
|
if (n == 0)
|
|
|
|
{
|
|
|
|
*buf++ = 0;
|
|
|
|
return 0;
|
|
|
|
}
|
2003-03-11 16:56:45 +00:00
|
|
|
|
2009-10-12 19:18:06 +00:00
|
|
|
if ((c == '\n'))
|
2009-04-27 19:13:49 +00:00
|
|
|
{
|
|
|
|
*buf++ = 0;
|
|
|
|
return 1;
|
|
|
|
}
|
2003-03-11 16:56:45 +00:00
|
|
|
|
2009-10-12 19:18:06 +00:00
|
|
|
/* Store all characters excluding CR */
|
|
|
|
if (c != '\r') *buf++ = c;
|
2003-03-11 16:56:45 +00:00
|
|
|
}
|
|
|
|
|
2009-04-27 19:13:49 +00:00
|
|
|
return -1;
|
2003-03-11 16:56:45 +00:00
|
|
|
}
|
|
|
|
|
2009-04-27 19:13:49 +00:00
|
|
|
/**
|
|
|
|
* Send a printf style string with arguments to an output stream (socket).
|
|
|
|
* @param out Output stream
|
|
|
|
* @param format printf style format string
|
|
|
|
* @param ... Arguments for the printf format string
|
|
|
|
* @return Number of bytes written or -1 on error
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
fluid_ostream_printf (fluid_ostream_t out, char* format, ...)
|
2003-03-11 16:56:45 +00:00
|
|
|
{
|
2009-04-27 19:13:49 +00:00
|
|
|
char buf[4096];
|
|
|
|
va_list args;
|
|
|
|
int len;
|
2003-03-11 16:56:45 +00:00
|
|
|
|
2009-04-27 19:13:49 +00:00
|
|
|
va_start (args, format);
|
|
|
|
len = vsnprintf (buf, 4095, format, args);
|
|
|
|
va_end (args);
|
2003-03-11 16:56:45 +00:00
|
|
|
|
2011-03-20 22:16:07 +00:00
|
|
|
if (len == 0)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (len < 0)
|
2009-04-27 19:13:49 +00:00
|
|
|
{
|
|
|
|
printf("fluid_ostream_printf: buffer overflow");
|
|
|
|
return -1;
|
2003-03-11 16:56:45 +00:00
|
|
|
}
|
|
|
|
|
2009-04-27 19:13:49 +00:00
|
|
|
buf[4095] = 0;
|
2003-03-11 16:56:45 +00:00
|
|
|
|
2009-04-27 19:13:49 +00:00
|
|
|
#ifndef WIN32
|
|
|
|
return write (out, buf, strlen (buf));
|
|
|
|
#else
|
|
|
|
{
|
|
|
|
int retval;
|
2003-03-11 16:56:45 +00:00
|
|
|
|
2009-10-12 19:18:06 +00:00
|
|
|
/* Handle write differently depending on if its a socket or file descriptor */
|
|
|
|
if (!(out & WIN32_SOCKET_FLAG))
|
|
|
|
return write(out, buf, strlen (buf));
|
|
|
|
|
|
|
|
/* Socket */
|
|
|
|
retval = send (out & ~WIN32_SOCKET_FLAG, buf, strlen (buf), 0);
|
2003-03-11 16:56:45 +00:00
|
|
|
|
2009-04-27 19:13:49 +00:00
|
|
|
return retval != SOCKET_ERROR ? retval : -1;
|
2003-03-11 16:56:45 +00:00
|
|
|
}
|
2009-04-27 19:13:49 +00:00
|
|
|
#endif
|
2003-03-11 16:56:45 +00:00
|
|
|
}
|
|
|
|
|
2009-04-27 19:13:49 +00:00
|
|
|
int fluid_server_socket_join(fluid_server_socket_t *server_socket)
|
2003-03-11 16:56:45 +00:00
|
|
|
{
|
2009-04-27 19:13:49 +00:00
|
|
|
return fluid_thread_join (server_socket->thread);
|
2003-03-11 16:56:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-04-27 19:13:49 +00:00
|
|
|
#ifndef WIN32 // Not win32?
|
|
|
|
|
2003-03-11 16:56:45 +00:00
|
|
|
#define SOCKET_ERROR -1
|
|
|
|
|
2009-10-12 19:18:06 +00:00
|
|
|
fluid_istream_t fluid_socket_get_istream (fluid_socket_t sock)
|
|
|
|
{
|
|
|
|
return sock;
|
|
|
|
}
|
|
|
|
|
|
|
|
fluid_ostream_t fluid_socket_get_ostream (fluid_socket_t sock)
|
|
|
|
{
|
|
|
|
return sock;
|
|
|
|
}
|
|
|
|
|
2003-03-11 16:56:45 +00:00
|
|
|
void fluid_socket_close(fluid_socket_t sock)
|
|
|
|
{
|
2009-04-27 19:13:49 +00:00
|
|
|
if (sock != INVALID_SOCKET)
|
|
|
|
close (sock);
|
2003-03-11 16:56:45 +00:00
|
|
|
}
|
|
|
|
|
2009-04-27 19:13:49 +00:00
|
|
|
static void
|
|
|
|
fluid_server_socket_run (void *data)
|
2003-03-11 16:56:45 +00:00
|
|
|
{
|
2009-04-27 19:13:49 +00:00
|
|
|
fluid_server_socket_t *server_socket = (fluid_server_socket_t *)data;
|
2003-03-11 16:56:45 +00:00
|
|
|
fluid_socket_t client_socket;
|
|
|
|
struct sockaddr_in addr;
|
2009-04-27 19:13:49 +00:00
|
|
|
socklen_t addrlen = sizeof (addr);
|
|
|
|
int retval;
|
2003-03-11 16:56:45 +00:00
|
|
|
|
2009-04-27 19:13:49 +00:00
|
|
|
FLUID_LOG (FLUID_DBG, "Server listening for connections");
|
2003-03-11 16:56:45 +00:00
|
|
|
|
2009-04-27 19:13:49 +00:00
|
|
|
while (server_socket->cont)
|
|
|
|
{
|
|
|
|
client_socket = accept (server_socket->socket, (struct sockaddr *)&addr, &addrlen);
|
2007-03-04 16:45:05 +00:00
|
|
|
|
2009-04-27 19:13:49 +00:00
|
|
|
FLUID_LOG (FLUID_DBG, "New client connection");
|
2003-03-11 16:56:45 +00:00
|
|
|
|
2009-04-27 19:13:49 +00:00
|
|
|
if (client_socket == INVALID_SOCKET)
|
|
|
|
{
|
|
|
|
if (server_socket->cont)
|
2003-03-11 16:56:45 +00:00
|
|
|
FLUID_LOG(FLUID_ERR, "Failed to accept connection");
|
2009-04-27 19:13:49 +00:00
|
|
|
|
2003-03-11 16:56:45 +00:00
|
|
|
server_socket->cont = 0;
|
|
|
|
return;
|
|
|
|
} else {
|
2009-04-27 19:13:49 +00:00
|
|
|
retval = server_socket->func (server_socket->data, client_socket,
|
|
|
|
inet_ntoa (addr.sin_addr)); // FIXME - inet_ntoa is not thread safe
|
|
|
|
|
|
|
|
if (retval != 0)
|
2003-03-11 16:56:45 +00:00
|
|
|
fluid_socket_close(client_socket);
|
2007-03-04 16:45:05 +00:00
|
|
|
}
|
2003-03-11 16:56:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
FLUID_LOG(FLUID_DBG, "Server closing");
|
|
|
|
}
|
|
|
|
|
2007-03-04 16:45:05 +00:00
|
|
|
fluid_server_socket_t*
|
2003-03-11 16:56:45 +00:00
|
|
|
new_fluid_server_socket(int port, fluid_server_func_t func, void* data)
|
|
|
|
{
|
|
|
|
fluid_server_socket_t* server_socket;
|
|
|
|
struct sockaddr_in addr;
|
|
|
|
fluid_socket_t sock;
|
|
|
|
|
2009-04-27 19:13:49 +00:00
|
|
|
g_return_val_if_fail (func != NULL, NULL);
|
2003-03-11 16:56:45 +00:00
|
|
|
|
|
|
|
sock = socket(AF_INET, SOCK_STREAM, 0);
|
|
|
|
if (sock == INVALID_SOCKET) {
|
|
|
|
FLUID_LOG(FLUID_ERR, "Failed to create server socket");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
FLUID_MEMSET((char *)&addr, 0, sizeof(struct sockaddr_in));
|
|
|
|
addr.sin_family = AF_INET;
|
|
|
|
addr.sin_addr.s_addr = htonl(INADDR_ANY);
|
|
|
|
addr.sin_port = htons(port);
|
|
|
|
|
|
|
|
if (bind(sock, (const struct sockaddr *) &addr, sizeof(struct sockaddr_in)) == SOCKET_ERROR) {
|
|
|
|
FLUID_LOG(FLUID_ERR, "Failed to bind server socket");
|
|
|
|
fluid_socket_close(sock);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (listen(sock, 10) == SOCKET_ERROR) {
|
|
|
|
FLUID_LOG(FLUID_ERR, "Failed listen on server socket");
|
|
|
|
fluid_socket_close(sock);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
server_socket = FLUID_NEW(fluid_server_socket_t);
|
|
|
|
if (server_socket == NULL) {
|
2007-03-04 16:45:05 +00:00
|
|
|
FLUID_LOG(FLUID_ERR, "Out of memory");
|
2003-03-11 16:56:45 +00:00
|
|
|
fluid_socket_close(sock);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
server_socket->socket = sock;
|
|
|
|
server_socket->func = func;
|
|
|
|
server_socket->data = data;
|
|
|
|
server_socket->cont = 1;
|
|
|
|
|
Added public API functions: fluid_synth_sysex, fluid_synth_activate_key_tuning, fluid_synth_activate_octave_tuning, fluid_synth_tune_notes, fluid_synth_activate_tuning and fluid_synth_deactivate_tuning.
Added audio.realtime, audio.realtime-prio, midi.realtime and midi.realtime-prio (only ALSA updated to use them at this point).
Fixed bug in new_fluid_channel() where tuning field was not initialized.
fluid_settings.h had wrong field order in prototypes for fluid_settings_register_num and fluid_settings_register_int.
Added multi core support: "synth.cpu-cores" setting, fluid_synth_core_thread_func() and many core_ variables to fluid_synth_t.
Switched fluid_mutex_t back to a normal non-recursive mutex and added fluid_rec_mutex_t.
Added fluid_cond_t thread synchronization stuff.
Added fluid_cond_mutex_t which is a dynamically allocated regular mutex for use with fluid_cond_t.
fluid_settings_t and fluid_synth_t are now using fluid_rec_mutex_t (same as before, just name change).
Added platform specific fluid_thread_self_set_prio() functions to fluid_sys.c for activating high priority for the calling thread.
Modified new_fluid_thread() to take a prio and prio_level parameters.
Added missing fluid_atomic_pointer_set().
fluid_voice_write() changed to only take a voice audio buffer to render to, mixing is done separately with new fluid_voice_mix().
fluid_voice_write() now returns the count of samples rendered.
fluid_voice_effects() split into fluid_voice_filter() and fluid_voice_mix().
Added dsp_buf_count field to fluid_voice_t to keep track of last count of samples rendered to dsp_buf.
fluid_voice_get_channel() converted to a macro.
Added FLUID_DEFAULT_AUDIO_RT_PRIO and FLUID_DEFAULT_MIDI_RT_PRIO in fluidsynth_priv.h, set to 90 and 80 respectively which get used for audio.realtime-prio and midi.realtime-prio (was 90 and 90 before).
2009-09-29 21:40:28 +00:00
|
|
|
server_socket->thread = new_fluid_thread(fluid_server_socket_run, server_socket,
|
2009-10-30 21:49:54 +00:00
|
|
|
0, FALSE);
|
2003-03-11 16:56:45 +00:00
|
|
|
if (server_socket->thread == NULL) {
|
|
|
|
FLUID_FREE(server_socket);
|
|
|
|
fluid_socket_close(sock);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return server_socket;
|
|
|
|
}
|
|
|
|
|
|
|
|
int delete_fluid_server_socket(fluid_server_socket_t* server_socket)
|
|
|
|
{
|
|
|
|
server_socket->cont = 0;
|
|
|
|
if (server_socket->socket != INVALID_SOCKET) {
|
|
|
|
fluid_socket_close(server_socket->socket);
|
|
|
|
}
|
|
|
|
if (server_socket->thread) {
|
|
|
|
delete_fluid_thread(server_socket->thread);
|
|
|
|
}
|
|
|
|
FLUID_FREE(server_socket);
|
|
|
|
return FLUID_OK;
|
|
|
|
}
|
|
|
|
|
2009-04-27 19:13:49 +00:00
|
|
|
|
|
|
|
#else // Win32 is "special"
|
|
|
|
|
|
|
|
|
|
|
|
#ifndef WIN32_LEAN_AND_MEAN
|
|
|
|
#define WIN32_LEAN_AND_MEAN
|
|
|
|
#endif
|
|
|
|
|
2009-10-12 19:18:06 +00:00
|
|
|
fluid_istream_t fluid_socket_get_istream (fluid_socket_t sock)
|
|
|
|
{
|
|
|
|
return sock | WIN32_SOCKET_FLAG;
|
|
|
|
}
|
|
|
|
|
|
|
|
fluid_ostream_t fluid_socket_get_ostream (fluid_socket_t sock)
|
|
|
|
{
|
|
|
|
return sock | WIN32_SOCKET_FLAG;
|
|
|
|
}
|
|
|
|
|
2009-04-27 19:13:49 +00:00
|
|
|
void fluid_socket_close (fluid_socket_t sock)
|
|
|
|
{
|
|
|
|
if (sock != INVALID_SOCKET)
|
|
|
|
closesocket (sock);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void fluid_server_socket_run (void *data)
|
|
|
|
{
|
|
|
|
fluid_server_socket_t *server_socket = (fluid_server_socket_t *)data;
|
|
|
|
fluid_socket_t client_socket;
|
|
|
|
struct sockaddr_in addr;
|
|
|
|
socklen_t addrlen = sizeof (addr);
|
|
|
|
int r;
|
|
|
|
|
|
|
|
FLUID_LOG(FLUID_DBG, "Server listening for connections");
|
|
|
|
|
|
|
|
while (server_socket->cont)
|
|
|
|
{
|
2009-10-12 19:18:06 +00:00
|
|
|
client_socket = accept (server_socket->socket, (struct sockaddr *)&addr, &addrlen);
|
2009-04-27 19:13:49 +00:00
|
|
|
|
|
|
|
FLUID_LOG (FLUID_DBG, "New client connection");
|
|
|
|
|
|
|
|
if (client_socket == INVALID_SOCKET)
|
|
|
|
{
|
|
|
|
if (server_socket->cont)
|
|
|
|
FLUID_LOG (FLUID_ERR, "Failed to accept connection: %ld", WSAGetLastError ());
|
|
|
|
|
|
|
|
server_socket->cont = 0;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
r = server_socket->func (server_socket->data, client_socket,
|
|
|
|
inet_ntoa (addr.sin_addr)); // FIXME - inet_ntoa is not thread safe
|
|
|
|
if (r != 0)
|
|
|
|
fluid_socket_close (client_socket);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
FLUID_LOG (FLUID_DBG, "Server closing");
|
|
|
|
}
|
|
|
|
|
|
|
|
fluid_server_socket_t*
|
|
|
|
new_fluid_server_socket(int port, fluid_server_func_t func, void* data)
|
|
|
|
{
|
|
|
|
fluid_server_socket_t* server_socket;
|
|
|
|
struct sockaddr_in addr;
|
|
|
|
fluid_socket_t sock;
|
|
|
|
WSADATA wsaData;
|
|
|
|
int retval;
|
|
|
|
|
|
|
|
g_return_val_if_fail (func != NULL, NULL);
|
|
|
|
|
|
|
|
// Win32 requires initialization of winsock
|
|
|
|
retval = WSAStartup (MAKEWORD (2,2), &wsaData);
|
|
|
|
|
|
|
|
if (retval != 0)
|
|
|
|
{
|
|
|
|
FLUID_LOG(FLUID_ERR, "Server socket creation error: WSAStartup failed: %d", retval);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2009-10-12 19:18:06 +00:00
|
|
|
sock = socket (AF_INET, SOCK_STREAM, 0);
|
2009-04-27 19:13:49 +00:00
|
|
|
|
|
|
|
if (sock == INVALID_SOCKET)
|
|
|
|
{
|
|
|
|
FLUID_LOG (FLUID_ERR, "Failed to create server socket: %ld", WSAGetLastError ());
|
|
|
|
WSACleanup ();
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2009-10-12 19:18:06 +00:00
|
|
|
addr.sin_family = AF_INET;
|
|
|
|
addr.sin_port = htons (port);
|
|
|
|
addr.sin_addr.s_addr = htonl (INADDR_ANY);
|
|
|
|
|
|
|
|
retval = bind (sock, (struct sockaddr *)&addr, sizeof (addr));
|
2009-04-27 19:13:49 +00:00
|
|
|
|
|
|
|
if (retval == SOCKET_ERROR)
|
|
|
|
{
|
|
|
|
FLUID_LOG (FLUID_ERR, "Failed to bind server socket: %ld", WSAGetLastError ());
|
|
|
|
fluid_socket_close (sock);
|
|
|
|
WSACleanup ();
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (listen (sock, SOMAXCONN) == SOCKET_ERROR)
|
|
|
|
{
|
|
|
|
FLUID_LOG (FLUID_ERR, "Failed to listen on server socket: %ld", WSAGetLastError ());
|
|
|
|
fluid_socket_close (sock);
|
|
|
|
WSACleanup ();
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
server_socket = FLUID_NEW (fluid_server_socket_t);
|
|
|
|
|
|
|
|
if (server_socket == NULL)
|
|
|
|
{
|
|
|
|
FLUID_LOG (FLUID_ERR, "Out of memory");
|
|
|
|
fluid_socket_close (sock);
|
|
|
|
WSACleanup ();
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
server_socket->socket = sock;
|
|
|
|
server_socket->func = func;
|
|
|
|
server_socket->data = data;
|
|
|
|
server_socket->cont = 1;
|
|
|
|
|
2009-10-12 19:18:06 +00:00
|
|
|
server_socket->thread = new_fluid_thread(fluid_server_socket_run, server_socket,
|
2009-10-30 21:49:54 +00:00
|
|
|
0, FALSE);
|
2009-04-27 19:13:49 +00:00
|
|
|
if (server_socket->thread == NULL)
|
|
|
|
{
|
|
|
|
FLUID_FREE (server_socket);
|
|
|
|
fluid_socket_close (sock);
|
|
|
|
WSACleanup ();
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return server_socket;
|
|
|
|
}
|
|
|
|
|
|
|
|
int delete_fluid_server_socket(fluid_server_socket_t *server_socket)
|
2003-03-11 16:56:45 +00:00
|
|
|
{
|
2009-04-27 19:13:49 +00:00
|
|
|
server_socket->cont = 0;
|
|
|
|
|
|
|
|
if (server_socket->socket != INVALID_SOCKET)
|
|
|
|
fluid_socket_close (server_socket->socket);
|
|
|
|
|
|
|
|
if (server_socket->thread)
|
|
|
|
delete_fluid_thread (server_socket->thread);
|
|
|
|
|
|
|
|
FLUID_FREE (server_socket);
|
|
|
|
|
|
|
|
WSACleanup (); // Should be called the same number of times as WSAStartup
|
|
|
|
|
|
|
|
return FLUID_OK;
|
2003-03-11 16:56:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|