[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 | */
|
---|
| 28 | /** @addtogroup dplay
|
---|
| 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 |
|
---|
| 41 | int wav_parse_header(void *file, const void **data, size_t *data_size,
|
---|
| 42 | unsigned *channels, unsigned *sampling_rate, pcm_sample_format_t *format,
|
---|
| 43 | const char **error)
|
---|
| 44 | {
|
---|
| 45 | if (!file) {
|
---|
| 46 | if (error)
|
---|
| 47 | *error = "file not present";
|
---|
| 48 | return EINVAL;
|
---|
| 49 | }
|
---|
| 50 |
|
---|
| 51 | const wave_header_t *header = file;
|
---|
| 52 | if (str_lcmp(header->chunk_id, CHUNK_ID, 4) != 0) {
|
---|
| 53 | if (error)
|
---|
| 54 | *error = "invalid chunk id";
|
---|
| 55 | return EINVAL;
|
---|
| 56 | }
|
---|
| 57 |
|
---|
| 58 | if (str_lcmp(header->format, FORMAT_STR, 4) != 0) {
|
---|
| 59 | if (error)
|
---|
| 60 | *error = "invalid format string";
|
---|
| 61 | return EINVAL;
|
---|
| 62 | }
|
---|
| 63 |
|
---|
| 64 | if (str_lcmp(header->subchunk1_id, SUBCHUNK1_ID, 4) != 0) {
|
---|
| 65 | if (error)
|
---|
| 66 | *error = "invalid subchunk1 id";
|
---|
| 67 | return EINVAL;
|
---|
| 68 | }
|
---|
| 69 |
|
---|
| 70 | if (uint16_t_le2host(header->subchunk1_size) != PCM_SUBCHUNK1_SIZE) {
|
---|
| 71 | if (error)
|
---|
| 72 | *error = "invalid subchunk1 size";
|
---|
| 73 | return EINVAL;
|
---|
| 74 | }
|
---|
| 75 |
|
---|
| 76 | if (uint16_t_le2host(header->audio_format) != FORMAT_LINEAR_PCM) {
|
---|
| 77 | if (error)
|
---|
| 78 | *error = "unknown format";
|
---|
| 79 | return ENOTSUP;
|
---|
| 80 | }
|
---|
| 81 |
|
---|
| 82 | if (str_lcmp(header->subchunk2_id, SUBCHUNK2_ID, 4) != 0) {
|
---|
| 83 | if (error)
|
---|
| 84 | *error = "invalid subchunk2 id";
|
---|
| 85 | return EINVAL;
|
---|
| 86 | }
|
---|
| 87 |
|
---|
| 88 |
|
---|
| 89 | if (data)
|
---|
| 90 | *data = header->data;
|
---|
| 91 | if (data_size)
|
---|
| 92 | *data_size = uint32_t_le2host(header->subchunk2_size);
|
---|
| 93 |
|
---|
| 94 | if (sampling_rate)
|
---|
| 95 | *sampling_rate = uint32_t_le2host(header->sampling_rate);
|
---|
| 96 | if (channels)
|
---|
| 97 | *channels = uint16_t_le2host(header->channels);
|
---|
| 98 | if (format) {
|
---|
| 99 | const unsigned size = uint32_t_le2host(header->sample_size);
|
---|
| 100 | switch (size) {
|
---|
| 101 | case 8: *format = PCM_SAMPLE_UINT8; break;
|
---|
| 102 | case 16: *format = PCM_SAMPLE_SINT16_LE; break;
|
---|
| 103 | case 24: *format = PCM_SAMPLE_SINT24_LE; break;
|
---|
| 104 | case 32: *format = PCM_SAMPLE_SINT32_LE; break;
|
---|
| 105 | default:
|
---|
| 106 | *error = "Unknown format";
|
---|
| 107 | return ENOTSUP;
|
---|
| 108 | }
|
---|
| 109 | }
|
---|
| 110 | if (error)
|
---|
| 111 | *error = "no error";
|
---|
| 112 |
|
---|
| 113 | return EOK;
|
---|
| 114 | }
|
---|
| 115 | /**
|
---|
| 116 | * @}
|
---|
| 117 | */
|
---|