From 89e9b51764e9784dcfdf477ed85985b466d384f0 Mon Sep 17 00:00:00 2001 From: Joursoir Date: Thu, 28 Jan 2021 17:04:04 +0000 Subject: add functional next argv: --help, --formats, --file-type, --file-types --- parecord/audio_types.c | 73 ++++++++++++++++++++++++++++++++++---------------- parecord/audio_types.h | 29 ++++++++++++++++---- parecord/parecord.c | 37 ++++++++++++++++++++----- 3 files changed, 105 insertions(+), 34 deletions(-) diff --git a/parecord/audio_types.c b/parecord/audio_types.c index eda78bd..bcf6a05 100644 --- a/parecord/audio_types.c +++ b/parecord/audio_types.c @@ -1,27 +1,28 @@ +#include +#include #include #include "audio_types.h" -static const struct audio_types_info support_types[] = { - {"wav", 44}, // AUDIO_TYPE_WAVE - {NULL, 0} +static const struct audio_types_info support_types[AUDIO_TYPE_MAX] = { + {"wav", 44} }; -static const struct audio_formats_info support_formats[] = { - {"U8", PA_SAMPLE_U8}, - {"A_LAW", PA_SAMPLE_ALAW}, - {"MU_LAW", PA_SAMPLE_ULAW}, - {"S16_LE", PA_SAMPLE_S16LE}, - {"S16_BE", PA_SAMPLE_S16BE}, - {"FLOAT32_LE", PA_SAMPLE_FLOAT32LE}, - {"FLOAT32_BE", PA_SAMPLE_FLOAT32BE}, - {"S32_LE", PA_SAMPLE_S32LE}, - {"S32_BE", PA_SAMPLE_S32BE}, - {"S24_LE", PA_SAMPLE_S24LE}, - {"S24_BE", PA_SAMPLE_S24BE}, - {"S24_32LE", PA_SAMPLE_S24_32LE}, - {"S24_32BE", PA_SAMPLE_S24_32BE}, - {NULL, 0} +static const char + audio_format_name[AUDIO_FORMAT_MAX][MAX_AUDIO_FORMAT_LEN] = { + "U8", + "A_LAW", + "MU_LAW", + "S16_LE", + "S16_BE", + "FLOAT32_LE", + "FLOAT32_BE", + "S32_LE", + "S32_BE", + "S24_LE", + "S24_BE", + "S24_32LE", + "S24_32BE" }; struct wav_header *init_wav_header(struct wav_header *header, @@ -48,7 +49,7 @@ struct wav_header *init_wav_header(struct wav_header *header, int checkAudioType(char *source) { int i; - for(i = 0; support_types[i].name != NULL; i++) { + for(i = 0; i < AUDIO_TYPE_MAX; i++) { if(strcmp(source, support_types[i].name) == 0) { return i; } @@ -57,19 +58,45 @@ int checkAudioType(char *source) return AUDIO_TYPE_NONE; } +char *getAllAudioTypes() +{ + int i; + char *dest = malloc(sizeof(char) * MAX_AUDIO_TYPE_LEN * + AUDIO_TYPE_MAX + sizeof(char)); + char *str = dest; + for(i = 0; i < AUDIO_TYPE_MAX; i++) { + str += sprintf(str, " %s", support_types[i].name); + } + + return dest; +} + off_t getOffset(int format) { return support_types[format].rsv_bytes; } -pa_sample_format_t checkAudioFormat(char *source) +int checkAudioFormat(char *source) { int i; - for(i = 0; support_formats[i].name != NULL; i++) { - if(strcmp(source, support_formats[i].name) == 0) { - return support_formats[i].pa_format; + for(i = 0; i < AUDIO_FORMAT_MAX; i++) { + if(strcmp(source, audio_format_name[i]) == 0) { + return i; } } return PA_SAMPLE_INVALID; +} + +char *getAllAudioFormats() +{ + int i; + char *dest = malloc(sizeof(char) * MAX_AUDIO_FORMAT_LEN * + AUDIO_FORMAT_MAX + sizeof(char)); + char *str = dest; + for(i = 0; i < AUDIO_FORMAT_MAX; i++) { + str += sprintf(str, " %s", audio_format_name[i]); + } + + return dest; } \ No newline at end of file diff --git a/parecord/audio_types.h b/parecord/audio_types.h index d151dce..e924cf4 100644 --- a/parecord/audio_types.h +++ b/parecord/audio_types.h @@ -7,26 +7,45 @@ #include "wav_header.h" +#define MAX_AUDIO_TYPE_LEN 5 +#define MAX_AUDIO_FORMAT_LEN 11 + enum audio_types { AUDIO_TYPE_WAVE, + AUDIO_TYPE_MAX, AUDIO_TYPE_NONE = -1 }; struct audio_types_info { - const char *name; + const char name[MAX_AUDIO_TYPE_LEN]; off_t rsv_bytes; }; -struct audio_formats_info { - const char *name; - pa_sample_format_t pa_format; +enum audio_formats { + AUDIO_FORMAT_U8 = PA_SAMPLE_U8, + AUDIO_FORMAT_ALAW = PA_SAMPLE_ALAW, + AUDIO_FORMAT_ULAW = PA_SAMPLE_ULAW, + AUDIO_FORMAT_S16LE = PA_SAMPLE_S16LE, + AUDIO_FORMAT_S16BE = PA_SAMPLE_S16BE, + AUDIO_FORMAT_FLOAT32LE = PA_SAMPLE_FLOAT32LE, + AUDIO_FORMAT_FLOAT32BE = PA_SAMPLE_FLOAT32BE, + AUDIO_FORMAT_S32LE = PA_SAMPLE_S32LE, + AUDIO_FORMAT_S32BE = PA_SAMPLE_S32BE, + AUDIO_FORMAT_S24LE = PA_SAMPLE_S24LE, + AUDIO_FORMAT_S24BE = PA_SAMPLE_S24BE, + AUDIO_FORMAT_S24_32LE = PA_SAMPLE_S24_32LE, + AUDIO_FORMAT_S24_32BE = PA_SAMPLE_S24_32BE, + AUDIO_FORMAT_MAX = 13, + AUDIO_FORMAT_NONE = -1 }; struct wav_header *init_wav_header(struct wav_header *header, uint32_t size, uint16_t audioFormat, uint16_t numChannels, uint32_t sampleRate, uint32_t bitsPerSample); int checkAudioType(char *source); +char *getAllAudioTypes(); off_t getOffset(int format); -pa_sample_format_t checkAudioFormat(char *source); +int checkAudioFormat(char *source); +char *getAllAudioFormats(); #endif /* AT_AUDIOTYPES_H */ diff --git a/parecord/parecord.c b/parecord/parecord.c index 4fff058..feda84b 100644 --- a/parecord/parecord.c +++ b/parecord/parecord.c @@ -15,7 +15,7 @@ #define PAR_CHANNELS_MIN 1U #define BUFSIZE 1024 -#define STD_REC_FORMAT PA_SAMPLE_S16LE +#define STD_REC_FORMAT AUDIO_FORMAT_S16LE #define STD_REC_RATE 44100U #define STD_REC_CHANNELS PAR_CHANNELS_MIN @@ -57,7 +57,7 @@ int main(int argc, char *argv[]) int fd_output = STDOUT_FILENO, result; int file_type = AUDIO_TYPE_NONE; - pa_sample_format_t record_format = STD_REC_FORMAT; + int record_format = STD_REC_FORMAT; uint32_t record_rate = STD_REC_RATE; uint8_t record_channels = STD_REC_CHANNELS; off_t offset; @@ -75,8 +75,29 @@ int main(int argc, char *argv[]) while((result = getopt_long(argc, argv, "ht:Tf:Fc:r:", long_options, NULL)) != -1) { switch(result) { - case 'h': { - // print help + case 'h': { + fprintf(stderr, "Synopsis:\n\tparecord [OPTION] ... [FILE]\n"); + + fprintf(stderr, "Option:\n\t-h, --help\n"); + fprintf(stderr, "\t\tPrint this text.\n"); + + fprintf(stderr, "\t-t, --file-type\n"); + fprintf(stderr, "\t\tFile type (pcm, wav). PCM is used by default\n"); + + fprintf(stderr, "\t-T, --file-types\n"); + fprintf(stderr, "\t\tPrint valid file types\n"); + + fprintf(stderr, "\t-f, --format\n"); + fprintf(stderr, "\t\tSample format. S16_LE is used by default\n"); + + fprintf(stderr, "\t-F, --formats\n"); + fprintf(stderr, "\t\tPrint valid sample formats\n"); + + fprintf(stderr, "\t-c, --channels\n"); + fprintf(stderr, "\t\tThe number of channels. One channel is used by default.\n"); + + fprintf(stderr, "\t-r, --rate\n"); + fprintf(stderr, "\t\tSample rate in Hz. %d Hz is used by default. \n", STD_REC_RATE); return 0; } case 't': { @@ -85,7 +106,9 @@ int main(int argc, char *argv[]) // else print file types (below) } case 'T': { - // print file types + char *types = getAllAudioTypes(); + fprintf(stderr, "Valid file types are:%s\n", types); + free(types); return 0; } case 'f': { @@ -94,7 +117,9 @@ int main(int argc, char *argv[]) // else print formats (below) } case 'F': { - // print formats + char *formats = getAllAudioFormats(); + fprintf(stderr, "Valid sample formats are:%s\n", formats); + free(formats); return 0; } case 'c': { -- cgit v1.2.3-18-g5258