From 8773d373f0e13b01260e6e1f2446c27333264a94 Mon Sep 17 00:00:00 2001 From: Jaroslav Kysela Date: Fri, 27 Nov 1998 15:17:45 +0000 Subject: [PATCH] Coding style... --- alsamixer/alsamixer.c | 1562 +++++++++++++++++----------------- amixer/amain.cpp | 236 ++--- aplay/aplay.c | 1892 +++++++++++++++++++++-------------------- aplay/formats.h | 74 +- 4 files changed, 1871 insertions(+), 1893 deletions(-) diff --git a/alsamixer/alsamixer.c b/alsamixer/alsamixer.c index eb4c82d..4d82105 100644 --- a/alsamixer/alsamixer.c +++ b/alsamixer/alsamixer.c @@ -77,874 +77,830 @@ /* --- variables --- */ -static WINDOW *mixer_window = NULL; -static int mixer_max_x = 0; -static int mixer_max_y = 0; -static int mixer_ofs_x = 0; -static float mixer_extra_space = 0; -static int mixer_ofs_y = 0; -static int mixer_cbar_height = 0; - -static int card_id = 0; -static int mixer_id = 0; -static void *mixer_handle; -static char *mixer_card_name = NULL; -static char *mixer_device_name = NULL; - -static int mixer_n_channels = 0; -static int mixer_n_vis_channels = 0; -static int mixer_first_vis_channel = 0; -static int mixer_focus_channel = 0; -static int mixer_exact = 0; - -static int mixer_lvolume_delta = 0; -static int mixer_rvolume_delta = 0; -static int mixer_balance_volumes = 0; -static int mixer_toggle_mute_left = 0; -static int mixer_toggle_mute_right = 0; -static int mixer_toggle_record = 0; +static WINDOW *mixer_window = NULL; +static int mixer_max_x = 0; +static int mixer_max_y = 0; +static int mixer_ofs_x = 0; +static float mixer_extra_space = 0; +static int mixer_ofs_y = 0; +static int mixer_cbar_height = 0; + +static int card_id = 0; +static int mixer_id = 0; +static void *mixer_handle; +static char *mixer_card_name = NULL; +static char *mixer_device_name = NULL; + +static int mixer_n_channels = 0; +static int mixer_n_vis_channels = 0; +static int mixer_first_vis_channel = 0; +static int mixer_focus_channel = 0; +static int mixer_exact = 0; + +static int mixer_lvolume_delta = 0; +static int mixer_rvolume_delta = 0; +static int mixer_balance_volumes = 0; +static int mixer_toggle_mute_left = 0; +static int mixer_toggle_mute_right = 0; +static int mixer_toggle_record = 0; /* By Carl */ -static int mixer_toggle_rec_left = 0; -static int mixer_toggle_rec_right = 0; -static int mixer_route_ltor_in = 0; -static int mixer_route_rtol_in = 0; +static int mixer_toggle_rec_left = 0; +static int mixer_toggle_rec_right = 0; +static int mixer_route_ltor_in = 0; +static int mixer_route_rtol_in = 0; #if 0 -static int mixer_route_ltor_out = 0; -static int mixer_route_rtol_out = 0; +static int mixer_route_ltor_out = 0; +static int mixer_route_rtol_out = 0; #endif /* --- draw contexts --- */ -enum -{ - DC_DEFAULT, - DC_BACK, - DC_TEXT, - DC_PROMPT, - DC_CBAR_MUTE, - DC_CBAR_NOMUTE, - DC_CBAR_RECORD, - DC_CBAR_NORECORD, - DC_CBAR_EMPTY, - DC_CBAR_FULL_1, - DC_CBAR_FULL_2, - DC_CBAR_FULL_3, - DC_CBAR_LABEL, - DC_CBAR_FOCUS_LABEL, - DC_FOCUS, - DC_LAST +enum { + DC_DEFAULT, + DC_BACK, + DC_TEXT, + DC_PROMPT, + DC_CBAR_MUTE, + DC_CBAR_NOMUTE, + DC_CBAR_RECORD, + DC_CBAR_NORECORD, + DC_CBAR_EMPTY, + DC_CBAR_FULL_1, + DC_CBAR_FULL_2, + DC_CBAR_FULL_3, + DC_CBAR_LABEL, + DC_CBAR_FOCUS_LABEL, + DC_FOCUS, + DC_LAST }; -static int dc_fg[DC_LAST] = { 0 }; -static int dc_attrib[DC_LAST] = { 0 }; -static int dc_char[DC_LAST] = { 0 }; +static int dc_fg[DC_LAST] = +{0}; +static int dc_attrib[DC_LAST] = +{0}; +static int dc_char[DC_LAST] = +{0}; static int mixer_do_color = 1; -static void -mixer_init_dc (int c, - int n, - int f, - int b, - int a) +static void mixer_init_dc(int c, + int n, + int f, + int b, + int a) { - dc_fg[n] = f; - dc_attrib[n] = a; - dc_char[n] = c; - if (n > 0) - init_pair (n, dc_fg[n] & 0xf, b & 0x0f); + dc_fg[n] = f; + dc_attrib[n] = a; + dc_char[n] = c; + if (n > 0) + init_pair(n, dc_fg[n] & 0xf, b & 0x0f); } -static int -mixer_dc (int n) +static int mixer_dc(int n) { - if (mixer_do_color) - attrset (COLOR_PAIR (n) | (dc_fg[n] & 0xfffffff0)); - else - attrset (dc_attrib[n]); - - return dc_char[n]; + if (mixer_do_color) + attrset(COLOR_PAIR(n) | (dc_fg[n] & 0xfffffff0)); + else + attrset(dc_attrib[n]); + + return dc_char[n]; } -static void -mixer_init_draw_contexts (void) +static void mixer_init_draw_contexts(void) { - start_color (); - - mixer_init_dc ('.', DC_BACK, MIXER_WHITE, MIXER_BLACK, A_NORMAL); - mixer_init_dc ('.', DC_TEXT, MIXER_YELLOW, MIXER_BLACK, A_BOLD); - mixer_init_dc ('.', DC_PROMPT, MIXER_DARK_CYAN, MIXER_BLACK, A_NORMAL); - mixer_init_dc ('M', DC_CBAR_MUTE, MIXER_CYAN, MIXER_BLACK, A_BOLD); - mixer_init_dc ('-', DC_CBAR_NOMUTE, MIXER_CYAN, MIXER_BLACK, A_NORMAL); - mixer_init_dc ('x', DC_CBAR_RECORD, MIXER_DARK_RED, MIXER_BLACK, A_BOLD); - mixer_init_dc ('-', DC_CBAR_NORECORD, MIXER_GRAY, MIXER_BLACK, A_NORMAL); - mixer_init_dc (' ', DC_CBAR_EMPTY, MIXER_GRAY, MIXER_BLACK, A_DIM); - mixer_init_dc ('#', DC_CBAR_FULL_1, MIXER_WHITE, MIXER_BLACK, A_BOLD); - mixer_init_dc ('#', DC_CBAR_FULL_2, MIXER_GREEN, MIXER_BLACK, A_BOLD); - mixer_init_dc ('#', DC_CBAR_FULL_3, MIXER_RED, MIXER_BLACK, A_BOLD); - mixer_init_dc ('.', DC_CBAR_LABEL, MIXER_WHITE, MIXER_BLUE, A_REVERSE | A_BOLD); - mixer_init_dc ('.', DC_CBAR_FOCUS_LABEL, MIXER_RED, MIXER_BLUE, A_REVERSE | A_BOLD); - mixer_init_dc ('.', DC_FOCUS, MIXER_RED, MIXER_BLACK, A_BOLD); + start_color(); + + mixer_init_dc('.', DC_BACK, MIXER_WHITE, MIXER_BLACK, A_NORMAL); + mixer_init_dc('.', DC_TEXT, MIXER_YELLOW, MIXER_BLACK, A_BOLD); + mixer_init_dc('.', DC_PROMPT, MIXER_DARK_CYAN, MIXER_BLACK, A_NORMAL); + mixer_init_dc('M', DC_CBAR_MUTE, MIXER_CYAN, MIXER_BLACK, A_BOLD); + mixer_init_dc('-', DC_CBAR_NOMUTE, MIXER_CYAN, MIXER_BLACK, A_NORMAL); + mixer_init_dc('x', DC_CBAR_RECORD, MIXER_DARK_RED, MIXER_BLACK, A_BOLD); + mixer_init_dc('-', DC_CBAR_NORECORD, MIXER_GRAY, MIXER_BLACK, A_NORMAL); + mixer_init_dc(' ', DC_CBAR_EMPTY, MIXER_GRAY, MIXER_BLACK, A_DIM); + mixer_init_dc('#', DC_CBAR_FULL_1, MIXER_WHITE, MIXER_BLACK, A_BOLD); + mixer_init_dc('#', DC_CBAR_FULL_2, MIXER_GREEN, MIXER_BLACK, A_BOLD); + mixer_init_dc('#', DC_CBAR_FULL_3, MIXER_RED, MIXER_BLACK, A_BOLD); + mixer_init_dc('.', DC_CBAR_LABEL, MIXER_WHITE, MIXER_BLUE, A_REVERSE | A_BOLD); + mixer_init_dc('.', DC_CBAR_FOCUS_LABEL, MIXER_RED, MIXER_BLUE, A_REVERSE | A_BOLD); + mixer_init_dc('.', DC_FOCUS, MIXER_RED, MIXER_BLACK, A_BOLD); } + #define DC_CBAR_FRAME (DC_CBAR_MUTE) #define DC_FRAME (DC_PROMPT) /* --- error types --- */ -typedef enum -{ - ERR_NONE, - ERR_OPEN, - ERR_FCN, - ERR_SIGNAL, - ERR_WINSIZE, +typedef enum { + ERR_NONE, + ERR_OPEN, + ERR_FCN, + ERR_SIGNAL, + ERR_WINSIZE, } ErrType; /* --- prototypes --- */ -static void mixer_abort (ErrType error, - const char *err_string) - __attribute__ -((noreturn)); +static void mixer_abort(ErrType error, + const char *err_string) + __attribute__ + ((noreturn)); /* --- functions --- */ -static void -mixer_clear (void) +static void mixer_clear(void) { - int x, y; - - mixer_dc (DC_BACK); - clear (); - - /* buggy ncurses doesn't really write spaces with the specified - * color into the screen on clear (); - */ - for (x = 0; x < mixer_max_x; x++) - for (y = 0; y < mixer_max_y; y++) - mvaddch (y, x, ' '); - refresh (); + int x, y; + + mixer_dc(DC_BACK); + clear(); + + /* buggy ncurses doesn't really write spaces with the specified + * color into the screen on clear (); + */ + for (x = 0; x < mixer_max_x; x++) + for (y = 0; y < mixer_max_y; y++) + mvaddch(y, x, ' '); + refresh(); } -static void -mixer_abort (ErrType error, - const char *err_string) +static void mixer_abort(ErrType error, + const char *err_string) { - if (mixer_window) - { - mixer_clear (); - endwin (); - mixer_window = NULL; - } - printf ("\n"); - - switch (error) - { - case ERR_OPEN: - fprintf (stderr, - PRGNAME ": failed to open mixer #%i/#%i: %s\n", - card_id, - mixer_id, - snd_strerror(errno)); - break; - case ERR_FCN: - fprintf (stderr, - PRGNAME ": function %s failed: %s\n", - err_string, - snd_strerror(errno)); - break; - case ERR_SIGNAL: - fprintf (stderr, - PRGNAME ": aborting due to signal `%s'\n", - err_string); - break; - case ERR_WINSIZE: - fprintf (stderr, - PRGNAME ": screen size too small (%dx%d)\n", - mixer_max_x, - mixer_max_y); - break; - default: - break; - } - - exit (error); + if (mixer_window) { + mixer_clear(); + endwin(); + mixer_window = NULL; + } + printf("\n"); + + switch (error) { + case ERR_OPEN: + fprintf(stderr, + PRGNAME ": failed to open mixer #%i/#%i: %s\n", + card_id, + mixer_id, + snd_strerror(errno)); + break; + case ERR_FCN: + fprintf(stderr, + PRGNAME ": function %s failed: %s\n", + err_string, + snd_strerror(errno)); + break; + case ERR_SIGNAL: + fprintf(stderr, + PRGNAME ": aborting due to signal `%s'\n", + err_string); + break; + case ERR_WINSIZE: + fprintf(stderr, + PRGNAME ": screen size too small (%dx%d)\n", + mixer_max_x, + mixer_max_y); + break; + default: + break; + } + + exit(error); } -static int -mixer_cbar_get_pos (int channel_index, - int *x_p, - int *y_p) +static int mixer_cbar_get_pos(int channel_index, + int *x_p, + int *y_p) { - int x; - int y; - - if (channel_index < mixer_first_vis_channel || - channel_index - mixer_first_vis_channel >= mixer_n_vis_channels) - return FALSE; - - channel_index -= mixer_first_vis_channel; - - x = mixer_ofs_x + 1; - y = mixer_ofs_y; - x += channel_index * (3 + 2 + 3 + 1 + mixer_extra_space); - y += mixer_max_y / 2; - y += mixer_cbar_height / 2 + 1; - - if (x_p) - *x_p = x; - if (y_p) - *y_p = y; - - return TRUE; + int x; + int y; + + if (channel_index < mixer_first_vis_channel || + channel_index - mixer_first_vis_channel >= mixer_n_vis_channels) + return FALSE; + + channel_index -= mixer_first_vis_channel; + + x = mixer_ofs_x + 1; + y = mixer_ofs_y; + x += channel_index * (3 + 2 + 3 + 1 + mixer_extra_space); + y += mixer_max_y / 2; + y += mixer_cbar_height / 2 + 1; + + if (x_p) + *x_p = x; + if (y_p) + *y_p = y; + + return TRUE; } -static void -mixer_update_cbar (int channel_index) +static void mixer_update_cbar(int channel_index) { - char string[64]; - char c; - snd_mixer_channel_info_t cinfo = { 0 }; - snd_mixer_channel_t cdata = { 0 }; - int vleft, vright; - int x, y, i; - - - /* set specified EXACT mode - */ - if (snd_mixer_exact_mode(mixer_handle, mixer_exact)<0) - mixer_abort (ERR_FCN, "snd_mixer_exact"); - - /* set new channel indices and read info - */ - if (snd_mixer_channel_info(mixer_handle, channel_index, &cinfo)<0) - mixer_abort (ERR_FCN, "snd_mixer_channel_info"); - - /* set new channel values - */ - if (channel_index == mixer_focus_channel && - (mixer_lvolume_delta || mixer_rvolume_delta || - mixer_toggle_mute_left || mixer_toggle_mute_right || - mixer_balance_volumes || - mixer_toggle_record || mixer_toggle_rec_left || - mixer_toggle_rec_right || - mixer_route_rtol_in || mixer_route_ltor_in)) - { - if (snd_mixer_channel_read(mixer_handle, channel_index, &cdata)<0) - mixer_abort (ERR_FCN, "snd_mixer_channel_read"); - - cdata.flags &= ~SND_MIXER_FLG_DECIBEL; - cdata.left = CLAMP (cdata.left + mixer_lvolume_delta, cinfo.min, cinfo.max); - cdata.right = CLAMP (cdata.right + mixer_rvolume_delta, cinfo.min, cinfo.max); - mixer_lvolume_delta = mixer_rvolume_delta = 0; - if (mixer_balance_volumes) - { - cdata.left = (cdata.left + cdata.right) / 2; - cdata.right = cdata.left; - mixer_balance_volumes = 0; - } - if (mixer_toggle_mute_left) - { - if (cdata.flags & SND_MIXER_FLG_MUTE_LEFT) - cdata.flags &= ~SND_MIXER_FLG_MUTE_LEFT; - else - cdata.flags |= SND_MIXER_FLG_MUTE_LEFT; - } - if (mixer_toggle_mute_right) - { - if (cdata.flags & SND_MIXER_FLG_MUTE_RIGHT) - cdata.flags &= ~SND_MIXER_FLG_MUTE_RIGHT; - else - cdata.flags |= SND_MIXER_FLG_MUTE_RIGHT; - } - mixer_toggle_mute_left = mixer_toggle_mute_right = 0; - if (mixer_toggle_record) - { - if (cdata.flags & SND_MIXER_FLG_RECORD) - cdata.flags &= ~SND_MIXER_FLG_RECORD; - else - cdata.flags |= SND_MIXER_FLG_RECORD; - } - mixer_toggle_record = 0; - - if (mixer_toggle_rec_left) - { - if (cdata.flags & SND_MIXER_FLG_RECORD_LEFT) - cdata.flags &= ~SND_MIXER_FLG_RECORD_LEFT; - else - cdata.flags |= SND_MIXER_FLG_RECORD_LEFT; - } - mixer_toggle_rec_left = 0; - - if (mixer_toggle_rec_right) - { - if (cdata.flags & SND_MIXER_FLG_RECORD_RIGHT) - cdata.flags &= ~SND_MIXER_FLG_RECORD_RIGHT; - else - cdata.flags |= SND_MIXER_FLG_RECORD_RIGHT; - } - mixer_toggle_rec_right = 0; - - if (mixer_route_ltor_in) - { - if (cdata.flags & SND_MIXER_FLG_LTOR_IN) - cdata.flags &= ~SND_MIXER_FLG_LTOR_IN; - else - cdata.flags |= SND_MIXER_FLG_LTOR_IN; -/* printf("state : \n %d \n",cdata.flags & SND_MIXER_FLG_LTOR_IN); -*/ } - mixer_route_ltor_in = 0; - - if (mixer_route_rtol_in) - { - if (cdata.flags & SND_MIXER_FLG_RTOL_IN) - cdata.flags &= ~SND_MIXER_FLG_RTOL_IN; - else - cdata.flags |= SND_MIXER_FLG_RTOL_IN; - } - mixer_route_rtol_in = 0; - - if (snd_mixer_channel_write(mixer_handle, channel_index, &cdata)<0) - mixer_abort (ERR_FCN, "snd_mixer_channel_write"); - } - - /* first, read values for the numbers to be displayed in - * specified EXACT mode - */ - if (snd_mixer_channel_read(mixer_handle, channel_index, &cdata)<0) - mixer_abort (ERR_FCN, "snd_mixer_ioctl_channel_read"); - vleft = cdata.left; - vright = cdata.right; - - /* then, always use percentage values for the bars. if we don't do - * this, we will see aliasing effects on specific circumstances. - * (actually they don't really dissapear, but they are transfered - * to bar<->smaller-scale ambiguities). - */ - if (mixer_exact) - { - i = 0; - if (snd_mixer_exact_mode(mixer_handle, 0)<0) - mixer_abort (ERR_FCN, "snd_mixer_exact"); - if (snd_mixer_channel_read(mixer_handle, channel_index, &cdata)<0) - mixer_abort (ERR_FCN, "snd_mixer_channel_read"); - } - - /* get channel bar position - */ - if (!mixer_cbar_get_pos (channel_index, &x, &y)) - return; - - /* channel bar name - */ - mixer_dc (channel_index == mixer_focus_channel ? DC_CBAR_FOCUS_LABEL : DC_CBAR_LABEL); - cinfo.name[8] = 0; - for (i = 0; i < 8; i++) - { - string[i] = ' '; - } - sprintf(string + (8 - strlen (cinfo.name)) / 2, "%s ", cinfo.name); - string[8] = 0; - mvaddstr (y, x, string); - y--; - - /* current channel values - */ - mixer_dc (DC_BACK); - mvaddstr (y, x, " "); - mixer_dc (DC_TEXT); - sprintf (string, "%d", vleft); - mvaddstr (y, x + 3 - strlen (string), string); - mixer_dc (DC_CBAR_FRAME); - mvaddch (y, x + 3, '<'); - mvaddch (y, x + 4, '>'); - mixer_dc (DC_TEXT); - sprintf (string, "%d", vright); - mvaddstr (y, x + 5, string); - y--; - - /* left/right bar - */ - mixer_dc (DC_CBAR_FRAME); - mvaddstr (y, x, " "); - mvaddch (y, x + 2, ACS_LLCORNER); - mvaddch (y, x + 3, ACS_HLINE); - mvaddch (y, x + 4, ACS_HLINE); - mvaddch (y, x + 5, ACS_LRCORNER); - y--; - for (i = 0; i < mixer_cbar_height; i++) - { - mvaddstr (y - i, x, " "); - mvaddch (y - i, x + 2, ACS_VLINE); - mvaddch (y - i, x + 5, ACS_VLINE); - } - string[2] = 0; - for (i = 0; i < mixer_cbar_height; i++) - { - int dc; - - if (i + 1 >= 0.8 * mixer_cbar_height) - dc = DC_CBAR_FULL_3; - else if (i + 1 >= 0.4 * mixer_cbar_height) - dc = DC_CBAR_FULL_2; - else - dc = DC_CBAR_FULL_1; - mvaddch (y, x + 3, mixer_dc (cdata.left > i * 100 / mixer_cbar_height ? dc : DC_CBAR_EMPTY)); - mvaddch (y, x + 4, mixer_dc (cdata.right > i * 100 / mixer_cbar_height ? dc : DC_CBAR_EMPTY)); - y--; - } - - /* muted? - */ - mixer_dc (DC_BACK); - mvaddstr (y, x, " "); - c = cinfo.caps & SND_MIXER_CINFO_CAP_MUTE ? '-' : ' '; - mixer_dc (DC_CBAR_FRAME); - mvaddch (y, x + 2, ACS_ULCORNER); - mvaddch (y, x + 3, mixer_dc (cdata.flags & SND_MIXER_FLG_MUTE_LEFT ? - DC_CBAR_MUTE : DC_CBAR_NOMUTE)); - mvaddch (y, x + 4, mixer_dc (cdata.flags & SND_MIXER_FLG_MUTE_RIGHT ? - DC_CBAR_MUTE : DC_CBAR_NOMUTE)); - mixer_dc (DC_CBAR_FRAME); - mvaddch (y, x + 5, ACS_URCORNER); - y--; - - /* record input? - */ - if (cdata.flags & SND_MIXER_FLG_RECORD) - { - mixer_dc (DC_CBAR_RECORD); - mvaddstr (y, x + 1, "RECORD"); - if (cdata.flags & SND_MIXER_FLG_RECORD_LEFT) - { - if (cdata.flags & SND_MIXER_FLG_LTOR_IN) - mvaddstr (y+2, x + 6, "L"); - else - mvaddstr (y+1, x + 1, "L"); - } - if (cdata.flags & SND_MIXER_FLG_RECORD_RIGHT) - { - if (cdata.flags & SND_MIXER_FLG_RTOL_IN) - mvaddstr (y+2, x + 1, "R"); - else - mvaddstr (y+1, x + 6, "R"); - } - } - else if (cinfo.caps & SND_MIXER_CINFO_CAP_RECORD) - for (i = 0; i < 6; i++) - mvaddch (y, x + 1 + i, mixer_dc (DC_CBAR_NORECORD)); - else - { - mixer_dc (DC_BACK); - mvaddstr (y, x, " "); - } - y--; + char string[64]; + char c; + snd_mixer_channel_info_t cinfo = + {0}; + snd_mixer_channel_t cdata = + {0}; + int vleft, vright; + int x, y, i; + + + /* set specified EXACT mode + */ + if (snd_mixer_exact_mode(mixer_handle, mixer_exact) < 0) + mixer_abort(ERR_FCN, "snd_mixer_exact"); + + /* set new channel indices and read info + */ + if (snd_mixer_channel_info(mixer_handle, channel_index, &cinfo) < 0) + mixer_abort(ERR_FCN, "snd_mixer_channel_info"); + + /* set new channel values + */ + if (channel_index == mixer_focus_channel && + (mixer_lvolume_delta || mixer_rvolume_delta || + mixer_toggle_mute_left || mixer_toggle_mute_right || + mixer_balance_volumes || + mixer_toggle_record || mixer_toggle_rec_left || + mixer_toggle_rec_right || + mixer_route_rtol_in || mixer_route_ltor_in)) { + if (snd_mixer_channel_read(mixer_handle, channel_index, &cdata) < 0) + mixer_abort(ERR_FCN, "snd_mixer_channel_read"); + + cdata.flags &= ~SND_MIXER_FLG_DECIBEL; + cdata.left = CLAMP(cdata.left + mixer_lvolume_delta, cinfo.min, cinfo.max); + cdata.right = CLAMP(cdata.right + mixer_rvolume_delta, cinfo.min, cinfo.max); + mixer_lvolume_delta = mixer_rvolume_delta = 0; + if (mixer_balance_volumes) { + cdata.left = (cdata.left + cdata.right) / 2; + cdata.right = cdata.left; + mixer_balance_volumes = 0; + } + if (mixer_toggle_mute_left) { + if (cdata.flags & SND_MIXER_FLG_MUTE_LEFT) + cdata.flags &= ~SND_MIXER_FLG_MUTE_LEFT; + else + cdata.flags |= SND_MIXER_FLG_MUTE_LEFT; + } + if (mixer_toggle_mute_right) { + if (cdata.flags & SND_MIXER_FLG_MUTE_RIGHT) + cdata.flags &= ~SND_MIXER_FLG_MUTE_RIGHT; + else + cdata.flags |= SND_MIXER_FLG_MUTE_RIGHT; + } + mixer_toggle_mute_left = mixer_toggle_mute_right = 0; + if (mixer_toggle_record) { + if (cdata.flags & SND_MIXER_FLG_RECORD) + cdata.flags &= ~SND_MIXER_FLG_RECORD; + else + cdata.flags |= SND_MIXER_FLG_RECORD; + } + mixer_toggle_record = 0; + + if (mixer_toggle_rec_left) { + if (cdata.flags & SND_MIXER_FLG_RECORD_LEFT) + cdata.flags &= ~SND_MIXER_FLG_RECORD_LEFT; + else + cdata.flags |= SND_MIXER_FLG_RECORD_LEFT; + } + mixer_toggle_rec_left = 0; + + if (mixer_toggle_rec_right) { + if (cdata.flags & SND_MIXER_FLG_RECORD_RIGHT) + cdata.flags &= ~SND_MIXER_FLG_RECORD_RIGHT; + else + cdata.flags |= SND_MIXER_FLG_RECORD_RIGHT; + } + mixer_toggle_rec_right = 0; + + if (mixer_route_ltor_in) { + if (cdata.flags & SND_MIXER_FLG_LTOR_IN) + cdata.flags &= ~SND_MIXER_FLG_LTOR_IN; + else + cdata.flags |= SND_MIXER_FLG_LTOR_IN; +/* printf("state : \n %d \n",cdata.flags & SND_MIXER_FLG_LTOR_IN); + */ + } + mixer_route_ltor_in = 0; + + if (mixer_route_rtol_in) { + if (cdata.flags & SND_MIXER_FLG_RTOL_IN) + cdata.flags &= ~SND_MIXER_FLG_RTOL_IN; + else + cdata.flags |= SND_MIXER_FLG_RTOL_IN; + } + mixer_route_rtol_in = 0; + + if (snd_mixer_channel_write(mixer_handle, channel_index, &cdata) < 0) + mixer_abort(ERR_FCN, "snd_mixer_channel_write"); + } + /* first, read values for the numbers to be displayed in + * specified EXACT mode + */ + if (snd_mixer_channel_read(mixer_handle, channel_index, &cdata) < 0) + mixer_abort(ERR_FCN, "snd_mixer_ioctl_channel_read"); + vleft = cdata.left; + vright = cdata.right; + + /* then, always use percentage values for the bars. if we don't do + * this, we will see aliasing effects on specific circumstances. + * (actually they don't really dissapear, but they are transfered + * to bar<->smaller-scale ambiguities). + */ + if (mixer_exact) { + i = 0; + if (snd_mixer_exact_mode(mixer_handle, 0) < 0) + mixer_abort(ERR_FCN, "snd_mixer_exact"); + if (snd_mixer_channel_read(mixer_handle, channel_index, &cdata) < 0) + mixer_abort(ERR_FCN, "snd_mixer_channel_read"); + } + /* get channel bar position + */ + if (!mixer_cbar_get_pos(channel_index, &x, &y)) + return; + + /* channel bar name + */ + mixer_dc(channel_index == mixer_focus_channel ? DC_CBAR_FOCUS_LABEL : DC_CBAR_LABEL); + cinfo.name[8] = 0; + for (i = 0; i < 8; i++) { + string[i] = ' '; + } + sprintf(string + (8 - strlen(cinfo.name)) / 2, "%s ", cinfo.name); + string[8] = 0; + mvaddstr(y, x, string); + y--; + + /* current channel values + */ + mixer_dc(DC_BACK); + mvaddstr(y, x, " "); + mixer_dc(DC_TEXT); + sprintf(string, "%d", vleft); + mvaddstr(y, x + 3 - strlen(string), string); + mixer_dc(DC_CBAR_FRAME); + mvaddch(y, x + 3, '<'); + mvaddch(y, x + 4, '>'); + mixer_dc(DC_TEXT); + sprintf(string, "%d", vright); + mvaddstr(y, x + 5, string); + y--; + + /* left/right bar + */ + mixer_dc(DC_CBAR_FRAME); + mvaddstr(y, x, " "); + mvaddch(y, x + 2, ACS_LLCORNER); + mvaddch(y, x + 3, ACS_HLINE); + mvaddch(y, x + 4, ACS_HLINE); + mvaddch(y, x + 5, ACS_LRCORNER); + y--; + for (i = 0; i < mixer_cbar_height; i++) { + mvaddstr(y - i, x, " "); + mvaddch(y - i, x + 2, ACS_VLINE); + mvaddch(y - i, x + 5, ACS_VLINE); + } + string[2] = 0; + for (i = 0; i < mixer_cbar_height; i++) { + int dc; + + if (i + 1 >= 0.8 * mixer_cbar_height) + dc = DC_CBAR_FULL_3; + else if (i + 1 >= 0.4 * mixer_cbar_height) + dc = DC_CBAR_FULL_2; + else + dc = DC_CBAR_FULL_1; + mvaddch(y, x + 3, mixer_dc(cdata.left > i * 100 / mixer_cbar_height ? dc : DC_CBAR_EMPTY)); + mvaddch(y, x + 4, mixer_dc(cdata.right > i * 100 / mixer_cbar_height ? dc : DC_CBAR_EMPTY)); + y--; + } + + /* muted? + */ + mixer_dc(DC_BACK); + mvaddstr(y, x, " "); + c = cinfo.caps & SND_MIXER_CINFO_CAP_MUTE ? '-' : ' '; + mixer_dc(DC_CBAR_FRAME); + mvaddch(y, x + 2, ACS_ULCORNER); + mvaddch(y, x + 3, mixer_dc(cdata.flags & SND_MIXER_FLG_MUTE_LEFT ? + DC_CBAR_MUTE : DC_CBAR_NOMUTE)); + mvaddch(y, x + 4, mixer_dc(cdata.flags & SND_MIXER_FLG_MUTE_RIGHT ? + DC_CBAR_MUTE : DC_CBAR_NOMUTE)); + mixer_dc(DC_CBAR_FRAME); + mvaddch(y, x + 5, ACS_URCORNER); + y--; + + /* record input? + */ + if (cdata.flags & SND_MIXER_FLG_RECORD) { + mixer_dc(DC_CBAR_RECORD); + mvaddstr(y, x + 1, "RECORD"); + if (cdata.flags & SND_MIXER_FLG_RECORD_LEFT) { + if (cdata.flags & SND_MIXER_FLG_LTOR_IN) + mvaddstr(y + 2, x + 6, "L"); + else + mvaddstr(y + 1, x + 1, "L"); + } + if (cdata.flags & SND_MIXER_FLG_RECORD_RIGHT) { + if (cdata.flags & SND_MIXER_FLG_RTOL_IN) + mvaddstr(y + 2, x + 1, "R"); + else + mvaddstr(y + 1, x + 6, "R"); + } + } else if (cinfo.caps & SND_MIXER_CINFO_CAP_RECORD) + for (i = 0; i < 6; i++) + mvaddch(y, x + 1 + i, mixer_dc(DC_CBAR_NORECORD)); + else { + mixer_dc(DC_BACK); + mvaddstr(y, x, " "); + } + y--; } -static void -mixer_update_cbars (void) +static void mixer_update_cbars(void) { - static int o_x = 0; - static int o_y = 0; - int i, x, y; - - if (!mixer_cbar_get_pos (mixer_focus_channel, &x, &y)) - { - if (mixer_focus_channel < mixer_first_vis_channel) - mixer_first_vis_channel = mixer_focus_channel; - else if (mixer_focus_channel >= mixer_first_vis_channel + mixer_n_vis_channels) - mixer_first_vis_channel = mixer_focus_channel - mixer_n_vis_channels + 1; - mixer_cbar_get_pos (mixer_focus_channel, &x, &y); - } - for (i = 0; i < mixer_n_vis_channels; i++) - mixer_update_cbar (i + mixer_first_vis_channel); - - /* draw focused cbar - */ - mixer_dc (DC_BACK); - mvaddstr (o_y, o_x, " "); - mvaddstr (o_y, o_x + 9, " "); - o_x = x - 1; - o_y = y; - mixer_dc (DC_FOCUS); - mvaddstr (o_y, o_x, "<"); - mvaddstr (o_y, o_x + 9, ">"); + static int o_x = 0; + static int o_y = 0; + int i, x, y; + + if (!mixer_cbar_get_pos(mixer_focus_channel, &x, &y)) { + if (mixer_focus_channel < mixer_first_vis_channel) + mixer_first_vis_channel = mixer_focus_channel; + else if (mixer_focus_channel >= mixer_first_vis_channel + mixer_n_vis_channels) + mixer_first_vis_channel = mixer_focus_channel - mixer_n_vis_channels + 1; + mixer_cbar_get_pos(mixer_focus_channel, &x, &y); + } + for (i = 0; i < mixer_n_vis_channels; i++) + mixer_update_cbar(i + mixer_first_vis_channel); + + /* draw focused cbar + */ + mixer_dc(DC_BACK); + mvaddstr(o_y, o_x, " "); + mvaddstr(o_y, o_x + 9, " "); + o_x = x - 1; + o_y = y; + mixer_dc(DC_FOCUS); + mvaddstr(o_y, o_x, "<"); + mvaddstr(o_y, o_x + 9, ">"); } -static void -mixer_draw_frame (void) +static void mixer_draw_frame(void) { - char string[128]; - int i; - int max_len; - - mixer_dc (DC_FRAME); - - /* corners - */ - mvaddch (0, 0, ACS_ULCORNER); - mvaddch (mixer_max_y - 1, 0, ACS_LLCORNER); - mvaddch (mixer_max_y - 1, mixer_max_x - 1, ACS_LRCORNER); - mvaddch (0, mixer_max_x - 1, ACS_URCORNER); - - /* lines - */ - for (i = 1; i < mixer_max_y - 1; i++) - { - mvaddch (i, 0, ACS_VLINE); - mvaddch (i, mixer_max_x - 1, ACS_VLINE); - } - for (i = 1; i < mixer_max_x - 1; i++) - { - mvaddch (0, i, ACS_HLINE); - mvaddch (mixer_max_y - 1, i, ACS_HLINE); - } - - /* program title - */ - sprintf (string, "%s %s", PRGNAME_UPPER, VERSION); - max_len = strlen (string); - mvaddch (0, mixer_max_x / 2 - max_len / 2 - 1, '['); - mvaddch (0, mixer_max_x / 2 - max_len / 2 + max_len, ']'); - mixer_dc (DC_TEXT); - mvaddstr (0, mixer_max_x / 2 - max_len / 2, string); - - /* card name - */ - mixer_dc (DC_PROMPT); - mvaddstr (1, 2, "Card:"); - mixer_dc (DC_TEXT); - sprintf (string, "%s", mixer_card_name); - max_len = mixer_max_x - 2 - 6 - 2; - if (strlen (string) > max_len) - string[max_len] = 0; - mvaddstr (1, 2 + 6, string); - - /* device name - */ - mixer_dc (DC_PROMPT); - mvaddstr (2, 2, "Chip: "); - mixer_dc (DC_TEXT); - sprintf (string, "%s", mixer_device_name); - max_len = mixer_max_x - 2 - 6 - 2; - if (strlen (string) > max_len) - string[max_len] = 0; - mvaddstr (2, 2 + 6, string); + char string[128]; + int i; + int max_len; + + mixer_dc(DC_FRAME); + + /* corners + */ + mvaddch(0, 0, ACS_ULCORNER); + mvaddch(mixer_max_y - 1, 0, ACS_LLCORNER); + mvaddch(mixer_max_y - 1, mixer_max_x - 1, ACS_LRCORNER); + mvaddch(0, mixer_max_x - 1, ACS_URCORNER); + + /* lines + */ + for (i = 1; i < mixer_max_y - 1; i++) { + mvaddch(i, 0, ACS_VLINE); + mvaddch(i, mixer_max_x - 1, ACS_VLINE); + } + for (i = 1; i < mixer_max_x - 1; i++) { + mvaddch(0, i, ACS_HLINE); + mvaddch(mixer_max_y - 1, i, ACS_HLINE); + } + + /* program title + */ + sprintf(string, "%s %s", PRGNAME_UPPER, VERSION); + max_len = strlen(string); + mvaddch(0, mixer_max_x / 2 - max_len / 2 - 1, '['); + mvaddch(0, mixer_max_x / 2 - max_len / 2 + max_len, ']'); + mixer_dc(DC_TEXT); + mvaddstr(0, mixer_max_x / 2 - max_len / 2, string); + + /* card name + */ + mixer_dc(DC_PROMPT); + mvaddstr(1, 2, "Card:"); + mixer_dc(DC_TEXT); + sprintf(string, "%s", mixer_card_name); + max_len = mixer_max_x - 2 - 6 - 2; + if (strlen(string) > max_len) + string[max_len] = 0; + mvaddstr(1, 2 + 6, string); + + /* device name + */ + mixer_dc(DC_PROMPT); + mvaddstr(2, 2, "Chip: "); + mixer_dc(DC_TEXT); + sprintf(string, "%s", mixer_device_name); + max_len = mixer_max_x - 2 - 6 - 2; + if (strlen(string) > max_len) + string[max_len] = 0; + mvaddstr(2, 2 + 6, string); } -static void -mixer_init (void) +static void mixer_init(void) { - static snd_mixer_info_t mixer_info = { 0 }; - static struct snd_ctl_hw_info hw_info; - void *ctl_handle; - - if (snd_ctl_open( &ctl_handle, card_id ) < 0 ) - mixer_abort (ERR_OPEN, "snd_ctl_open" ); - if (snd_ctl_hw_info( ctl_handle, &hw_info ) < 0 ) - mixer_abort (ERR_FCN, "snd_ctl_hw_info" ); - snd_ctl_close( ctl_handle ); - /* open mixer device - */ - if (snd_mixer_open( &mixer_handle, card_id, mixer_id ) < 0) - mixer_abort (ERR_OPEN, "snd_mixer_open" ); - - /* setup global variables - */ - if (snd_mixer_info( mixer_handle, &mixer_info) < 0) - mixer_abort (ERR_FCN, "snd_mixer_info" ); - mixer_n_channels = mixer_info.channels; - mixer_card_name = hw_info.name; - mixer_device_name = mixer_info.name; + static snd_mixer_info_t mixer_info = + {0}; + static struct snd_ctl_hw_info hw_info; + void *ctl_handle; + + if (snd_ctl_open(&ctl_handle, card_id) < 0) + mixer_abort(ERR_OPEN, "snd_ctl_open"); + if (snd_ctl_hw_info(ctl_handle, &hw_info) < 0) + mixer_abort(ERR_FCN, "snd_ctl_hw_info"); + snd_ctl_close(ctl_handle); + /* open mixer device + */ + if (snd_mixer_open(&mixer_handle, card_id, mixer_id) < 0) + mixer_abort(ERR_OPEN, "snd_mixer_open"); + + /* setup global variables + */ + if (snd_mixer_info(mixer_handle, &mixer_info) < 0) + mixer_abort(ERR_FCN, "snd_mixer_info"); + mixer_n_channels = mixer_info.channels; + mixer_card_name = hw_info.name; + mixer_device_name = mixer_info.name; } -static void -mixer_iteration_update(void *dummy, int channel) +static void mixer_iteration_update(void *dummy, int channel) { #if 0 - fprintf( stderr, "*** channel = %i\n", channel ); + fprintf(stderr, "*** channel = %i\n", channel); #endif - mixer_update_cbar(channel); - refresh (); + mixer_update_cbar(channel); + refresh(); } -static int -mixer_iteration (void) +static int mixer_iteration(void) { - snd_mixer_callbacks_t callbacks; - int key; - int finished = 0; - int mixer_fd; - fd_set in; - - bzero( &callbacks, sizeof( callbacks ) ); - callbacks.channel_was_changed = mixer_iteration_update; - mixer_fd = snd_mixer_file_descriptor( mixer_handle ); - while ( 1 ) { - FD_ZERO(&in); - FD_SET(fileno(stdin), &in); - FD_SET(mixer_fd, &in); - if (select(mixer_fd+1, &in, NULL, NULL, NULL)<=0) - return 1; - if (FD_ISSET(mixer_fd, &in)) - snd_mixer_read(mixer_handle, &callbacks); - if (FD_ISSET(fileno(stdin), &in)) break; - } - key = getch (); - switch (key) - { - case 27: /* Escape */ - finished = 1; - break; - case 9: /* Tab */ - mixer_exact = !mixer_exact; - break; - case KEY_RIGHT: - case 'n': - mixer_focus_channel += 1; - break; - case KEY_LEFT: - case 'p': - mixer_focus_channel -= 1; - break; - case KEY_PPAGE: - if (mixer_exact) - { - mixer_lvolume_delta = 8; - mixer_rvolume_delta = 8; - } - else - { - mixer_lvolume_delta = 10; - mixer_rvolume_delta = 10; - } - break; - case KEY_NPAGE: - if (mixer_exact) - { - mixer_lvolume_delta = -8; - mixer_rvolume_delta = -8; - } - else - { - mixer_lvolume_delta = -10; - mixer_rvolume_delta = -10; - } - break; - case KEY_BEG: - case KEY_HOME: - mixer_lvolume_delta = 512; - mixer_rvolume_delta = 512; - break; - case KEY_LL: - case KEY_END: - mixer_lvolume_delta = -512; - mixer_rvolume_delta = -512; - break; - case '+': - mixer_lvolume_delta = 1; - mixer_rvolume_delta = 1; - break; - case '-': - mixer_lvolume_delta = -1; - mixer_rvolume_delta = -1; - break; - case 'w': - case KEY_UP: - mixer_lvolume_delta = 1; - mixer_rvolume_delta = 1; - case 'W': - mixer_lvolume_delta += 1; - mixer_rvolume_delta += 1; - break; - case 'x': - case KEY_DOWN: - mixer_lvolume_delta = -1; - mixer_rvolume_delta = -1; - case 'X': - mixer_lvolume_delta += -1; - mixer_rvolume_delta += -1; - break; - case 'q': - mixer_lvolume_delta = 1; - case 'Q': - mixer_lvolume_delta += 1; - break; - case 'y': - case 'z': - mixer_lvolume_delta = -1; - case 'Y': - case 'Z': - mixer_lvolume_delta += -1; - break; - case 'e': - mixer_rvolume_delta = 1; - case 'E': - mixer_rvolume_delta += 1; - break; - case 'c': - mixer_rvolume_delta = -1; - case 'C': - mixer_rvolume_delta += -1; - break; - case 'm': - case 'M': - mixer_toggle_mute_left = 1; - mixer_toggle_mute_right = 1; - break; - case 'b': - case 'B': - case '=': - mixer_balance_volumes = 1; - break; - case '<': - case ',': - mixer_toggle_mute_left = 1; - break; - case '>': - case '.': - mixer_toggle_mute_right = 1; - break; - case 'R': - case 'r': - case 'L': - case 'l': - mixer_clear (); - break; - case ' ': - mixer_toggle_record = 1; - break; - case KEY_IC: - case ';': - mixer_toggle_rec_left = 1; - break; - case '\'': - case KEY_DC: - mixer_toggle_rec_right = 1; - break; - case '1': - mixer_route_rtol_in = 1; - break; - case '2': - mixer_route_ltor_in = 1; - break; - } - mixer_focus_channel = CLAMP (mixer_focus_channel, 0, mixer_n_channels - 1); - - return finished; + snd_mixer_callbacks_t callbacks; + int key; + int finished = 0; + int mixer_fd; + fd_set in; + + bzero(&callbacks, sizeof(callbacks)); + callbacks.channel_was_changed = mixer_iteration_update; + mixer_fd = snd_mixer_file_descriptor(mixer_handle); + while (1) { + FD_ZERO(&in); + FD_SET(fileno(stdin), &in); + FD_SET(mixer_fd, &in); + if (select(mixer_fd + 1, &in, NULL, NULL, NULL) <= 0) + return 1; + if (FD_ISSET(mixer_fd, &in)) + snd_mixer_read(mixer_handle, &callbacks); + if (FD_ISSET(fileno(stdin), &in)) + break; + } + key = getch(); + switch (key) { + case 27: /* Escape */ + finished = 1; + break; + case 9: /* Tab */ + mixer_exact = !mixer_exact; + break; + case KEY_RIGHT: + case 'n': + mixer_focus_channel += 1; + break; + case KEY_LEFT: + case 'p': + mixer_focus_channel -= 1; + break; + case KEY_PPAGE: + if (mixer_exact) { + mixer_lvolume_delta = 8; + mixer_rvolume_delta = 8; + } else { + mixer_lvolume_delta = 10; + mixer_rvolume_delta = 10; + } + break; + case KEY_NPAGE: + if (mixer_exact) { + mixer_lvolume_delta = -8; + mixer_rvolume_delta = -8; + } else { + mixer_lvolume_delta = -10; + mixer_rvolume_delta = -10; + } + break; + case KEY_BEG: + case KEY_HOME: + mixer_lvolume_delta = 512; + mixer_rvolume_delta = 512; + break; + case KEY_LL: + case KEY_END: + mixer_lvolume_delta = -512; + mixer_rvolume_delta = -512; + break; + case '+': + mixer_lvolume_delta = 1; + mixer_rvolume_delta = 1; + break; + case '-': + mixer_lvolume_delta = -1; + mixer_rvolume_delta = -1; + break; + case 'w': + case KEY_UP: + mixer_lvolume_delta = 1; + mixer_rvolume_delta = 1; + case 'W': + mixer_lvolume_delta += 1; + mixer_rvolume_delta += 1; + break; + case 'x': + case KEY_DOWN: + mixer_lvolume_delta = -1; + mixer_rvolume_delta = -1; + case 'X': + mixer_lvolume_delta += -1; + mixer_rvolume_delta += -1; + break; + case 'q': + mixer_lvolume_delta = 1; + case 'Q': + mixer_lvolume_delta += 1; + break; + case 'y': + case 'z': + mixer_lvolume_delta = -1; + case 'Y': + case 'Z': + mixer_lvolume_delta += -1; + break; + case 'e': + mixer_rvolume_delta = 1; + case 'E': + mixer_rvolume_delta += 1; + break; + case 'c': + mixer_rvolume_delta = -1; + case 'C': + mixer_rvolume_delta += -1; + break; + case 'm': + case 'M': + mixer_toggle_mute_left = 1; + mixer_toggle_mute_right = 1; + break; + case 'b': + case 'B': + case '=': + mixer_balance_volumes = 1; + break; + case '<': + case ',': + mixer_toggle_mute_left = 1; + break; + case '>': + case '.': + mixer_toggle_mute_right = 1; + break; + case 'R': + case 'r': + case 'L': + case 'l': + mixer_clear(); + break; + case ' ': + mixer_toggle_record = 1; + break; + case KEY_IC: + case ';': + mixer_toggle_rec_left = 1; + break; + case '\'': + case KEY_DC: + mixer_toggle_rec_right = 1; + break; + case '1': + mixer_route_rtol_in = 1; + break; + case '2': + mixer_route_ltor_in = 1; + break; + } + mixer_focus_channel = CLAMP(mixer_focus_channel, 0, mixer_n_channels - 1); + + return finished; } -static void -mixer_init_screen (void) +static void mixer_init_screen(void) { - signal (SIGWINCH, (void *)mixer_init_screen); - - getmaxyx (mixer_window, mixer_max_y, mixer_max_x); - mixer_clear (); - mixer_max_x = MAX (MIXER_MIN_X, mixer_max_x); - mixer_max_y = MAX (MIXER_MIN_Y, mixer_max_y); - mixer_clear (); - mixer_ofs_x = 2; - mixer_ofs_y = 2; - mixer_extra_space = 0; - mixer_n_vis_channels = MIN ((mixer_max_x - 2 * mixer_ofs_x + 1) / (9 + mixer_extra_space), - mixer_n_channels); - mixer_extra_space = ((mixer_max_x - 2 * mixer_ofs_x - 1 - mixer_n_vis_channels * 9.0) / - (mixer_n_vis_channels - 1)); - if (mixer_n_vis_channels < mixer_n_channels) - { - /* recalc - */ - mixer_extra_space = MAX (mixer_extra_space, 1); - mixer_n_vis_channels = MIN ((mixer_max_x - 2 * mixer_ofs_x + 1) / (9 + mixer_extra_space), - mixer_n_channels); - mixer_extra_space = ((mixer_max_x - 2 * mixer_ofs_x - 1 - mixer_n_vis_channels * 9.0) / - (mixer_n_vis_channels - 1)); - } - mixer_first_vis_channel = 0; - mixer_cbar_height = 10 + MAX (0, (mixer_max_y - MIXER_MIN_Y - 1)) / 2; + signal(SIGWINCH, (void *) mixer_init_screen); + + getmaxyx(mixer_window, mixer_max_y, mixer_max_x); + mixer_clear(); + mixer_max_x = MAX(MIXER_MIN_X, mixer_max_x); + mixer_max_y = MAX(MIXER_MIN_Y, mixer_max_y); + mixer_clear(); + mixer_ofs_x = 2; + mixer_ofs_y = 2; + mixer_extra_space = 0; + mixer_n_vis_channels = MIN((mixer_max_x - 2 * mixer_ofs_x + 1) / (9 + mixer_extra_space), + mixer_n_channels); + mixer_extra_space = ((mixer_max_x - 2 * mixer_ofs_x - 1 - mixer_n_vis_channels * 9.0) / + (mixer_n_vis_channels - 1)); + if (mixer_n_vis_channels < mixer_n_channels) { + /* recalc + */ + mixer_extra_space = MAX(mixer_extra_space, 1); + mixer_n_vis_channels = MIN((mixer_max_x - 2 * mixer_ofs_x + 1) / (9 + mixer_extra_space), + mixer_n_channels); + mixer_extra_space = ((mixer_max_x - 2 * mixer_ofs_x - 1 - mixer_n_vis_channels * 9.0) / + (mixer_n_vis_channels - 1)); + } + mixer_first_vis_channel = 0; + mixer_cbar_height = 10 + MAX(0, (mixer_max_y - MIXER_MIN_Y - 1)) / 2; } -static void -mixer_signal_handler (int signal) +static void mixer_signal_handler(int signal) { - mixer_abort (ERR_SIGNAL, sys_siglist[signal]); + mixer_abort(ERR_SIGNAL, sys_siglist[signal]); } -int -main (int argc, - char **argv) +int main(int argc, + char **argv) { - int opt; - - /* parse args - */ - do - { - opt = getopt (argc, argv, "c:m:ehg" ); - switch (opt) - { - case '?': - case 'h': - fprintf (stderr, "%s %s\n", PRGNAME_UPPER, VERSION); - fprintf (stderr, "Usage: %s [-e] [-c ] [-m ]\n", PRGNAME, snd_cards() ); - mixer_abort (ERR_NONE, ""); - case 'c': - card_id = snd_card_name( optarg ); - break; - case 'e': - mixer_exact = !mixer_exact; - break; - case 'g': - mixer_do_color = !mixer_do_color; - break; - case 'm': - mixer_id = CLAMP (optarg[0], '0', '1') - '0'; - break; - } - } - while (opt > 0); - - /* initialize mixer - */ - mixer_init (); - - /* setup signal handlers - */ - signal (SIGINT, mixer_signal_handler); - signal (SIGTRAP, mixer_signal_handler); - signal (SIGABRT, mixer_signal_handler); - signal (SIGQUIT, mixer_signal_handler); - signal (SIGBUS, mixer_signal_handler); - signal (SIGSEGV, mixer_signal_handler); - signal (SIGPIPE, mixer_signal_handler); - signal (SIGTERM, mixer_signal_handler); - - /* initialize ncurses - */ - mixer_window = initscr (); - if (mixer_do_color) - mixer_do_color = has_colors (); - mixer_init_draw_contexts (); - mixer_init_screen (); - if (mixer_max_x < MIXER_MIN_X || - mixer_max_y < MIXER_MIN_Y) - mixer_abort (ERR_WINSIZE, ""); - - /* react on key presses - * and draw window - */ - keypad (mixer_window, TRUE); - leaveok (mixer_window, TRUE); - cbreak (); - noecho (); - do - { - mixer_update_cbars (); - mixer_draw_frame (); - refresh (); - } - while (!mixer_iteration ()); - - mixer_abort (ERR_NONE, ""); + int opt; + + /* parse args + */ + do { + opt = getopt(argc, argv, "c:m:ehg"); + switch (opt) { + case '?': + case 'h': + fprintf(stderr, "%s %s\n", PRGNAME_UPPER, VERSION); + fprintf(stderr, "Usage: %s [-e] [-c ] [-m ]\n", PRGNAME, snd_cards()); + mixer_abort(ERR_NONE, ""); + case 'c': + card_id = snd_card_name(optarg); + break; + case 'e': + mixer_exact = !mixer_exact; + break; + case 'g': + mixer_do_color = !mixer_do_color; + break; + case 'm': + mixer_id = CLAMP(optarg[0], '0', '1') - '0'; + break; + } + } + while (opt > 0); + + /* initialize mixer + */ + mixer_init(); + + /* setup signal handlers + */ + signal(SIGINT, mixer_signal_handler); + signal(SIGTRAP, mixer_signal_handler); + signal(SIGABRT, mixer_signal_handler); + signal(SIGQUIT, mixer_signal_handler); + signal(SIGBUS, mixer_signal_handler); + signal(SIGSEGV, mixer_signal_handler); + signal(SIGPIPE, mixer_signal_handler); + signal(SIGTERM, mixer_signal_handler); + + /* initialize ncurses + */ + mixer_window = initscr(); + if (mixer_do_color) + mixer_do_color = has_colors(); + mixer_init_draw_contexts(); + mixer_init_screen(); + if (mixer_max_x < MIXER_MIN_X || + mixer_max_y < MIXER_MIN_Y) + mixer_abort(ERR_WINSIZE, ""); + + /* react on key presses + * and draw window + */ + keypad(mixer_window, TRUE); + leaveok(mixer_window, TRUE); + cbreak(); + noecho(); + do { + mixer_update_cbars(); + mixer_draw_frame(); + refresh(); + } + while (!mixer_iteration()); + + mixer_abort(ERR_NONE, ""); }; diff --git a/amixer/amain.cpp b/amixer/amain.cpp index e98b593..8c9b67a 100644 --- a/amixer/amain.cpp +++ b/amixer/amain.cpp @@ -27,17 +27,17 @@ char *rc_file(void) { - static char rc_path[1024]; - char *p; - - p = getenv("HOME"); - if (p) { - sprintf(rc_path, "%s/%s", p, MIXER_RC); - } else { - printf("Error reading HOME env. variable\n"); - return NULL; - } - return rc_path; + static char rc_path[1024]; + char *p; + + p = getenv("HOME"); + if (p) { + sprintf(rc_path, "%s/%s", p, MIXER_RC); + } else { + printf("Error reading HOME env. variable\n"); + return NULL; + } + return rc_path; } void copyright() @@ -47,72 +47,74 @@ void copyright() void usage() { - printf("\n" - "Usage: amixer [-c card] [-d dev] device [vol|L:R] [mute|unmute] [rec|norec]\n\n" - " amixer [-p path] -r\tRead %s or settings\n" - " amixer -w\t\tWrite %s settings\n" - " amixer -q ...\t\tQuiet mode\n" - " amixer -h\t\tHelp\n\n" - "Example: amixer line-out 80:50 unmute rec\n\n", rc_file(), rc_file()); + printf("\n" + "Usage: amixer [-c card] [-d dev] device [vol|L:R] [mute|unmute] [rec|norec]\n\n" + " amixer [-p path] -r\tRead %s or settings\n" + " amixer -w\t\tWrite %s settings\n" + " amixer -q ...\t\tQuiet mode\n" + " amixer -h\t\tHelp\n\n" + "Example: amixer line-out 80:50 unmute rec\n\n", rc_file(), rc_file()); } -void read_config(Mixer *mix, const char *path) +void read_config(Mixer * mix, const char *path) { - FILE *rc; - char buf[1024]; - int opt1; - int opt2; - int left, right; - int dev; - int count = 0; - int flags; - - if ((rc = fopen(path ? path : rc_file(), "r")) == NULL) { - printf("Mixer values not read\n"); - return; - } - while (!feof(rc) && fgets(buf, 1024, rc)) { - count++; - if (buf[0] == '\n') - continue; - if (buf[0] == '#' || strlen(buf) == 0) - continue; - - if (sscanf(buf, "%d %d:%d %d %d\n", &dev, &left, &right, &opt1, &opt2) != 5) { - printf("WARNING: unable to make out line %d of .rc file -> \"%s\"\n", count, buf); - continue; + FILE *rc; + char buf[1024]; + int opt1; + int opt2; + int left, right; + int dev; + int count = 0; + int flags; + + if ((rc = fopen(path ? path : rc_file(), "r")) == NULL) { + printf("Mixer values not read\n"); + return; } - flags = 0; - if (opt1) flags |= E_MIXER_MUTE; - if (opt2) flags |= E_MIXER_RECORD; - // Set mixer settings - mix->DeviceSet(dev); - mix->Write(left, right, flags); - } - - fclose(rc); - return; + while (!feof(rc) && fgets(buf, 1024, rc)) { + count++; + if (buf[0] == '\n') + continue; + if (buf[0] == '#' || strlen(buf) == 0) + continue; + + if (sscanf(buf, "%d %d:%d %d %d\n", &dev, &left, &right, &opt1, &opt2) != 5) { + printf("WARNING: unable to make out line %d of .rc file -> \"%s\"\n", count, buf); + continue; + } + flags = 0; + if (opt1) + flags |= E_MIXER_MUTE; + if (opt2) + flags |= E_MIXER_RECORD; + // Set mixer settings + mix->DeviceSet(dev); + mix->Write(left, right, flags); + } + + fclose(rc); + return; } -void write_config(Mixer *mix) +void write_config(Mixer * mix) { - FILE *rc; - int32 left, right, flags; + FILE *rc; + int32 left, right, flags; - if ((rc = fopen(rc_file(), "w+")) == NULL) { - printf("Mixer values not written\n"); + if ((rc = fopen(rc_file(), "w+")) == NULL) { + printf("Mixer values not written\n"); + return; + } + fprintf(rc, "# CLI ALSA mixer settings file. Autogenerated\n" + "# Modify at your own risk :)\n\n"); + for (int i = 0; i < mix->NumDevices(); i++) { + mix->DeviceSet(i); + mix->Read(&left, &right, &flags); + fprintf(rc, "%d %d:%d %d %d\n", i, mix->Left(), mix->Right(), flags & E_MIXER_MUTE ? 1 : 0, flags & E_MIXER_RECORD ? 1 : 0); + } + fclose(rc); return; - } - fprintf(rc, "# CLI ALSA mixer settings file. Autogenerated\n" - "# Modify at your own risk :)\n\n"); - for (int i=0; i < mix->NumDevices(); i++) { - mix->DeviceSet(i); - mix->Read(&left, &right, &flags); - fprintf(rc, "%d %d:%d %d %d\n", i, mix->Left(), mix->Right(), flags & E_MIXER_MUTE ? 1 : 0, flags & E_MIXER_RECORD ? 1 : 0); - } - fclose(rc); - return; } int main(int argc, char **argv) @@ -123,30 +125,30 @@ int main(int argc, char **argv) int32 left_dB, right_dB; int32 cur_left, cur_right, cur_flags; int count, quiet = 0; - int i,add; + int i, add; int pathind = 0; Mixer *the_mixer; exact = mute = rec = norec = unmute = device_index = left = right = -1; left_dB = right_dB = -1; - + for (add = 0; add + 1 < argc; i++) { - if (!strcmp(argv[add+1],"--help")) { + if (!strcmp(argv[add + 1], "--help")) { usage(); return 0; } - if (argv[add+1][0] == '-') { + if (argv[add + 1][0] == '-') { add++; if (argv[add][1] == 'c') { card = snd_card_name(argv[++add]); if (card < 0) { - fprintf(stderr, "Invalid card: %s\n",argv[2]); + fprintf(stderr, "Invalid card: %s\n", argv[2]); exit(1); } } else if (argv[add][1] == 'd') { device = atoi(argv[++add]); if (device < 0 || device > 128) { - fprintf(stderr, "Invalid device: %s\n",argv[2]); + fprintf(stderr, "Invalid device: %s\n", argv[2]); exit(1); } } else if (argv[add][1] == 'h') { @@ -155,80 +157,88 @@ int main(int argc, char **argv) } else if (argv[add][1] == 'p') { pathind = ++add; } else if (argv[add][1] == 'r') { - the_mixer = new Mixer(card,device); - if (the_mixer && the_mixer->Init()) read_config(the_mixer, pathind ? argv[pathind] : (const char *)NULL); - delete the_mixer; - return 0; + the_mixer = new Mixer(card, device); + if (the_mixer && the_mixer->Init()) + read_config(the_mixer, pathind ? argv[pathind] : (const char *) NULL); + delete the_mixer; + return 0; } else if (argv[add][1] == 'w') { - the_mixer = new Mixer(card,device); - if (the_mixer && the_mixer->Init()) write_config(the_mixer); + the_mixer = new Mixer(card, device); + if (the_mixer && the_mixer->Init()) + write_config(the_mixer); delete the_mixer; return 0; } else if (argv[add][1] == 'q') { quiet = 1; } else { - fprintf(stderr, "Invalid option: %s\n", argv[add]+1); + fprintf(stderr, "Invalid option: %s\n", argv[add] + 1); return 1; } } else { break; } - } - for (i=1+add; i < argc; i++) { - if (strcmp(argv[i],"exact")==0) { + } + for (i = 1 + add; i < argc; i++) { + if (strcmp(argv[i], "exact") == 0) { exact = 1; - } else if (strcmp(argv[i],"mute")==0) { + } else if (strcmp(argv[i], "mute") == 0) { mute = 1; - } else if (strcmp(argv[i],"unmute")==0) { + } else if (strcmp(argv[i], "unmute") == 0) { unmute = 1; - } else if (strcmp(argv[i],"rec")==0) { + } else if (strcmp(argv[i], "rec") == 0) { rec = 1; - } else if (strcmp(argv[i],"norec")==0) { + } else if (strcmp(argv[i], "norec") == 0) { norec = 1; - } else if (sscanf(argv[i], "%d:%d", &left, &right)==2) { - } else if (sscanf(argv[i], "%d", &left)==1) { + } else if (sscanf(argv[i], "%d:%d", &left, &right) == 2) { + } else if (sscanf(argv[i], "%d", &left) == 1) { right = left; } else { strncpy(device_name, argv[i], sizeof(device_name)); - device_name[sizeof(device_name)-1] = 0; + device_name[sizeof(device_name) - 1] = 0; } } - Mixer mixer(card,device); - + Mixer mixer(card, device); + if (mixer.Init() == false) { fprintf(stderr, "Failed to open mixer device\n"); return 1; } count = mixer.NumDevices(); - - for (i=0; i < count; i++) { + + for (i = 0; i < count; i++) { mixer.DeviceSet(i); - if (strcasecmp(device_name, mixer.Name())==0) - device_index = i; + if (strcasecmp(device_name, mixer.Name()) == 0) + device_index = i; } - if ( !quiet ) + if (!quiet) copyright(); if (device_index >= 0) { mixer.DeviceSet(device_index); mixer.Read(&cur_left, &cur_right, &cur_flags); - if (left >= 0) cur_left = left; - if (right >= 0) cur_right = right; - if (rec == 1) cur_flags |= E_MIXER_RECORD; - else if (norec == 1) cur_flags &= ~E_MIXER_RECORD; - if (mute == 1) cur_flags |= E_MIXER_MUTE; - else if (unmute == 1) cur_flags &= ~E_MIXER_MUTE; + if (left >= 0) + cur_left = left; + if (right >= 0) + cur_right = right; + if (rec == 1) + cur_flags |= E_MIXER_RECORD; + else if (norec == 1) + cur_flags &= ~E_MIXER_RECORD; + if (mute == 1) + cur_flags |= E_MIXER_MUTE; + else if (unmute == 1) + cur_flags &= ~E_MIXER_MUTE; if (left != -1 || rec != -1 || norec != -1 || mute != -1 || unmute != -1) { mixer.Write(cur_left, cur_right, cur_flags); } - if ( !quiet ) { + if (!quiet) { printf("%-16s", mixer.Name()); mixer.Read(&left, &right, &flags); mixer.Read_dB(&left_dB, &right_dB); printf("%-3d%% (%6.2fdB) : %-3d%% (%6.2fdB) %s %s\n\n", - left, ((float)left_dB) / 100.0, - right, ((float)right_dB) / 100.0, - (flags & E_MIXER_MUTE) ? "Mute" : " ", - (flags & E_MIXER_RECORD) ? "Rec" : " " ); + left, ((float) left_dB) / 100.0, + right, ((float) right_dB) / 100.0, + (flags & E_MIXER_MUTE) ? "Mute" : " ", + (flags & E_MIXER_RECORD) ? "Rec" : " "); } } else { if (quiet) { @@ -236,19 +246,19 @@ int main(int argc, char **argv) return 1; } if (strlen(device_name)) - printf("Device not found: %s\n\n", device_name); - for (i=0; i < count; i++) { + printf("Device not found: %s\n\n", device_name); + for (i = 0; i < count; i++) { mixer.DeviceSet(i); printf("%-16s", mixer.Name()); mixer.Read(&left, &right, &flags); mixer.Read_dB(&left_dB, &right_dB); - printf("%-3d%% (%6.2fdB) : %-3d%% (%6.2fdB) %s %s\n", - left, ((float)left_dB)/100.0, right, ((float)right_dB)/100.0, + printf("%-3d%% (%6.2fdB) : %-3d%% (%6.2fdB) %s %s\n", + left, ((float) left_dB) / 100.0, right, ((float) right_dB) / 100.0, (flags & E_MIXER_MUTE) ? "Mute" : " ", (flags & E_MIXER_RECORD) ? "Rec" : " "); } return 0; } - + return 0; } diff --git a/aplay/aplay.c b/aplay/aplay.c index f667b26..3b98981 100644 --- a/aplay/aplay.c +++ b/aplay/aplay.c @@ -1,8 +1,8 @@ /* * aplay.c - plays and records * - * CREATIVE LABS VOICE-files - * Microsoft WAVE-files + * CREATIVE LABS VOICE-files + * Microsoft WAVE-files * SPARC AUDIO .AU-files * Raw Data * @@ -54,8 +54,8 @@ void *pcm_handle; struct snd_pcm_playback_info pinfo; struct snd_pcm_record_info rinfo; snd_pcm_format_t rformat, format; -int timelimit = 0; -int quiet_mode = 0; +int timelimit = 0; +int quiet_mode = 0; int verbose_mode = 0; int active_format = FORMAT_DEFAULT; int direction = SND_PCM_OPEN_PLAYBACK; @@ -67,368 +67,369 @@ int vocmajor, vocminor; /* needed prototypes */ -static void playback( char *filename ); -static void record( char *filename ); +static void playback(char *filename); +static void record(char *filename); -static void begin_voc( int fd, u_long count ); -static void end_voc( int fd ); -static void begin_wave( int fd, u_long count ); -static void end_wave( int fd ); -static void begin_au( int fd, u_long count ); +static void begin_voc(int fd, u_long count); +static void end_voc(int fd); +static void begin_wave(int fd, u_long count); +static void end_wave(int fd); +static void begin_au(int fd, u_long count); struct fmt_record { - void (*start)(int fd, u_long count); - void (*end) (int fd); - char *what; + void (*start) (int fd, u_long count); + void (*end) (int fd); + char *what; } fmt_rec_table[] = { - { NULL, end_wave, "raw data" }, - { begin_voc, end_voc, "VOC" }, - { begin_wave, end_wave, "WAVE" }, - { begin_au, end_wave, "Sparc Audio" } -}; + { NULL, end_wave, "raw data" }, + { begin_voc, end_voc, "VOC" }, + { begin_wave, end_wave, "WAVE" }, + { begin_au, end_wave, "Sparc Audio" } +}; -static char *get_format( int format ) +static char *get_format(int format) { - static char *formats[] = { - "Mu-Law", - "A-Law", - "Ima-ADPCM", - "Unsigned 8-bit", - "Signed 16-bit Little Endian", - "Signed 16-bit Big Endian", - "Signed 8-bit", - "Unsigned 16-bit Little Endian", - "Unsigned 16-bit Big Endian", - "MPEG", - "GSM" - }; - if ( format < 0 || format > SND_PCM_SFMT_GSM ) - return "Unknown"; - return formats[ format ]; + static char *formats[] = + { + "Mu-Law", + "A-Law", + "Ima-ADPCM", + "Unsigned 8-bit", + "Signed 16-bit Little Endian", + "Signed 16-bit Big Endian", + "Signed 8-bit", + "Unsigned 16-bit Little Endian", + "Unsigned 16-bit Big Endian", + "MPEG", + "GSM" + }; + if (format < 0 || format > SND_PCM_SFMT_GSM) + return "Unknown"; + return formats[format]; } -static void check_new_format( snd_pcm_format_t *format ) +static void check_new_format(snd_pcm_format_t * format) { - if ( direction == SND_PCM_OPEN_PLAYBACK ) { - if ( pinfo.min_rate > format -> rate || pinfo.max_rate < format -> rate ) { - fprintf( stderr, "%s: unsupported rate %iHz for playback (valid range is %iHz-%iHz)\n", command, format -> rate, pinfo.min_rate, pinfo.max_rate ); - exit( 1 ); - } - if ( !(pinfo.formats & (1 << format -> format)) ) { - fprintf( stderr, "%s: requested format %s isn't supported with hardware\n", command, get_format( format -> format ) ); - exit( 1 ); - } - } else { - if ( rinfo.min_rate > format -> rate || rinfo.max_rate < format -> rate ) { - fprintf( stderr, "%s: unsupported rate %iHz for record (valid range is %iHz-%iHz)\n", command, format -> rate, rinfo.min_rate, rinfo.max_rate ); - exit( 1 ); - } - if ( !(rinfo.formats & (1 << format -> format)) ) { - fprintf( stderr, "%s: requested format %s isn't supported with hardware\n", command, get_format( rformat.format ) ); - exit( 1 ); - } - } + if (direction == SND_PCM_OPEN_PLAYBACK) { + if (pinfo.min_rate > format->rate || pinfo.max_rate < format->rate) { + fprintf(stderr, "%s: unsupported rate %iHz for playback (valid range is %iHz-%iHz)\n", command, format->rate, pinfo.min_rate, pinfo.max_rate); + exit(1); + } + if (!(pinfo.formats & (1 << format->format))) { + fprintf(stderr, "%s: requested format %s isn't supported with hardware\n", command, get_format(format->format)); + exit(1); + } + } else { + if (rinfo.min_rate > format->rate || rinfo.max_rate < format->rate) { + fprintf(stderr, "%s: unsupported rate %iHz for record (valid range is %iHz-%iHz)\n", command, format->rate, rinfo.min_rate, rinfo.max_rate); + exit(1); + } + if (!(rinfo.formats & (1 << format->format))) { + fprintf(stderr, "%s: requested format %s isn't supported with hardware\n", command, get_format(rformat.format)); + exit(1); + } + } } -static void usage( char *command ) +static void usage(char *command) { - fprintf (stderr, -"Usage: %s [switches] [filename] ...\n" -"Available switches:\n" -"\n" -" -h,--help help\n" -" -V,--version print current version\n" -" -l list all soundcards and digital audio devices\n" -" -c select card # or card id (1-%i), defaults to 1\n" -" -d select device #, defaults to 0\n" -" -q quiet mode\n" -" -v file format Voc\n" -" -u file format Sparc Audio (.au)\n" -" -w file format Wave\n" -" -r file format Raw\n" -" -S stereo\n" -" -t timelimit (seconds)\n" -" -s speed (Hz)\n" -" -b sample size (8,16 bits)\n" -" -m set CD-ROM quality (44100Hz,stereo,16-bit linear)\n" -" -M set DAT quality (48000Hz,stereo,16-bit linear)\n" -" -p compression type (alaw, ulaw, adpcm)\n" -, command, snd_cards() ); + fprintf(stderr, + "Usage: %s [switches] [filename] ...\n" + "Available switches:\n" + "\n" + " -h,--help help\n" + " -V,--version print current version\n" + " -l list all soundcards and digital audio devices\n" + " -c select card # or card id (1-%i), defaults to 1\n" + " -d select device #, defaults to 0\n" + " -q quiet mode\n" + " -v file format Voc\n" + " -u file format Sparc Audio (.au)\n" + " -w file format Wave\n" + " -r file format Raw\n" + " -S stereo\n" + " -t timelimit (seconds)\n" + " -s speed (Hz)\n" + " -b sample size (8,16 bits)\n" + " -m set CD-ROM quality (44100Hz,stereo,16-bit linear)\n" + " -M set DAT quality (48000Hz,stereo,16-bit linear)\n" + " -p compression type (alaw, ulaw, adpcm)\n" + ,command, snd_cards()); } -static void device_list( void ) +static void device_list(void) { - void *handle; - int card, err, dev, idx; - unsigned int mask; - struct snd_ctl_hw_info info; - snd_pcm_info_t pcminfo; - snd_pcm_playback_info_t playinfo; - snd_pcm_record_info_t recinfo; - - mask = snd_cards_mask(); - if ( !mask ) { - printf( "%s: no soundcards found...\n", command ); - return; - } - for ( card = 0; card < SND_CARDS; card++ ) { - if ( !(mask & (1 << card)) ) continue; - if ( (err = snd_ctl_open( &handle, card )) < 0 ) { - printf( "Error: control open (%i): %s\n", card, snd_strerror( err ) ); - continue; - } - if ( (err = snd_ctl_hw_info( handle, &info )) < 0 ) { - printf( "Error: control hardware info (%i): %s\n", card, snd_strerror( err ) ); - snd_ctl_close( handle ); - continue; - } - for ( dev = 0; dev < info.pcmdevs; dev++ ) { - if ( (err = snd_ctl_pcm_info( handle, dev, &pcminfo )) < 0 ) { - printf( "Error: control digital audio info (%i): %s\n", card, snd_strerror( err ) ); - continue; - } - printf( "%s: %i [%s] / #%i: %s\n", - info.name, - card + 1, - info.id, - dev, - pcminfo.name ); - printf( " Directions: %s%s%s\n", - pcminfo.flags & SND_PCM_INFO_PLAYBACK ? "playback " : "", - pcminfo.flags & SND_PCM_INFO_RECORD ? "record " : "", - pcminfo.flags & SND_PCM_INFO_DUPLEX ? "duplex " : "" ); - if ( (err = snd_ctl_pcm_playback_info( handle, dev, &playinfo )) < 0 ) { - printf( "Error: control digital audio playback info (%i): %s\n", card, snd_strerror( err ) ); - continue; - } - if ( pcminfo.flags & SND_PCM_INFO_PLAYBACK ) { - printf( " Playback:\n" ); - printf( " Rate range: %iHz-%iHz\n", playinfo.min_rate, playinfo.max_rate ); - printf( " Voices range: %i-%i\n", playinfo.min_channels, playinfo.max_channels ); - printf( " Formats:\n" ); - for ( idx = 0; idx < SND_PCM_SFMT_GSM; idx++ ) { - if ( playinfo.formats & (1 << idx) ) - printf( " %s%s\n", get_format( idx ), - playinfo.hw_formats & (1 << idx) ? " [hardware]" : "" ); - } - if ( (err = snd_ctl_pcm_record_info( handle, dev, &recinfo )) < 0 ) { - printf( "Error: control digital audio record info (%i): %s\n", card, snd_strerror( err ) ); - continue; - } - } - if ( pcminfo.flags & SND_PCM_INFO_RECORD ) { - printf( " Record:\n" ); - printf( " Rate range: %iHz-%iHz\n", recinfo.min_rate, recinfo.max_rate ); - printf( " Voices range: %i-%i\n", recinfo.min_channels, recinfo.max_channels ); - printf( " Formats:\n" ); - for ( idx = 0; idx < SND_PCM_SFMT_GSM; idx++ ) { - if ( recinfo.formats & (1 << idx) ) - printf( " %s%s\n", get_format( idx ), - recinfo.hw_formats & (1 << idx) ? " [hardware]" : "" ); - } - } - } - snd_ctl_close( handle ); - } + void *handle; + int card, err, dev, idx; + unsigned int mask; + struct snd_ctl_hw_info info; + snd_pcm_info_t pcminfo; + snd_pcm_playback_info_t playinfo; + snd_pcm_record_info_t recinfo; + + mask = snd_cards_mask(); + if (!mask) { + printf("%s: no soundcards found...\n", command); + return; + } + for (card = 0; card < SND_CARDS; card++) { + if (!(mask & (1 << card))) + continue; + if ((err = snd_ctl_open(&handle, card)) < 0) { + printf("Error: control open (%i): %s\n", card, snd_strerror(err)); + continue; + } + if ((err = snd_ctl_hw_info(handle, &info)) < 0) { + printf("Error: control hardware info (%i): %s\n", card, snd_strerror(err)); + snd_ctl_close(handle); + continue; + } + for (dev = 0; dev < info.pcmdevs; dev++) { + if ((err = snd_ctl_pcm_info(handle, dev, &pcminfo)) < 0) { + printf("Error: control digital audio info (%i): %s\n", card, snd_strerror(err)); + continue; + } + printf("%s: %i [%s] / #%i: %s\n", + info.name, + card + 1, + info.id, + dev, + pcminfo.name); + printf(" Directions: %s%s%s\n", + pcminfo.flags & SND_PCM_INFO_PLAYBACK ? "playback " : "", + pcminfo.flags & SND_PCM_INFO_RECORD ? "record " : "", + pcminfo.flags & SND_PCM_INFO_DUPLEX ? "duplex " : ""); + if ((err = snd_ctl_pcm_playback_info(handle, dev, &playinfo)) < 0) { + printf("Error: control digital audio playback info (%i): %s\n", card, snd_strerror(err)); + continue; + } + if (pcminfo.flags & SND_PCM_INFO_PLAYBACK) { + printf(" Playback:\n"); + printf(" Rate range: %iHz-%iHz\n", playinfo.min_rate, playinfo.max_rate); + printf(" Voices range: %i-%i\n", playinfo.min_channels, playinfo.max_channels); + printf(" Formats:\n"); + for (idx = 0; idx < SND_PCM_SFMT_GSM; idx++) { + if (playinfo.formats & (1 << idx)) + printf(" %s%s\n", get_format(idx), + playinfo.hw_formats & (1 << idx) ? " [hardware]" : ""); + } + if ((err = snd_ctl_pcm_record_info(handle, dev, &recinfo)) < 0) { + printf("Error: control digital audio record info (%i): %s\n", card, snd_strerror(err)); + continue; + } + } + if (pcminfo.flags & SND_PCM_INFO_RECORD) { + printf(" Record:\n"); + printf(" Rate range: %iHz-%iHz\n", recinfo.min_rate, recinfo.max_rate); + printf(" Voices range: %i-%i\n", recinfo.min_channels, recinfo.max_channels); + printf(" Formats:\n"); + for (idx = 0; idx < SND_PCM_SFMT_GSM; idx++) { + if (recinfo.formats & (1 << idx)) + printf(" %s%s\n", get_format(idx), + recinfo.hw_formats & (1 << idx) ? " [hardware]" : ""); + } + } + } + snd_ctl_close(handle); + } } -static void version( void ) +static void version(void) { - printf( "%s: version " SND_UTIL_VERSION " by Jaroslav Kysela \n", command ); + printf("%s: version " SND_UTIL_VERSION " by Jaroslav Kysela \n", command); } -int main( int argc, char *argv[] ) +int main(int argc, char *argv[]) { - int card, dev, tmp, err, c; - - card = 0; - dev = 0; - command = argv[0]; - active_format = FORMAT_DEFAULT; - if ( strstr( argv[0], "arecord" ) ) { - direction = SND_PCM_OPEN_RECORD; - active_format = FORMAT_WAVE; - command = "Arecord"; - } else if ( strstr( argv[0], "aplay" ) ) { - direction = SND_PCM_OPEN_PLAYBACK; - command = "Aplay"; - } else { - fprintf( stderr, "Error: command should be named either arecord or aplay\n"); - return 1; - } - - buffer_size = -1; - memset( &rformat, 0, sizeof( rformat ) ); - rformat.format = SND_PCM_SFMT_U8; - rformat.rate = DEFAULT_SPEED; - rformat.channels = 1; - - if ( argc > 1 && !strcmp( argv[1], "--help" ) ) { - usage( command ); - return 0; - } - if ( argc > 1 && !strcmp( argv[1], "--version" ) ) { - version(); - return 0; - } - while ( (c = getopt( argc, argv, "hlc:d:qs:So:t:b:vrwuxB:c:p:mMV" )) != EOF ) - switch ( c ) { - case 'h': - usage( command ); - return 0; - case 'l': - device_list(); - return 0; - case 'c': - card = snd_card_name( optarg ); - if ( card < 0 ) { - fprintf( stderr, "Error: soundcard '%s' not found\n", optarg ); - return 1; - } - break; - case 'd': - dev = atoi( optarg ); - if ( dev < 0 || dev > 32 ) { - fprintf( stderr, "Error: device %i is invalid\n", dev ); - return 1; - } - break; - case 'S': - rformat.channels = 2; - break; - case 'o': - tmp = atoi( optarg ); - if ( tmp < 1 || tmp > 32 ) { - fprintf( stderr, "Error: value %i for channels is invalid\n", tmp ); - return 1; - } - break; - case 'q': - quiet_mode = 1; - break; - case 'r': - active_format = FORMAT_RAW; - break; - case 'v': - active_format = FORMAT_VOC; - break; - case 'w': - active_format = FORMAT_WAVE; - break; - case 'u': - active_format = FORMAT_AU; - rformat.format = SND_PCM_SFMT_MU_LAW; - break; - case 's': - tmp = atoi( optarg ); - if ( tmp < 300 ) - tmp *= 1000; - rformat.rate = tmp; - if ( tmp < 2000 || tmp > 128000 ) { - fprintf( stderr, "Error: bad speed value %i\n", tmp ); - return 1; - } - break; - case 't': - timelimit = atoi( optarg ); - break; - case 'b': - tmp = atoi( optarg ); - switch( tmp ) { - case 8: - rformat.format = SND_PCM_SFMT_U8; - break; - case 16: - rformat.format = SND_PCM_SFMT_S16_LE; - break; + int card, dev, tmp, err, c; + + card = 0; + dev = 0; + command = argv[0]; + active_format = FORMAT_DEFAULT; + if (strstr(argv[0], "arecord")) { + direction = SND_PCM_OPEN_RECORD; + active_format = FORMAT_WAVE; + command = "Arecord"; + } else if (strstr(argv[0], "aplay")) { + direction = SND_PCM_OPEN_PLAYBACK; + command = "Aplay"; + } else { + fprintf(stderr, "Error: command should be named either arecord or aplay\n"); + return 1; + } + + buffer_size = -1; + memset(&rformat, 0, sizeof(rformat)); + rformat.format = SND_PCM_SFMT_U8; + rformat.rate = DEFAULT_SPEED; + rformat.channels = 1; + + if (argc > 1 && !strcmp(argv[1], "--help")) { + usage(command); + return 0; + } + if (argc > 1 && !strcmp(argv[1], "--version")) { + version(); + return 0; } - break; - case 'x': - verbose_mode = 1; quiet_mode = 0; - break; - case 'p': - if ( !strcmp( optarg, "alaw" ) ) { - rformat.format = SND_PCM_SFMT_A_LAW; - active_format = FORMAT_RAW; - break; - } else if ( !strcmp( optarg, "ulaw" ) || !strcmp( optarg, "mulaw" ) ) { - rformat.format = SND_PCM_SFMT_MU_LAW; - active_format = FORMAT_RAW; - break; - } if ( !strcmp( optarg, "adpcm" ) ) { - rformat.format = SND_PCM_SFMT_IMA_ADPCM; - active_format = FORMAT_RAW; - break; + while ((c = getopt(argc, argv, "hlc:d:qs:So:t:b:vrwuxB:c:p:mMV")) != EOF) + switch (c) { + case 'h': + usage(command); + return 0; + case 'l': + device_list(); + return 0; + case 'c': + card = snd_card_name(optarg); + if (card < 0) { + fprintf(stderr, "Error: soundcard '%s' not found\n", optarg); + return 1; + } + break; + case 'd': + dev = atoi(optarg); + if (dev < 0 || dev > 32) { + fprintf(stderr, "Error: device %i is invalid\n", dev); + return 1; + } + break; + case 'S': + rformat.channels = 2; + break; + case 'o': + tmp = atoi(optarg); + if (tmp < 1 || tmp > 32) { + fprintf(stderr, "Error: value %i for channels is invalid\n", tmp); + return 1; + } + break; + case 'q': + quiet_mode = 1; + break; + case 'r': + active_format = FORMAT_RAW; + break; + case 'v': + active_format = FORMAT_VOC; + break; + case 'w': + active_format = FORMAT_WAVE; + break; + case 'u': + active_format = FORMAT_AU; + rformat.format = SND_PCM_SFMT_MU_LAW; + break; + case 's': + tmp = atoi(optarg); + if (tmp < 300) + tmp *= 1000; + rformat.rate = tmp; + if (tmp < 2000 || tmp > 128000) { + fprintf(stderr, "Error: bad speed value %i\n", tmp); + return 1; + } + break; + case 't': + timelimit = atoi(optarg); + break; + case 'b': + tmp = atoi(optarg); + switch (tmp) { + case 8: + rformat.format = SND_PCM_SFMT_U8; + break; + case 16: + rformat.format = SND_PCM_SFMT_S16_LE; + break; + } + break; + case 'x': + verbose_mode = 1; + quiet_mode = 0; + break; + case 'p': + if (!strcmp(optarg, "alaw")) { + rformat.format = SND_PCM_SFMT_A_LAW; + active_format = FORMAT_RAW; + break; + } else if (!strcmp(optarg, "ulaw") || !strcmp(optarg, "mulaw")) { + rformat.format = SND_PCM_SFMT_MU_LAW; + active_format = FORMAT_RAW; + break; + } + if (!strcmp(optarg, "adpcm")) { + rformat.format = SND_PCM_SFMT_IMA_ADPCM; + active_format = FORMAT_RAW; + break; + } + fprintf(stderr, "Error: wrong extended format '%s'\n", optarg); + return 1; + case 'm': + case 'M': + rformat.format = SND_PCM_SFMT_S16_LE; + rformat.rate = c == 'M' ? 48000 : 44100; + rformat.channels = 2; + break; + case 'V': + version(); + return 0; + default: + usage(command); + return 1; + } + + if (!quiet_mode) + version(); + + if ((err = snd_pcm_open(&pcm_handle, card, dev, direction)) < 0) { + fprintf(stderr, "Error: audio open error: %s\n", snd_strerror(err)); + return 1; } - fprintf( stderr, "Error: wrong extended format '%s'\n", optarg ); - return 1; - case 'm': - case 'M': - rformat.format = SND_PCM_SFMT_S16_LE; - rformat.rate = c == 'M' ? 48000 : 44100; - rformat.channels = 2; - break; - case 'V': - version(); - return 0; - default: - usage( command ); - return 1; - } - - if ( !quiet_mode ) - version(); - - if ( (err = snd_pcm_open( &pcm_handle, card, dev, direction )) < 0 ) { - fprintf( stderr, "Error: audio open error: %s\n", snd_strerror( err ) ); - return 1; - } - - if ( direction == SND_PCM_OPEN_PLAYBACK ) { - if ( (err = snd_pcm_playback_info( pcm_handle, &pinfo )) < 0 ) { - fprintf( stderr, "Error: playback info error: %s\n", snd_strerror( err ) ); - return 1; - } - tmp = pinfo.buffer_size; - tmp /= 4; /* 4 fragments are best */ - } else { - if ( (err = snd_pcm_record_info( pcm_handle, &rinfo )) < 0 ) { - fprintf( stderr, "Error: record info error: %s\n", snd_strerror( err ) ); - return 1; - } - tmp = rinfo.buffer_size; - tmp /= 8; /* 8 fragments are best */ - } - - buffer_size = tmp; - if (buffer_size < 512 || buffer_size > 16L * 1024L * 1024L ) { - fprintf( stderr, "Error: Invalid audio buffer size %d\n", buffer_size ); - return 1; - } - - check_new_format( &rformat ); - format = rformat; - - if ( (audiobuf = malloc( buffer_size )) == NULL ) { - fprintf( stderr, "%s: unable to allocate input/output buffer\n", command ); - return 1; - } - - if ( optind > argc - 1 ) { - if ( direction == SND_PCM_OPEN_PLAYBACK ) - playback( NULL ); - else - record( NULL ); - } else { - while ( optind <= argc - 1 ) { - if ( direction == SND_PCM_OPEN_PLAYBACK ) - playback( argv[optind++] ); - else - record( argv[optind++] ); - } - } - snd_pcm_close( pcm_handle ); - return 0; + if (direction == SND_PCM_OPEN_PLAYBACK) { + if ((err = snd_pcm_playback_info(pcm_handle, &pinfo)) < 0) { + fprintf(stderr, "Error: playback info error: %s\n", snd_strerror(err)); + return 1; + } + tmp = pinfo.buffer_size; + tmp /= 4; /* 4 fragments are best */ + } else { + if ((err = snd_pcm_record_info(pcm_handle, &rinfo)) < 0) { + fprintf(stderr, "Error: record info error: %s\n", snd_strerror(err)); + return 1; + } + tmp = rinfo.buffer_size; + tmp /= 8; /* 8 fragments are best */ + } + + buffer_size = tmp; + if (buffer_size < 512 || buffer_size > 16L * 1024L * 1024L) { + fprintf(stderr, "Error: Invalid audio buffer size %d\n", buffer_size); + return 1; + } + check_new_format(&rformat); + format = rformat; + + if ((audiobuf = malloc(buffer_size)) == NULL) { + fprintf(stderr, "%s: unable to allocate input/output buffer\n", command); + return 1; + } + if (optind > argc - 1) { + if (direction == SND_PCM_OPEN_PLAYBACK) + playback(NULL); + else + record(NULL); + } else { + while (optind <= argc - 1) { + if (direction == SND_PCM_OPEN_PLAYBACK) + playback(argv[optind++]); + else + record(argv[optind++]); + } + } + snd_pcm_close(pcm_handle); + return 0; } /* @@ -437,639 +438,650 @@ int main( int argc, char *argv[] ) */ static int test_vocfile(void *buffer) { - VocHeader *vp = buffer; - - if (strstr(vp->magic, VOC_MAGIC_STRING) ) { - vocminor = vp->version & 0xFF; - vocmajor = vp->version / 256; - if (vp->version != (0x1233 - vp->coded_ver) ) - return -2; /* coded version mismatch */ - return vp->headerlen - sizeof(VocHeader); /* 0 mostly */ - } - return -1; /* magic string fail */ + VocHeader *vp = buffer; + + if (strstr(vp->magic, VOC_MAGIC_STRING)) { + vocminor = vp->version & 0xFF; + vocmajor = vp->version / 256; + if (vp->version != (0x1233 - vp->coded_ver)) + return -2; /* coded version mismatch */ + return vp->headerlen - sizeof(VocHeader); /* 0 mostly */ + } + return -1; /* magic string fail */ } /* * test, if it's a .WAV file, 0 if ok (and set the speed, stereo etc.) * < 0 if not */ -static int test_wavefile( void *buffer ) +static int test_wavefile(void *buffer) { - WaveHeader *wp = buffer; - - if (wp->main_chunk == WAV_RIFF && wp->chunk_type == WAV_WAVE && - wp->sub_chunk == WAV_FMT && wp->data_chunk == WAV_DATA) { - if (wp->format != WAV_PCM_CODE) { - fprintf( stderr, "%s: can't play not PCM-coded WAVE-files\n", command); - exit( 1 ); - } - if (wp -> modus < 1 || wp->modus > 32) { - fprintf(stderr, "%s: can't play WAVE-files with %d tracks\n", - command, wp->modus); - exit( 1 ); - } - format.channels = wp->modus; - switch ( wp->bit_p_spl ) { - case 8: - format.format = SND_PCM_SFMT_U8; - break; - case 16: - format.format = SND_PCM_SFMT_S16_LE; - break; - default: - fprintf(stderr, "%s: can't play WAVE-files with sample %d bits wide\n", - command, wp->bit_p_spl); - } - format.rate = wp -> sample_fq; - count = wp -> data_length; - check_new_format( &format ); - return 0; - } - return -1; + WaveHeader *wp = buffer; + + if (wp->main_chunk == WAV_RIFF && wp->chunk_type == WAV_WAVE && + wp->sub_chunk == WAV_FMT && wp->data_chunk == WAV_DATA) { + if (wp->format != WAV_PCM_CODE) { + fprintf(stderr, "%s: can't play not PCM-coded WAVE-files\n", command); + exit(1); + } + if (wp->modus < 1 || wp->modus > 32) { + fprintf(stderr, "%s: can't play WAVE-files with %d tracks\n", + command, wp->modus); + exit(1); + } + format.channels = wp->modus; + switch (wp->bit_p_spl) { + case 8: + format.format = SND_PCM_SFMT_U8; + break; + case 16: + format.format = SND_PCM_SFMT_S16_LE; + break; + default: + fprintf(stderr, "%s: can't play WAVE-files with sample %d bits wide\n", + command, wp->bit_p_spl); + } + format.rate = wp->sample_fq; + count = wp->data_length; + check_new_format(&format); + return 0; + } + return -1; } /* - * + */ - -static int test_au( int fd, void *buffer ) + +static int test_au(int fd, void *buffer) { - AuHeader *ap = buffer; - - if ( ntohl( ap -> magic ) != AU_MAGIC ) - return -1; - if ( ntohl( ap -> hdr_size ) > 128 || ntohl( ap -> hdr_size ) < 24 ) - return -1; - count = ntohl( ap -> data_size ); - switch ( ntohl( ap -> encoding ) ) { - case AU_FMT_ULAW: - format.format = SND_PCM_SFMT_MU_LAW; - break; - case AU_FMT_LIN8: - format.format = SND_PCM_SFMT_U8; - break; - case AU_FMT_LIN16: - format.format = SND_PCM_SFMT_U16_LE; - break; - default: - return -1; - } - format.rate = ntohl( ap -> sample_rate ); - if ( format.rate < 2000 || format.rate > 256000 ) return -1; - format.channels = ntohl( ap -> channels ); - if ( format.channels < 1 || format.channels > 128 ) return -1; - if ( read( fd, buffer + sizeof( AuHeader ), ntohl( ap -> hdr_size ) - sizeof( AuHeader ) ) < 0 ) { - fprintf( stderr, "%s: read error\n", command ); - exit( 1 ); - } - check_new_format( &format ); - return 0; + AuHeader *ap = buffer; + + if (ntohl(ap->magic) != AU_MAGIC) + return -1; + if (ntohl(ap->hdr_size) > 128 || ntohl(ap->hdr_size) < 24) + return -1; + count = ntohl(ap->data_size); + switch (ntohl(ap->encoding)) { + case AU_FMT_ULAW: + format.format = SND_PCM_SFMT_MU_LAW; + break; + case AU_FMT_LIN8: + format.format = SND_PCM_SFMT_U8; + break; + case AU_FMT_LIN16: + format.format = SND_PCM_SFMT_U16_LE; + break; + default: + return -1; + } + format.rate = ntohl(ap->sample_rate); + if (format.rate < 2000 || format.rate > 256000) + return -1; + format.channels = ntohl(ap->channels); + if (format.channels < 1 || format.channels > 128) + return -1; + if (read(fd, buffer + sizeof(AuHeader), ntohl(ap->hdr_size) - sizeof(AuHeader)) < 0) { + fprintf(stderr, "%s: read error\n", command); + exit(1); + } + check_new_format(&format); + return 0; } - + /* * writing zeros from the zerobuf to simulate silence, * perhaps it's enough to use a long var instead of zerobuf ? */ -static void write_zeros( unsigned x ) +static void write_zeros(unsigned x) { - unsigned l; - char *buf; - - buf = (char *)malloc( buffer_size ); - if ( !buf ) { - fprintf( stderr, "%s: can allocate buffer for zeros\n", command ); - return; /* not fatal error */ - } - memset( buf, 128, buffer_size ); - while ( x > 0 ) { - l = x; - if ( l > buffer_size ) l = buffer_size; - if ( snd_pcm_write( pcm_handle, buf, l ) != l ) { - fprintf( stderr, "%s: write error\n", command ); - exit( 1 ); - } - x -= l; - } -} + unsigned l; + char *buf; + + buf = (char *) malloc(buffer_size); + if (!buf) { + fprintf(stderr, "%s: can allocate buffer for zeros\n", command); + return; /* not fatal error */ + } + memset(buf, 128, buffer_size); + while (x > 0) { + l = x; + if (l > buffer_size) + l = buffer_size; + if (snd_pcm_write(pcm_handle, buf, l) != l) { + fprintf(stderr, "%s: write error\n", command); + exit(1); + } + x -= l; + } +} static void set_format(void) { - unsigned int bps; /* bytes per second */ - unsigned int size; /* fragment size */ - struct snd_pcm_playback_params pparams; - struct snd_pcm_record_params rparams; - - bps = format.rate * format.channels; - switch ( format.format ) { - case SND_PCM_SFMT_U16_LE: - case SND_PCM_SFMT_U16_BE: - bps <<= 1; - break; - case SND_PCM_SFMT_IMA_ADPCM: - bps >>= 2; - break; - } - bps >>= 2; /* ok.. this buffer should be 0.25 sec */ - if ( bps < 16 ) bps = 16; - size = buffer_size; - while ( size > bps ) size >>= 1; - if ( size < 16 ) size = 16; - - if ( direction == SND_PCM_OPEN_PLAYBACK ) { - if ( snd_pcm_playback_format( pcm_handle, &format ) < 0 ) { - fprintf( stderr, "%s: unable to set playback format %s, %iHz, %i voices\n", command, get_format( format.format ), format.rate, format.channels ); - exit( 1 ); - } - memset( &pparams, 0, sizeof( pparams ) ); - pparams.fragment_size = size; - pparams.fragments_max = -1; /* little trick */ - pparams.fragments_room = 1; - if ( snd_pcm_playback_params( pcm_handle, &pparams ) < 0 ) { - fprintf( stderr, "%s: unable to set playback params\n", command ); - exit( 1 ); - } - } else { - if ( snd_pcm_record_format( pcm_handle, &format ) < 0 ) { - fprintf( stderr, "%s: unable to set record format %s, %iHz, %i voices\n", command, get_format( format.format ), format.rate, format.channels ); - exit( 1 ); - } - memset( &rparams, 0, sizeof( rparams ) ); - rparams.fragment_size = size; - rparams.fragments_min = 1; - if ( snd_pcm_record_params( pcm_handle, &rparams ) < 0 ) { - fprintf( stderr, "%s: unable to set record params\n", command ); - exit( 1 ); - } - } + unsigned int bps; /* bytes per second */ + unsigned int size; /* fragment size */ + struct snd_pcm_playback_params pparams; + struct snd_pcm_record_params rparams; + + bps = format.rate * format.channels; + switch (format.format) { + case SND_PCM_SFMT_U16_LE: + case SND_PCM_SFMT_U16_BE: + bps <<= 1; + break; + case SND_PCM_SFMT_IMA_ADPCM: + bps >>= 2; + break; + } + bps >>= 2; /* ok.. this buffer should be 0.25 sec */ + if (bps < 16) + bps = 16; + size = buffer_size; + while (size > bps) + size >>= 1; + if (size < 16) + size = 16; + + if (direction == SND_PCM_OPEN_PLAYBACK) { + if (snd_pcm_playback_format(pcm_handle, &format) < 0) { + fprintf(stderr, "%s: unable to set playback format %s, %iHz, %i voices\n", command, get_format(format.format), format.rate, format.channels); + exit(1); + } + memset(&pparams, 0, sizeof(pparams)); + pparams.fragment_size = size; + pparams.fragments_max = -1; /* little trick */ + pparams.fragments_room = 1; + if (snd_pcm_playback_params(pcm_handle, &pparams) < 0) { + fprintf(stderr, "%s: unable to set playback params\n", command); + exit(1); + } + } else { + if (snd_pcm_record_format(pcm_handle, &format) < 0) { + fprintf(stderr, "%s: unable to set record format %s, %iHz, %i voices\n", command, get_format(format.format), format.rate, format.channels); + exit(1); + } + memset(&rparams, 0, sizeof(rparams)); + rparams.fragment_size = size; + rparams.fragments_min = 1; + if (snd_pcm_record_params(pcm_handle, &rparams) < 0) { + fprintf(stderr, "%s: unable to set record params\n", command); + exit(1); + } + } } /* * ok, let's play a .voc file - */ + */ -static void voc_play( int fd, int ofs, char *name ) +static void voc_play(int fd, int ofs, char *name) { - int l; - VocBlockType *bp; - VocVoiceData *vd; - VocExtBlock *eb; - u_long nextblock, in_buffer; - u_char *data = audiobuf; - char was_extended = 0, output = 0; - u_short *sp, repeat = 0; - u_long silence; - int filepos = 0; + int l; + VocBlockType *bp; + VocVoiceData *vd; + VocExtBlock *eb; + u_long nextblock, in_buffer; + u_char *data = audiobuf; + char was_extended = 0, output = 0; + u_short *sp, repeat = 0; + u_long silence; + int filepos = 0; #define COUNT(x) nextblock -= x; in_buffer -=x ;data += x #define COUNT1(x) in_buffer -=x ;data += x - if ( !quiet_mode ) { - fprintf( stderr, "Playing Creative Labs Voice file '%s'...\n", name ); - } - - /* first we waste the rest of header, ugly but we don't need seek */ - while ( ofs > buffer_size ) { - if ( read( fd, audiobuf, buffer_size ) != buffer_size ) { - fprintf( stderr, "%s: read error\n", command ); - exit( 1 ); - } - ofs -= buffer_size; - } - if (ofs) - if ( read( fd, audiobuf, ofs ) != ofs ) { - fprintf( stderr, "%s: read error\n", command ); - exit( 1 ); - } - - format.format = SND_PCM_SFMT_U8; - format.channels = 1; - format.rate = DEFAULT_SPEED; - set_format(); - - in_buffer = nextblock = 0; - while (1) { - Fill_the_buffer: /* need this for repeat */ - if ( in_buffer < 32 ) { - /* move the rest of buffer to pos 0 and fill the audiobuf up */ - if (in_buffer) - memcpy (audiobuf, data, in_buffer); - data = audiobuf; - if ((l = read (fd, audiobuf + in_buffer, buffer_size - in_buffer) ) > 0) - in_buffer += l; - else if (! in_buffer) { - /* the file is truncated, so simulate 'Terminator' - and reduce the datablock for save landing */ - nextblock = audiobuf[0] = 0; - if (l == -1) { - perror (name); - exit (-1); - } - } - } - while (! nextblock) { /* this is a new block */ - if (in_buffertype) { - case 0: + if (!quiet_mode) { + fprintf(stderr, "Playing Creative Labs Voice file '%s'...\n", name); + } + /* first we waste the rest of header, ugly but we don't need seek */ + while (ofs > buffer_size) { + if (read(fd, audiobuf, buffer_size) != buffer_size) { + fprintf(stderr, "%s: read error\n", command); + exit(1); + } + ofs -= buffer_size; + } + if (ofs) + if (read(fd, audiobuf, ofs) != ofs) { + fprintf(stderr, "%s: read error\n", command); + exit(1); + } + format.format = SND_PCM_SFMT_U8; + format.channels = 1; + format.rate = DEFAULT_SPEED; + set_format(); + + in_buffer = nextblock = 0; + while (1) { + Fill_the_buffer: /* need this for repeat */ + if (in_buffer < 32) { + /* move the rest of buffer to pos 0 and fill the audiobuf up */ + if (in_buffer) + memcpy(audiobuf, data, in_buffer); + data = audiobuf; + if ((l = read(fd, audiobuf + in_buffer, buffer_size - in_buffer)) > 0) + in_buffer += l; + else if (!in_buffer) { + /* the file is truncated, so simulate 'Terminator' + and reduce the datablock for save landing */ + nextblock = audiobuf[0] = 0; + if (l == -1) { + perror(name); + exit(-1); + } + } + } + while (!nextblock) { /* this is a new block */ + if (in_buffer < sizeof(VocBlockType)) + return; + bp = (VocBlockType *) data; + COUNT1(sizeof(VocBlockType)); + nextblock = VOC_DATALEN(bp); + if (output && !quiet_mode) + fprintf(stderr, "\n"); /* write /n after ASCII-out */ + output = 0; + switch (bp->type) { + case 0: #if 0 - d_printf ("Terminator\n"); + d_printf("Terminator\n"); #endif - return; /* VOC-file stop */ - case 1: - vd = (VocVoiceData *)data; COUNT1(sizeof(VocVoiceData)); - /* we need a SYNC, before we can set new SPEED, STEREO ... */ - - if (! was_extended) { - format.rate = (int)(vd->tc); - format.rate = 1000000 / (256 - format.rate); + return; /* VOC-file stop */ + case 1: + vd = (VocVoiceData *) data; + COUNT1(sizeof(VocVoiceData)); + /* we need a SYNC, before we can set new SPEED, STEREO ... */ + + if (!was_extended) { + format.rate = (int) (vd->tc); + format.rate = 1000000 / (256 - format.rate); #if 0 - d_printf ("Voice data %d Hz\n", dsp_speed); + d_printf("Voice data %d Hz\n", dsp_speed); #endif - if (vd->pack) { /* /dev/dsp can't it */ - fprintf (stderr, "%s: can't play packed .voc files\n", command); - return; - } - if (format.channels == 2) { /* if we are in Stereo-Mode, switch back */ - format.channels = 1; - set_format(); - } - } - else { /* there was extended block */ - format.channels = 2; - was_extended = 0; - } - set_format(); - break; - case 2: /* nothing to do, pure data */ + if (vd->pack) { /* /dev/dsp can't it */ + fprintf(stderr, "%s: can't play packed .voc files\n", command); + return; + } + if (format.channels == 2) { /* if we are in Stereo-Mode, switch back */ + format.channels = 1; + set_format(); + } + } else { /* there was extended block */ + format.channels = 2; + was_extended = 0; + } + set_format(); + break; + case 2: /* nothing to do, pure data */ #if 0 - d_printf ("Voice continuation\n"); + d_printf("Voice continuation\n"); #endif - break; - case 3: /* a silence block, no data, only a count */ - sp = (u_short *)data; COUNT1(sizeof(u_short)); - format.rate = (int)(*data); COUNT1(1); - format.rate = 1000000 / (256 - format.rate); - set_format(); - silence = ( ((u_long)*sp) * 1000) / format.rate; + break; + case 3: /* a silence block, no data, only a count */ + sp = (u_short *) data; + COUNT1(sizeof(u_short)); + format.rate = (int) (*data); + COUNT1(1); + format.rate = 1000000 / (256 - format.rate); + set_format(); + silence = (((u_long) * sp) * 1000) / format.rate; #if 0 - d_printf ("Silence for %d ms\n", (int)silence); + d_printf("Silence for %d ms\n", (int) silence); #endif - write_zeros (*sp); - snd_pcm_flush_playback( pcm_handle ); - break; - case 4: /* a marker for syncronisation, no effect */ - sp = (u_short *)data; COUNT1(sizeof(u_short)); + write_zeros(*sp); + snd_pcm_flush_playback(pcm_handle); + break; + case 4: /* a marker for syncronisation, no effect */ + sp = (u_short *) data; + COUNT1(sizeof(u_short)); #if 0 - d_printf ("Marker %d\n", *sp); + d_printf("Marker %d\n", *sp); #endif - break; - case 5: /* ASCII text, we copy to stderr */ - output = 1; + break; + case 5: /* ASCII text, we copy to stderr */ + output = 1; #if 0 - d_printf ("ASCII - text :\n"); + d_printf("ASCII - text :\n"); #endif - break; - case 6: /* repeat marker, says repeatcount */ - /* my specs don't say it: maybe this can be recursive, but - I don't think somebody use it */ - repeat = *(u_short *)data; COUNT1(sizeof(u_short)); + break; + case 6: /* repeat marker, says repeatcount */ + /* my specs don't say it: maybe this can be recursive, but + I don't think somebody use it */ + repeat = *(u_short *) data; + COUNT1(sizeof(u_short)); #if 0 - d_printf ("Repeat loop %d times\n", repeat); + d_printf("Repeat loop %d times\n", repeat); #endif - if (filepos >= 0) /* if < 0, one seek fails, why test another */ - if ( (filepos = lseek (fd, 0, 1)) < 0 ) { - fprintf(stderr, "%s: can't play loops; %s isn't seekable\n", - command, name); - repeat = 0; - } - else - filepos -= in_buffer; /* set filepos after repeat */ - else - repeat = 0; - break; - case 7: /* ok, lets repeat that be rewinding tape */ - if (repeat) { - if (repeat != 0xFFFF) { + if (filepos >= 0) /* if < 0, one seek fails, why test another */ + if ((filepos = lseek(fd, 0, 1)) < 0) { + fprintf(stderr, "%s: can't play loops; %s isn't seekable\n", + command, name); + repeat = 0; + } else + filepos -= in_buffer; /* set filepos after repeat */ + else + repeat = 0; + break; + case 7: /* ok, lets repeat that be rewinding tape */ + if (repeat) { + if (repeat != 0xFFFF) { #if 0 - d_printf ("Repeat loop %d\n", repeat); + d_printf("Repeat loop %d\n", repeat); #endif - --repeat; - } + --repeat; + } #if 0 - else - d_printf ("Neverending loop\n"); + else + d_printf("Neverending loop\n"); #endif - lseek (fd, filepos, 0); - in_buffer = 0; /* clear the buffer */ - goto Fill_the_buffer; - } + lseek(fd, filepos, 0); + in_buffer = 0; /* clear the buffer */ + goto Fill_the_buffer; + } #if 0 - else - d_printf ("End repeat loop\n"); + else + d_printf("End repeat loop\n"); #endif - break; - case 8: /* the extension to play Stereo, I have SB 1.0 :-( */ - was_extended = 1; - eb = (VocExtBlock *)data; COUNT1(sizeof(VocExtBlock)); - format.rate = (int)(eb->tc); - format.rate = 256000000L / (65536 - format.rate); - format.channels = eb->mode == VOC_MODE_STEREO ? 2 : 1; - if (format.channels == 2) - format.rate = format.rate >> 1; - if (eb->pack) { /* /dev/dsp can't it */ - fprintf (stderr, "%s: can't play packed .voc files\n", command); - return; - } + break; + case 8: /* the extension to play Stereo, I have SB 1.0 :-( */ + was_extended = 1; + eb = (VocExtBlock *) data; + COUNT1(sizeof(VocExtBlock)); + format.rate = (int) (eb->tc); + format.rate = 256000000L / (65536 - format.rate); + format.channels = eb->mode == VOC_MODE_STEREO ? 2 : 1; + if (format.channels == 2) + format.rate = format.rate >> 1; + if (eb->pack) { /* /dev/dsp can't it */ + fprintf(stderr, "%s: can't play packed .voc files\n", command); + return; + } #if 0 - d_printf ("Extended block %s %d Hz\n", - (eb->mode ? "Stereo" : "Mono"), dsp_speed); + d_printf("Extended block %s %d Hz\n", + (eb->mode ? "Stereo" : "Mono"), dsp_speed); #endif - break; - default: - fprintf (stderr, "%s: unknown blocktype %d. terminate.\n", - command, bp->type); - return; - } /* switch (bp->type) */ - } /* while (! nextblock) */ - /* put nextblock data bytes to dsp */ - l = in_buffer; - if ( nextblock < l ) l = nextblock; - if (l) { - if (output && !quiet_mode) { - if ( write( 2, data, l) != l ) { /* to stderr */ - fprintf( stderr, "%s: write error\n", command ); - exit(1); - } - } else { - if (snd_pcm_write(pcm_handle, data, l) != l) { - fprintf( stderr, "%s: write error\n", command ); - exit(1); - } - } - COUNT(l); - } - } /* while(1) */ + break; + default: + fprintf(stderr, "%s: unknown blocktype %d. terminate.\n", + command, bp->type); + return; + } /* switch (bp->type) */ + } /* while (! nextblock) */ + /* put nextblock data bytes to dsp */ + l = in_buffer; + if (nextblock < l) + l = nextblock; + if (l) { + if (output && !quiet_mode) { + if (write(2, data, l) != l) { /* to stderr */ + fprintf(stderr, "%s: write error\n", command); + exit(1); + } + } else { + if (snd_pcm_write(pcm_handle, data, l) != l) { + fprintf(stderr, "%s: write error\n", command); + exit(1); + } + } + COUNT(l); + } + } /* while(1) */ } /* that was a big one, perhaps somebody split it :-) */ /* setting the globals for playing raw data */ static void init_raw_data(void) { - format = rformat; + format = rformat; } /* calculate the data count to read from/to dsp */ static u_long calc_count(void) { - u_long count; - - if (!timelimit) - count = 0x7fffffff; - else { - count = timelimit * format.rate * format.channels; - switch ( format.format ) { - case SND_PCM_SFMT_S16_LE: - case SND_PCM_SFMT_S16_BE: - case SND_PCM_SFMT_U16_LE: - case SND_PCM_SFMT_U16_BE: - count *= 2; - break; - case SND_PCM_SFMT_IMA_ADPCM: - count /= 4; - break; - } - } - return count; + u_long count; + + if (!timelimit) + count = 0x7fffffff; + else { + count = timelimit * format.rate * format.channels; + switch (format.format) { + case SND_PCM_SFMT_S16_LE: + case SND_PCM_SFMT_S16_BE: + case SND_PCM_SFMT_U16_LE: + case SND_PCM_SFMT_U16_BE: + count *= 2; + break; + case SND_PCM_SFMT_IMA_ADPCM: + count /= 4; + break; + } + } + return count; } -/* write a .VOC-header */ -static void begin_voc( int fd, u_long cnt ) +/* write a .VOC-header */ +static void begin_voc(int fd, u_long cnt) { - VocHeader vh; - VocBlockType bt; - VocVoiceData vd; - VocExtBlock eb; - - strncpy( vh.magic, VOC_MAGIC_STRING, 20 ); - vh.magic[19] = 0x1A; - vh.headerlen = sizeof( VocHeader ); - vh.version = VOC_ACTUAL_VERSION; - vh.coded_ver = 0x1233 - VOC_ACTUAL_VERSION; - - if ( write( fd, &vh, sizeof(VocHeader) ) != sizeof(VocHeader) ) { - fprintf( stderr, "%s: write error\n", command ); - exit(1); - } - - if (format.channels > 1) { - /* write a extended block */ - bt.type = 8; - bt.datalen = 4; - bt.datalen_m = bt.datalen_h = 0; - if ( write (fd, &bt, sizeof(VocBlockType)) != sizeof( VocBlockType ) ) { - fprintf( stderr, "%s: write error\n", command ); - exit(1); - } - eb.tc = (u_short)(65536 - 256000000L / (format.rate << 1)); - eb.pack = 0; - eb.mode = 1; - if ( write(fd, &eb, sizeof(VocExtBlock)) != sizeof(VocExtBlock) ) { - fprintf( stderr, "%s: write error\n", command ); - exit(1); - } - } - bt.type = 1; - cnt += sizeof(VocVoiceData); /* Voice_data block follows */ - bt.datalen = (u_char) (cnt & 0xFF); - bt.datalen_m = (u_char)( (cnt & 0xFF00) >> 8 ); - bt.datalen_h = (u_char)( (cnt & 0xFF0000) >> 16 ); - if ( write (fd, &bt, sizeof(VocBlockType)) != sizeof( VocBlockType ) ) { - fprintf( stderr, "%s: write error\n", command ); - exit(1); - } - vd.tc = (u_char)(256 - (1000000 / format.rate) ); - vd.pack = 0; - if ( write (fd, &vd, sizeof(VocVoiceData) ) != sizeof( VocVoiceData ) ) { - fprintf( stderr, "%s: write error\n", command ); - exit(1); - } -} + VocHeader vh; + VocBlockType bt; + VocVoiceData vd; + VocExtBlock eb; + + strncpy(vh.magic, VOC_MAGIC_STRING, 20); + vh.magic[19] = 0x1A; + vh.headerlen = sizeof(VocHeader); + vh.version = VOC_ACTUAL_VERSION; + vh.coded_ver = 0x1233 - VOC_ACTUAL_VERSION; + + if (write(fd, &vh, sizeof(VocHeader)) != sizeof(VocHeader)) { + fprintf(stderr, "%s: write error\n", command); + exit(1); + } + if (format.channels > 1) { + /* write a extended block */ + bt.type = 8; + bt.datalen = 4; + bt.datalen_m = bt.datalen_h = 0; + if (write(fd, &bt, sizeof(VocBlockType)) != sizeof(VocBlockType)) { + fprintf(stderr, "%s: write error\n", command); + exit(1); + } + eb.tc = (u_short) (65536 - 256000000L / (format.rate << 1)); + eb.pack = 0; + eb.mode = 1; + if (write(fd, &eb, sizeof(VocExtBlock)) != sizeof(VocExtBlock)) { + fprintf(stderr, "%s: write error\n", command); + exit(1); + } + } + bt.type = 1; + cnt += sizeof(VocVoiceData); /* Voice_data block follows */ + bt.datalen = (u_char) (cnt & 0xFF); + bt.datalen_m = (u_char) ((cnt & 0xFF00) >> 8); + bt.datalen_h = (u_char) ((cnt & 0xFF0000) >> 16); + if (write(fd, &bt, sizeof(VocBlockType)) != sizeof(VocBlockType)) { + fprintf(stderr, "%s: write error\n", command); + exit(1); + } + vd.tc = (u_char) (256 - (1000000 / format.rate)); + vd.pack = 0; + if (write(fd, &vd, sizeof(VocVoiceData)) != sizeof(VocVoiceData)) { + fprintf(stderr, "%s: write error\n", command); + exit(1); + } +} /* write a WAVE-header */ static void begin_wave(int fd, u_long cnt) { - WaveHeader wh; - int bits; - - switch ( format.format ) { - case SND_PCM_SFMT_U8: - bits = 8; - break; - case SND_PCM_SFMT_S16_LE: - bits = 16; - break; - default: - fprintf( stderr, "%s: Wave doesn't support %s format...\n", command, get_format( format.format ) ); - exit( 1 ); - } - wh.main_chunk = WAV_RIFF; - wh.length = cnt + sizeof(WaveHeader) - 8; - wh.chunk_type = WAV_WAVE; - wh.sub_chunk = WAV_FMT; - wh.sc_len = 16; - wh.format = WAV_PCM_CODE; - wh.modus = format.channels; - wh.sample_fq = format.rate; + WaveHeader wh; + int bits; + + switch (format.format) { + case SND_PCM_SFMT_U8: + bits = 8; + break; + case SND_PCM_SFMT_S16_LE: + bits = 16; + break; + default: + fprintf(stderr, "%s: Wave doesn't support %s format...\n", command, get_format(format.format)); + exit(1); + } + wh.main_chunk = WAV_RIFF; + wh.length = cnt + sizeof(WaveHeader) - 8; + wh.chunk_type = WAV_WAVE; + wh.sub_chunk = WAV_FMT; + wh.sc_len = 16; + wh.format = WAV_PCM_CODE; + wh.modus = format.channels; + wh.sample_fq = format.rate; #if 0 - wh.byte_p_spl = (samplesize == 8) ? 1 : 2; - wh.byte_p_sec = dsp_speed * wh.modus * wh.byte_p_spl; + wh.byte_p_spl = (samplesize == 8) ? 1 : 2; + wh.byte_p_sec = dsp_speed * wh.modus * wh.byte_p_spl; #else - wh.byte_p_spl = wh.modus * ((bits + 7) / 8); - wh.byte_p_sec = wh.byte_p_spl * format.rate; + wh.byte_p_spl = wh.modus * ((bits + 7) / 8); + wh.byte_p_sec = wh.byte_p_spl * format.rate; #endif - wh.bit_p_spl = bits; - wh.data_chunk = WAV_DATA; - wh.data_length= cnt; - if ( write (fd, &wh, sizeof(WaveHeader)) != sizeof( WaveHeader ) ) { - fprintf( stderr, "%s: write error\n", command ); - exit(1); - } + wh.bit_p_spl = bits; + wh.data_chunk = WAV_DATA; + wh.data_length = cnt; + if (write(fd, &wh, sizeof(WaveHeader)) != sizeof(WaveHeader)) { + fprintf(stderr, "%s: write error\n", command); + exit(1); + } } /* write a Au-header */ static void begin_au(int fd, u_long cnt) { - AuHeader ah; - - ah.magic = htonl( AU_MAGIC ); - ah.hdr_size = htonl( 24 ); - ah.data_size = htonl( cnt ); - switch ( format.format ) { - case SND_PCM_SFMT_MU_LAW: - ah.encoding = htonl( AU_FMT_ULAW ); - break; - case SND_PCM_SFMT_U8: - ah.encoding = htonl( AU_FMT_LIN8 ); - break; - case SND_PCM_SFMT_U16_LE: - ah.encoding = htonl( AU_FMT_LIN16 ); - break; - default: - fprintf( stderr, "%s: Sparc Audio doesn't support %s format...\n", command, get_format( format.format ) ); - exit( 1 ); - } - ah.sample_rate = htonl( format.rate ); - ah.channels = htonl( format.channels ); - if ( write (fd, &ah, sizeof(AuHeader)) != sizeof( AuHeader ) ) { - fprintf( stderr, "%s: write error\n", command ); - exit(1); - } + AuHeader ah; + + ah.magic = htonl(AU_MAGIC); + ah.hdr_size = htonl(24); + ah.data_size = htonl(cnt); + switch (format.format) { + case SND_PCM_SFMT_MU_LAW: + ah.encoding = htonl(AU_FMT_ULAW); + break; + case SND_PCM_SFMT_U8: + ah.encoding = htonl(AU_FMT_LIN8); + break; + case SND_PCM_SFMT_U16_LE: + ah.encoding = htonl(AU_FMT_LIN16); + break; + default: + fprintf(stderr, "%s: Sparc Audio doesn't support %s format...\n", command, get_format(format.format)); + exit(1); + } + ah.sample_rate = htonl(format.rate); + ah.channels = htonl(format.channels); + if (write(fd, &ah, sizeof(AuHeader)) != sizeof(AuHeader)) { + fprintf(stderr, "%s: write error\n", command); + exit(1); + } } /* closing .VOC */ static void end_voc(int fd) { - char dummy = 0; /* Write a Terminator */ - if ( write (fd, &dummy, 1) != 1 ) { - fprintf( stderr, "%s: write error", command ); - exit( 1 ); - } - if (fd != 1) - close (fd); + char dummy = 0; /* Write a Terminator */ + if (write(fd, &dummy, 1) != 1) { + fprintf(stderr, "%s: write error", command); + exit(1); + } + if (fd != 1) + close(fd); } static void end_wave(int fd) { /* only close output */ - if (fd != 1) - close (fd); + if (fd != 1) + close(fd); } -static void header( int rtype, char *name ) +static void header(int rtype, char *name) { - if (!quiet_mode) { - fprintf (stderr, "%s %s '%s' : ", - (direction == SND_PCM_OPEN_PLAYBACK) ? "Playing" : "Recording", - fmt_rec_table[rtype].what, - name ); - fprintf (stderr, "%s, ", get_format( format.format ) ); - fprintf (stderr, "Rate %d Hz, ", format.rate); - if ( format.channels == 1 ) - fprintf (stderr, "Mono" ); - else if ( format.channels == 2 ) - fprintf (stderr, "Stereo" ); - else fprintf (stderr, "Voices %i", format.channels); - fprintf (stderr, "\n" ); - } + if (!quiet_mode) { + fprintf(stderr, "%s %s '%s' : ", + (direction == SND_PCM_OPEN_PLAYBACK) ? "Playing" : "Recording", + fmt_rec_table[rtype].what, + name); + fprintf(stderr, "%s, ", get_format(format.format)); + fprintf(stderr, "Rate %d Hz, ", format.rate); + if (format.channels == 1) + fprintf(stderr, "Mono"); + else if (format.channels == 2) + fprintf(stderr, "Stereo"); + else + fprintf(stderr, "Voices %i", format.channels); + fprintf(stderr, "\n"); + } } /* playing raw data */ -void playback_go (int fd, int loaded, u_long count, int rtype, char *name) +void playback_go(int fd, int loaded, u_long count, int rtype, char *name) { - int l; - u_long c; + int l; + u_long c; - header( rtype, name ); - set_format(); + header(rtype, name); + set_format(); - while (count) { - c = count; + while (count) { + c = count; - if (c > buffer_size) - c = buffer_size; + if (c > buffer_size) + c = buffer_size; - if ((l = read (fd, audiobuf + loaded, c - loaded)) > 0) { - l += loaded; loaded = 0; /* correct the count; ugly but ... */ + if ((l = read(fd, audiobuf + loaded, c - loaded)) > 0) { + l += loaded; + loaded = 0; /* correct the count; ugly but ... */ #if 0 - sleep( 1 ); + sleep(1); #endif - if (snd_pcm_write(pcm_handle, audiobuf, l) != l) { - fprintf( stderr, "write error\n" ); - exit (1); - } - count -= l; - } else { - if (l == -1) { - perror (name); - exit (-1); - } - count = 0; /* Stop */ - } - } /* while (count) */ + if (snd_pcm_write(pcm_handle, audiobuf, l) != l) { + fprintf(stderr, "write error\n"); + exit(1); + } + count -= l; + } else { + if (l == -1) { + perror(name); + exit(-1); + } + count = 0; /* Stop */ + } + } /* while (count) */ } -/* recording raw data, this proc handels WAVE files and .VOCs (as one block) */ +/* recording raw data, this proc handels WAVE files and .VOCs (as one block) */ void record_go(int fd, int loaded, u_long count, int rtype, char *name) { - int l; - u_long c; - - header( rtype, name ); - set_format(); - - while (count) { - c = count; - if (c > buffer_size) - c = buffer_size; - - if ((l = snd_pcm_read(pcm_handle, audiobuf, c)) > 0) { - if (write (fd, audiobuf, l) != l) { - perror (name); - exit (-1); - } - count -= l; - } - - if (l == -1) { - fprintf( stderr, "read error\n" ); - exit (-1); - } - } + int l; + u_long c; + + header(rtype, name); + set_format(); + + while (count) { + c = count; + if (c > buffer_size) + c = buffer_size; + + if ((l = snd_pcm_read(pcm_handle, audiobuf, c)) > 0) { + if (write(fd, audiobuf, l) != l) { + perror(name); + exit(-1); + } + count -= l; + } + if (l == -1) { + fprintf(stderr, "read error\n"); + exit(-1); + } + } } /* @@ -1078,77 +1090,77 @@ void record_go(int fd, int loaded, u_long count, int rtype, char *name) static void playback(char *name) { - int fd, ofs; - - snd_pcm_flush_playback( pcm_handle ); - if (!name) { - fd = 0; - name = "stdin"; - } else { - if ((fd = open (name, O_RDONLY, 0)) == -1) { - perror (name); - exit(1); - } - } - /* read the file header */ - if ( read( fd, audiobuf, sizeof(AuHeader) ) != sizeof( AuHeader ) ) { - fprintf( stderr, "%s: read error", command ); - exit( 1 ); - } - if ( test_au( fd, audiobuf ) >= 0 ) { - playback_go( fd, 0, count, FORMAT_AU, name ); - goto __end; - } - if ( read( fd, audiobuf + sizeof(AuHeader), - sizeof(VocHeader)-sizeof(AuHeader) ) != - sizeof(VocHeader)-sizeof(AuHeader) ) { - fprintf( stderr, "%s: read error", command ); - exit( 1 ); - } - if ( (ofs = test_vocfile (audiobuf) ) >= 0) { - voc_play (fd, ofs, name); - goto __end; - } - /* read bytes for WAVE-header */ - if ( read (fd, audiobuf + sizeof(VocHeader), - sizeof(WaveHeader) - sizeof(VocHeader) ) != - sizeof(WaveHeader) - sizeof(VocHeader) ) { - fprintf( stderr, "%s: read error", command ); - exit( 1 ); - } - if (test_wavefile (audiobuf) >= 0) { - playback_go(fd, 0, count, FORMAT_WAVE, name); - } else { - /* should be raw data */ - init_raw_data(); - count = calc_count(); - playback_go(fd, sizeof(WaveHeader), count, FORMAT_RAW, name); - } - __end: - if (fd != 0) - close(fd); -} + int fd, ofs; + + snd_pcm_flush_playback(pcm_handle); + if (!name) { + fd = 0; + name = "stdin"; + } else { + if ((fd = open(name, O_RDONLY, 0)) == -1) { + perror(name); + exit(1); + } + } + /* read the file header */ + if (read(fd, audiobuf, sizeof(AuHeader)) != sizeof(AuHeader)) { + fprintf(stderr, "%s: read error", command); + exit(1); + } + if (test_au(fd, audiobuf) >= 0) { + playback_go(fd, 0, count, FORMAT_AU, name); + goto __end; + } + if (read(fd, audiobuf + sizeof(AuHeader), + sizeof(VocHeader) - sizeof(AuHeader)) != + sizeof(VocHeader) - sizeof(AuHeader)) { + fprintf(stderr, "%s: read error", command); + exit(1); + } + if ((ofs = test_vocfile(audiobuf)) >= 0) { + voc_play(fd, ofs, name); + goto __end; + } + /* read bytes for WAVE-header */ + if (read(fd, audiobuf + sizeof(VocHeader), + sizeof(WaveHeader) - sizeof(VocHeader)) != + sizeof(WaveHeader) - sizeof(VocHeader)) { + fprintf(stderr, "%s: read error", command); + exit(1); + } + if (test_wavefile(audiobuf) >= 0) { + playback_go(fd, 0, count, FORMAT_WAVE, name); + } else { + /* should be raw data */ + init_raw_data(); + count = calc_count(); + playback_go(fd, sizeof(WaveHeader), count, FORMAT_RAW, name); + } + __end: + if (fd != 0) + close(fd); +} static void record(char *name) { - int fd; - - snd_pcm_flush_record( pcm_handle ); - if (!name) { - fd = 1; - name = "stdout"; - } else { - remove( name ); - if ((fd = open (name, O_WRONLY | O_CREAT, 0644)) == -1) { - perror (name); - exit(1); - } - } - count = calc_count() & 0xFFFFFFFE; - /* WAVE-file should be even (I'm not sure), but wasting one byte - isn't a problem (this can only be in 8 bit mono) */ - if (fmt_rec_table[active_format].start) - fmt_rec_table[active_format].start(fd, count); - record_go( fd, 0, count, active_format, name); - fmt_rec_table[active_format].end(fd); -} + int fd; + + snd_pcm_flush_record(pcm_handle); + if (!name) { + fd = 1; + name = "stdout"; + } else { + remove(name); + if ((fd = open(name, O_WRONLY | O_CREAT, 0644)) == -1) { + perror(name); + exit(1); + } + } + count = calc_count() & 0xFFFFFFFE; + /* WAVE-file should be even (I'm not sure), but wasting one byte + isn't a problem (this can only be in 8 bit mono) */ + if (fmt_rec_table[active_format].start) + fmt_rec_table[active_format].start(fd, count); + record_go(fd, 0, count, active_format, name); + fmt_rec_table[active_format].end(fd); +} diff --git a/aplay/formats.h b/aplay/formats.h index 57f4277..7f41326 100644 --- a/aplay/formats.h +++ b/aplay/formats.h @@ -17,33 +17,33 @@ ((u_long)(bp->datalen_h) << 16) ) typedef struct voc_header { - u_char magic[20]; /* must be MAGIC_STRING */ - u_short headerlen; /* Headerlength, should be 0x1A */ - u_short version; /* VOC-file version */ - u_short coded_ver; /* 0x1233-version */ + u_char magic[20]; /* must be MAGIC_STRING */ + u_short headerlen; /* Headerlength, should be 0x1A */ + u_short version; /* VOC-file version */ + u_short coded_ver; /* 0x1233-version */ } VocHeader; typedef struct voc_blocktype { - u_char type; - u_char datalen; /* low-byte */ - u_char datalen_m; /* medium-byte */ - u_char datalen_h; /* high-byte */ + u_char type; + u_char datalen; /* low-byte */ + u_char datalen_m; /* medium-byte */ + u_char datalen_h; /* high-byte */ } VocBlockType; typedef struct voc_voice_data { - u_char tc; - u_char pack; + u_char tc; + u_char pack; } VocVoiceData; typedef struct voc_ext_block { - u_short tc; - u_char pack; - u_char mode; + u_short tc; + u_char pack; + u_char mode; } VocExtBlock; /* Definitions for Microsoft WAVE format */ -#define WAV_RIFF 0x46464952 +#define WAV_RIFF 0x46464952 #define WAV_WAVE 0x45564157 #define WAV_FMT 0x20746D66 #define WAV_DATA 0x61746164 @@ -52,23 +52,23 @@ typedef struct voc_ext_block { /* it's in chunks like .voc and AMIGA iff, but my source say there are in only in this combination, so I combined them in one header; it works on all WAVE-file I have -*/ + */ typedef struct wav_header { - u_int main_chunk; /* 'RIFF' */ - u_int length; /* filelen */ - u_int chunk_type; /* 'WAVE' */ - - u_int sub_chunk; /* 'fmt ' */ - u_int sc_len; /* length of sub_chunk, =16 */ - u_short format; /* should be 1 for PCM-code */ - u_short modus; /* 1 Mono, 2 Stereo */ - u_int sample_fq; /* frequence of sample */ - u_int byte_p_sec; - u_short byte_p_spl; /* samplesize; 1 or 2 bytes */ - u_short bit_p_spl; /* 8, 12 or 16 bit */ - - u_int data_chunk; /* 'data' */ - u_int data_length; /* samplecount */ + u_int main_chunk; /* 'RIFF' */ + u_int length; /* filelen */ + u_int chunk_type; /* 'WAVE' */ + + u_int sub_chunk; /* 'fmt ' */ + u_int sc_len; /* length of sub_chunk, =16 */ + u_short format; /* should be 1 for PCM-code */ + u_short modus; /* 1 Mono, 2 Stereo */ + u_int sample_fq; /* frequence of sample */ + u_int byte_p_sec; + u_short byte_p_spl; /* samplesize; 1 or 2 bytes */ + u_short bit_p_spl; /* 8, 12 or 16 bit */ + + u_int data_chunk; /* 'data' */ + u_int data_length; /* samplecount */ } WaveHeader; /* Definitions for Sparc .au header */ @@ -80,12 +80,12 @@ typedef struct wav_header { #define AU_FMT_LIN16 3 typedef struct au_header { - u_int magic; /* magic '.snd' */ - u_int hdr_size; /* size of header (min 24) */ - u_int data_size; /* size of data */ - u_int encoding; /* see to AU_FMT_XXXX */ - u_int sample_rate; /* sample rate */ - u_int channels; /* number of channels (voices) */ + u_int magic; /* magic '.snd' */ + u_int hdr_size; /* size of header (min 24) */ + u_int data_size; /* size of data */ + u_int encoding; /* see to AU_FMT_XXXX */ + u_int sample_rate; /* sample rate */ + u_int channels; /* number of channels (voices) */ } AuHeader; -#endif /* FORMATS */ +#endif /* FORMATS */ -- 2.47.1