5 t_size size = samples * nch;
18 return (val&mask)!=0 && (val&mask)!=mask;
22 for(
size_t walk = 0; walk < count; ++walk) {
25 *(out++) = (
float) (int32_t) i / (
float) 0x80;
30 for(
size_t walk = 0; walk < count; ++walk) {
31 int32_t i = (int8_t) *(in++);
32 *(out++) = (
float) i / (float) 0x80;
39 return (
float) (int32_t) i / (
float) 0x800000;
43 const uint8_t * in = (
const uint8_t*) in_;
45 while(count > 0 && !pfc::is_ptr_aligned_t<4>(in)) {
47 i |= (uint32_t) *(in++) << 8;
48 i |= (uint32_t) *(in++) << 16;
53 for(
size_t loop = count >> 2; loop; --loop) {
54 uint32_t i1 = * (uint32_t*) in; in += 4;
55 uint32_t i2 = * (uint32_t*) in; in += 4;
56 uint32_t i3 = * (uint32_t*) in; in += 4;
58 *out++ =
_import24s( (i1 >> 24) | ((i2 & 0xFFFF) << 8) );
59 *out++ =
_import24s( (i2 >> 16) | ((i3 & 0xFF) << 16) );
64 for( ; count ; --count) {
66 i |= (uint32_t) *(in++) << 8;
67 i |= (uint32_t) *(in++) << 16;
73 i |= (int32_t) *(in++) << 8;
74 i |= (int32_t) (int8_t) *in << 16;
80 int32_t i = *( int32_t*) in; in += 3;
88 uint16_t
const * inPtr = (uint16_t
const*) in;
90 for(
size_t walk = 0; walk < count; ++walk) {
91 uint16_t v = *inPtr++;
93 if (!isSigned) v ^= 0x8000;
99 uint32_t
const * inPtr = (uint32_t
const*) in;
101 for(
size_t walk = 0; walk < count; ++walk) {
102 uint32_t v = *inPtr++;
104 if (!isSigned) v ^= 0x80000000;
110 uint8_t
const * inPtr = (uint8_t
const*) in;
112 for(
size_t walk = 0; walk < count; ++walk) {
114 if (byteSwap) v = (uint32_t) inPtr[2] | ( (uint32_t) inPtr[1] << 8 ) | ( (uint32_t) inPtr[0] << 16 );
115 else v = (uint32_t) inPtr[0] | ( (uint32_t) inPtr[1] << 8 ) | ( (uint32_t) inPtr[2] << 16 );
117 if (isSigned) v ^= 0x800000;
131 t_size count = size / (bps/8);
140 if (isSigned)
_import8s( (
const uint8_t*) source , buffer, count);
141 else _import8u( (
const uint8_t*) source , buffer, count);
146 _import16any<true, true>( source, buffer, count );
148 _import16any<true, false>( source, buffer, count );
155 _import16any<false, false>( source, buffer, count);
162 _import24any<true, true>( source, buffer, count );
164 _import24any<true, false>( source, buffer, count );
171 _import24any<false, false>( source, buffer, count);
178 _import32any<true, true>( source, buffer, count );
180 _import32any<true, false>( source, buffer, count );
186 _import32any<false, false>( source, buffer, count);
202 PFC_ASSERT( bps != 0 );
203 size_t count = bytes / (bps/8);
208 _import8u((
const uint8_t*)ptr, buffer, count);
220 PFC_ASSERT(!
"Unknown bit depth!");
230 PFC_ASSERT( bps != 0 );
231 size_t count = bytes / (bps/8);
236 _import8s((
const uint8_t*)ptr, buffer, count);
248 PFC_ASSERT(!
"Unknown bit depth!");
258 const size_t count = samples * nch;
267 template<
class t_
float>
270 for(
size_t n=0;n<p_count;n++) p_out[n] = (
audio_sample)p_in[n];
273 template<
class t_
float>
276 for(
size_t n=0;n<p_count;n++) {
284 PFC_ASSERT(bps==32 || bps==64 || bps == 16 || bps == 24);
290 const t_size count = size / (bps/8);
307 }
else if (bps == 16) {
308 const uint16_t * in =
reinterpret_cast<const uint16_t*
>(ptr);
310 for(
size_t walk = 0; walk < count; ++walk) out[walk] = audio_math::decodeFloat16(
pfc::byteswap_t(in[walk]));
312 for(
size_t walk = 0; walk < count; ++walk) out[walk] = audio_math::decodeFloat16(in[walk]);
314 }
else if (bps == 24) {
315 const uint8_t * in =
reinterpret_cast<const uint8_t*
>(ptr);
317 for(
size_t walk = 0; walk < count; ++walk) out[walk] = audio_math::decodeFloat24ptrbs(&in[walk*3]);
319 for(
size_t walk = 0; walk < count; ++walk) out[walk] = audio_math::decodeFloat24ptr(&in[walk*3]);
321 }
else pfc::throw_exception_with_message< exception_io_data >(
"invalid bit depth");
331 if (nch==0 || nch>256)
return false;
334 if (samples==0 || samples >= 0x80000000 / (
sizeof(
audio_sample) * nch) )
return false;
336 if (samples * nch > size)
return false;
348 if (hint_srate && hint_nch) {
349 return set_data(0,samples,hint_nch,hint_srate);
350 }
else throw exception_io_data();
391 t_size new_size = old_size + delta;
401 if (skipSamples == 0) {skipDuration = 0;
return true;}
403 if (skipSamples < mySamples) {
408 if (skipSamples == mySamples) {
412 skipDuration -= audio_math::samples_to_time(mySamples,
get_sample_rate());
419 if (samples_delta >= samples_old)
427 t_size samples_new = samples_old - samples_delta;
433 return samples_delta;
454 struct sampleToIntDesc {
455 unsigned bps, bpsValid;
459 template<
typename int_t>
class sampleToInt {
461 sampleToInt(sampleToIntDesc
const & d) {
462 clipLo = - ( (int_t) 1 << (d.bpsValid-1));
463 clipHi = ( (int_t) 1 << (d.bpsValid-1)) - 1;
464 scale = (float) ( (int64_t) 1 << (d.bpsValid - 1) ) * d.scale;
465 if (d.useUpperBits) {
466 shift = d.bps - d.bpsValid;
475 return pfc::clip_t<int_t>( v, clipLo, clipHi) << shift;
478 int_t clipLo, clipHi;
485 sampleToInt<int32_t> gen(d);
486 for(
t_size walk = 0; walk < inLen; ++walk) {
487 int32_t v = gen(in[walk]);
488 *(outWalk ++) = (
t_uint8) (v & 0xFF);
489 *(outWalk ++) = (
t_uint8) ((v >> 8) & 0xFF);
490 *(outWalk ++) = (
t_uint8) ((v >> 16) & 0xFF);
494 sampleToInt<int32_t> gen(d);
496 for(
t_size walk = 0; walk < inLen; ++walk) {
497 *outWalk++ = (
t_int8)gen(in[walk]);
501 sampleToInt<int32_t> gen(d);
502 int16_t * outWalk =
reinterpret_cast<int16_t*
>(out);
503 for(
t_size walk = 0; walk < inLen; ++walk) {
504 *outWalk++ = (int16_t)gen(in[walk]);
508 template<
typename internal_t>
510 sampleToInt<internal_t> gen(d);
511 int32_t * outWalk =
reinterpret_cast<int32_t*
>(out);
512 for(
t_size walk = 0; walk < inLen; ++walk) {
513 *outWalk++ = (int32_t)gen(in[walk]);
518 const sampleToIntDesc d = {bps, bpsValid, useUpperBits, scale};
520 PFC_ASSERT(!
"How did we get here?");
522 }
else if (bps <= 8) {
524 }
else if (bps <= 16) {
526 }
else if (bps <= 24) {
528 }
else if (bps <= 32) {
529 if (bpsValid <= 28) {
530 render_32bit_<int32_t>(in, count, out, d);
532 render_32bit_<int64_t>(in, count, out, d);
535 PFC_ASSERT(!
"How did we get here?");
543 bps = (bps + 7) & ~7;
544 if (bps < bpsValid)
return false;
551 uint32_t bpsValid = bps;
552 bps = (bps + 7) & ~7;
558 float * f = (
float*) outPtr;
559 for(
size_t w = 0; w < count; ++w) f[w] = inPtr[w] * scale;
562 return g_toFixedPoint(inPtr, outPtr, count, bps, bpsValid, useUpperBits, scale);
593 if (this->chanCount==0 || this->chanCount>256)
return false;
599 double acc = m_offset;
601 acc += audio_math::samples_to_time(walk->m_value, walk->m_key);
607 uint64_t samples = 0;
608 double acc = m_offset;
610 if (walk->m_key == rate) samples += walk->m_value;
611 else acc += audio_math::samples_to_time(walk->m_value, walk->m_key);
613 return samples + audio_math::time_to_samples(acc, rate );
virtual void set_size(t_size p_size)=0
static void render_24bit(const audio_sample *in, t_size inLen, void *out, sampleToIntDesc const &d)
static void render_16bit(const audio_sample *in, t_size inLen, void *out, sampleToIntDesc const &d)
static void _import24(const void *in_, audio_sample *out, size_t count)
void SHARED_EXPORT scale(const audio_sample *p_source, t_size p_count, audio_sample *p_output, audio_sample p_scale)
p_source/p_output can point to same buffer
void set_silence_seconds(double seconds)
audio_sample get_peak() const
void SHARED_EXPORT convert_from_int32(const t_int32 *p_source, t_size p_count, audio_sample *p_output, audio_sample p_scale)
virtual void set_srate(unsigned val)=0
Sets sample rate of contained audio data.
t_int multiply_guarded(t_int v1, t_int v2)
t_size MulDiv_Size(t_size x, t_size y, t_size z)
unsigned get_sample_rate() const
Helper, same as get_srate().
void set_data_fixedpoint_ex(const void *ptr, t_size bytes, unsigned p_sample_rate, unsigned p_channels, unsigned p_bits_per_sample, unsigned p_flags, unsigned p_channel_config)
static bool g_toFixedPoint(const audio_sample *in, void *out, size_t count, uint32_t bps, uint32_t bpsValid, bool useUpperBits=true, float scale=1.0)
Convert a buffer of audio_samples to fixed-point PCM format.
void pad_with_silence(t_size samples)
void set_data_fixedpoint_signed(const void *ptr, t_size bytes, unsigned srate, unsigned nch, unsigned bps, unsigned channel_config)
bool process_skip(double &skipDuration)
void set_data_fixedpoint_ms(const void *ptr, size_t bytes, unsigned sampleRate, unsigned channels, unsigned bps, unsigned channelConfig)
static const bool byte_order_is_big_endian
virtual unsigned get_channels() const =0
Retrieves channel count of contained audio data.
void SHARED_EXPORT convert_from_int16(const t_int16 *p_source, t_size p_count, audio_sample *p_output, audio_sample p_scale)
uint64_t queryAsSampleCount(uint32_t rate)
virtual unsigned get_srate() const =0
Retrieves sample rate of contained audio data.
static bool equals(const spec_t &v1, const spec_t &v2)
void set_spec(const spec_t &)
static void render_32bit_(const audio_sample *in, t_size inLen, void *out, sampleToIntDesc const &d)
bool toFixedPoint(class mem_block_container &out, uint32_t bps, uint32_t bpsValid, bool useUpperBits=true, float scale=1.0) const
Convert audio_chunk contents to fixed-point PCM format.
virtual unsigned get_channel_config() const =0
Retrieves channel map of contained audio data. Conditions where number of channels specified by chann...
void set_data(const audio_sample *src, t_size samples, unsigned nch, unsigned srate, unsigned channel_config)
Helper, sets chunk data to contents of specified buffer, with specified number of channels / sample r...
void insert_silence_fromstart(t_size samples)
void scale(audio_sample p_value)
Helper function; scales entire chunk content by specified value.
static void _import16any(const void *in, audio_sample *out, size_t count)
void memmove_t(T *p_dst, const T *p_src, t_size p_count)
void set_data_floatingpoint_ex(const void *ptr, t_size bytes, unsigned p_sample_rate, unsigned p_channels, unsigned p_bits_per_sample, unsigned p_flags, unsigned p_channel_config)
virtual audio_sample * get_data()=0
Retrieves audio data buffer pointer (non-const version). Returned pointer is for temporary use only; ...
t_int64 rint64(double p_val)
t_int32 rint32(double p_val)
void memset_null_t(T *p_buffer, t_size p_count)
bool is_spec_valid() const
Returns whether the chunk contains valid sample rate & channel info (but allows an empty chunk)...
static void _import24any(const void *in, audio_sample *out, size_t count)
bool is_valid() const
Returns whether the chunk contents are valid (for bug check purposes).
virtual const void * get_ptr() const =0
static spec_t makeSpec(uint32_t rate, uint32_t channels)
static unsigned g_guess_channel_config(unsigned count)
Helper function; guesses default channel map for the specified channel count. Returns 0 on failure...
static void process_float_multi_swap(audio_sample *p_out, const t_float *p_in, const t_size p_count)
static void process_float_multi(audio_sample *p_out, const t_float *p_in, const t_size p_count)
virtual void set_channels(unsigned p_count, unsigned p_config)=0
Sets channel count / channel map.
static void _import32any(const void *in, audio_sample *out, size_t count)
virtual t_size get_data_size() const =0
Retrieves size of allocated buffer space, in audio_samples.
static audio_sample _import24s(uint32_t i)
t_size skip_first_samples(t_size samples)
virtual void set_data_size(t_size p_new_size)=0
Resizes audio data buffer to specified size. Throws std::bad_alloc on failure.
static void _import8s(uint8_t const *in, audio_sample *out, size_t count)
void pad_with_silence_ex(t_size samples, unsigned hint_nch, unsigned hint_srate)
void memcpy_t(t_dst *p_dst, const t_src *p_src, t_size p_count)
Generic interface for a memory block; used by various other interfaces to return memory blocks while ...
bool check_exclusive(unsigned val, unsigned mask)
bool is_empty() const
Returns whether the chunk is empty (contains no audio data).
static bool g_is_valid_sample_rate(t_uint32 p_val)
audio_sample SHARED_EXPORT calculate_peak(const audio_sample *p_source, t_size p_count)
static void render_8bit(const audio_sample *in, t_size inLen, void *out, sampleToIntDesc const &d)
T max_t(const T &item1, const T &item2)
void set_data_int16(const int16_t *src, t_size samples, unsigned nch, unsigned srate, unsigned channel_config)
void memset_t(T *p_buffer, const t_val &p_val, t_size p_count)
unsigned get_channel_count() const
Helper - for consistency - same as get_channels().
virtual t_size get_sample_count() const =0
Retrieves number of valid samples in the buffer. Note that a "sample" means a unit of interleaved PC...
virtual void set_sample_count(t_size val)=0
Sets number of valid samples in the buffer. WARNING: sample count * channel count should never be abo...
void set_silence(t_size samples)
static void _import8u(uint8_t const *in, audio_sample *out, size_t count)
void set_sample_rate(unsigned val)
Helper, same as set_srate().
bool to_raw_data(class mem_block_container &out, t_uint32 bps, bool useUpperBits=true, float scale=1.0) const
Simple function to get original PCM stream back. Assumes host's endianness, integers are signed - inc...