mirror of
https://github.com/mudler/LocalAI.git
synced 2026-08-01 02:49:51 -04:00
Integer nanosecond conversion so 44.1 kHz stays exact, float seconds for the VAD and diarization messages, and saturating s16le encode so an overshooting sample cannot wrap to the opposite sign. Assisted-by: Claude:claude-opus-5 [Claude Code] Signed-off-by: Ettore Di Giacinto <mudler@localai.io>
34 lines
1.2 KiB
C++
34 lines
1.2 KiB
C++
#pragma once
|
|
|
|
// Time and sample-format conversion between audio.cpp's runtime types (sample
|
|
// indices, float PCM) and LocalAI's proto types. Standard library only.
|
|
//
|
|
// LocalAI uses three different time units:
|
|
// TranscriptSegment / TranscriptWord start,end : int64 nanoseconds
|
|
// VADSegment start,end : float seconds
|
|
// DiarizeSegment start,end : float seconds
|
|
|
|
#include <cstdint>
|
|
#include <string>
|
|
#include <vector>
|
|
|
|
namespace audiocpp_backend {
|
|
|
|
// Returns 0 when sample_rate is not positive rather than dividing by zero.
|
|
// Uses integer arithmetic so 44.1 kHz does not lose precision.
|
|
std::int64_t samples_to_nanoseconds(std::int64_t samples, int sample_rate);
|
|
|
|
float samples_to_seconds(std::int64_t samples, int sample_rate);
|
|
|
|
// Negative seconds clamp to 0.
|
|
std::int64_t seconds_to_samples(double seconds, int sample_rate);
|
|
|
|
// Decodes little-endian signed 16-bit PCM. A trailing odd byte is dropped.
|
|
std::vector<float> s16le_to_f32(const std::string &bytes);
|
|
|
|
// Encodes to little-endian signed 16-bit PCM, clamping to [-1, 1] first so an
|
|
// overshooting sample saturates instead of wrapping to the opposite sign.
|
|
std::string f32_to_s16le(const std::vector<float> &samples);
|
|
|
|
} // namespace audiocpp_backend
|