From bde3e4f1bb7b8f8abca0884a7d994ee1c17a66b1 Mon Sep 17 00:00:00 2001 From: sanine Date: Fri, 26 Aug 2022 12:42:30 -0500 Subject: refactor: move mossrose.h to include/ dir --- src/CMakeLists.txt | 7 +++++ src/channel.c | 85 ++++++++++++++++++++++++++++++++++++++++++++++++++ src/channel.h | 26 +++++++++++++++ src/mossrose-channel.c | 85 -------------------------------------------------- src/mossrose-channel.h | 26 --------------- src/mossrose-mutex.c | 58 ---------------------------------- src/mossrose-mutex.h | 28 ----------------- src/mossrose.c | 15 +++++---- src/mossrose.h | 12 ------- src/mutex.c | 58 ++++++++++++++++++++++++++++++++++ src/mutex.h | 28 +++++++++++++++++ 11 files changed, 213 insertions(+), 215 deletions(-) create mode 100644 src/CMakeLists.txt create mode 100644 src/channel.c create mode 100644 src/channel.h delete mode 100644 src/mossrose-channel.c delete mode 100644 src/mossrose-channel.h delete mode 100644 src/mossrose-mutex.c delete mode 100644 src/mossrose-mutex.h delete mode 100644 src/mossrose.h create mode 100644 src/mutex.c create mode 100644 src/mutex.h (limited to 'src') diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt new file mode 100644 index 0000000..069d00a --- /dev/null +++ b/src/CMakeLists.txt @@ -0,0 +1,7 @@ +project(mossrose) + +target_sources(mossrose PUBLIC + ${CMAKE_CURRENT_LIST_DIR}/mossrose.c + ${CMAKE_CURRENT_LIST_DIR}/mutex.c + ${CMAKE_CURRENT_LIST_DIR}/channel.c +) diff --git a/src/channel.c b/src/channel.c new file mode 100644 index 0000000..169b245 --- /dev/null +++ b/src/channel.c @@ -0,0 +1,85 @@ +#include +#include +#include "mutex.h" +#include "channel.h" + + +void mossrose_channel_init(struct mossrose_channel_t *chan) +{ + mossrose_mutex_init(&(chan->mutex)); + chan->left = NULL; + chan->right = NULL; + chan->n_samples = 0; + chan->pos = 0; +} + + +int mossrose_channel_set(struct mossrose_channel_t *chan, float *left, float *right, size_t len, int force) +{ + int result = 0; + mossrose_mutex_lock(&(chan->mutex)); + if (chan->n_samples != 0 && !force) { + /* channel is still playing! */ + result = 1; goto unlock; + } + + chan->n_samples = len; + chan->pos = 0; + + /* left channel */ + if (chan->left != NULL) free(chan->left); + if (left == NULL) + chan->left = NULL; + else { + chan->left = malloc(len * sizeof(float)); + if (chan->left == NULL) { result = 2; goto unlock; } + memcpy(chan->left, left, len * sizeof(float)); + } + + /* right channel */ + if (chan->right != NULL) free(chan->right); + if (right == NULL) + chan->right = NULL; + else { + chan->right = malloc(len * sizeof(float)); + if (chan->right == NULL) { result = 3; goto unlock; } + memcpy(chan->right, right, len * sizeof(float)); + } + + unlock: + mossrose_mutex_unlock(&(chan->mutex)); + return result; +} + + +void mossrose_channel_reset(struct mossrose_channel_t *chan) +{ + chan->n_samples = 0; + chan->pos = 0; +} + + +int mossrose_channel_advance(float *left, float *right, struct mossrose_channel_t *chan) +{ + if (chan->pos >= chan->n_samples) return 1; + if (chan->left != NULL) + *left = chan->left[chan->pos]; + else + *left = 0; + if (chan->right != NULL) + *right = chan->right[chan->pos]; + else + *right = 0; + chan->pos += 1; + return 0; +} + + +void mossrose_channel_destroy(struct mossrose_channel_t *chan) +{ + mossrose_mutex_destroy(&(chan->mutex)); + if (chan->left != NULL) free(chan->left); + if (chan->right != NULL) free(chan->right); +} + + diff --git a/src/channel.h b/src/channel.h new file mode 100644 index 0000000..23c11d6 --- /dev/null +++ b/src/channel.h @@ -0,0 +1,26 @@ +#ifndef MOSSROSE_CHANNEL_H +#define MOSSROSE_CHANNEL_H + +#include +#include "mutex.h" + +struct mossrose_channel_t { + mossrose_mutex_t mutex; + float *left; + float *right; + size_t n_samples; + size_t pos; +}; + + +void mossrose_channel_init(struct mossrose_channel_t *chan); + +int mossrose_channel_set(struct mossrose_channel_t *chan, float *left, float *right, size_t len, int force); + +void mossrose_channel_reset(struct mossrose_channel_t *chan); + +int mossrose_channel_advance(float *left, float *right, struct mossrose_channel_t *chan); + +void mossrose_channel_destroy(struct mossrose_channel_t *chan); + +#endif diff --git a/src/mossrose-channel.c b/src/mossrose-channel.c deleted file mode 100644 index 7232417..0000000 --- a/src/mossrose-channel.c +++ /dev/null @@ -1,85 +0,0 @@ -#include -#include -#include "mossrose-mutex.h" -#include "mossrose-channel.h" - - -void mossrose_channel_init(struct mossrose_channel_t *chan) -{ - mossrose_mutex_init(&(chan->mutex)); - chan->left = NULL; - chan->right = NULL; - chan->n_samples = 0; - chan->pos = 0; -} - - -int mossrose_channel_set(struct mossrose_channel_t *chan, float *left, float *right, size_t len, int force) -{ - int result = 0; - mossrose_mutex_lock(&(chan->mutex)); - if (chan->n_samples != 0 && !force) { - /* channel is still playing! */ - result = 1; goto unlock; - } - - chan->n_samples = len; - chan->pos = 0; - - /* left channel */ - if (chan->left != NULL) free(chan->left); - if (left == NULL) - chan->left = NULL; - else { - chan->left = malloc(len * sizeof(float)); - if (chan->left == NULL) { result = 2; goto unlock; } - memcpy(chan->left, left, len * sizeof(float)); - } - - /* right channel */ - if (chan->right != NULL) free(chan->right); - if (right == NULL) - chan->right = NULL; - else { - chan->right = malloc(len * sizeof(float)); - if (chan->right == NULL) { result = 3; goto unlock; } - memcpy(chan->right, right, len * sizeof(float)); - } - - unlock: - mossrose_mutex_unlock(&(chan->mutex)); - return result; -} - - -void mossrose_channel_reset(struct mossrose_channel_t *chan) -{ - chan->n_samples = 0; - chan->pos = 0; -} - - -int mossrose_channel_advance(float *left, float *right, struct mossrose_channel_t *chan) -{ - if (chan->pos >= chan->n_samples) return 1; - if (chan->left != NULL) - *left = chan->left[chan->pos]; - else - *left = 0; - if (chan->right != NULL) - *right = chan->right[chan->pos]; - else - *right = 0; - chan->pos += 1; - return 0; -} - - -void mossrose_channel_destroy(struct mossrose_channel_t *chan) -{ - mossrose_mutex_destroy(&(chan->mutex)); - if (chan->left != NULL) free(chan->left); - if (chan->right != NULL) free(chan->right); -} - - diff --git a/src/mossrose-channel.h b/src/mossrose-channel.h deleted file mode 100644 index e1db89d..0000000 --- a/src/mossrose-channel.h +++ /dev/null @@ -1,26 +0,0 @@ -#ifndef MOSSROSE_CHANNEL_H -#define MOSSROSE_CHANNEL_H - -#include -#include "mossrose-mutex.h" - -struct mossrose_channel_t { - mossrose_mutex_t mutex; - float *left; - float *right; - size_t n_samples; - size_t pos; -}; - - -void mossrose_channel_init(struct mossrose_channel_t *chan); - -int mossrose_channel_set(struct mossrose_channel_t *chan, float *left, float *right, size_t len, int force); - -void mossrose_channel_reset(struct mossrose_channel_t *chan); - -int mossrose_channel_advance(float *left, float *right, struct mossrose_channel_t *chan); - -void mossrose_channel_destroy(struct mossrose_channel_t *chan); - -#endif diff --git a/src/mossrose-mutex.c b/src/mossrose-mutex.c deleted file mode 100644 index b6865f8..0000000 --- a/src/mossrose-mutex.c +++ /dev/null @@ -1,58 +0,0 @@ -#include "mossrose-mutex.h" - -#ifdef WIN32 -#include -void mossrose_mutex_init(mossrose_mutex_t *mutex) -{ - *mutex = CreateMutex(NULL, false, NULL); -} - -void mossrose_mutex_lock(mossrose_mutex_t *mutex) -{ - WaitForSingleObject(*mutex, INFINITE); -} - -int mossrose_mutex_trylock(mossrose_mutex_t *mutex) -{ - int result = WaitForSingleObject(*mutex, 0); - return result != WAIT_OBJECT_0; -} - -void mossrose_mutex_unlock(mossrose_mutex_t *mutex) -{ - ReleaseMutex(*mutex); -} - -void mossrose_mutex_destroy(mossrose_mutex_t *mutex) -{ - ReleaseMutex(*mutex); -} - - -#else -#include -void mossrose_mutex_init(mossrose_mutex_t *mutex) -{ - pthread_mutex_init(mutex, NULL); -} - -void mossrose_mutex_lock(mossrose_mutex_t *mutex) -{ - pthread_mutex_lock(mutex); -} - -int mossrose_mutex_trylock(mossrose_mutex_t *mutex) -{ - return pthread_mutex_trylock(mutex); -} - -void mossrose_mutex_unlock(mossrose_mutex_t *mutex) -{ - pthread_mutex_unlock(mutex); -} - -void mossrose_mutex_destroy(mossrose_mutex_t *mutex) -{ - pthread_mutex_destroy(mutex); -} -#endif diff --git a/src/mossrose-mutex.h b/src/mossrose-mutex.h deleted file mode 100644 index 84d1f63..0000000 --- a/src/mossrose-mutex.h +++ /dev/null @@ -1,28 +0,0 @@ -#ifndef MOSSROSE_MUTEX_H -#define MOSSROSE_MUTEX_H - - -#ifdef WIN32 -#include -typedef HANDLE mossrose_mutex_t; -#else -#include -typedef pthread_mutex_t mossrose_mutex_t; -#endif - -/* initialize a mutex */ -void mossrose_mutex_init(mossrose_mutex_t *mutex); - -/* lock a mutex, hanging until locked */ -void mossrose_mutex_lock(mossrose_mutex_t *mutex); - -/* attempt to lock a mutex. returns 0 on success and 1 otherwise */ -int mossrose_mutex_trylock(mossrose_mutex_t *mutex); - -/* unlock a mutex */ -void mossrose_mutex_unlock(mossrose_mutex_t *mutex); - -/* destroy a mutex */ -void mossrose_mutex_destroy(mossrose_mutex_t *mutex); - -#endif diff --git a/src/mossrose.c b/src/mossrose.c index 02f063a..4f31711 100644 --- a/src/mossrose.c +++ b/src/mossrose.c @@ -1,9 +1,9 @@ #include #include #include -#include "mossrose.h" -#include "mossrose-mutex.h" -#include "mossrose-channel.h" +#include +#include "mutex.h" +#include "channel.h" /* ~~~~~~~~~~~~~~~~ type definitions ~~~~~~~~~~~~~~~~ */ @@ -124,15 +124,18 @@ int mossrose_init(double sample_rate, int n_channels) int mossrose_play(float *left, float *right, size_t len, int channel) { if (channel > 0) { - return mossrose_channel_set(mossrose_global.channels+channel, left, right, len, 1); + if (mossrose_channel_set(mossrose_global.channels+channel, left, right, len, 1) == 0) + return channel; + else + return -1; } else { struct mossrose_channel_t *chan; for (int i=0; i - -int mossrose_init(double sample_rate, int n_channels); - -int mossrose_terminate(); - -int mossrose_play(float *left, float *right, size_t n_samples, int channel); - -#endif diff --git a/src/mutex.c b/src/mutex.c new file mode 100644 index 0000000..4a51fa8 --- /dev/null +++ b/src/mutex.c @@ -0,0 +1,58 @@ +#include "mutex.h" + +#ifdef WIN32 +#include +void mossrose_mutex_init(mossrose_mutex_t *mutex) +{ + *mutex = CreateMutex(NULL, false, NULL); +} + +void mossrose_mutex_lock(mossrose_mutex_t *mutex) +{ + WaitForSingleObject(*mutex, INFINITE); +} + +int mossrose_mutex_trylock(mossrose_mutex_t *mutex) +{ + int result = WaitForSingleObject(*mutex, 0); + return result != WAIT_OBJECT_0; +} + +void mossrose_mutex_unlock(mossrose_mutex_t *mutex) +{ + ReleaseMutex(*mutex); +} + +void mossrose_mutex_destroy(mossrose_mutex_t *mutex) +{ + ReleaseMutex(*mutex); +} + + +#else +#include +void mossrose_mutex_init(mossrose_mutex_t *mutex) +{ + pthread_mutex_init(mutex, NULL); +} + +void mossrose_mutex_lock(mossrose_mutex_t *mutex) +{ + pthread_mutex_lock(mutex); +} + +int mossrose_mutex_trylock(mossrose_mutex_t *mutex) +{ + return pthread_mutex_trylock(mutex); +} + +void mossrose_mutex_unlock(mossrose_mutex_t *mutex) +{ + pthread_mutex_unlock(mutex); +} + +void mossrose_mutex_destroy(mossrose_mutex_t *mutex) +{ + pthread_mutex_destroy(mutex); +} +#endif diff --git a/src/mutex.h b/src/mutex.h new file mode 100644 index 0000000..84d1f63 --- /dev/null +++ b/src/mutex.h @@ -0,0 +1,28 @@ +#ifndef MOSSROSE_MUTEX_H +#define MOSSROSE_MUTEX_H + + +#ifdef WIN32 +#include +typedef HANDLE mossrose_mutex_t; +#else +#include +typedef pthread_mutex_t mossrose_mutex_t; +#endif + +/* initialize a mutex */ +void mossrose_mutex_init(mossrose_mutex_t *mutex); + +/* lock a mutex, hanging until locked */ +void mossrose_mutex_lock(mossrose_mutex_t *mutex); + +/* attempt to lock a mutex. returns 0 on success and 1 otherwise */ +int mossrose_mutex_trylock(mossrose_mutex_t *mutex); + +/* unlock a mutex */ +void mossrose_mutex_unlock(mossrose_mutex_t *mutex); + +/* destroy a mutex */ +void mossrose_mutex_destroy(mossrose_mutex_t *mutex); + +#endif -- cgit v1.2.1