[346643c] | 1 | /*
|
---|
| 2 | * Copyright (c) 2012 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 |
|
---|
| 29 | /** @addtogroup audio
|
---|
| 30 | * @brief PCM sample format
|
---|
| 31 | * @{
|
---|
| 32 | */
|
---|
| 33 | /** @file
|
---|
| 34 | */
|
---|
| 35 |
|
---|
| 36 | #ifndef PCM_SAMPLE_FORMAT_H_
|
---|
| 37 | #define PCM_SAMPLE_FORMAT_H_
|
---|
| 38 |
|
---|
[03362fbd] | 39 | #include <stdbool.h>
|
---|
[20d77fb] | 40 | #include <time.h>
|
---|
[d30292e] | 41 |
|
---|
[b1dfe13] | 42 | /** Known and supported PCM sample formats */
|
---|
[346643c] | 43 | typedef enum {
|
---|
| 44 | PCM_SAMPLE_UINT8,
|
---|
| 45 | PCM_SAMPLE_SINT8,
|
---|
| 46 | PCM_SAMPLE_UINT16_LE,
|
---|
| 47 | PCM_SAMPLE_UINT16_BE,
|
---|
| 48 | PCM_SAMPLE_SINT16_LE,
|
---|
| 49 | PCM_SAMPLE_SINT16_BE,
|
---|
| 50 | PCM_SAMPLE_UINT24_LE,
|
---|
| 51 | PCM_SAMPLE_UINT24_BE,
|
---|
| 52 | PCM_SAMPLE_SINT24_LE,
|
---|
| 53 | PCM_SAMPLE_SINT24_BE,
|
---|
| 54 | PCM_SAMPLE_UINT24_32_LE,
|
---|
| 55 | PCM_SAMPLE_UINT24_32_BE,
|
---|
| 56 | PCM_SAMPLE_SINT24_32_LE,
|
---|
| 57 | PCM_SAMPLE_SINT24_32_BE,
|
---|
| 58 | PCM_SAMPLE_UINT32_LE,
|
---|
| 59 | PCM_SAMPLE_UINT32_BE,
|
---|
| 60 | PCM_SAMPLE_SINT32_LE,
|
---|
| 61 | PCM_SAMPLE_SINT32_BE,
|
---|
| 62 | PCM_SAMPLE_FLOAT32,
|
---|
[d30292e] | 63 | PCM_SAMPLE_FORMAT_LAST = PCM_SAMPLE_FLOAT32,
|
---|
[346643c] | 64 | } pcm_sample_format_t;
|
---|
| 65 |
|
---|
[b1dfe13] | 66 | /**
|
---|
| 67 | * Query if the format uses signed values.
|
---|
| 68 | * @param format PCM sample format.
|
---|
| 69 | * @return True if the format uses signed values, false otherwise.
|
---|
| 70 | */
|
---|
[d30292e] | 71 | static inline bool pcm_sample_format_is_signed(pcm_sample_format_t format)
|
---|
| 72 | {
|
---|
[1433ecda] | 73 | switch (format) {
|
---|
[d30292e] | 74 | case PCM_SAMPLE_SINT8:
|
---|
| 75 | case PCM_SAMPLE_SINT16_LE:
|
---|
| 76 | case PCM_SAMPLE_SINT16_BE:
|
---|
| 77 | case PCM_SAMPLE_SINT24_LE:
|
---|
| 78 | case PCM_SAMPLE_SINT24_BE:
|
---|
| 79 | case PCM_SAMPLE_SINT24_32_LE:
|
---|
| 80 | case PCM_SAMPLE_SINT24_32_BE:
|
---|
| 81 | case PCM_SAMPLE_SINT32_LE:
|
---|
| 82 | case PCM_SAMPLE_SINT32_BE:
|
---|
| 83 | return true;
|
---|
| 84 | case PCM_SAMPLE_UINT8:
|
---|
| 85 | case PCM_SAMPLE_UINT16_LE:
|
---|
| 86 | case PCM_SAMPLE_UINT16_BE:
|
---|
| 87 | case PCM_SAMPLE_UINT24_LE:
|
---|
| 88 | case PCM_SAMPLE_UINT24_BE:
|
---|
| 89 | case PCM_SAMPLE_UINT24_32_LE:
|
---|
| 90 | case PCM_SAMPLE_UINT24_32_BE:
|
---|
| 91 | case PCM_SAMPLE_UINT32_LE:
|
---|
| 92 | case PCM_SAMPLE_UINT32_BE:
|
---|
| 93 | case PCM_SAMPLE_FLOAT32:
|
---|
| 94 | default:
|
---|
| 95 | return false;
|
---|
| 96 | }
|
---|
| 97 | }
|
---|
| 98 |
|
---|
[b1dfe13] | 99 | /**
|
---|
| 100 | * Query byte-size of samples.
|
---|
| 101 | * @param format PCM sample format.
|
---|
| 102 | * @return Size in bytes of a single sample.
|
---|
| 103 | */
|
---|
[346643c] | 104 | static inline size_t pcm_sample_format_size(pcm_sample_format_t format)
|
---|
| 105 | {
|
---|
[1433ecda] | 106 | switch (format) {
|
---|
[039337e8] | 107 | case PCM_SAMPLE_UINT8:
|
---|
| 108 | case PCM_SAMPLE_SINT8:
|
---|
| 109 | return 1;
|
---|
| 110 | case PCM_SAMPLE_UINT16_LE:
|
---|
| 111 | case PCM_SAMPLE_UINT16_BE:
|
---|
| 112 | case PCM_SAMPLE_SINT16_LE:
|
---|
| 113 | case PCM_SAMPLE_SINT16_BE:
|
---|
| 114 | return 2;
|
---|
| 115 | case PCM_SAMPLE_UINT24_LE:
|
---|
| 116 | case PCM_SAMPLE_UINT24_BE:
|
---|
| 117 | case PCM_SAMPLE_SINT24_LE:
|
---|
| 118 | case PCM_SAMPLE_SINT24_BE:
|
---|
| 119 | return 3;
|
---|
| 120 | case PCM_SAMPLE_UINT24_32_LE:
|
---|
| 121 | case PCM_SAMPLE_UINT24_32_BE:
|
---|
| 122 | case PCM_SAMPLE_SINT24_32_LE:
|
---|
| 123 | case PCM_SAMPLE_SINT24_32_BE:
|
---|
| 124 | case PCM_SAMPLE_UINT32_LE:
|
---|
| 125 | case PCM_SAMPLE_UINT32_BE:
|
---|
| 126 | case PCM_SAMPLE_SINT32_LE:
|
---|
| 127 | case PCM_SAMPLE_SINT32_BE:
|
---|
| 128 | case PCM_SAMPLE_FLOAT32:
|
---|
| 129 | return 4;
|
---|
| 130 | default:
|
---|
| 131 | return 0;
|
---|
[346643c] | 132 | }
|
---|
| 133 | }
|
---|
| 134 |
|
---|
[b1dfe13] | 135 | /**
|
---|
| 136 | * Query sie of the entire frame.
|
---|
| 137 | * @param channels Number of samples in every frame.
|
---|
| 138 | * @param format PCM sample format.
|
---|
| 139 | * @return Size in bytes.
|
---|
| 140 | */
|
---|
[20d77fb] | 141 | static inline size_t pcm_sample_format_frame_size(unsigned channels,
|
---|
| 142 | pcm_sample_format_t format)
|
---|
| 143 | {
|
---|
| 144 | return pcm_sample_format_size(format) * channels;
|
---|
| 145 | }
|
---|
| 146 |
|
---|
[b1dfe13] | 147 | /**
|
---|
| 148 | * Count number of frames that fit into a buffer (even incomplete frames).
|
---|
| 149 | * @param size Size of the buffer.
|
---|
| 150 | * @param channels Number of samples in every frame.
|
---|
| 151 | * @param format PCM sample format.
|
---|
| 152 | * @return Number of frames (even incomplete).
|
---|
| 153 | */
|
---|
[20d77fb] | 154 | static inline size_t pcm_sample_format_size_to_frames(size_t size,
|
---|
| 155 | unsigned channels, pcm_sample_format_t format)
|
---|
| 156 | {
|
---|
| 157 | const size_t frame_size = pcm_sample_format_frame_size(channels, format);
|
---|
| 158 | return (size + frame_size - 1) / frame_size;
|
---|
| 159 | }
|
---|
| 160 |
|
---|
[b1dfe13] | 161 | /**
|
---|
| 162 | * Convert byte size to time.
|
---|
| 163 | * @param size Size of the buffer.
|
---|
| 164 | * @param sample_rate Samples per second.
|
---|
| 165 | * @param channels Number of samples in every frame.
|
---|
| 166 | * @param format PCM sample format.
|
---|
| 167 | * @return Number of useconds of audio data.
|
---|
| 168 | */
|
---|
[bd41ac52] | 169 | static inline usec_t pcm_sample_format_size_to_usec(size_t size,
|
---|
[20d77fb] | 170 | unsigned sample_rate, unsigned channels, pcm_sample_format_t format)
|
---|
| 171 | {
|
---|
[b1dfe13] | 172 | const unsigned long long frames =
|
---|
[20d77fb] | 173 | pcm_sample_format_size_to_frames(size, channels, format);
|
---|
| 174 | return (frames * 1000000ULL) / sample_rate;
|
---|
| 175 | }
|
---|
| 176 |
|
---|
[b1dfe13] | 177 | /**
|
---|
| 178 | * Get readable name of a sample format.
|
---|
| 179 | * @param format PCM sample format.
|
---|
| 180 | * @return Valid string representation.
|
---|
| 181 | */
|
---|
[1433ecda] | 182 | static inline const char *pcm_sample_format_str(pcm_sample_format_t format)
|
---|
[346643c] | 183 | {
|
---|
[1433ecda] | 184 | switch (format) {
|
---|
[039337e8] | 185 | case PCM_SAMPLE_UINT8:
|
---|
[2752620b] | 186 | return "8 bit unsigned";
|
---|
[039337e8] | 187 | case PCM_SAMPLE_SINT8:
|
---|
[2752620b] | 188 | return "8 bit signed";
|
---|
[039337e8] | 189 | case PCM_SAMPLE_UINT16_LE:
|
---|
| 190 | return "16 bit unsigned(LE)";
|
---|
| 191 | case PCM_SAMPLE_SINT16_LE:
|
---|
[2752620b] | 192 | return "16 bit signed(LE)";
|
---|
[039337e8] | 193 | case PCM_SAMPLE_UINT16_BE:
|
---|
| 194 | return "16 bit unsigned(BE)";
|
---|
| 195 | case PCM_SAMPLE_SINT16_BE:
|
---|
| 196 | return "16 bit signed(BE)";
|
---|
| 197 | case PCM_SAMPLE_UINT24_LE:
|
---|
| 198 | return "24 bit unsigned(LE)";
|
---|
| 199 | case PCM_SAMPLE_SINT24_LE:
|
---|
| 200 | return "24 bit signed(LE)";
|
---|
| 201 | case PCM_SAMPLE_UINT24_BE:
|
---|
| 202 | return "24 bit unsigned(BE)";
|
---|
| 203 | case PCM_SAMPLE_SINT24_BE:
|
---|
| 204 | return "24 bit signed(BE)";
|
---|
| 205 | case PCM_SAMPLE_UINT24_32_LE:
|
---|
| 206 | return "24 bit(4byte aligned) unsigned(LE)";
|
---|
| 207 | case PCM_SAMPLE_UINT24_32_BE:
|
---|
| 208 | return "24 bit(4byte aligned) unsigned(BE)";
|
---|
| 209 | case PCM_SAMPLE_SINT24_32_LE:
|
---|
| 210 | return "24 bit(4byte aligned) signed(LE)";
|
---|
| 211 | case PCM_SAMPLE_SINT24_32_BE:
|
---|
| 212 | return "24 bit(4byte aligned) signed(BE)";
|
---|
| 213 | case PCM_SAMPLE_UINT32_LE:
|
---|
| 214 | return "32 bit unsigned(LE)";
|
---|
| 215 | case PCM_SAMPLE_UINT32_BE:
|
---|
| 216 | return "32 bit unsigned(BE)";
|
---|
| 217 | case PCM_SAMPLE_SINT32_LE:
|
---|
| 218 | return "32 bit signed(LE)";
|
---|
| 219 | case PCM_SAMPLE_SINT32_BE:
|
---|
| 220 | return "32 bit signed(BE)";
|
---|
| 221 | case PCM_SAMPLE_FLOAT32:
|
---|
| 222 | return "32 bit float";
|
---|
| 223 | default:
|
---|
| 224 | return "Unknown sample format";
|
---|
[346643c] | 225 | }
|
---|
| 226 | }
|
---|
| 227 |
|
---|
| 228 | #endif
|
---|
| 229 |
|
---|
| 230 | /**
|
---|
| 231 | * @}
|
---|
| 232 | */
|
---|