Prime desktop speech PCM before enabling device consumption
This commit is contained in:
1 parent
5240dfe02e
commit
684adddd94
3 files changed
+38
-6
No files matched your search
@@ -48,6 +48,7 @@ bool VoiceAudioIo::start(Kind kind, uint64_t session, unsigned bufferMs) {
|
||||
m_progressDropped = 0;
|
||||
{ std::lock_guard lock(m_latencyMutex); m_outputLatency = {}; }
|
||||
m_playPosition = 960; m_playEpoch = 1;
|
||||
m_outputStartedEpoch = 0;
|
||||
m_captureDropped = m_referenceDropped = m_playbackUnderruns = 0;
|
||||
m_running = true;
|
||||
try { m_playbackThread = std::thread(&VoiceAudioIo::playbackLoop, this); }
|
||||
@@ -132,6 +133,17 @@ void VoiceAudioIo::capture(const int16_t* pcm, unsigned frames, int64_t firstTim
|
||||
void VoiceAudioIo::reference(const float* pcm, unsigned frames, int64_t firstTimeUs) {
|
||||
if (!m_capturing) return;
|
||||
if (!firstTimeUs) firstTimeUs = voice_time_us();
|
||||
if (m_kind == Kind::Call && m_outputStartedEpoch != epoch) {
|
||||
unsigned required = std::max(1920u, callbackFrames ? callbackFrames : frames);
|
||||
if (m_playback.size() * 960 < required) {
|
||||
// PREBUFFER: устройство работает, но речь ещё не началась. Это не starvation.
|
||||
PlaybackProgress waiting{}; waiting.epoch = epoch; waiting.callbackFrames = callbackFrames ? callbackFrames : frames;
|
||||
if (!m_playProgress.push(waiting)) m_progressDropped.fetch_add(1, std::memory_order_relaxed);
|
||||
m_wake.notify_all();
|
||||
return;
|
||||
}
|
||||
m_outputStartedEpoch = epoch;
|
||||
}
|
||||
for (unsigned offset = 0; offset < frames;) {
|
||||
Event event{}; event.frames = std::min(frames - offset, 960u);
|
||||
event.time = firstTimeUs + (int64_t)offset * 1000000 / 48000;
|
||||
@@ -184,6 +196,7 @@ void VoiceAudioIo::playbackLoop() {
|
||||
VoicePlaybackBuffer buffering(m_playbackBufferMs);
|
||||
PlaybackProgress lastProgress{};
|
||||
int64_t lastProgressUs = 0;
|
||||
bool outputStarted = false;
|
||||
call_clock_snapshot clock{};
|
||||
if (m_kind == Kind::Call && call_audio_get_clock(m_session, voice_time_us(), &clock) != 0)
|
||||
DEBUG_WARN(DEBUG_CATEGORY_CALL, "voice-io: call clock unavailable session=%llu", (unsigned long long)m_session);
|
||||
@@ -200,6 +213,7 @@ void VoiceAudioIo::playbackLoop() {
|
||||
position = 0;
|
||||
epoch = requested;
|
||||
buffering.reset(); lastProgress = {};
|
||||
outputStarted = false;
|
||||
{ std::lock_guard lock(m_latencyMutex); m_outputLatency = {}; }
|
||||
}
|
||||
PlaybackProgress progress{};
|
||||
@@ -219,6 +233,12 @@ void VoiceAudioIo::playbackLoop() {
|
||||
lastProgress = progress;
|
||||
lastProgressUs = voice_time_us();
|
||||
if (!progress.consumed) continue;
|
||||
if (!outputStarted) {
|
||||
outputStarted = true;
|
||||
DEBUG_INFO(DEBUG_CATEGORY_CALL, "voice-io: output started session=%llu epoch=%llu consumed=%u queued=%u callback=%u",
|
||||
(unsigned long long)m_session, (unsigned long long)epoch, progress.consumed,
|
||||
progress.queuedFrames, progress.callbackFrames);
|
||||
}
|
||||
int64_t now = voice_time_us(), remaining = progress.endTimeUs - now;
|
||||
if (m_kind == Kind::Call)
|
||||
call_audio_playback_progress(m_session, clock.epoch, progress.position, progress.endTimeUs, now, progress.measured);
|
||||
@@ -247,12 +267,19 @@ void VoiceAudioIo::playbackLoop() {
|
||||
(unsigned long long)snapshot.played_samples, (unsigned long long)snapshot.written_samples,
|
||||
(long long)snapshot.pts_us, snapshot.valid, snapshot.measured, snapshot.held_back_us);
|
||||
}
|
||||
if (m_playback.size() < buffering.blocks) {
|
||||
unsigned goal = m_kind == Kind::Call && !outputStarted ? std::max(2u, buffering.blocks) : buffering.blocks;
|
||||
if (m_playback.size() < goal) {
|
||||
Playback block{}; block.epoch = epoch; block.position = position;
|
||||
int64_t pullAt = voice_time_us();
|
||||
if (m_kind == Kind::Call) {
|
||||
int16_t mono[960]{};
|
||||
int count = std::clamp(call_audio_pull_pcm(m_session, mono, 960), 0, 960);
|
||||
call_audio_render state{};
|
||||
int count = std::clamp(call_audio_render_pcm(m_session, mono, 960, &state), 0, 960);
|
||||
if (!count && state.phase == CALL_AUDIO_WAITING) {
|
||||
std::unique_lock lock(m_waitMutex);
|
||||
m_wake.wait_for(lock, std::chrono::milliseconds(5));
|
||||
continue;
|
||||
}
|
||||
for (int i = 0; i < count; ++i) block.pcm[2*i] = block.pcm[2*i+1] = mono[i];
|
||||
} else radio_audio_pull_pcm(m_session, block.pcm, 1920);
|
||||
if (m_kind == Kind::Call) pullUs.add(voice_time_us() - pullAt);
|
||||
|
||||
@@ -78,6 +78,7 @@ private:
|
||||
std::atomic<unsigned> m_progressDropped{0}; // Потерянные progress-сообщения до отчёта RX.
|
||||
std::atomic<bool> m_running{false}, m_capturing{false}; // Живут RX/capture worker; прекращение будит wait.
|
||||
std::atomic<uint64_t> m_playEpoch{0}, m_captureDropped{0}, m_referenceDropped{0}, m_playbackUnderruns{0}; // Эпоха и потери во фреймах.
|
||||
uint64_t m_outputStartedEpoch = 0; // Только callback: первая речь разрешена после заполнения PCM под его размер.
|
||||
uint64_t m_captureSequence = 0; // Только capture callback; reset после его остановки.
|
||||
Kind m_kind = Kind::Call; // Не меняется при работающих workers.
|
||||
uint64_t m_session = 0; // ID core-сессии, не продлевает жизнь core DSP.
|
||||
|
||||
@@ -36,7 +36,11 @@ static unsigned playbackSource;
|
||||
extern "C" void radio_audio_playback_progress(uint64_t, uint64_t position, uint64_t delay) {
|
||||
output_delay = delay; output_position = position;
|
||||
}
|
||||
extern "C" int call_audio_pull_pcm(uint64_t, int16_t* out, int count) {
|
||||
static std::atomic<bool> mediaReady{true};
|
||||
extern "C" int call_audio_render_pcm(uint64_t, int16_t* out, int count, call_audio_render* state) {
|
||||
*state = {CALL_AUDIO_WAITING, 0};
|
||||
if (!mediaReady) return 0;
|
||||
*state = {CALL_AUDIO_SPEECH, count};
|
||||
for (int i = 0; i < count; ++i) out[i] = rampPlayback ? int(playbackSource++ % 16000) - 8000 : 1000;
|
||||
++pulls; return count;
|
||||
}
|
||||
@@ -110,7 +114,7 @@ static void small_chunks() {
|
||||
VoiceAudioIo voice;
|
||||
REQUIRE(voice.start(VoiceAudioIo::Kind::Call, 46));
|
||||
for (unsigned offset = 0; offset < total; offset += chunk) {
|
||||
await([&] { return unsigned(pulls.load()) * 960 >= offset + chunk; });
|
||||
await([&] { return unsigned(pulls.load()) * 960 >= std::max(offset + chunk, 1920u); });
|
||||
float output[chunk * 2]{};
|
||||
voice.mix(output, chunk, now_us() + 10000, true);
|
||||
await([&] { return output_position.load() == offset + chunk; });
|
||||
@@ -200,7 +204,7 @@ int main() {
|
||||
REQUIRE(!voice.start(VoiceAudioIo::Kind::Call, 1, 100));
|
||||
for (auto kind : {VoiceAudioIo::Kind::Call, VoiceAudioIo::Kind::Radio}) {
|
||||
pulls = 0; REQUIRE(voice.start(kind, 1));
|
||||
unsigned expected = kind == VoiceAudioIo::Kind::Call ? 1 : 2;
|
||||
unsigned expected = 2;
|
||||
await([&] { return unsigned(pulls.load()) >= expected; });
|
||||
voice.stop(); REQUIRE(unsigned(pulls.load()) == expected);
|
||||
}
|
||||
@@ -255,7 +259,7 @@ int main() {
|
||||
voice.stop(); reset();
|
||||
pulls = 0; output_position = 0;
|
||||
REQUIRE(voice.start(VoiceAudioIo::Kind::Call, 42));
|
||||
await([] { return pulls.load() >= 1; });
|
||||
await([] { return pulls.load() >= 2; });
|
||||
REQUIRE(output_position == 0);
|
||||
voice.mix(partial, 240, now_us() + 500000, true);
|
||||
await([] { return output_position.load() == 240; });
|
||||
|
||||
Reference in new issue
Block a user