Documentation Index
Fetch the complete documentation index at: https://mintlify.com/hitenpatel/songprint/llms.txt
Use this file to discover all available pages before exploring further.
Pcm is a Kotlin object (singleton) providing two utility functions for converting raw PCM audio into the 16kHz mono ShortArray format required by SignatureGenerator. It handles the two most common preparation steps: decoding raw little-endian PCM bytes into samples, and downmixing and resampling from arbitrary formats to 16kHz mono. It is pure common Kotlin — no platform-specific APIs, no external dependencies.
Object declaration
Functions
bytesToShorts
Raw 16-bit little-endian PCM bytes, as produced by most audio APIs (Android
AudioRecord, WAV file data chunks, etc.). Must contain an even number of bytes; the last byte is silently ignored if the length is odd.ShortArray — the decoded PCM samples. The output length is pcm.size / 2. Each pair of consecutive bytes [i*2, i*2+1] is interpreted as a little-endian signed 16-bit integer: (pcm[i*2] & 0xFF) | (pcm[i*2+1] << 8).
resampleToMono16k
Interleaved multi-channel PCM samples at the original sample rate. For stereo (
channels = 2) the layout is [L0, R0, L1, R1, ...]; for mono (channels = 1) the layout is a plain sample sequence. The array length must be a multiple of channels.Original sample rate in Hz (e.g.
44100, 48000, 22050). The function resamples from this rate to SignatureGenerator.SAMPLE_RATE (16000 Hz).Number of interleaved channels (e.g.
1 for mono, 2 for stereo). All channels are averaged together to produce the mono signal before resampling.ShortArray — resampled 16kHz mono PCM samples. If sampleRate is already 16000 and channels is 1, the input ShortArray is returned unchanged with no allocation.
The function performs two steps in order:
- Channel averaging — if
channels >= 2, pairs of interleaved frames are averaged into a single mono sample per frame. - Linear-interpolation resampling — the mono signal is resampled to 16kHz. For each output sample, a source position is computed as
outputIndex / ratio; the fractional part is used to linearly interpolate between the two surrounding input samples.
Typical usage pattern
The two functions chain naturally into the preparation step before fingerprinting:pcmBytes; SignatureGenerator processes up to MAX_TIME_SECONDS (3.1 s) of audio before stopping peak collection.
The resampler uses linear interpolation. It is fast and produces results that are entirely appropriate for audio recognition workloads. It is not a studio-grade resampler — do not use it where high-fidelity audio reconstruction matters.