MCPcopy Create free account
hub / github.com/adamstark/AudioFile / sampleToThirtyTwoBitInt

Method sampleToThirtyTwoBitInt

AudioFile.h:1357–1385  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1355//=============================================================
1356template <class T>
1357int32_t AudioSampleConverter<T>::sampleToThirtyTwoBitInt (T sample)
1358{
1359 if constexpr (std::is_floating_point<T>::value)
1360 {
1361 // multiplying a float by a the max int32_t is problematic because
1362 // of roundng errors which can cause wrong values to come out, so
1363 // we use a different implementation here compared to other types
1364 if constexpr (std::is_same_v<T, float>)
1365 {
1366 if (sample >= 1.f)
1367 return std::numeric_limits<int32_t>::max();
1368 else if (sample <= -1.f)
1369 return std::numeric_limits<int32_t>::lowest() + 1; // starting at 1 preserves symmetry
1370 else
1371 return static_cast<int32_t> (sample * std::numeric_limits<int32_t>::max());
1372 }
1373 else
1374 {
1375 return static_cast<int32_t> (clamp (sample, -1., 1.) * std::numeric_limits<int32_t>::max());
1376 }
1377 }
1378 else
1379 {
1380 if constexpr (std::is_signed_v<T>)
1381 return static_cast<int32_t> (clamp (sample, -2147483648LL, 2147483647LL));
1382 else
1383 return static_cast<int32_t> (clamp (sample, 0, 4294967295) - 2147483648);
1384 }
1385}
1386
1387//=============================================================
1388template <class T>

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected