| 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 |
|
|---|
| 39 | #include <stdbool.h>
|
|---|
| 40 | #include <time.h>
|
|---|
| 41 |
|
|---|
| 42 | typedef enum {
|
|---|
| 43 | PCM_SAMPLE_UINT8,
|
|---|
| 44 | PCM_SAMPLE_SINT8,
|
|---|
| 45 | PCM_SAMPLE_UINT16_LE,
|
|---|
| 46 | PCM_SAMPLE_UINT16_BE,
|
|---|
| 47 | PCM_SAMPLE_SINT16_LE,
|
|---|
| 48 | PCM_SAMPLE_SINT16_BE,
|
|---|
| 49 | PCM_SAMPLE_UINT24_LE,
|
|---|
| 50 | PCM_SAMPLE_UINT24_BE,
|
|---|
| 51 | PCM_SAMPLE_SINT24_LE,
|
|---|
| 52 | PCM_SAMPLE_SINT24_BE,
|
|---|
| 53 | PCM_SAMPLE_UINT24_32_LE,
|
|---|
| 54 | PCM_SAMPLE_UINT24_32_BE,
|
|---|
| 55 | PCM_SAMPLE_SINT24_32_LE,
|
|---|
| 56 | PCM_SAMPLE_SINT24_32_BE,
|
|---|
| 57 | PCM_SAMPLE_UINT32_LE,
|
|---|
| 58 | PCM_SAMPLE_UINT32_BE,
|
|---|
| 59 | PCM_SAMPLE_SINT32_LE,
|
|---|
| 60 | PCM_SAMPLE_SINT32_BE,
|
|---|
| 61 | PCM_SAMPLE_FLOAT32,
|
|---|
| 62 | PCM_SAMPLE_FORMAT_LAST = PCM_SAMPLE_FLOAT32,
|
|---|
| 63 | } pcm_sample_format_t;
|
|---|
| 64 |
|
|---|
| 65 | static inline bool pcm_sample_format_is_signed(pcm_sample_format_t format)
|
|---|
| 66 | {
|
|---|
| 67 | switch(format) {
|
|---|
| 68 | case PCM_SAMPLE_SINT8:
|
|---|
| 69 | case PCM_SAMPLE_SINT16_LE:
|
|---|
| 70 | case PCM_SAMPLE_SINT16_BE:
|
|---|
| 71 | case PCM_SAMPLE_SINT24_LE:
|
|---|
| 72 | case PCM_SAMPLE_SINT24_BE:
|
|---|
| 73 | case PCM_SAMPLE_SINT24_32_LE:
|
|---|
| 74 | case PCM_SAMPLE_SINT24_32_BE:
|
|---|
| 75 | case PCM_SAMPLE_SINT32_LE:
|
|---|
| 76 | case PCM_SAMPLE_SINT32_BE:
|
|---|
| 77 | return true;
|
|---|
| 78 | case PCM_SAMPLE_UINT8:
|
|---|
| 79 | case PCM_SAMPLE_UINT16_LE:
|
|---|
| 80 | case PCM_SAMPLE_UINT16_BE:
|
|---|
| 81 | case PCM_SAMPLE_UINT24_LE:
|
|---|
| 82 | case PCM_SAMPLE_UINT24_BE:
|
|---|
| 83 | case PCM_SAMPLE_UINT24_32_LE:
|
|---|
| 84 | case PCM_SAMPLE_UINT24_32_BE:
|
|---|
| 85 | case PCM_SAMPLE_UINT32_LE:
|
|---|
| 86 | case PCM_SAMPLE_UINT32_BE:
|
|---|
| 87 | case PCM_SAMPLE_FLOAT32:
|
|---|
| 88 | default:
|
|---|
| 89 | return false;
|
|---|
| 90 | }
|
|---|
| 91 | }
|
|---|
| 92 |
|
|---|
| 93 | static inline size_t pcm_sample_format_size(pcm_sample_format_t format)
|
|---|
| 94 | {
|
|---|
| 95 | switch(format) {
|
|---|
| 96 | case PCM_SAMPLE_UINT8:
|
|---|
| 97 | case PCM_SAMPLE_SINT8:
|
|---|
| 98 | return 1;
|
|---|
| 99 | case PCM_SAMPLE_UINT16_LE:
|
|---|
| 100 | case PCM_SAMPLE_UINT16_BE:
|
|---|
| 101 | case PCM_SAMPLE_SINT16_LE:
|
|---|
| 102 | case PCM_SAMPLE_SINT16_BE:
|
|---|
| 103 | return 2;
|
|---|
| 104 | case PCM_SAMPLE_UINT24_LE:
|
|---|
| 105 | case PCM_SAMPLE_UINT24_BE:
|
|---|
| 106 | case PCM_SAMPLE_SINT24_LE:
|
|---|
| 107 | case PCM_SAMPLE_SINT24_BE:
|
|---|
| 108 | return 3;
|
|---|
| 109 | case PCM_SAMPLE_UINT24_32_LE:
|
|---|
| 110 | case PCM_SAMPLE_UINT24_32_BE:
|
|---|
| 111 | case PCM_SAMPLE_SINT24_32_LE:
|
|---|
| 112 | case PCM_SAMPLE_SINT24_32_BE:
|
|---|
| 113 | case PCM_SAMPLE_UINT32_LE:
|
|---|
| 114 | case PCM_SAMPLE_UINT32_BE:
|
|---|
| 115 | case PCM_SAMPLE_SINT32_LE:
|
|---|
| 116 | case PCM_SAMPLE_SINT32_BE:
|
|---|
| 117 | case PCM_SAMPLE_FLOAT32:
|
|---|
| 118 | return 4;
|
|---|
| 119 | default:
|
|---|
| 120 | return 0;
|
|---|
| 121 | }
|
|---|
| 122 | }
|
|---|
| 123 |
|
|---|
| 124 | static inline size_t pcm_sample_format_frame_size(unsigned channels,
|
|---|
| 125 | pcm_sample_format_t format)
|
|---|
| 126 | {
|
|---|
| 127 | return pcm_sample_format_size(format) * channels;
|
|---|
| 128 | }
|
|---|
| 129 |
|
|---|
| 130 | static inline size_t pcm_sample_format_size_to_frames(size_t size,
|
|---|
| 131 | unsigned channels, pcm_sample_format_t format)
|
|---|
| 132 | {
|
|---|
| 133 | const size_t frame_size = pcm_sample_format_frame_size(channels, format);
|
|---|
| 134 | return (size + frame_size - 1) / frame_size;
|
|---|
| 135 | }
|
|---|
| 136 |
|
|---|
| 137 | static inline useconds_t pcm_sample_format_size_to_usec(size_t size,
|
|---|
| 138 | unsigned sample_rate, unsigned channels, pcm_sample_format_t format)
|
|---|
| 139 | {
|
|---|
| 140 | const long long frames =
|
|---|
| 141 | pcm_sample_format_size_to_frames(size, channels, format);
|
|---|
| 142 | return (frames * 1000000ULL) / sample_rate;
|
|---|
| 143 | }
|
|---|
| 144 |
|
|---|
| 145 | static inline const char * pcm_sample_format_str(pcm_sample_format_t format)
|
|---|
| 146 | {
|
|---|
| 147 | switch(format) {
|
|---|
| 148 | case PCM_SAMPLE_UINT8:
|
|---|
| 149 | return "8 bit unsinged";
|
|---|
| 150 | case PCM_SAMPLE_SINT8:
|
|---|
| 151 | return "8 bit singed";
|
|---|
| 152 | case PCM_SAMPLE_UINT16_LE:
|
|---|
| 153 | return "16 bit unsigned(LE)";
|
|---|
| 154 | case PCM_SAMPLE_SINT16_LE:
|
|---|
| 155 | return "16 bit singed(LE)";
|
|---|
| 156 | case PCM_SAMPLE_UINT16_BE:
|
|---|
| 157 | return "16 bit unsigned(BE)";
|
|---|
| 158 | case PCM_SAMPLE_SINT16_BE:
|
|---|
| 159 | return "16 bit signed(BE)";
|
|---|
| 160 | case PCM_SAMPLE_UINT24_LE:
|
|---|
| 161 | return "24 bit unsigned(LE)";
|
|---|
| 162 | case PCM_SAMPLE_SINT24_LE:
|
|---|
| 163 | return "24 bit signed(LE)";
|
|---|
| 164 | case PCM_SAMPLE_UINT24_BE:
|
|---|
| 165 | return "24 bit unsigned(BE)";
|
|---|
| 166 | case PCM_SAMPLE_SINT24_BE:
|
|---|
| 167 | return "24 bit signed(BE)";
|
|---|
| 168 | case PCM_SAMPLE_UINT24_32_LE:
|
|---|
| 169 | return "24 bit(4byte aligned) unsigned(LE)";
|
|---|
| 170 | case PCM_SAMPLE_UINT24_32_BE:
|
|---|
| 171 | return "24 bit(4byte aligned) unsigned(BE)";
|
|---|
| 172 | case PCM_SAMPLE_SINT24_32_LE:
|
|---|
| 173 | return "24 bit(4byte aligned) signed(LE)";
|
|---|
| 174 | case PCM_SAMPLE_SINT24_32_BE:
|
|---|
| 175 | return "24 bit(4byte aligned) signed(BE)";
|
|---|
| 176 | case PCM_SAMPLE_UINT32_LE:
|
|---|
| 177 | return "32 bit unsigned(LE)";
|
|---|
| 178 | case PCM_SAMPLE_UINT32_BE:
|
|---|
| 179 | return "32 bit unsigned(BE)";
|
|---|
| 180 | case PCM_SAMPLE_SINT32_LE:
|
|---|
| 181 | return "32 bit signed(LE)";
|
|---|
| 182 | case PCM_SAMPLE_SINT32_BE:
|
|---|
| 183 | return "32 bit signed(BE)";
|
|---|
| 184 | case PCM_SAMPLE_FLOAT32:
|
|---|
| 185 | return "32 bit float";
|
|---|
| 186 | default:
|
|---|
| 187 | return "Unknown sample format";
|
|---|
| 188 | }
|
|---|
| 189 | }
|
|---|
| 190 |
|
|---|
| 191 | #endif
|
|---|
| 192 |
|
|---|
| 193 | /**
|
|---|
| 194 | * @}
|
|---|
| 195 | */
|
|---|
| 196 |
|
|---|