source: mainline/uspace/app/wavplay/wave.c

Last change on this file was 09ab0a9a, checked in by Jiri Svoboda <jiri@…>, 7 years ago

Fix vertical spacing with new Ccheck revision.

  • Property mode set to 100644
File size: 5.0 KB
Line 
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 wavplay
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/**
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 */
54errno_t wav_parse_header(const void *hdata, const void **data, size_t *data_size,
55 unsigned *channels, unsigned *sampling_rate, pcm_sample_format_t *format,
56 const char **error)
57{
58 if (!hdata) {
59 if (error)
60 *error = "no header";
61 return EINVAL;
62 }
63
64 const wave_header_t *header = hdata;
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
83 if (uint32_t_le2host(header->subchunk1_size) != PCM_SUBCHUNK1_SIZE) {
84 //TODO subchunk 1 sizes other than 16 are allowed ( 18, 40)
85 //http://www-mmsp.ece.mcgill.ca/documents/AudioFormats/WAVE/WAVE.html
86 if (error)
87 *error = "invalid subchunk1 size";
88 return EINVAL;
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) {
98 //TODO basedd on subchunk1 size, we might be reading wrong
99 //offset
100 if (error)
101 *error = "invalid subchunk2 id";
102 return EINVAL;
103 }
104
105 //TODO data and data_size are incorrect in extended wav formats
106 //pcm params are OK
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) {
117 const unsigned size = uint16_t_le2host(header->sample_size);
118 switch (size) {
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;
131 default:
132 *error = "Unknown format";
133 return ENOTSUP;
134 }
135 }
136 if (error)
137 *error = "no error";
138
139 return EOK;
140}
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 */
150void 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 =
166 host2uint16_t_le(pcm_sample_format_size(format.sample_format) * 8);
167}
168/**
169 * @}
170 */
Note: See TracBrowser for help on using the repository browser.