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

Method decodeAiffSampleRate

AudioFile.h:1538–1578  ·  view source on GitHub ↗

=============================================================

Source from the content-addressed store, hash-verified

1536
1537//=============================================================
1538inline double AiffUtilities::decodeAiffSampleRate (const uint8_t* bytes)
1539{
1540 // Note: Sample rate is 80 bits made up of
1541 // * 1 sign bit
1542 // * 15 exponent bits
1543 // * 64 mantissa bits
1544
1545 // ----------------------------------------------
1546 // Sign
1547
1548 // Extract the sign (most significant bit of byte 0)
1549 int sign = (bytes[0] & 0x80) ? -1 : 1;
1550
1551 // ----------------------------------------------
1552 // Exponent
1553
1554 // byte 0: ignore the sign and shift the most significant bits to the left by one byte
1555 uint16_t msbShifted = (static_cast<uint16_t> (bytes[0] & 0x7F) << 8);
1556
1557 // calculate exponent by combining byte 0 and byte 1 and subtract bias
1558 uint16_t exponent = (msbShifted | static_cast<uint16_t> (bytes[1])) - 16383;
1559
1560 // ----------------------------------------------
1561 // Mantissa
1562
1563 // Extract the mantissa (remaining 64 bits) by looping over the remaining
1564 // bytes and combining them while shifting the result to the left by
1565 // 8 bits each time
1566 uint64_t mantissa = 0;
1567
1568 for (int i = 2; i < 10; ++i)
1569 mantissa = (mantissa << 8) | bytes[i];
1570
1571 // Normalize the mantissa (implicit leading 1 for normalized values)
1572 double normalisedMantissa = static_cast<double> (mantissa) / (1ULL << 63);
1573
1574 // ----------------------------------------------
1575 // Combine sign, exponent, and mantissa into a double
1576
1577 return sign * std::ldexp (normalisedMantissa, exponent);
1578}
1579
1580//=============================================================
1581inline void AiffUtilities::encodeAiffSampleRate (double sampleRate, uint8_t* bytes)

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected