/* * ringbuffer.c * Ring Buffer utility.. * * Author: Phil Burk, http://www.softsynth.com * * This program uses the PortAudio Portable Audio Library. * For more information see: http://www.audiomulch.com/portaudio/ * Copyright (c) 1999-2000 Ross Bencina and Phil Burk * * Permission is hereby granted, free of charge, to any person obtaining * a copy of this software and associated documentation files * (the "Software"), to deal in the Software without restriction, * including without limitation the rights to use, copy, modify, merge, * publish, distribute, sublicense, and/or sell copies of the Software, * and to permit persons to whom the Software is furnished to do so, * subject to the following conditions: * * The above copyright notice and this permission notice shall be * included in all copies or substantial portions of the Software. * * Any person wishing to distribute modifications to the Software is * requested to send the modifications to the original developer so that * they can be incorporated into the canonical version. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. * * modified 2002/07/13 by [email protected] to allow any number if channels * * extensively hacked by [email protected] for various reasons * */ #include "s_audio_paring.h" #include <stdlib.h> #include <stdio.h> #include <string.h> #ifdef _WIN32 #include <windows.h> #else /* _WIN32 */ #include <sys/time.h> #include <errno.h> #if defined(__APPLE__) && defined(__MACH__) /* macOS does not support unnamed posix semaphores, * so we use Mach semaphores instead. */ #include <mach/mach.h> #define HAVE_MACH_SEMAPHORE #elif defined(__linux__) || defined(__FreeBSD__) || \ defined(__NetBSD__) || defined(__OpenBSD__) #include <semaphore.h> #define HAVE_POSIX_SEMAPHORE #else /* Some platforms (including Apple) do not support unnamed posix semaphores, * others might not implement sem_timedwait(); to be on the safe side, we * we simply emulate it with a counter, mutex and condition variable. * The critical section is so short that there should not be any locking. */ #include <pthread.h> #endif #endif /* _WIN32 */ /* Clear buffer. Should only be called when buffer is NOT being read. */ static void sys_ringbuf_Flush(sys_ringbuf *rbuf, void *dataPtr, long nfill); /* Get address of region(s) to which we can write data. ** If the region is contiguous, size2 will be zero. ** If non-contiguous, size2 will be the size of second region. ** Returns room available to be written or numBytes, whichever is smaller. */ static long sys_ringbuf_GetWriteRegions(sys_ringbuf *rbuf, long numBytes, void **dataPtr1, long *sizePtr1, void **dataPtr2, long *sizePtr2, char *buffer); static long sys_ringbuf_AdvanceWriteIndex(sys_ringbuf *rbuf, long numBytes); /* Get address of region(s) from which we can read data. ** If the region is contiguous, size2 will be zero. ** If non-contiguous, size2 will be the size of second region. ** Returns room available to be read or numBytes, whichever is smaller. */ static long sys_ringbuf_GetReadRegions(sys_ringbuf *rbuf, long numBytes, void **dataPtr1, long *sizePtr1, void **dataPtr2, long *sizePtr2, char *buffer); static long sys_ringbuf_AdvanceReadIndex(sys_ringbuf *rbuf, long numBytes); /*************************************************************************** * Initialize FIFO. */ long sys_ringbuf_init(sys_ringbuf *rbuf, long numBytes, char *dataPtr, long nfill) { … } /*************************************************************************** ** Return number of bytes available for reading. */ long sys_ringbuf_getreadavailable(sys_ringbuf *rbuf) { … } /*************************************************************************** ** Return number of bytes available for writing. */ long sys_ringbuf_getwriteavailable(sys_ringbuf *rbuf) { … } /*************************************************************************** ** Clear buffer. Should only be called when buffer is NOT being read. */ static void sys_ringbuf_Flush(sys_ringbuf *rbuf, void *dataPtr, long nfill) { … } /*************************************************************************** ** Get address of region(s) to which we can write data. ** If the region is contiguous, size2 will be zero. ** If non-contiguous, size2 will be the size of second region. ** Returns room available to be written or numBytes, whichever is smaller. */ static long sys_ringbuf_GetWriteRegions(sys_ringbuf *rbuf, long numBytes, void **dataPtr1, long *sizePtr1, void **dataPtr2, long *sizePtr2, char *buffer) { … } /*************************************************************************** */ static long sys_ringbuf_AdvanceWriteIndex(sys_ringbuf *rbuf, long numBytes) { … } /*************************************************************************** ** Get address of region(s) from which we can read data. ** If the region is contiguous, size2 will be zero. ** If non-contiguous, size2 will be the size of second region. ** Returns room available to be written or numBytes, whichever is smaller. */ static long sys_ringbuf_GetReadRegions(sys_ringbuf *rbuf, long numBytes, void **dataPtr1, long *sizePtr1, void **dataPtr2, long *sizePtr2, char *buffer) { … } /*************************************************************************** */ static long sys_ringbuf_AdvanceReadIndex(sys_ringbuf *rbuf, long numBytes) { … } /*************************************************************************** ** Return bytes written. */ long sys_ringbuf_write(sys_ringbuf *rbuf, const void *data, long numBytes, char *buffer) { … } /*************************************************************************** ** Return bytes read. */ long sys_ringbuf_read(sys_ringbuf *rbuf, void *data, long numBytes, char *buffer) { … } /*************************************************************************** ** t_semaphore */ #ifndef _WIN32 struct _semaphore { … }; #endif t_semaphore * sys_semaphore_create(void) { … } void sys_semaphore_destroy(t_semaphore *sem) { … } void sys_semaphore_wait(t_semaphore *sem) { … } int sys_semaphore_waitfor(t_semaphore *sem, double seconds) { … } void sys_semaphore_post(t_semaphore *sem) { … }