From f27846f746c2cb35da58bde8513cfbb6c9285493 Mon Sep 17 00:00:00 2001 From: torben Date: Sat, 7 Mar 2009 02:57:08 +0000 Subject: [PATCH] [alsa-io] remove ALSASAMPLE and syncup alsa-in git-svn-id: svn+ssh://jackaudio.org/trunk/jack@3406 0c269be4-1314-0410-8aa9-9f06e86f4224 --- tools/alsa_in.c | 58 +++++++++++++++++++++++++++++++++++++----------- tools/alsa_out.c | 27 ++++------------------ 2 files changed, 49 insertions(+), 36 deletions(-) diff --git a/tools/alsa_in.c b/tools/alsa_in.c index 69fd5f8..bb97f0b 100644 --- a/tools/alsa_in.c +++ b/tools/alsa_in.c @@ -16,14 +16,14 @@ #include #include +#include "memops.h" + #include "alsa/asoundlib.h" #include #define OFF_D_SIZE 256 -typedef signed short ALSASAMPLE; - // Here are the lists of the jack ports... JSList *capture_ports = NULL; @@ -32,9 +32,6 @@ JSList *playback_ports = NULL; JSList *playback_srcs = NULL; jack_client_t *client; -// TODO: make the sample format configurable soon... -snd_pcm_format_t format = SND_PCM_FORMAT_S16; /* sample format */ - snd_pcm_t *alsa_handle; int jack_sample_rate; @@ -76,6 +73,23 @@ volatile float output_diff = 0.0; snd_pcm_uframes_t real_buffer_size; snd_pcm_uframes_t real_period_size; +// format selection, and corresponding functions from memops in a nice set of structs. + +typedef struct alsa_format { + snd_pcm_format_t format_id; + size_t sample_size; + void (*jack_to_soundcard) (char *dst, jack_default_audio_sample_t *src, unsigned long nsamples, unsigned long dst_skip, dither_state_t *state); + void (*soundcard_to_jack) (jack_default_audio_sample_t *dst, char *src, unsigned long nsamples, unsigned long src_skip); +} alsa_format_t; + +alsa_format_t formats[] = { + { SND_PCM_FORMAT_S32, 4, sample_move_d32u24_sS, sample_move_dS_s32u24 }, + { SND_PCM_FORMAT_S24, 4, sample_move_d24_sS, sample_move_dS_s24 }, + { SND_PCM_FORMAT_S16, 2, sample_move_d16_sS, sample_move_dS_s16 } +}; +#define NUMFORMATS (sizeof(formats)/sizeof(formats[0])) +int format=0; + // Alsa stuff... i dont want to touch this bullshit in the next years.... please... static int xrun_recovery(snd_pcm_t *handle, int err) { @@ -98,6 +112,23 @@ static int xrun_recovery(snd_pcm_t *handle, int err) { return err; } +static int set_hwformat( snd_pcm_t *handle, snd_pcm_hw_params_t *params ) +{ + int i; + int err; + + for( i=0; i (target_delay+max_diff) ) { - ALSASAMPLE *tmp = alloca( (delay-target_delay) * sizeof( ALSASAMPLE ) * num_channels ); + char *tmp = alloca( (delay-target_delay) * formats[format].sample_size * num_channels ); snd_pcm_readi( alsa_handle, tmp, delay-target_delay ); output_new_delay = (int) delay; @@ -358,7 +390,7 @@ int process (jack_nframes_t nframes, void *arg) { * now this should do it... */ - outbuf = alloca( rlen * sizeof( ALSASAMPLE ) * num_channels ); + outbuf = alloca( rlen * formats[format].sample_size * num_channels ); resampbuf = alloca( rlen * sizeof( float ) ); @@ -388,15 +420,15 @@ again: while ( node != NULL) { - int i; jack_port_t *port = (jack_port_t *) node->data; float *buf = jack_port_get_buffer (port, nframes); SRC_STATE *src_state = src_node->data; - for (i=0; i < rlen; i++) { - resampbuf[i] = (float) outbuf[chn+ i*num_channels] / 32767; - } + formats[format].soundcard_to_jack( resampbuf, outbuf + format[formats].sample_size * chn, rlen, num_channels*format[formats].sample_size ); + //for (i=0; i < rlen; i++) { + // resampbuf[i] = (float) outbuf[chn+ i*num_channels] / 32767; + //} src.data_in = resampbuf; src.input_frames = rlen; diff --git a/tools/alsa_out.c b/tools/alsa_out.c index 453c92a..0ba7512 100644 --- a/tools/alsa_out.c +++ b/tools/alsa_out.c @@ -25,26 +25,8 @@ #include -#define SAMPLE_16BIT_SCALING 32767.0f -#define SAMPLE_16BIT_MAX 32767 -#define SAMPLE_16BIT_MIN -32767 -#define NORMALIZED_FLOAT_MIN -1.0f -#define NORMALIZED_FLOAT_MAX 1.0f -#define f_round(f) lrintf(f) - -#define float_16(s, d)\ - if ((s) <= NORMALIZED_FLOAT_MIN) {\ - (d) = SAMPLE_16BIT_MIN;\ - } else if ((s) >= NORMALIZED_FLOAT_MAX) {\ - (d) = SAMPLE_16BIT_MAX;\ - } else {\ - (d) = f_round ((s) * SAMPLE_16BIT_SCALING);\ - } - #define OFF_D_SIZE 256 -typedef signed short ALSASAMPLE; - // Here are the lists of the jack ports... JSList *capture_ports = NULL; @@ -301,8 +283,8 @@ static snd_pcm_t *open_audiofd( char *device_name, int capture, int rate, int ch //snd_pcm_start( handle ); //snd_pcm_wait( handle, 200 ); int num_null_samples = nperiods * period * channels; - ALSASAMPLE *tmp = alloca( num_null_samples * sizeof( ALSASAMPLE ) ); - memset( tmp, 0, num_null_samples * sizeof( ALSASAMPLE ) ); + char *tmp = alloca( num_null_samples * formats[format].sample_size ); + memset( tmp, 0, num_null_samples * formats[format].sample_size ); snd_pcm_writei( handle, tmp, num_null_samples ); @@ -348,8 +330,8 @@ int process (jack_nframes_t nframes, void *arg) { offset_array[i] = 0.0; } if( delay < (target_delay-max_diff) ) { - ALSASAMPLE *tmp = alloca( (target_delay-delay) * sizeof( ALSASAMPLE ) * num_channels ); - memset( tmp, 0, sizeof( ALSASAMPLE ) * num_channels * (target_delay-delay) ); + char *tmp = alloca( (target_delay-delay) * formats[format].sample_size * num_channels ); + memset( tmp, 0, formats[format].sample_size * num_channels * (target_delay-delay) ); snd_pcm_writei( alsa_handle, tmp, target_delay-delay ); output_new_delay = (int) delay; @@ -432,7 +414,6 @@ int process (jack_nframes_t nframes, void *arg) { while ( node != NULL) { - int i; jack_port_t *port = (jack_port_t *) node->data; float *buf = jack_port_get_buffer (port, nframes);