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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since cb500a2 was 0a4ad7d, checked in by Jan Vesely <jano.vesely@…>, 12 years ago

wavplay: Add direct recording functionality

  • Property mode set to 100644
File size: 4.7 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 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/**
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 */
54int 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 (uint16_t_le2host(header->subchunk1_size) != PCM_SUBCHUNK1_SIZE) {
84 if (error)
85 *error = "invalid subchunk1 size";
86 return EINVAL;
87 }
88
89 if (uint16_t_le2host(header->audio_format) != FORMAT_LINEAR_PCM) {
90 if (error)
91 *error = "unknown format";
92 return ENOTSUP;
93 }
94
95 if (str_lcmp(header->subchunk2_id, SUBCHUNK2_ID, 4) != 0) {
96 if (error)
97 *error = "invalid subchunk2 id";
98 return EINVAL;
99 }
100
101
102 if (data)
103 *data = header->data;
104 if (data_size)
105 *data_size = uint32_t_le2host(header->subchunk2_size);
106
107 if (sampling_rate)
108 *sampling_rate = uint32_t_le2host(header->sampling_rate);
109 if (channels)
110 *channels = uint16_t_le2host(header->channels);
111 if (format) {
112 const unsigned size = uint32_t_le2host(header->sample_size);
113 switch (size) {
114 case 8: *format = PCM_SAMPLE_UINT8; break;
115 case 16: *format = PCM_SAMPLE_SINT16_LE; break;
116 case 24: *format = PCM_SAMPLE_SINT24_LE; break;
117 case 32: *format = PCM_SAMPLE_SINT32_LE; break;
118 default:
119 *error = "Unknown format";
120 return ENOTSUP;
121 }
122 }
123 if (error)
124 *error = "no error";
125
126 return EOK;
127}
128
129/**
130 * Initialize wave fromat ehader structure.
131 * @param header Structure to initialize.
132 * @param format Desired PCM format
133 * @param size Size of the stored data.
134 *
135 * Initializes format specific elements and covnerts endian
136 */
137void wav_init_header(wave_header_t *header, pcm_format_t format, size_t size)
138{
139 assert(header);
140#define COPY_STR(dst, src) memcpy(dst, src, str_size(src))
141
142 COPY_STR(&header->chunk_id, CHUNK_ID);
143 COPY_STR(&header->format, FORMAT_STR);
144 COPY_STR(&header->subchunk1_id, SUBCHUNK1_ID);
145 header->subchunk1_size = host2uint16_t_le(PCM_SUBCHUNK1_SIZE);
146 header->audio_format = host2uint16_t_le(FORMAT_LINEAR_PCM);
147
148 COPY_STR(&header->subchunk2_id, SUBCHUNK2_ID);
149 header->subchunk2_size = host2uint32_t_le(size);
150 header->sampling_rate = host2uint32_t_le(format.sampling_rate);
151 header->channels = host2uint32_t_le(format.channels);
152 header->sample_size =
153 host2uint32_t_le(pcm_sample_format_size(format.sample_format));
154}
155/**
156 * @}
157 */
Note: See TracBrowser for help on using the repository browser.