]> git.alsa-project.org Git - tinycompress.git/commitdiff
wave: Introduce parse_wave header
authorDaniel Baluta <daniel.baluta@nxp.com>
Thu, 7 Jan 2021 20:57:14 +0000 (22:57 +0200)
committerDaniel Baluta <daniel.baluta@nxp.com>
Thu, 21 Jan 2021 15:42:30 +0000 (17:42 +0200)
This parses a canonical WAVE header and fills in
informationlike channels, rate and format.

In tinycompress, we use canonical WAVE format described here:

http://soundfile.sapp.org/doc/WaveFormat/

A WAVE file is often just a RIFF file with a single "WAVE" chunk which
consists of two sub-chunks -- a "fmt " chunk specifying the data format
and a "data" chunk containing the actual sample data.

Signed-off-by: Daniel Baluta <daniel.baluta@nxp.com>
include/tinycompress/tinywave.h
src/utils/wave.c

index ac128e598dc80b30b6b1c96314dec68b7a83390c..619cb8d09f2fb0dbb1d5d483a29047a6704418bb 100644 (file)
@@ -38,4 +38,6 @@ void init_wave_header(struct wave_header *header, uint16_t channels,
                      uint32_t rate, uint16_t samplebits);
 void size_wave_header(struct wave_header *header, uint32_t size);
 
+int parse_wave_header(struct wave_header *header, unsigned int *channels,
+                     unsigned int *rate, unsigned int *format);
 #endif
index 23691fc88117131dd7df853d053190621060eef1..a74149aec08254c5861842beb5b8f4bd44818341 100644 (file)
@@ -5,8 +5,10 @@
 //
 // Copyright 2021 NXP
 
+#include <stdio.h>
 #include <stdint.h>
 #include <string.h>
+#include <sound/asound.h>
 
 #include "tinycompress/tinywave.h"
 
@@ -50,3 +52,43 @@ void size_wave_header(struct wave_header *header, uint32_t size)
                                  sizeof(header->riff.chunk) + size;
        header->data.chunk.size = size;
 }
+
+int parse_wave_header(struct wave_header *header, unsigned int *channels,
+                     unsigned int *rate, unsigned int *format)
+{
+       if (strncmp(header->riff.chunk.desc, "RIFF", 4) != 0) {
+               fprintf(stderr, "RIFF magic not found\n");
+               return -1;
+       }
+
+       if (strncmp(header->riff.format, "WAVE", 4) != 0) {
+               fprintf(stderr, "WAVE magic not found\n");
+               return -1;
+       }
+
+       if (strncmp(header->fmt.chunk.desc, "fmt", 3) != 0) {
+               fprintf(stderr, "FMT section not found");
+               return -1;
+       }
+
+       *channels = header->fmt.channels;
+       *rate = header->fmt.rate;
+
+       switch(header->fmt.samplebits) {
+       case 8:
+               *format = SNDRV_PCM_FORMAT_U8;
+               break;
+       case 16:
+               *format = SNDRV_PCM_FORMAT_S16_LE;
+               break;
+       case 32:
+               *format = SNDRV_PCM_FORMAT_S32_LE;
+               break;
+       default:
+               fprintf(stderr, "Unsupported sample bits %d\n",
+                       header->fmt.samplebits);
+               return -1;
+       }
+
+       return 0;
+}