[e677b08] | 1 | /*
|
---|
| 2 | * Copyright (c) 2011 Jan Vesely
|
---|
| 3 | * All rights reserved.
|
---|
| 4 | *
|
---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
---|
| 6 | * modification, are permitted provided that the following conditions
|
---|
| 7 | * are met:
|
---|
| 8 | *
|
---|
| 9 | * - Redistributions of source code must retain the above copyright
|
---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
---|
| 11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
| 12 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 13 | * documentation and/or other materials provided with the distribution.
|
---|
| 14 | * - The name of the author may not be used to endorse or promote products
|
---|
| 15 | * derived from this software without specific prior written permission.
|
---|
| 16 | *
|
---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 27 | */
|
---|
[a63966d] | 28 | /** @addtogroup wavplay
|
---|
[e677b08] | 29 | * @{
|
---|
| 30 | */
|
---|
| 31 | /**
|
---|
| 32 | * @file PCM playback audio devices
|
---|
| 33 | */
|
---|
| 34 |
|
---|
| 35 | #include <byteorder.h>
|
---|
| 36 | #include <str.h>
|
---|
| 37 | #include <errno.h>
|
---|
| 38 |
|
---|
| 39 | #include "wave.h"
|
---|
| 40 |
|
---|
[0a4ad7d] | 41 | /**
|
---|
| 42 | * Parse wav header data.
|
---|
| 43 | * @param[in] hdata Header data to parse.
|
---|
| 44 | * @param[out] data Pointer to audio data.
|
---|
| 45 | * @param[out] data_size Size of the data after the header.
|
---|
| 46 | * @param[out] channels Number of channels in stored audio format.
|
---|
| 47 | * @param[out] sampling_rate Sampling rate of the store data.
|
---|
| 48 | * @param[out] format Sample format.
|
---|
| 49 | * @param[out] error String representatoin of error, if any.
|
---|
| 50 | * @return Error code.
|
---|
| 51 | *
|
---|
| 52 | * Does sanity checks and endian conversion.
|
---|
| 53 | */
|
---|
[b7fd2a0] | 54 | errno_t wav_parse_header(const void *hdata, const void **data, size_t *data_size,
|
---|
[e677b08] | 55 | unsigned *channels, unsigned *sampling_rate, pcm_sample_format_t *format,
|
---|
| 56 | const char **error)
|
---|
| 57 | {
|
---|
[0a4ad7d] | 58 | if (!hdata) {
|
---|
[e677b08] | 59 | if (error)
|
---|
[0a4ad7d] | 60 | *error = "no header";
|
---|
[e677b08] | 61 | return EINVAL;
|
---|
| 62 | }
|
---|
| 63 |
|
---|
[0a4ad7d] | 64 | const wave_header_t *header = hdata;
|
---|
[e677b08] | 65 | if (str_lcmp(header->chunk_id, CHUNK_ID, 4) != 0) {
|
---|
| 66 | if (error)
|
---|
| 67 | *error = "invalid chunk id";
|
---|
| 68 | return EINVAL;
|
---|
| 69 | }
|
---|
| 70 |
|
---|
| 71 | if (str_lcmp(header->format, FORMAT_STR, 4) != 0) {
|
---|
| 72 | if (error)
|
---|
| 73 | *error = "invalid format string";
|
---|
| 74 | return EINVAL;
|
---|
| 75 | }
|
---|
| 76 |
|
---|
| 77 | if (str_lcmp(header->subchunk1_id, SUBCHUNK1_ID, 4) != 0) {
|
---|
| 78 | if (error)
|
---|
| 79 | *error = "invalid subchunk1 id";
|
---|
| 80 | return EINVAL;
|
---|
| 81 | }
|
---|
| 82 |
|
---|
[60efa35] | 83 | if (uint32_t_le2host(header->subchunk1_size) != PCM_SUBCHUNK1_SIZE) {
|
---|
[da41cc2d] | 84 | //TODO subchunk 1 sizes other than 16 are allowed ( 18, 40)
|
---|
| 85 | //http://www-mmsp.ece.mcgill.ca/documents/AudioFormats/WAVE/WAVE.html
|
---|
[e677b08] | 86 | if (error)
|
---|
| 87 | *error = "invalid subchunk1 size";
|
---|
[3fafe5e0] | 88 | return EINVAL;
|
---|
[e677b08] | 89 | }
|
---|
| 90 |
|
---|
| 91 | if (uint16_t_le2host(header->audio_format) != FORMAT_LINEAR_PCM) {
|
---|
| 92 | if (error)
|
---|
| 93 | *error = "unknown format";
|
---|
| 94 | return ENOTSUP;
|
---|
| 95 | }
|
---|
| 96 |
|
---|
| 97 | if (str_lcmp(header->subchunk2_id, SUBCHUNK2_ID, 4) != 0) {
|
---|
[da41cc2d] | 98 | //TODO basedd on subchunk1 size, we might be reading wrong
|
---|
| 99 | //offset
|
---|
[e677b08] | 100 | if (error)
|
---|
| 101 | *error = "invalid subchunk2 id";
|
---|
[3fafe5e0] | 102 | return EINVAL;
|
---|
[e677b08] | 103 | }
|
---|
| 104 |
|
---|
[da41cc2d] | 105 | //TODO data and data_size are incorrect in extended wav formats
|
---|
| 106 | //pcm params are OK
|
---|
[e677b08] | 107 | if (data)
|
---|
| 108 | *data = header->data;
|
---|
| 109 | if (data_size)
|
---|
| 110 | *data_size = uint32_t_le2host(header->subchunk2_size);
|
---|
| 111 |
|
---|
| 112 | if (sampling_rate)
|
---|
| 113 | *sampling_rate = uint32_t_le2host(header->sampling_rate);
|
---|
| 114 | if (channels)
|
---|
| 115 | *channels = uint16_t_le2host(header->channels);
|
---|
| 116 | if (format) {
|
---|
[0e4c5f0] | 117 | const unsigned size = uint16_t_le2host(header->sample_size);
|
---|
[e677b08] | 118 | switch (size) {
|
---|
[1433ecda] | 119 | case 8:
|
---|
| 120 | *format = PCM_SAMPLE_UINT8;
|
---|
| 121 | break;
|
---|
| 122 | case 16:
|
---|
| 123 | *format = PCM_SAMPLE_SINT16_LE;
|
---|
| 124 | break;
|
---|
| 125 | case 24:
|
---|
| 126 | *format = PCM_SAMPLE_SINT24_LE;
|
---|
| 127 | break;
|
---|
| 128 | case 32:
|
---|
| 129 | *format = PCM_SAMPLE_SINT32_LE;
|
---|
| 130 | break;
|
---|
[e677b08] | 131 | default:
|
---|
| 132 | *error = "Unknown format";
|
---|
| 133 | return ENOTSUP;
|
---|
| 134 | }
|
---|
| 135 | }
|
---|
| 136 | if (error)
|
---|
| 137 | *error = "no error";
|
---|
| 138 |
|
---|
| 139 | return EOK;
|
---|
| 140 | }
|
---|
[0a4ad7d] | 141 |
|
---|
| 142 | /**
|
---|
| 143 | * Initialize wave fromat ehader structure.
|
---|
| 144 | * @param header Structure to initialize.
|
---|
| 145 | * @param format Desired PCM format
|
---|
| 146 | * @param size Size of the stored data.
|
---|
| 147 | *
|
---|
| 148 | * Initializes format specific elements and covnerts endian
|
---|
| 149 | */
|
---|
| 150 | void wav_init_header(wave_header_t *header, pcm_format_t format, size_t size)
|
---|
| 151 | {
|
---|
| 152 | assert(header);
|
---|
| 153 | #define COPY_STR(dst, src) memcpy(dst, src, str_size(src))
|
---|
| 154 |
|
---|
| 155 | COPY_STR(&header->chunk_id, CHUNK_ID);
|
---|
| 156 | COPY_STR(&header->format, FORMAT_STR);
|
---|
| 157 | COPY_STR(&header->subchunk1_id, SUBCHUNK1_ID);
|
---|
| 158 | header->subchunk1_size = host2uint16_t_le(PCM_SUBCHUNK1_SIZE);
|
---|
| 159 | header->audio_format = host2uint16_t_le(FORMAT_LINEAR_PCM);
|
---|
| 160 |
|
---|
| 161 | COPY_STR(&header->subchunk2_id, SUBCHUNK2_ID);
|
---|
| 162 | header->subchunk2_size = host2uint32_t_le(size);
|
---|
| 163 | header->sampling_rate = host2uint32_t_le(format.sampling_rate);
|
---|
| 164 | header->channels = host2uint32_t_le(format.channels);
|
---|
| 165 | header->sample_size =
|
---|
[0e4c5f0] | 166 | host2uint16_t_le(pcm_sample_format_size(format.sample_format) * 8);
|
---|
[0a4ad7d] | 167 | }
|
---|
[e677b08] | 168 | /**
|
---|
| 169 | * @}
|
---|
| 170 | */
|
---|