📄 io.c
字号:
/* IO handlers *//* * -------------------------------- * int mus_file_read(int fd, int beg, int end, int chans, mus_sample_t **bufs) * int mus_file_write(int tfd, int beg, int end, int chans, mus_sample_t **bufs) * int mus_file_open_read(const char *arg) * int mus_file_open_write(const char *arg) * int mus_file_create(const char *arg) * int mus_file_reopen_write(const char *arg) * int mus_file_close(int fd) * bool mus_file_probe(const char *arg) * char *mus_format(const char *format, ...) * off_t mus_file_seek_frame(int tfd, off_t frame) * -------------------------------- */#include <mus-config.h>#if USE_SND #include "snd.h"#endif#include <math.h>#include <stdio.h>#if HAVE_FCNTL_H #include <fcntl.h>#endif#if HAVE_LIMITS_H #include <limits.h>#endif#include <errno.h>#include <stdlib.h>#if (defined(HAVE_LIBC_H) && (!defined(HAVE_UNISTD_H))) #include <libc.h>#else #if (!(defined(_MSC_VER))) #include <unistd.h> #endif#endif#if HAVE_STRING_H #include <string.h>#endif#include <stdarg.h>#include "_sndlib.h"/* data translations for big/little endian machines * the m_* forms are macros where possible for speed (dating back to 1991 -- probably not needed) */void mus_bint_to_char(unsigned char *j, int x){ unsigned char *ox = (unsigned char *)&x;#if MUS_LITTLE_ENDIAN j[0] = ox[3]; j[1] = ox[2]; j[2] = ox[1]; j[3] = ox[0];#else memcpy((void *)j, (void *)ox, 4);#endif}int mus_char_to_bint(const unsigned char *inp){ int o; unsigned char *outp = (unsigned char *)&o;#if MUS_LITTLE_ENDIAN outp[0] = inp[3]; outp[1] = inp[2]; outp[2] = inp[1]; outp[3] = inp[0];#else memcpy((void *)outp, (void *)inp, 4);#endif return(o);}void mus_lint_to_char(unsigned char *j, int x){ unsigned char *ox = (unsigned char *)&x;#if (!MUS_LITTLE_ENDIAN) j[0] = ox[3]; j[1] = ox[2]; j[2] = ox[1]; j[3] = ox[0];#else memcpy((void *)j, (void *)ox, 4);#endif}int mus_char_to_lint(const unsigned char *inp){ int o; unsigned char *outp = (unsigned char *)&o;#if (!MUS_LITTLE_ENDIAN) outp[0] = inp[3]; outp[1] = inp[2]; outp[2] = inp[1]; outp[3] = inp[0];#else memcpy((void *)outp, (void *)inp, 4);#endif return(o);}int mus_char_to_uninterpreted_int(const unsigned char *inp){ int o; unsigned char *outp = (unsigned char *)&o; memcpy((void *)outp, (void *)inp, 4); return(o);}unsigned int mus_char_to_ubint(const unsigned char *inp){ unsigned int o; unsigned char *outp = (unsigned char *)&o;#if MUS_LITTLE_ENDIAN outp[0] = inp[3]; outp[1] = inp[2]; outp[2] = inp[1]; outp[3] = inp[0];#else memcpy((void *)outp, (void *)inp, 4);#endif return(o);}unsigned int mus_char_to_ulint(const unsigned char *inp){ unsigned int o; unsigned char *outp = (unsigned char *)&o;#if (!MUS_LITTLE_ENDIAN) outp[0] = inp[3]; outp[1] = inp[2]; outp[2] = inp[1]; outp[3] = inp[0];#else memcpy((void *)outp, (void *)inp, 4);#endif return(o);}void mus_bfloat_to_char(unsigned char *j, float x){ unsigned char *ox = (unsigned char *)&x;#if MUS_LITTLE_ENDIAN j[0] = ox[3]; j[1] = ox[2]; j[2] = ox[1]; j[3] = ox[0];#else memcpy((void *)j, (void *)ox, 4);#endif}float mus_char_to_bfloat(const unsigned char *inp){ float o; unsigned char *outp = (unsigned char *)&o;#if MUS_LITTLE_ENDIAN outp[0] = inp[3]; outp[1] = inp[2]; outp[2] = inp[1]; outp[3] = inp[0];#else memcpy((void *)outp, (void *)inp, 4);#endif return(o);}void mus_lfloat_to_char(unsigned char *j, float x){ unsigned char *ox = (unsigned char *)&x;#if (!MUS_LITTLE_ENDIAN) j[0] = ox[3]; j[1] = ox[2]; j[2] = ox[1]; j[3] = ox[0];#else memcpy((void *)j, (void *)ox, 4);#endif}float mus_char_to_lfloat(const unsigned char *inp){ float o; unsigned char *outp = (unsigned char *)&o;#if (!MUS_LITTLE_ENDIAN) outp[0] = inp[3]; outp[1] = inp[2]; outp[2] = inp[1]; outp[3] = inp[0];#else memcpy((void *)outp, (void *)inp, 4);#endif return(o);}void mus_bshort_to_char(unsigned char *j, short x){ unsigned char *ox = (unsigned char *)&x;#if MUS_LITTLE_ENDIAN j[0] = ox[1]; j[1] = ox[0];#else memcpy((void *)j, (void *)ox, 2); /* I wonder if this is faster */#endif}short mus_char_to_bshort(const unsigned char *inp){ short o; unsigned char *outp = (unsigned char *)&o;#if MUS_LITTLE_ENDIAN outp[0] = inp[1]; outp[1] = inp[0];#else memcpy((void *)outp, (void *)inp, 2);#endif return(o);}void mus_lshort_to_char(unsigned char *j, short x){ unsigned char *ox = (unsigned char *)&x;#if (!MUS_LITTLE_ENDIAN) j[0] = ox[1]; j[1] = ox[0];#else memcpy((void *)j, (void *)ox, 2);#endif}short mus_char_to_lshort(const unsigned char *inp){ short o; unsigned char *outp = (unsigned char *)&o;#if (!MUS_LITTLE_ENDIAN) outp[0] = inp[1]; outp[1] = inp[0];#else memcpy((void *)outp, (void *)inp, 2);#endif return(o);}void mus_ubshort_to_char(unsigned char *j, unsigned short x){ unsigned char *ox = (unsigned char *)&x;#if MUS_LITTLE_ENDIAN j[0] = ox[1]; j[1] = ox[0];#else memcpy((void *)j, (void *)ox, 2);#endif}unsigned short mus_char_to_ubshort(const unsigned char *inp){ unsigned short o; unsigned char *outp = (unsigned char *)&o;#if MUS_LITTLE_ENDIAN outp[0] = inp[1]; outp[1] = inp[0];#else memcpy((void *)outp, (void *)inp, 2);#endif return(o);}void mus_ulshort_to_char(unsigned char *j, unsigned short x){ unsigned char *ox = (unsigned char *)&x;#if (!MUS_LITTLE_ENDIAN) j[0] = ox[1]; j[1] = ox[0];#else memcpy((void *)j, (void *)ox, 2);#endif}unsigned short mus_char_to_ulshort(const unsigned char *inp){ unsigned short o; unsigned char *outp = (unsigned char *)&o;#if (!MUS_LITTLE_ENDIAN) outp[0] = inp[1]; outp[1] = inp[0];#else memcpy((void *)outp, (void *)inp, 2);#endif return(o);}double mus_char_to_ldouble(const unsigned char *inp){ double o; unsigned char *outp = (unsigned char *)&o;#if (MUS_LITTLE_ENDIAN) memcpy((void *)outp, (void *)inp, 8);#else outp[0] = inp[7]; outp[1] = inp[6]; outp[2] = inp[5]; outp[3] = inp[4]; outp[4] = inp[3]; outp[5] = inp[2]; outp[6] = inp[1]; outp[7] = inp[0];#endif return(o);}double mus_char_to_bdouble(const unsigned char *inp){ double o; unsigned char *outp = (unsigned char *)&o;#if (MUS_LITTLE_ENDIAN) outp[0] = inp[7]; outp[1] = inp[6]; outp[2] = inp[5]; outp[3] = inp[4]; outp[4] = inp[3]; outp[5] = inp[2]; outp[6] = inp[1]; outp[7] = inp[0];#else memcpy((void *)outp, (void *)inp, 8);#endif return(o);}void mus_bdouble_to_char(unsigned char *j, double x){ unsigned char *ox = (unsigned char *)&x;#if (MUS_LITTLE_ENDIAN) j[0] = ox[7]; j[1] = ox[6]; j[2] = ox[5]; j[3] = ox[4]; j[4] = ox[3]; j[5] = ox[2]; j[6] = ox[1]; j[7] = ox[0];#else memcpy((void *)j, (void *)ox, 8);#endif}void mus_ldouble_to_char(unsigned char *j, double x){ unsigned char *ox = (unsigned char *)&x;#if (MUS_LITTLE_ENDIAN) memcpy((void *)j, (void *)ox, 8);#else j[0] = ox[7]; j[1] = ox[6]; j[2] = ox[5]; j[3] = ox[4]; j[4] = ox[3]; j[5] = ox[2]; j[6] = ox[1]; j[7] = ox[0];#endif}#if HAVE_BYTESWAP_H #include <byteswap.h>#endif#if MUS_LITTLE_ENDIAN #if HAVE_BYTESWAP_H #define m_big_endian_short(n) ((short)(bswap_16((*((unsigned short *)n))))) #define m_big_endian_int(n) ((int)(bswap_32((*((unsigned int *)n))))) #define m_big_endian_unsigned_short(n) ((unsigned short)(bswap_16((*((unsigned short *)n))))) #else #define m_big_endian_short(n) (mus_char_to_bshort(n)) #define m_big_endian_int(n) (mus_char_to_bint(n)) #define m_big_endian_unsigned_short(n) (mus_char_to_ubshort(n)) #endif #define m_big_endian_float(n) (mus_char_to_bfloat(n)) #define m_big_endian_double(n) (mus_char_to_bdouble(n)) #define m_little_endian_short(n) (*((short *)n)) #define m_little_endian_int(n) (*((int *)n)) #define m_little_endian_float(n) (*((float *)n)) #define m_little_endian_double(n) (*((double *)n)) #define m_little_endian_unsigned_short(n) (*((unsigned short *)n)) #if HAVE_BYTESWAP_H #define m_set_big_endian_short(n, x) (*((short *)n)) = ((short)(bswap_16(x))) #define m_set_big_endian_int(n, x) (*((int *)n)) = ((int)(bswap_32(x))) #define m_set_big_endian_unsigned_short(n, x) (*((unsigned short *)n)) = ((unsigned short)(bswap_16(x))) #else #define m_set_big_endian_short(n, x) mus_bshort_to_char(n, x) #define m_set_big_endian_int(n, x) mus_bint_to_char(n, x) #define m_set_big_endian_unsigned_short(n, x) mus_ubshort_to_char(n, x) #endif #define m_set_big_endian_float(n, x) mus_bfloat_to_char(n, x) #define m_set_big_endian_double(n, x) mus_bdouble_to_char(n, x) #define m_set_little_endian_short(n, x) (*((short *)n)) = x #define m_set_little_endian_int(n, x) (*((int *)n)) = x #define m_set_little_endian_float(n, x) (*((float *)n)) = x #define m_set_little_endian_double(n, x) (*((double *)n)) = x #define m_set_little_endian_unsigned_short(n, x) (*((unsigned short *)n)) = x#else #ifndef MUS_SUN #define m_big_endian_short(n) (*((short *)n)) #define m_big_endian_int(n) (*((int *)n)) #define m_big_endian_float(n) (*((float *)n)) #define m_big_endian_double(n) (*((double *)n)) #define m_big_endian_unsigned_short(n) (*((unsigned short *)n)) #define m_set_big_endian_short(n, x) (*((short *)n)) = x #define m_set_big_endian_int(n, x) (*((int *)n)) = x #define m_set_big_endian_float(n, x) (*((float *)n)) = x #define m_set_big_endian_double(n, x) (*((double *)n)) = x #define m_set_big_endian_unsigned_short(n, x) (*((unsigned short *)n)) = x #else #define m_big_endian_short(n) (mus_char_to_bshort(n)) #define m_big_endian_int(n) (mus_char_to_bint(n)) #define m_big_endian_float(n) (mus_char_to_bfloat(n)) #define m_big_endian_double(n) (mus_char_to_bdouble(n)) #define m_big_endian_unsigned_short(n) (mus_char_to_ubshort(n)) #define m_set_big_endian_short(n, x) mus_bshort_to_char(n, x) #define m_set_big_endian_int(n, x) mus_bint_to_char(n, x) #define m_set_big_endian_float(n, x) mus_bfloat_to_char(n, x) #define m_set_big_endian_double(n, x) mus_bdouble_to_char(n, x) #define m_set_big_endian_unsigned_short(n, x) mus_ubshort_to_char(n, x) #endif #if HAVE_BYTESWAP_H #define m_little_endian_short(n) ((short)(bswap_16((*((unsigned short *)n))))) #define m_little_endian_int(n) ((int)(bswap_32((*((unsigned int *)n))))) #define m_little_endian_unsigned_short(n) ((unsigned short)(bswap_16((*((unsigned short *)n))))) #else #define m_little_endian_short(n) (mus_char_to_lshort(n)) #define m_little_endian_int(n) (mus_char_to_lint(n)) #define m_little_endian_unsigned_short(n) (mus_char_to_ulshort(n)) #endif #define m_little_endian_float(n) (mus_char_to_lfloat(n)) #define m_little_endian_double(n) (mus_char_to_ldouble(n)) #if HAVE_BYTESWAP_H #define m_set_little_endian_short(n, x) (*((short *)n)) = ((short)(bswap_16(x))) #define m_set_little_endian_int(n, x) (*((int *)n)) = ((int)(bswap_32(x))) #define m_set_little_endian_unsigned_short(n, x) (*((unsigned short *)n)) = ((unsigned short)(bswap_16(x))) #else #define m_set_little_endian_short(n, x) mus_lshort_to_char(n, x) #define m_set_little_endian_int(n, x) mus_lint_to_char(n, x) #define m_set_little_endian_unsigned_short(n, x) mus_ulshort_to_char(n, x) #endif #define m_set_little_endian_float(n, x) mus_lfloat_to_char(n, x) #define m_set_little_endian_double(n, x) mus_ldouble_to_char(n, x)#endif/* ---------------- file descriptors ---------------- */typedef struct { char *name; int data_format, bytes_per_sample, chans, header_type; bool clipping; off_t data_location; Float prescaler;} io_fd;static int io_fd_size = 0;static io_fd **io_fds = NULL;#define IO_FD_ALLOC_SIZE 8static bool clipping_default = false;static Float prescaler_default = 1.0;bool mus_clipping(void) {return(clipping_default);}bool mus_set_clipping(bool new_value) {clipping_default = new_value; return(new_value);}Float mus_prescaler(void) {return(prescaler_default);}Float mus_set_prescaler(Float new_value) {prescaler_default = new_value; return(new_value);}int mus_file_open_descriptors(int tfd, const char *name, int format, int size /* datum size */, off_t location, int chans, int type){ io_fd *fd; int i, lim = -1; if (io_fd_size == 0) { io_fd_size = tfd + IO_FD_ALLOC_SIZE; io_fds = (io_fd **)CALLOC(io_fd_size, sizeof(io_fd *)); if (!io_fds) return(MUS_MEMORY_ALLOCATION_FAILED); } if (io_fd_size <= tfd) { lim = io_fd_size; io_fd_size = tfd + IO_FD_ALLOC_SIZE; io_fds = (io_fd **)REALLOC(io_fds, io_fd_size * sizeof(io_fd *)); for (i = lim; i < io_fd_size; i++) io_fds[i] = NULL; } if (io_fds[tfd] == NULL) { io_fds[tfd] = (io_fd *)CALLOC(1, sizeof(io_fd)); if (!(io_fds[tfd])) return(MUS_MEMORY_ALLOCATION_FAILED); } fd = io_fds[tfd]; fd->data_format = format; fd->bytes_per_sample = size;#if MUS_DEBUGGING if (size != mus_bytes_per_sample(format)) fprintf(stderr, "format trouble in mus_file_open_descriptors: %d != %d\n", size, mus_bytes_per_sample(format));#endif fd->data_location = location; fd->clipping = clipping_default; fd->prescaler = prescaler_default; fd->header_type = type; fd->chans = chans; if (name) { fd->name = (char *)CALLOC(strlen(name) + 1, sizeof(char)); strcpy(fd->name, name); } return(MUS_NO_ERROR);}bool mus_file_clipping(int tfd){ io_fd *fd; if ((io_fds == NULL) || (tfd >= io_fd_size) || (tfd < 0) || (io_fds[tfd] == NULL)) return(false); fd = io_fds[tfd]; return(fd->clipping);}int mus_file_set_clipping(int tfd, bool clipped){ io_fd *fd; if ((io_fds == NULL) || (tfd >= io_fd_size) || (tfd < 0) || (io_fds[tfd] == NULL)) return(MUS_FILE_DESCRIPTORS_NOT_INITIALIZED); fd = io_fds[tfd]; fd->clipping = clipped; return(MUS_NO_ERROR);}int mus_file_set_header_type(int tfd, int type){ io_fd *fd; if ((io_fds == NULL) || (tfd >= io_fd_size) || (tfd < 0) || (io_fds[tfd] == NULL)) return(MUS_FILE_DESCRIPTORS_NOT_INITIALIZED); fd = io_fds[tfd]; fd->header_type = type; return(MUS_NO_ERROR);}int mus_file_header_type(int tfd){ io_fd *fd; if ((io_fds == NULL) || (tfd >= io_fd_size) || (tfd < 0) || (io_fds[tfd] == NULL)) return(MUS_FILE_DESCRIPTORS_NOT_INITIALIZED); fd = io_fds[tfd]; return(fd->header_type);}Float mus_file_prescaler(int tfd) { io_fd *fd; if ((io_fds == NULL) || (tfd >= io_fd_size) || (tfd < 0) || (io_fds[tfd] == NULL)) return(0.0); fd = io_fds[tfd]; return(fd->prescaler);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -