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;
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);
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);
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);
123 template <
typename IntegerType,
bool InputIsBigEndian>
125 static_assert(std::is_integral<IntegerType>::value,
"IntegerType must be an integer.");
127 for(
size_t i = 0;
i < samples; ++
i) {
146 template <
typename IntegerType,
bool OutputIsBigEndian>
148 static_assert(std::is_integral<IntegerType>::value,
"IntegerType must be an integer.");
150 for(
size_t i = 0;
i < samples; ++
i) {
261 _dataType(
dt), _sampleRate(
sr),
278 return _dataType ==
other._dataType &&
279 _sampleRate ==
other._sampleRate &&
280 _channels ==
other._channels;
290 _metadata ==
other._metadata;
298 return _dataType != 0 && _sampleRate > 0.0f && _channels > 0;
323 _format->name.cstr(), _sampleRate, _channels);
332 ret.
set(
"DataType", _format->name);
333 ret.
set(
"SampleRate", _sampleRate);
334 ret.
set(
"Channels", _channels);
344 return _format->bytesPerSample;
356 return _format->isPlanar ?
357 _format->bytesPerSample :
358 _format->bytesPerSample * _channels;
372 return _format->isPlanar ?
374 _format->bytesPerSample *
chan;
383 return _format->bytesPerSample * _channels * samples;
454 _format->samplesToFloat(
out,
in, samples * _channels);
465 _format->floatToSamples(
out,
in, samples * _channels);
471 float _sampleRate = 0.0f;
472 unsigned int _channels = 0;
474 const Format *_format =
nullptr;
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
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