Clang-format fluid_samplecache

This commit is contained in:
Marcus Weseloh 2018-04-04 11:03:47 +02:00
parent 4ec1cfe73e
commit a274a394e0
2 changed files with 241 additions and 201 deletions

View file

@ -22,15 +22,16 @@
*/ */
#include "fluid_samplecache.h" #include "fluid_samplecache.h"
#include "fluidsynth.h"
#include "fluid_sys.h" #include "fluid_sys.h"
#include "fluidsynth.h"
/*************************************************************** /***************************************************************
* *
* CACHED SAMPLEDATA LOADER * CACHED SAMPLEDATA LOADER
*/ */
typedef struct _fluid_cached_sampledata_t { typedef struct _fluid_cached_sampledata_t
{
struct _fluid_cached_sampledata_t *next; struct _fluid_cached_sampledata_t *next;
char *filename; char *filename;
@ -56,7 +57,8 @@ static int fluid_get_file_modification_time(char *filename, time_t *modification
#else #else
struct stat buf; struct stat buf;
if (stat(filename, &buf) == -1) { if (stat(filename, &buf) == -1)
{
return FLUID_FAILED; return FLUID_FAILED;
} }
@ -66,9 +68,14 @@ static int fluid_get_file_modification_time(char *filename, time_t *modification
} }
int fluid_cached_sampledata_load(char *filename, int fluid_cached_sampledata_load(char *filename,
unsigned int samplepos, unsigned int samplesize, short **sampledata, unsigned int samplepos,
unsigned int sample24pos, unsigned int sample24size, char **sample24data, unsigned int samplesize,
int try_mlock, const fluid_file_callbacks_t* fcbs) short **sampledata,
unsigned int sample24pos,
unsigned int sample24size,
char **sample24data,
int try_mlock,
const fluid_file_callbacks_t *fcbs)
{ {
fluid_file fd = NULL; fluid_file fd = NULL;
short *loaded_sampledata = NULL; short *loaded_sampledata = NULL;
@ -78,31 +85,39 @@ int fluid_cached_sampledata_load(char *filename,
fluid_mutex_lock(cached_sampledata_mutex); fluid_mutex_lock(cached_sampledata_mutex);
if (fluid_get_file_modification_time(filename, &modification_time) == FLUID_FAILED) { if (fluid_get_file_modification_time(filename, &modification_time) == FLUID_FAILED)
{
FLUID_LOG(FLUID_WARN, "Unable to read modificaton time of soundfont file."); FLUID_LOG(FLUID_WARN, "Unable to read modificaton time of soundfont file.");
modification_time = 0; modification_time = 0;
} }
for (cached_sampledata = all_cached_sampledata; cached_sampledata; cached_sampledata = cached_sampledata->next) { for (cached_sampledata = all_cached_sampledata; cached_sampledata;
cached_sampledata = cached_sampledata->next)
{
if (FLUID_STRCMP(filename, cached_sampledata->filename)) if (FLUID_STRCMP(filename, cached_sampledata->filename))
continue; continue;
if (cached_sampledata->modification_time != modification_time) if (cached_sampledata->modification_time != modification_time)
continue; continue;
if (cached_sampledata->samplesize != samplesize || cached_sampledata->sample24size != sample24size) { if (cached_sampledata->samplesize != samplesize || cached_sampledata->sample24size != sample24size)
FLUID_LOG(FLUID_ERR, "Cached size of soundfont doesn't match actual size of soundfont (cached: %u. actual: %u)", {
FLUID_LOG(FLUID_ERR, "Cached size of soundfont doesn't match actual size of soundfont "
"(cached: %u. actual: %u)",
cached_sampledata->samplesize, samplesize); cached_sampledata->samplesize, samplesize);
continue; continue;
} }
if (try_mlock && !cached_sampledata->mlock) { if (try_mlock && !cached_sampledata->mlock)
{
if (fluid_mlock(cached_sampledata->sampledata, samplesize) != 0) if (fluid_mlock(cached_sampledata->sampledata, samplesize) != 0)
FLUID_LOG(FLUID_WARN, "Failed to pin the sample data to RAM; swapping is possible."); FLUID_LOG(FLUID_WARN,
"Failed to pin the sample data to RAM; swapping is possible.");
else else
cached_sampledata->mlock = try_mlock; cached_sampledata->mlock = try_mlock;
if (cached_sampledata->sample24data != NULL) if (cached_sampledata->sample24data != NULL)
if (fluid_mlock(cached_sampledata->sample24data, sample24size) != 0) if (fluid_mlock(cached_sampledata->sample24data, sample24size) != 0)
FLUID_LOG(FLUID_WARN, "Failed to pin the sample24 data to RAM; swapping is possible."); FLUID_LOG(FLUID_WARN,
"Failed to pin the sample24 data to RAM; swapping is possible.");
} }
cached_sampledata->num_references++; cached_sampledata->num_references++;
@ -112,39 +127,46 @@ int fluid_cached_sampledata_load(char *filename,
} }
fd = fcbs->fopen(filename); fd = fcbs->fopen(filename);
if (fd == NULL) { if (fd == NULL)
{
FLUID_LOG(FLUID_ERR, "Can't open soundfont file"); FLUID_LOG(FLUID_ERR, "Can't open soundfont file");
goto error_exit; goto error_exit;
} }
if (fcbs->fseek(fd, samplepos, SEEK_SET) == FLUID_FAILED) { if (fcbs->fseek(fd, samplepos, SEEK_SET) == FLUID_FAILED)
{
perror("error"); perror("error");
FLUID_LOG(FLUID_ERR, "Failed to seek position in data file"); FLUID_LOG(FLUID_ERR, "Failed to seek position in data file");
goto error_exit; goto error_exit;
} }
loaded_sampledata = (short *)FLUID_MALLOC(samplesize); loaded_sampledata = (short *)FLUID_MALLOC(samplesize);
if (loaded_sampledata == NULL) { if (loaded_sampledata == NULL)
{
FLUID_LOG(FLUID_ERR, "Out of memory"); FLUID_LOG(FLUID_ERR, "Out of memory");
goto error_exit; goto error_exit;
} }
if (fcbs->fread(loaded_sampledata, samplesize, fd) == FLUID_FAILED) { if (fcbs->fread(loaded_sampledata, samplesize, fd) == FLUID_FAILED)
{
FLUID_LOG(FLUID_ERR, "Failed to read sample data"); FLUID_LOG(FLUID_ERR, "Failed to read sample data");
goto error_exit; goto error_exit;
} }
if (sample24pos > 0) if (sample24pos > 0)
{ {
if (fcbs->fseek(fd, sample24pos, SEEK_SET) == FLUID_FAILED) { if (fcbs->fseek(fd, sample24pos, SEEK_SET) == FLUID_FAILED)
{
perror("error"); perror("error");
FLUID_LOG(FLUID_ERR, "Failed to seek position in data file"); FLUID_LOG(FLUID_ERR, "Failed to seek position in data file");
goto error_exit; goto error_exit;
} }
loaded_sample24data = (char *)FLUID_MALLOC(sample24size); loaded_sample24data = (char *)FLUID_MALLOC(sample24size);
if (loaded_sample24data == NULL) { if (loaded_sample24data == NULL)
{
FLUID_LOG(FLUID_ERR, "Out of memory when allocating 24bit sample, ignoring"); FLUID_LOG(FLUID_ERR, "Out of memory when allocating 24bit sample, ignoring");
} }
else if (fcbs->fread(loaded_sample24data, sample24size, fd) == FLUID_FAILED) { else if (fcbs->fread(loaded_sample24data, sample24size, fd) == FLUID_FAILED)
{
FLUID_LOG(FLUID_ERR, "Failed to read sample24 data"); FLUID_LOG(FLUID_ERR, "Failed to read sample24 data");
FLUID_FREE(loaded_sample24data); FLUID_FREE(loaded_sample24data);
loaded_sample24data = NULL; loaded_sample24data = NULL;
@ -156,7 +178,8 @@ int fluid_cached_sampledata_load(char *filename,
cached_sampledata = (fluid_cached_sampledata_t *)FLUID_MALLOC(sizeof(fluid_cached_sampledata_t)); cached_sampledata = (fluid_cached_sampledata_t *)FLUID_MALLOC(sizeof(fluid_cached_sampledata_t));
if (cached_sampledata == NULL) { if (cached_sampledata == NULL)
{
FLUID_LOG(FLUID_ERR, "Out of memory."); FLUID_LOG(FLUID_ERR, "Out of memory.");
goto error_exit; goto error_exit;
} }
@ -164,7 +187,8 @@ int fluid_cached_sampledata_load(char *filename,
/* Lock the memory to disable paging. It's okay if this fails. It /* Lock the memory to disable paging. It's okay if this fails. It
probably means that the user doesn't have the required permission. */ probably means that the user doesn't have the required permission. */
cached_sampledata->mlock = 0; cached_sampledata->mlock = 0;
if (try_mlock) { if (try_mlock)
{
if (fluid_mlock(loaded_sampledata, samplesize) != 0) if (fluid_mlock(loaded_sampledata, samplesize) != 0)
FLUID_LOG(FLUID_WARN, "Failed to pin the sample data to RAM; swapping is possible."); FLUID_LOG(FLUID_WARN, "Failed to pin the sample data to RAM; swapping is possible.");
else else
@ -172,13 +196,15 @@ int fluid_cached_sampledata_load(char *filename,
} }
/* If this machine is big endian, the sample have to byte swapped */ /* If this machine is big endian, the sample have to byte swapped */
if (FLUID_IS_BIG_ENDIAN) { if (FLUID_IS_BIG_ENDIAN)
{
unsigned char *cbuf; unsigned char *cbuf;
unsigned char hi, lo; unsigned char hi, lo;
unsigned int i, j; unsigned int i, j;
short s; short s;
cbuf = (unsigned char *)loaded_sampledata; cbuf = (unsigned char *)loaded_sampledata;
for (i = 0, j = 0; j < samplesize; i++) { for (i = 0, j = 0; j < samplesize; i++)
{
lo = cbuf[j++]; lo = cbuf[j++];
hi = cbuf[j++]; hi = cbuf[j++];
s = (hi << 8) | lo; s = (hi << 8) | lo;
@ -187,7 +213,8 @@ int fluid_cached_sampledata_load(char *filename,
} }
cached_sampledata->filename = FLUID_STRDUP(filename); cached_sampledata->filename = FLUID_STRDUP(filename);
if (cached_sampledata->filename == NULL) { if (cached_sampledata->filename == NULL)
{
FLUID_LOG(FLUID_ERR, "Out of memory."); FLUID_LOG(FLUID_ERR, "Out of memory.");
goto error_exit; goto error_exit;
} }
@ -210,14 +237,16 @@ int fluid_cached_sampledata_load(char *filename,
return FLUID_OK; return FLUID_OK;
error_exit: error_exit:
if (fd != NULL) { if (fd != NULL)
{
fcbs->fclose(fd); fcbs->fclose(fd);
} }
FLUID_FREE(loaded_sampledata); FLUID_FREE(loaded_sampledata);
FLUID_FREE(loaded_sample24data); FLUID_FREE(loaded_sample24data);
if (cached_sampledata != NULL) { if (cached_sampledata != NULL)
{
FLUID_FREE(cached_sampledata->filename); FLUID_FREE(cached_sampledata->filename);
} }
FLUID_FREE(cached_sampledata); FLUID_FREE(cached_sampledata);
@ -236,12 +265,15 @@ int fluid_cached_sampledata_unload(const short *sampledata)
fluid_mutex_lock(cached_sampledata_mutex); fluid_mutex_lock(cached_sampledata_mutex);
cached_sampledata = all_cached_sampledata; cached_sampledata = all_cached_sampledata;
while (cached_sampledata != NULL) { while (cached_sampledata != NULL)
if (sampledata == cached_sampledata->sampledata) { {
if (sampledata == cached_sampledata->sampledata)
{
cached_sampledata->num_references--; cached_sampledata->num_references--;
if (cached_sampledata->num_references == 0) { if (cached_sampledata->num_references == 0)
{
if (cached_sampledata->mlock) if (cached_sampledata->mlock)
{ {
fluid_munlock(cached_sampledata->sampledata, cached_sampledata->samplesize); fluid_munlock(cached_sampledata->sampledata, cached_sampledata->samplesize);
@ -251,9 +283,12 @@ int fluid_cached_sampledata_unload(const short *sampledata)
FLUID_FREE(cached_sampledata->sample24data); FLUID_FREE(cached_sampledata->sample24data);
FLUID_FREE(cached_sampledata->filename); FLUID_FREE(cached_sampledata->filename);
if (prev != NULL) { if (prev != NULL)
{
prev->next = cached_sampledata->next; prev->next = cached_sampledata->next;
} else { }
else
{
all_cached_sampledata = cached_sampledata->next; all_cached_sampledata = cached_sampledata->next;
} }

View file

@ -25,9 +25,14 @@
#include "fluid_sfont.h" #include "fluid_sfont.h"
int fluid_cached_sampledata_load(char *filename, int fluid_cached_sampledata_load(char *filename,
unsigned int samplepos, unsigned int samplesize, short **sampledata, unsigned int samplepos,
unsigned int sample24pos, unsigned int sample24size, char **sample24data, unsigned int samplesize,
int try_mlock, const fluid_file_callbacks_t* fcbs); short **sampledata,
unsigned int sample24pos,
unsigned int sample24size,
char **sample24data,
int try_mlock,
const fluid_file_callbacks_t *fcbs);
int fluid_cached_sampledata_unload(const short *sampledata); int fluid_cached_sampledata_unload(const short *sampledata);