OSSIA
Open Scenario System for Interactive Application
sample_to_float.hpp
1 #pragma once
2 
3 #include <ossia/dataflow/nodes/media.hpp>
4 
5 #include <cstdint>
6 #include <limits>
7 
8 namespace ossia
9 {
10 template <typename SampleFormat, int N>
11 constexpr audio_sample sample_to_float(SampleFormat i);
12 
13 template <>
14 constexpr audio_sample sample_to_float<uint8_t, 8>(uint8_t i)
15 {
16  // 0 -> 255 to -1 -> 1
17  if constexpr(std::is_same_v<ossia::audio_sample, float>)
18  return i / 127.f - 1.f;
19  else
20  return i / 127. - 1.;
21 }
22 
23 template <>
24 constexpr audio_sample sample_to_float<int16_t, 16>(int16_t i)
25 {
26  // TODO division -> multiplication
27  if constexpr(std::is_same_v<ossia::audio_sample, float>)
28  return (i + .5f) / (0x7FFF + .5f);
29  else
30  return (i + .5) / (0x7FFF + .5);
31 }
32 
33 template <>
34 constexpr audio_sample sample_to_float<int32_t, 24>(int32_t i)
35 {
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);
39  else
40  return ((int32_t)i >> 8)
41  / ((audio_sample)std::numeric_limits<int32_t>::max() / 256.);
42 }
43 
44 template <>
45 constexpr audio_sample sample_to_float<int32_t, 32>(int32_t i)
46 {
47  return i / (audio_sample)(std::numeric_limits<int32_t>::max());
48 }
49 
50 template <>
51 constexpr audio_sample sample_to_float<float, 32>(float i)
52 {
53  return i;
54 }
55 
56 template <>
57 constexpr audio_sample sample_to_float<double, 64>(double i)
58 {
59  return i;
60 }
61 
62 inline void read_u8(ossia::mutable_audio_span<float>& ap, void* data, int64_t samples)
63 {
64  const auto channels = ap.size();
65  auto d = reinterpret_cast<int8_t*>(data);
66 
67  for(int64_t j = 0; j < samples; j++)
68  {
69  for(std::size_t i = 0; i < channels; i++)
70  {
71  ap[i][j] = sample_to_float<uint8_t, 8>(d[j * channels + i]);
72  }
73  }
74 }
75 
76 inline void read_s16(ossia::mutable_audio_span<float>& ap, void* data, int64_t samples)
77 {
78  const auto channels = ap.size();
79  auto d = reinterpret_cast<int16_t*>(data);
80 
81  for(int64_t j = 0; j < samples; j++)
82  {
83  for(std::size_t i = 0; i < channels; i++)
84  {
85  ap[i][j] = sample_to_float<int16_t, 16>(d[j * channels + i]);
86  }
87  }
88 }
89 
90 inline void read_s24(ossia::mutable_audio_span<float>& ap, void* data, int64_t samples)
91 {
92  const auto channels = ap.size();
93  const auto frame_bytes = channels * 3;
94 
95  auto bytes = reinterpret_cast<uint8_t*>(data);
96  for(int64_t j = 0; j < samples; j++)
97  {
98  for(std::size_t i = 0; i < channels; i++)
99  {
100  int32_t sample = 0;
101  sample += (bytes[3 * i] << 8);
102  sample += (bytes[3 * i + 1] << 16);
103  sample += (bytes[3 * i + 2] << 24);
104 
105  ap[i][j] = sample_to_float<int32_t, 24>(sample);
106  }
107  bytes += frame_bytes;
108  }
109 }
110 
111 inline void read_s32(ossia::mutable_audio_span<float>& ap, void* data, int64_t samples)
112 {
113  const auto channels = ap.size();
114  auto d = reinterpret_cast<int32_t*>(data);
115 
116  for(int64_t j = 0; j < samples; j++)
117  {
118  for(std::size_t i = 0; i < channels; i++)
119  {
120  ap[i][j] = sample_to_float<int32_t, 32>(d[j * channels + i]);
121  }
122  }
123 }
124 
125 inline void read_f32(ossia::mutable_audio_span<float>& ap, void* data, int64_t samples)
126 {
127  const auto channels = ap.size();
128  auto d = reinterpret_cast<float*>(data);
129 
130  for(int64_t j = 0; j < samples; j++)
131  {
132  for(std::size_t i = 0; i < channels; i++)
133  {
134  ap[i][j] = d[j * channels + i];
135  }
136  }
137 }
138 
139 inline void read_f64(ossia::mutable_audio_span<float>& ap, void* data, int64_t samples)
140 {
141  const auto channels = ap.size();
142  auto d = reinterpret_cast<double*>(data);
143 
144  for(int64_t j = 0; j < samples; j++)
145  {
146  for(std::size_t i = 0; i < channels; i++)
147  {
148  ap[i][j] = d[j * channels + i];
149  }
150  }
151 }
152 
153 }
Definition: git_info.h:7
constexpr OSSIA_INLINE auto max(const T a, const U b) noexcept -> typename std::conditional<(sizeof(T) > sizeof(U)), T, U >::type
max function tailored for values
Definition: math.hpp:96