From 550fa1ac138e9671050018b9d1dc2fb643a4c0b0 Mon Sep 17 00:00:00 2001 From: angel Date: Tue, 31 May 2005 11:39:49 +0000 Subject: [PATCH] More ss_outdev rewriting. git-svn-id: file:///home/angel/tmp/svn-triptico/ahxm/trunk@626 c87de0a0-a11c-0410-a1e5-866214bc28b2 --- ss_outdev.c | 271 +++++++++++------------------------------------------------- 1 file changed, 50 insertions(+), 221 deletions(-) diff --git a/ss_outdev.c b/ss_outdev.c index bdfdec0..e52e778 100644 --- a/ss_outdev.c +++ b/ss_outdev.c @@ -34,20 +34,26 @@ #include #include +struct ss_outdrv +{ + char * drvname; /* driver name */ + int (*open)(char *, int, int); /* open function */ + void (*write)(int *, int); /* write frame function */ + void (*close)(void); /* close function */ + char * deffile; /* default file or device */ + int max_channels; /* maximum channels (0, unlimited) */ +}; + +struct ss_outdrv * drv=NULL; + /******************* Code ********************/ -static int o_unsupp(int i, char * o, int f, int n) { return(0); } - -/************************************************/ - #ifdef CONFOPT_ARTS #include -#ifdef OUTDEV_REWRITTEN - static arts_stream_t arts; static int d_arts_open(char * file, int freq, int n_channels) @@ -73,36 +79,13 @@ static void d_arts_close(void) arts_free(); } -#endif - -static int o_arts(int i, char * file, int freq, int n_channels) -{ - int ret=0; - short int ibuf[128]; - int n, nc; - static arts_stream_t arts; - - nc=n_channels >= 2 ? 2 : 1; - - if(!arts_init() && (arts=arts_play_stream(freq, 16, nc, "annhell"))) - { - n_channels *= sizeof(short int); - nc *= sizeof(short int); - - while((n=read(i, ibuf, n_channels)) > 0) - arts_write(arts, ibuf, nc); - - arts_free(); - - ret=1; - } - - return(ret); -} +struct ss_outdrv outdrv_arts= +{ "arts", d_arts_open, d_arts_write, d_arts_close, NULL, 2 }; #else /* CONFOPT_ARTS */ -#define o_arts o_unsupp +struct ss_outdrv outdrv_arts= +{ "arts", NULL, NULL, NULL, NULL, 2 }; #endif /* CONFOPT_ARTS */ @@ -113,11 +96,9 @@ static int o_arts(int i, char * file, int freq, int n_channels) #include #include -#ifdef OUTDEV_REWRITTEN - static int esd_fd; -static int o_esd_open(char * file, int freq, int n_channels) +static int d_esd_open(char * file, int freq, int n_channels) { int ret=0; esd_format_t format; @@ -132,51 +113,24 @@ static int o_esd_open(char * file, int freq, int n_channels) return(ret); } -static void o_esd_write(int frame[], int size) +static void d_esd_write(int frame[], int size) { write(esd_fd, frame, size); } -static void o_esd_close(void) +static void d_esd_close(void) { close(esd_fd); } -#endif - -static int o_esd(int i, char * file, int freq, int n_channels) -{ - int ret=0; - short int ibuf[128]; - int o, nc, n; - esd_format_t format; - - nc=n_channels >= 2 ? 2 : 1; - - format = ESD_STREAM | ESD_PLAY | ESD_BITS16; - format |= nc == 2 ? ESD_STEREO : ESD_MONO; - - if(esd_open_sound(file) >= 0 && - (o=esd_play_stream_fallback(format, freq, NULL, "annhell")) >= 0) - { - n_channels *= sizeof(short int); - nc *= sizeof(short int); - - while((n=read(i, ibuf, n_channels)) > 0) - write(o, ibuf, nc); - - esd_close(o); - - ret=1; - } - - return(ret); -} +struct ss_outdrv outdrv_esd= +{ "esd", d_esd_open, d_esd_write, d_esd_close, NULL, 2 }; #else /* CONFOPT_ESD */ -#define o_esd o_unsupp +struct ss_outdrv outdrv_esd= +{ "esd", NULL, NULL, NULL, NULL, 2 }; #endif /* CONFOPT_ESD */ @@ -186,12 +140,10 @@ static int o_esd(int i, char * file, int freq, int n_channels) #include -#ifdef OUTDEV_REWRITTEN - static ALconfig ac; static ALport ap; -static int o_sgi_open(char * file, int freq, int n_channels) +static int d_sgi_open(char * file, int freq, int n_channels) { int ret=0; ALpv p[2]; @@ -217,62 +169,26 @@ static int o_sgi_open(char * file, int freq, int n_channels) } -static int o_sgi_write(int frame[], int size) +static int d_sgi_write(int frame[], int size) { alWriteFrames(ap, frame, 1); } -static int o_sgi_close(void) +static int d_sgi_close(void) { alClosePort(ap); alFreeConfig(ac); } -#endif - -static int o_sgi(int i, char * file, int freq, int n_channels) -{ - int ret=0; - short int ibuf[128]; - ALconfig ac; - ALport ap; - ALpv p[2]; - - p[0].param=AL_MASTER_CLOCK; - p[0].value.i=AL_CRYSTAL_MCLK_TYPE; - p[1].param=AL_RATE; - p[1].value.ll=alDoubleToFixed((double) freq); - - if((ac=alNewConfig())) - { - if(alSetChannels(ac, n_channels) >= 0 && - alSetWidth(ac, AL_SAMPLE_16) >= 0 && - alSetSampFmt(ac, AL_SAMPFMT_TWOSCOMP) >= 0 && - alSetQueueSize(ac, 2048) >=0 && - alSetDevice(ac, AL_DEFAULT_OUTPUT) >= 0 && - alSetParams(alGetDevice(ac), p, 2) >= 0 && - (ap=alOpenPort("annhell", "w", ac))) - { - n_channels *= sizeof(short int); - - while((n=read(i, ibuf, n_channels)) > 0) - alWriteFrames(ap, ibuf, 1); - - alClosePort(ap); - - ret=1; - } - - alFreeConfig(ac); - } - - return(ret); -} +struct ss_outdrv outdrv_sgi= +{ "sgi", d_sgi_open, d_sgi_write, d_sgi_close, NULL, 0 }; #else /* CONFOPT_SGI */ -#define o_sgi o_unsupp +struct ss_outdrv outdrv_sgi= +{ "sgi", NULL, NULL, NULL, NULL, 0 }; + #endif /* CONFOPT_SGI */ @@ -292,11 +208,9 @@ static int is_little_endian(void) return(*c == 1); } -#ifdef OUTDEV_REWRITTEN - static int oss_fd; -static int o_oss_open(char * file, int freq, int n_channels) +static int d_oss_open(char * file, int freq, int n_channels) { int ret=0; int fr, st, fm; @@ -319,56 +233,24 @@ static int o_oss_open(char * file, int freq, int n_channels) } -static void o_oss_write(int frame[], int size) +static void d_oss_write(int frame[], int size) { write(oss_fd, frame, size); } -static void o_oss_close(void) +static void d_oss_close(void) { close(oss_fd); } -#endif - -static int o_oss(int i, char * file, int freq, int n_channels) -{ - int ret=0; - short int ibuf[128]; - int o, n, nc, fr, st, fm; - - nc=n_channels >= 2 ? 2 : 1; - fr=(2 << 16)|(1 << 10); - st=nc == 2 ? 1 : 0; - fm=is_little_endian() ? AFMT_S16_LE : AFMT_S16_BE; - - if((o=open(file, O_WRONLY)) >= 0) - { - if(ioctl(o, SNDCTL_DSP_SETFRAGMENT, &fr) >= 0 && - ioctl(o, SNDCTL_DSP_RESET, 0) >= 0 && - ioctl(o, SNDCTL_DSP_SPEED, &freq) >= 0 && - ioctl(o, SNDCTL_DSP_STEREO, &st) >= 0 && - ioctl(o, SNDCTL_DSP_SETFMT, &fm) >= 0) - { - n_channels *= sizeof(short int); - nc *= sizeof(short int); - - while((n=read(i, ibuf, n_channels)) > 0) - write(o, ibuf, nc); - - ret=1; - } - - close(o); - } - - return(ret); -} +struct ss_outdrv outdrv_oss= +{ "oss", d_oss_open, d_oss_write, d_oss_close, "/dev/dsp", 2 }; #else /* CONFOPT_LINUX_OSS */ -#define o_oss o_unsupp +struct ss_outdrv outdrv_oss= +{ "oss", NULL, NULL, NULL, NULL, 2 }; #endif /* CONFOPT_LINUX_OSS */ @@ -389,12 +271,10 @@ static void fput32(int i, FILE * f) fputc((i & 0xff000000) >> 24, f); } -#ifdef OUTDEV_REWRITTEN - static FILE * wav_fd; static int wav_frames; -static int o_wav_open(char * file, int freq, int n_channels) +static int d_wav_open(char * file, int freq, int n_channels) { if((wav_fd=fopen(file, "w")) == NULL) return(0); @@ -420,7 +300,7 @@ static int o_wav_open(char * file, int freq, int n_channels) } -static void o_wav_write(int frame[], int size) +static void d_wav_write(int frame[], int size) { int n; @@ -433,7 +313,7 @@ static void o_wav_write(int frame[], int size) } -static void o_wav_close(void) +static void d_wav_close(void) { /* rewind file and write total size */ fseek(wav_fd, 4, SEEK_SET); @@ -445,67 +325,16 @@ static void o_wav_close(void) fclose(wav_fd); } -#endif - -static int o_wav(int i, char * file, int freq, int n_channels) -{ - short int ibuf[128]; - FILE * f; - int n, m, c; - - if((f=fopen(file, "w")) == NULL) return(0); - - /* write wav header */ +struct ss_outdrv outdrv_wav= +{ "wav", d_wav_open, d_wav_write, d_wav_close, "output.wav", 0 }; - fwrite("RIFF",1,4,f); - fput32(36, f); /* first checkpoint (offset: 4) */ - fwrite("WAVE",1,4,f); - fwrite("fmt ",1,4,f); - fput32(16,f); /* chunk size */ - fput16(1, f); /* 1: uncompressed PCM */ - fput16(n_channels, f); /* # of channels */ - fput32(freq, f); /* sample rate */ - fput32(freq * n_channels * 2, f); /* bytes per second */ - fput16(n_channels * 2, f); /* 'block align' */ - fput16(16, f); /* 16 bits per sample */ - fwrite("data",1,4,f); - fput32(0, f); /* second checkpoint (offset: 40) */ - - n_channels *= sizeof(short int); - - for(c=0;(n=read(i, ibuf, n_channels)) > 0;c++) - { - n /= sizeof(short int); - - for(m=0;m < n;m++) - fput16(ibuf[m], f); - } - - /* rewind file and write total size */ - fseek(f, 4, SEEK_SET); - fput32((c * n_channels) + 36, f); - - fseek(f, 40, SEEK_SET); - fput32(c * n_channels, f); - - fclose(f); - - return(1); -} - - -static struct -{ - char * drvname; - char * deffile; - int (* func)(int, char *, int, int); -} drivers[] = { - { "arts", NULL, o_arts }, - { "esd", NULL, o_esd }, - { "sgi", NULL, o_sgi }, - { "oss", "/dev/dsp", o_oss }, - { "wav", "output.wav", o_wav }, - { NULL, NULL, NULL } +static struct ss_outdrvs[] = { + &outdrv_arts, + &outdrv_esd, + &outdrv_sgi, + &outdrv_oss, + &outdrv_wav, + NULL }; int ss_outdev(char * drv, char * file, int freq, int n_channels) -- 2.11.4.GIT