Skip to content

Commit c5af376

Browse files
committed
improved Sample format coversion
- use constant values from AudioFormat
1 parent a37dabf commit c5af376

File tree

1 file changed

+10
-4
lines changed

1 file changed

+10
-4
lines changed

engine/src/Sample.h

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,22 @@ namespace YoaEngine
1818
// amaount of channels present in the buffer
1919
uint8_t Channels;
2020

21-
float GetSample(const uint32_t position) noexcept {
21+
float GetSample(const uint32_t position) noexcept
22+
{
23+
// setup constants
24+
constexpr auto uint8Max = 255.0f;
25+
constexpr auto sint16Max = 32768.0f;
26+
constexpr auto sint32Max = 2147483648.0f;
27+
// output a sample format
2228
switch (Format) {
2329
case YOA_Format_Float:
2430
return ((float*)Buffer)[position];
2531
case YOA_Format_Uint8:
26-
return (float)(Buffer)[position] / 128.0f - 1.0f;
32+
return Buffer[position] / uint8Max;
2733
case YOA_Format_Sint16:
28-
return (float)((int16_t*)Buffer)[position] / 32768.0f;
34+
return (float)((int16_t*)Buffer)[position] / sint16Max;
2935
case YOA_Format_Sint32:
30-
return (float)((int32_t*)Buffer)[position] / 2147483648.0f;
36+
return (float)((int32_t*)Buffer)[position] / sint32Max;
3137
case YOA_Format_Unknown:
3238
default:
3339
return 0.0f;

0 commit comments

Comments
 (0)