3#include <ossia/dataflow/nodes/media.hpp>
10template <
typename SampleFormat,
int N>
11constexpr audio_sample sample_to_float(SampleFormat i);
14constexpr audio_sample sample_to_float<uint8_t, 8>(uint8_t i)
17 if constexpr(std::is_same_v<ossia::audio_sample, float>)
18 return i / 127.f - 1.f;
24constexpr audio_sample sample_to_float<int16_t, 16>(int16_t i)
27 if constexpr(std::is_same_v<ossia::audio_sample, float>)
28 return (i + .5f) / (0x7FFF + .5f);
30 return (i + .5) / (0x7FFF + .5);
34constexpr audio_sample sample_to_float<int32_t, 24>(int32_t i)
36 if constexpr(std::is_same_v<ossia::audio_sample, float>)
37 return ((int32_t)i >> 8)
38 / ((audio_sample)std::numeric_limits<int32_t>::max() / 256.f);
40 return ((int32_t)i >> 8)
41 / ((audio_sample)std::numeric_limits<int32_t>::max() / 256.);
45constexpr audio_sample sample_to_float<int32_t, 32>(int32_t i)
47 return i / (audio_sample)(std::numeric_limits<int32_t>::max());
51constexpr audio_sample sample_to_float<float, 32>(
float i)
57constexpr audio_sample sample_to_float<double, 64>(
double i)
62inline void read_u8(ossia::mutable_audio_span<float>& ap,
void* data, int64_t samples)
64 const auto channels = ap.size();
65 auto d =
reinterpret_cast<int8_t*
>(data);
67 for(int64_t j = 0; j < samples; j++)
69 for(std::size_t i = 0; i < channels; i++)
71 ap[i][j] = sample_to_float<uint8_t, 8>(d[j * channels + i]);
76inline void read_s16(ossia::mutable_audio_span<float>& ap,
void* data, int64_t samples)
78 const auto channels = ap.size();
79 auto d =
reinterpret_cast<int16_t*
>(data);
81 for(int64_t j = 0; j < samples; j++)
83 for(std::size_t i = 0; i < channels; i++)
85 ap[i][j] = sample_to_float<int16_t, 16>(d[j * channels + i]);
90inline void read_s24(ossia::mutable_audio_span<float>& ap,
void* data, int64_t samples)
92 const auto channels = ap.size();
93 const auto frame_bytes = channels * 3;
95 auto bytes =
reinterpret_cast<uint8_t*
>(data);
96 for(int64_t j = 0; j < samples; j++)
98 for(std::size_t i = 0; i < channels; i++)
101 sample += (bytes[3 * i] << 8);
102 sample += (bytes[3 * i + 1] << 16);
103 sample += (bytes[3 * i + 2] << 24);
105 ap[i][j] = sample_to_float<int32_t, 24>(sample);
107 bytes += frame_bytes;
111inline void read_s32(ossia::mutable_audio_span<float>& ap,
void* data, int64_t samples)
113 const auto channels = ap.size();
114 auto d =
reinterpret_cast<int32_t*
>(data);
116 for(int64_t j = 0; j < samples; j++)
118 for(std::size_t i = 0; i < channels; i++)
120 ap[i][j] = sample_to_float<int32_t, 32>(d[j * channels + i]);
125inline void read_f32(ossia::mutable_audio_span<float>& ap,
void* data, int64_t samples)
127 const auto channels = ap.size();
128 auto d =
reinterpret_cast<float*
>(data);
130 for(int64_t j = 0; j < samples; j++)
132 for(std::size_t i = 0; i < channels; i++)
134 ap[i][j] = d[j * channels + i];
139inline void read_f64(ossia::mutable_audio_span<float>& ap,
void* data, int64_t samples)
141 const auto channels = ap.size();
142 auto d =
reinterpret_cast<double*
>(data);
144 for(int64_t j = 0; j < samples; j++)
146 for(std::size_t i = 0; i < channels; i++)
148 ap[i][j] = d[j * channels + i];