OSSIA
Open Scenario System for Interactive Application
Loading...
Searching...
No Matches
sample_to_float.hpp
1#pragma once
2
3#include <ossia/dataflow/nodes/media.hpp>
4
5#include <cstdint>
6#include <limits>
7
8namespace ossia
9{
10template <typename SampleFormat, int N>
11constexpr audio_sample sample_to_float(SampleFormat i);
12
13template <>
14constexpr 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
23template <>
24constexpr 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
33template <>
34constexpr 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
44template <>
45constexpr 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
50template <>
51constexpr audio_sample sample_to_float<float, 32>(float i)
52{
53 return i;
54}
55
56template <>
57constexpr audio_sample sample_to_float<double, 64>(double i)
58{
59 return i;
60}
61
62inline 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
76inline 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
90inline 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
111inline 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
125inline 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
139inline 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