]> git.alsa-project.org Git - tinycompress.git/commitdiff
cplay: move around MP3 header parsing method
authorKatsuhiro Suzuki <suzuki.katsuhiro@socionext.com>
Fri, 2 Feb 2018 04:32:45 +0000 (13:32 +0900)
committerVinod Koul <vinod.koul@intel.com>
Fri, 2 Feb 2018 17:30:37 +0000 (23:00 +0530)
This patch moves MP3 parsing method for -I option, this is for
adding other codec ID in the future.

Signed-off-by: Katsuhiro Suzuki <suzuki.katsuhiro@socionext.com>
Signed-off-by: Vinod Koul <vinod.koul@intel.com>
src/utils/cplay.c

index 1eb21ce0d47423ff409885aa275f042a372aa0bd..beec45f1c949866221877d9ed743e70084703c62 100644 (file)
@@ -268,6 +268,42 @@ int main(int argc, char **argv)
        exit(EXIT_SUCCESS);
 }
 
+void get_codec_mp3(FILE *file, struct compr_config *config,
+               struct snd_codec *codec)
+{
+       size_t read;
+       struct mp3_header header;
+       unsigned int channels, rate, bits;
+
+       read = fread(&header, 1, sizeof(header), file);
+       if (read != sizeof(header)) {
+               fprintf(stderr, "Unable to read header \n");
+               fclose(file);
+               exit(EXIT_FAILURE);
+       }
+
+       if (parse_mp3_header(&header, &channels, &rate, &bits) == -1) {
+               fclose(file);
+               exit(EXIT_FAILURE);
+       }
+
+       codec->id = SND_AUDIOCODEC_MP3;
+       codec->ch_in = channels;
+       codec->ch_out = channels;
+       codec->sample_rate = rate;
+       if (!codec->sample_rate) {
+               fprintf(stderr, "invalid sample rate %d\n", rate);
+               fclose(file);
+               exit(EXIT_FAILURE);
+       }
+       codec->bit_rate = bits;
+       codec->rate_control = 0;
+       codec->profile = 0;
+       codec->level = 0;
+       codec->ch_mode = 0;
+       codec->format = 0;
+}
+
 void play_samples(char *name, unsigned int card, unsigned int device,
                unsigned long buffer_size, unsigned int frag,
                unsigned long codec_id)
@@ -275,12 +311,9 @@ void play_samples(char *name, unsigned int card, unsigned int device,
        struct compr_config config;
        struct snd_codec codec;
        struct compress *compress;
-       struct mp3_header header;
        FILE *file;
        char *buffer;
        int size, num_read, wrote;
-       unsigned int channels, rate, bits;
-       size_t read;
 
        if (verbose)
                printf("%s: entry\n", __func__);
@@ -290,33 +323,15 @@ void play_samples(char *name, unsigned int card, unsigned int device,
                exit(EXIT_FAILURE);
        }
 
-       read = fread(&header, 1, sizeof(header), file);
-       if (read != sizeof(header)) {
-               fprintf(stderr, "Unable to read header \n");
-               fclose(file);
+       switch (codec_id) {
+       case SND_AUDIOCODEC_MP3:
+               get_codec_mp3(file, &config, &codec);
+               break;
+       default:
+               fprintf(stderr, "codec ID %d is not supported\n", codec_id);
                exit(EXIT_FAILURE);
        }
 
-       if (parse_mp3_header(&header, &channels, &rate, &bits) == -1) {
-               fclose(file);
-               exit(EXIT_FAILURE);
-       }
-
-       codec.id = SND_AUDIOCODEC_MP3;
-       codec.ch_in = channels;
-       codec.ch_out = channels;
-       codec.sample_rate = rate;
-       if (!codec.sample_rate) {
-               fprintf(stderr, "invalid sample rate %d\n", rate);
-               fclose(file);
-               exit(EXIT_FAILURE);
-       }
-       codec.bit_rate = bits;
-       codec.rate_control = 0;
-       codec.profile = 0;
-       codec.level = 0;
-       codec.ch_mode = 0;
-       codec.format = 0;
        if ((buffer_size != 0) && (frag != 0)) {
                config.fragment_size = buffer_size/frag;
                config.fragments = frag;