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

Method encodeAiffSampleRate

AudioFile.h:1581–1611  ·  view source on GitHub ↗

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

Source from the content-addressed store, hash-verified

1579
1580//=============================================================
1581inline void AiffUtilities::encodeAiffSampleRate (double sampleRate, uint8_t* bytes)
1582{
1583 // Determine the sign
1584 int sign = (sampleRate < 0) ? -1 : 1;
1585
1586 if (sign == -1)
1587 sampleRate = -sampleRate;
1588
1589 // Set most significant bit of byte 0 for the sign
1590 bytes[0] = (sign == -1) ? 0x80 : 0x00;
1591
1592 // Calculate the exponent using logarithm (log base 2)
1593 int exponent = (log (sampleRate) / log (2.0));
1594
1595 // Add bias to exponent for AIFF
1596 uint16_t biasedExponent = static_cast<uint16_t> (exponent + 16383);
1597
1598 // Normalize the sample rate
1599 double normalizedSampleRate = sampleRate / pow (2.0, exponent);
1600
1601 // Calculate the mantissa
1602 uint64_t mantissa = static_cast<uint64_t> (normalizedSampleRate * (1ULL << 63));
1603
1604 // Pack the exponent into first two bytes of 10-byte AIFF format
1605 bytes[0] |= (biasedExponent >> 8) & 0x7F; // Upper 7 bits of exponent
1606 bytes[1] = biasedExponent & 0xFF; // Lower 8 bits of exponent
1607
1608 // Put the mantissa into byte array
1609 for (int i = 0; i < 8; ++i)
1610 bytes[2 + i] = (mantissa >> (8 * (7 - i))) & 0xFF;
1611}
1612
1613#if defined (_MSC_VER)
1614 __pragma(warning (pop))

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected