libpromeki main
PROfessional MEdia toolKIt
 
Loading...
Searching...
No Matches
audiodesc.h
Go to the documentation of this file.
1
8#pragma once
9
13#include <promeki/core/string.h>
14#include <promeki/core/system.h>
15#include <promeki/core/json.h>
16
18
28class AudioDesc {
30 public:
33
35 static const int32_t MinS24 = -8388608;
37 static const int32_t MaxS24 = 8388607;
39 static const int32_t MinU24 = 0;
41 static const int32_t MaxU24 = 16777215;
42
54 template <typename IntegerType, IntegerType Min, IntegerType Max>
56 static_assert(std::is_integral<IntegerType>::value, "IntegerType must be an integer.");
57 constexpr float min = static_cast<float>(Min);
58 constexpr float max = static_cast<float>(Max);
59 return((static_cast<float>(value) - min) * 2.0f / (max - min)) - 1.0f;
60 }
61
68 template <typename IntegerType>
70 static_assert(std::is_integral<IntegerType>::value, "IntegerType must be an integer.");
72 std::numeric_limits<IntegerType>::min(),
73 std::numeric_limits<IntegerType>::max()>(value);
74 }
75
88 template <typename IntegerType, IntegerType Min, IntegerType Max>
90 static_assert(std::is_integral<IntegerType>::value, "IntegerType must be an integer.");
91 const float min = static_cast<float>(Min);
92 const float max = static_cast<float>(Max);
93 if(value <= -1.0f) return Min;
94 else if(value >= 1.0f) return Max;
95 return static_cast<IntegerType>((value + 1.0f) * 0.5f * (max - min) + min);
96 }
97
104 template <typename IntegerType>
106 static_assert(std::is_integral<IntegerType>::value, "IntegerType must be an integer.");
108 std::numeric_limits<IntegerType>::min(),
109 std::numeric_limits<IntegerType>::max()>(value);
110 }
111
123 template <typename IntegerType, bool InputIsBigEndian>
124 static void samplesToFloat(float *out, const uint8_t *inbuf, size_t samples) {
125 static_assert(std::is_integral<IntegerType>::value, "IntegerType must be an integer.");
126 const IntegerType *in = reinterpret_cast<const IntegerType *>(inbuf);
127 for(size_t i = 0; i < samples; ++i) {
128 IntegerType val = *in++;
131 }
132 return;
133 }
134
146 template <typename IntegerType, bool OutputIsBigEndian>
147 static void floatToSamples(uint8_t *outbuf, const float *in, size_t samples) {
148 static_assert(std::is_integral<IntegerType>::value, "IntegerType must be an integer.");
149 IntegerType *out = reinterpret_cast<IntegerType *>(outbuf);
150 for(size_t i = 0; i < samples; ++i) {
153 *out++ = val;
154 }
155 return;
156 }
157
164 struct Format {
165 int id;
170 bool isSigned;
171 bool isPlanar;
173 void (*samplesToFloat)(float *out, const uint8_t *in, size_t samples);
174 void (*floatToSamples)(uint8_t *out, const float *in, size_t samples);
175 };
176
182 static const Format *lookupFormat(int id);
183
209
212
219
226 static AudioDesc fromJson(const JsonObject &json, Error *err = nullptr);
227
230
239 AudioDesc(float sr, unsigned int ch) :
240 _dataType(NativeType), _sampleRate(sr),
241 _channels(ch), _format(lookupFormat(NativeType))
242 {
243 if(!isValid()) {
244 _dataType = Invalid;
245 _sampleRate = 0.0f;
246 _channels = 0;
247 _format = lookupFormat(Invalid);
248 }
249 }
250
260 AudioDesc(DataType dt, float sr, unsigned int ch) :
261 _dataType(dt), _sampleRate(sr),
262 _channels(ch), _format(lookupFormat(dt))
263 {
264 if(!isValid()) {
265 _dataType = Invalid;
266 _sampleRate = 0.0f;
267 _channels = 0;
268 _format = lookupFormat(Invalid);
269 }
270 }
271
277 bool formatEquals(const AudioDesc &other) const {
278 return _dataType == other._dataType &&
279 _sampleRate == other._sampleRate &&
280 _channels == other._channels;
281 }
282
288 bool operator==(const AudioDesc &other) const {
289 return formatEquals(other) &&
290 _metadata == other._metadata;
291 }
292
297 bool isValid() const {
298 return _dataType != 0 && _sampleRate > 0.0f && _channels > 0;
299 }
300
305 bool isNative() const {
306 return _dataType == NativeType;
307 }
308
314 return AudioDesc(NativeType, _sampleRate, _channels);
315 }
316
321 String toString() const {
322 return String::sprintf("[%s %fHz %uc]",
323 _format->name.cstr(), _sampleRate, _channels);
324 }
325
332 ret.set("DataType", _format->name);
333 ret.set("SampleRate", _sampleRate);
334 ret.set("Channels", _channels);
335 if(!_metadata.isEmpty()) ret.set("Metadata", _metadata.toJson());
336 return ret;
337 }
338
343 size_t bytesPerSample() const {
344 return _format->bytesPerSample;
345 }
346
355 size_t bytesPerSampleStride() const {
356 return _format->isPlanar ?
357 _format->bytesPerSample :
358 _format->bytesPerSample * _channels;
359 }
360
371 size_t channelBufferOffset(unsigned int chan, size_t bufferSamples) const {
372 return _format->isPlanar ?
373 _format->bytesPerSample * bufferSamples * chan :
374 _format->bytesPerSample * chan;
375 }
376
382 size_t bufferSize(size_t samples) const {
383 return _format->bytesPerSample * _channels * samples;
384 }
385
391 return (DataType)_dataType;
392 }
393
399 _dataType = val;
400 return;
401 }
402
407 float sampleRate() const {
408 return _sampleRate;
409 }
410
415 void setSampleRate(float val) {
416 _sampleRate = val;
417 return;
418 }
419
424 unsigned int channels() const {
425 return _channels;
426 }
427
432 void setChannels(unsigned int val) {
433 _channels = val;
434 return;
435 }
436
438 const Metadata &metadata() const {
439 return _metadata;
440 }
441
444 return _metadata;
445 }
446
453 void samplesToFloat(float *out, const uint8_t *in, size_t samples) const {
454 _format->samplesToFloat(out, in, samples * _channels);
455 return;
456 }
457
464 void floatToSamples(uint8_t *out, const float *in, size_t samples) const {
465 _format->floatToSamples(out, in, samples * _channels);
466 return;
467 }
468
469 private:
470 int _dataType = 0;
471 float _sampleRate = 0.0f;
472 unsigned int _channels = 0;
473 Metadata _metadata;
474 const Format *_format = nullptr;
475};
476
478
Describes an audio format including sample type, rate, and channel count.
Definition audiodesc.h:28
void samplesToFloat(float *out, const uint8_t *in, size_t samples) const
Converts samples from this description's format to normalized floats.
Definition audiodesc.h:453
static AudioDesc fromJson(const JsonObject &json, Error *err=nullptr)
Constructs an AudioDesc from a JSON object.
AudioDesc(DataType dt, float sr, unsigned int ch)
Constructs an audio description with the specified data type, sample rate, and channels.
Definition audiodesc.h:260
const Metadata & metadata() const
Returns a const reference to the metadata.
Definition audiodesc.h:438
static IntegerType floatToInteger(float value)
Converts a normalized float in [-1, 1] to an integer sample value.
Definition audiodesc.h:89
unsigned int channels() const
Returns the number of audio channels.
Definition audiodesc.h:424
void setChannels(unsigned int val)
Sets the number of audio channels.
Definition audiodesc.h:432
void setSampleRate(float val)
Sets the sample rate.
Definition audiodesc.h:415
size_t bytesPerSample() const
Returns the number of bytes per single sample.
Definition audiodesc.h:343
void setDataType(DataType val)
Sets the data type.
Definition audiodesc.h:398
String toString() const
Returns a human-readable string representation of this audio description.
Definition audiodesc.h:321
static const int32_t MinS24
Minimum value of a signed 24-bit integer.
Definition audiodesc.h:35
size_t bufferSize(size_t samples) const
Returns the total buffer size in bytes needed to store the given number of samples.
Definition audiodesc.h:382
AudioDesc(float sr, unsigned int ch)
Constructs an audio description with the native float format.
Definition audiodesc.h:239
bool operator==(const AudioDesc &other) const
Returns true if both audio descriptions are fully equal, including metadata.
Definition audiodesc.h:288
static constexpr DataType NativeType
The native float format for the current platform's endianness.
Definition audiodesc.h:211
static float integerToFloat(IntegerType value)
Converts an integer sample value to a normalized float using the type's full range.
Definition audiodesc.h:69
void floatToSamples(uint8_t *out, const float *in, size_t samples) const
Converts normalized floats to samples in this description's format.
Definition audiodesc.h:464
AudioDesc()
Constructs an invalid (default) audio description.
Definition audiodesc.h:229
static const int32_t MinU24
Minimum value of an unsigned 24-bit integer.
Definition audiodesc.h:39
bool formatEquals(const AudioDesc &other) const
Returns true if both audio descriptions have equal format (type, rate, channels).
Definition audiodesc.h:277
float sampleRate() const
Returns the sample rate in Hz.
Definition audiodesc.h:407
static IntegerType floatToInteger(float value)
Converts a normalized float to an integer sample using the type's full range.
Definition audiodesc.h:105
static float integerToFloat(IntegerType value)
Converts an integer sample value to a normalized float in [-1, 1].
Definition audiodesc.h:55
static const Format * lookupFormat(int id)
Looks up a Format descriptor by its DataType id.
static void samplesToFloat(float *out, const uint8_t *inbuf, size_t samples)
Converts a buffer of integer samples to normalized floats.
Definition audiodesc.h:124
size_t channelBufferOffset(unsigned int chan, size_t bufferSamples) const
Returns the byte offset to a specific channel within a sample buffer.
Definition audiodesc.h:371
DataType
Enumeration of supported audio sample data types.
Definition audiodesc.h:190
@ PCMI_S32BE
Signed 32-bit integer, big-endian, interleaved.
Definition audiodesc.h:206
@ PCMI_S32LE
Signed 32-bit integer, little-endian, interleaved.
Definition audiodesc.h:204
@ PCMI_U32BE
Unsigned 32-bit integer, big-endian, interleaved.
Definition audiodesc.h:207
@ PCMI_Float32BE
32-bit float, big-endian, interleaved.
Definition audiodesc.h:193
@ PCMI_U24LE
Unsigned 24-bit integer, little-endian, interleaved.
Definition audiodesc.h:201
@ PCMI_S16BE
Signed 16-bit integer, big-endian, interleaved.
Definition audiodesc.h:198
@ PCMI_U24BE
Unsigned 24-bit integer, big-endian, interleaved.
Definition audiodesc.h:203
@ PCMI_U32LE
Unsigned 32-bit integer, little-endian, interleaved.
Definition audiodesc.h:205
@ PCMI_U8
Unsigned 8-bit integer, interleaved.
Definition audiodesc.h:195
@ PCMI_Float32LE
32-bit float, little-endian, interleaved.
Definition audiodesc.h:192
@ PCMI_S16LE
Signed 16-bit integer, little-endian, interleaved.
Definition audiodesc.h:196
@ Invalid
Invalid / unset format.
Definition audiodesc.h:191
@ PCMI_U16LE
Unsigned 16-bit integer, little-endian, interleaved.
Definition audiodesc.h:197
@ PCMI_S24BE
Signed 24-bit integer, big-endian, interleaved.
Definition audiodesc.h:202
@ PCMI_S24LE
Signed 24-bit integer, little-endian, interleaved.
Definition audiodesc.h:200
@ PCMI_S8
Signed 8-bit integer, interleaved.
Definition audiodesc.h:194
@ PCMI_U16BE
Unsigned 16-bit integer, big-endian, interleaved.
Definition audiodesc.h:199
JsonObject toJson() const
Serializes this audio description to a JSON object.
Definition audiodesc.h:330
size_t bytesPerSampleStride() const
Returns the byte stride between consecutive samples of the same channel.
Definition audiodesc.h:355
static DataType stringToDataType(const String &val)
Converts a string name to its corresponding DataType enum value.
static void floatToSamples(uint8_t *outbuf, const float *in, size_t samples)
Converts a buffer of normalized floats to integer samples.
Definition audiodesc.h:147
bool isValid() const
Returns true if this audio description has a valid data type, sample rate, and channel count.
Definition audiodesc.h:297
bool isNative() const
Returns true if the data type is the platform's native float format.
Definition audiodesc.h:305
AudioDesc workingDesc() const
Returns a new AudioDesc with the same sample rate and channels but using the native float format.
Definition audiodesc.h:313
static const int32_t MaxU24
Maximum value of an unsigned 24-bit integer.
Definition audiodesc.h:41
DataType dataType() const
Returns the data type of this audio description.
Definition audiodesc.h:390
static const int32_t MaxS24
Maximum value of a signed 24-bit integer.
Definition audiodesc.h:37
Metadata & metadata()
Returns a mutable reference to the metadata.
Definition audiodesc.h:443
Lightweight error code wrapper for the promeki library.
Definition error.h:39
JSON object container wrapping nlohmann::json.
Definition json.h:44
Dynamic array container wrapping std::vector.
Definition list.h:40
bool set(size_t index, const T &val)
Sets an item in the list by index.
Definition list.h:541
Key-value metadata container using typed Variant values.
Definition metadata.h:68
JsonObject toJson() const
Serializes this Metadata to a JSON object.
Definition metadata.h:182
bool isEmpty() const
Returns true if no metadata entries are stored.
Definition metadata.h:148
Encoding-aware string class with copy-on-write semantics.
Definition string.h:35
static String sprintf(const char *fmt,...)
Creates a formatted string using printf-style syntax.
static constexpr bool isBigEndian()
Returns true if the host byte order is big-endian.
Definition system.h:49
static void swapEndian(T &value)
Reverses the byte order of an arithmetic value in place.
Definition system.h:58
static constexpr bool isLittleEndian()
Returns true if the host byte order is little-endian.
Definition system.h:41
#define PROMEKI_NAMESPACE_BEGIN
Starts a promeki namespace block.
Definition namespace.h:14
#define PROMEKI_NAMESPACE_END
Ends a promeki namespace block.
Definition namespace.h:19
const T & value(const Result< T > &r)
Returns the value from a Result.
Definition result.h:56
#define PROMEKI_SHARED_FINAL(TYPE)
Macro for non-polymorphic native shared objects.
Definition sharedptr.h:88
Descriptor for a specific audio sample format.
Definition audiodesc.h:164
int id
Format identifier matching a DataType enum value.
Definition audiodesc.h:165
String name
Short format name (e.g. "PCMI_S16LE").
Definition audiodesc.h:166
bool isPlanar
True if channels are stored in separate planes.
Definition audiodesc.h:171
void(* samplesToFloat)(float *out, const uint8_t *in, size_t samples)
Conversion function from this format to float.
Definition audiodesc.h:173
size_t bitsPerSample
Number of bits per single sample.
Definition audiodesc.h:169
void(* floatToSamples)(uint8_t *out, const float *in, size_t samples)
Conversion function from float to this format.
Definition audiodesc.h:174
bool isSigned
True if the format uses signed integers.
Definition audiodesc.h:170
bool isBigEndian
True if the format uses big-endian byte order.
Definition audiodesc.h:172
String desc
Human-readable format description.
Definition audiodesc.h:167
size_t bytesPerSample
Number of bytes per single sample.
Definition audiodesc.h:168