ascii-chat
Binary v0.16.50-198-g78f51665f
Library lib/v0.3.2-2240-g78f51665f
Video chat in your terminal
Loading...
Searching...
No Matches
src/client/audio.c
Go to the documentation of this file.
1
2/* Storage capacities in elements; text buffers include the terminating NUL. */
3
5#include <ascii-chat/otel.h>
80
81#include "audio.h"
82#include "capture.h"
85#include "main.h"
86#include "../main.h" // Global exit API
87#include "server.h"
88#include <ascii-chat/util/fps.h>
90#include <ascii-chat/util/time.h> // For timing instrumentation
91#include <libplatform/lifecycle.h>
92
93#include <ascii-chat/audio/audio.h> // lib/audio/audio.h for PortAudio wrapper
94#include <ascii-chat/audio/client_pipeline.h> // Unified audio processing pipeline
95#include <ascii-chat/audio/wav_writer.h> // WAV file dumping for debugging
96#include <ascii-chat/common.h>
97#include <options/options.h>
98#include <options/rcu.h> // For RCU-based options access
99#include <libplatform/system.h> // For platform_memcpy
100
101#include <libplatform/atomic.h>
102#include <ascii-chat/debug/named.h> // For NAMED_REGISTER_ATOMIC macro
103#include <string.h>
104#include <math.h>
105
106#include <libplatform/compiler.h>
107#include <libplatform/system.h>
108#include <libplatform/filesystem.h>
109#include <libplatform/terminal.h>
112
113/* ============================================================================
114 * Audio System State
115 * ============================================================================ */
116
126static audio_context_t g_audio_context = {0};
127
136static lifecycle_t g_audio_client_init_lc = LIFECYCLE_INIT;
137
138/* ============================================================================
139 * Audio Debugging - WAV File Dumpers
140 * ============================================================================ */
141
143static wav_writer_t *g_wav_capture_raw = NULL;
144
146static wav_writer_t *g_wav_capture_processed = NULL;
147
149static wav_writer_t *g_wav_playback_received = NULL;
150
151static bool audio_dump_directory(char *directory, size_t capacity) {
152 const char *selected = SAFE_GETENV("ASCIICHAT_DUMP_AUDIO_DIR");
153 if (selected && selected[0]) {
154 int written = safe_snprintf(directory, capacity, "%s", selected);
155 return written >= 0 && (size_t)written < capacity;
156 }
157 return platform_get_temp_dir(directory, capacity);
158}
159
160/* ============================================================================
161 * Audio Capture Thread Management
162 * ============================================================================ */
163
164/* ============================================================================
165 * Async Audio Packet Queue (decouples capture from network I/O)
166 * ============================================================================ */
167
179static int audio_queue_packet(client_connection_t *connection, const uint8_t *opus_data, size_t opus_size, const uint16_t *frame_sizes, int frame_count) {
180 if (connection->audio_send_direct) {
182 asciichat_error_t result = threaded_send_audio_opus_batch(connection, opus_data, opus_size, frame_sizes, frame_count);
184 return result == ASCIICHAT_OK ? 0 : -1;
185 }
186 if (!lifecycle_is_initialized(&connection->audio_send_queue_lc) || !opus_data || opus_size == 0) {
187 return -1;
188 }
189
190 mutex_lock(&connection->audio_send_queue_mutex);
191
192 // Check if queue is full
193 int next_head = (connection->audio_send_queue_head + 1) % AUDIO_SEND_QUEUE_SIZE;
194 if (next_head == connection->audio_send_queue_tail) {
195 mutex_unlock(&connection->audio_send_queue_mutex);
196 log_warn_every(LOG_RATE_FAST, "Audio send queue full, dropping packet");
197 return -1;
198 }
199
200 // Copy packet to queue
201 audio_send_packet_t *packet = &connection->audio_send_queue[connection->audio_send_queue_head];
202 if (opus_size > sizeof(packet->data)) {
203 mutex_unlock(&connection->audio_send_queue_mutex);
204 log_warn("Audio packet too large for queue: %zu > %zu bytes", opus_size, sizeof(packet->data));
205 return -1;
206 }
207
208 memcpy(packet->data, opus_data, opus_size);
209 packet->size = opus_size;
210 packet->frame_count = frame_count;
212 for (int i = 0; i < frame_count && i < MAX_BATCH_FRAMES; i++) {
213 packet->frame_sizes[i] = frame_sizes[i];
214 }
215 connection->audio_send_queue_head = next_head;
216
217 // Signal sender thread (must be held while signaling)
218 cond_signal(&connection->audio_send_queue_cond);
219 mutex_unlock(&connection->audio_send_queue_mutex);
220
221 return 0;
222}
223
230static void *audio_sender_thread_func(void *arg) {
231 client_connection_t *connection = arg;
232 log_debug("Audio sender thread started");
233
234 int send_count = 0;
235
236 while (!atomic_load_bool(&connection->audio_sender_should_exit)) {
237 mutex_lock(&connection->audio_send_queue_mutex);
238
239 // Wait for packet or exit signal (cond_wait re-acquires mutex before returning)
240 while (connection->audio_send_queue_head == connection->audio_send_queue_tail && !atomic_load_bool(&connection->audio_sender_should_exit)) {
241 cond_wait(&connection->audio_send_queue_cond, &connection->audio_send_queue_mutex);
242 }
243
244 // Check exit flag after waking from cond_wait
245 if (atomic_load_bool(&connection->audio_sender_should_exit)) {
246 mutex_unlock(&connection->audio_send_queue_mutex);
247 break;
248 }
249
250 // Safety check: ensure queue actually has data (handle spurious wakeups)
251 if (connection->audio_send_queue_head == connection->audio_send_queue_tail) {
252 // Spurious wakeup - queue is empty, go back to waiting
253 mutex_unlock(&connection->audio_send_queue_mutex);
254 continue;
255 }
256
257 // Dequeue packet
258 audio_send_packet_t packet = connection->audio_send_queue[connection->audio_send_queue_tail];
259 connection->audio_send_queue_tail = (connection->audio_send_queue_tail + 1) % AUDIO_SEND_QUEUE_SIZE;
260
261 mutex_unlock(&connection->audio_send_queue_mutex);
262 ASCII_OTEL_SCOPE_PARENT(CLIENT_AUDIO_SEND, &packet.telemetry_context);
263
264 // Send packet (may block on network I/O - that's OK, we're not in capture thread)
266 uint64_t network_send_audio_start_ns = time_get_ns();
267 asciichat_error_t send_result = threaded_send_audio_opus_batch(connection, packet.data, packet.size, packet.frame_sizes, packet.frame_count);
268 double send_time_ns = (double)time_elapsed_ns(network_send_audio_start_ns, time_get_ns());
269
270 send_count++;
271 if (send_result != ASCIICHAT_OK) {
272 LOG_ERRNO_IF_SET("Failed to send audio packet");
273 if (!network_error_is_local_rejection(send_result))
274 break;
276 } else {
277 ASSERT_NO_ERRNO_SINCE(send_scope);
279 }
280 if (send_result == ASCIICHAT_OK && send_count % 50 == 0) {
281 char duration_str[TIME_DURATION_BUFFER_SIZE];
282 time_pretty((uint64_t)(send_time_ns), -1, duration_str, sizeof(duration_str));
283 log_debug("Audio network send #%d: %zu bytes (%d frames) in %s", send_count, packet.size, packet.frame_count, duration_str);
284 }
285 }
286
287 // Clean up thread-local error context before exit
288
289 // Signal that audio sender thread has exited
290 atomic_store_bool(&connection->audio_sender_exited, true);
291
292 return NULL;
293}
294
305 if (mutex_init(&connection->audio_send_queue_mutex, "audio_send_queue_mutex") != 0) {
306 return SET_ERRNO(ERROR_THREAD, "Audio queue mutex initialization failed");
307 }
308 connection->audio_queue_mutex_initialized = true;
309 if (client_connection_checkpoint("audio-queue-mutex") != ASCIICHAT_OK)
310 return ERROR_THREAD;
311 if (cond_init(&connection->audio_send_queue_cond, "audio_send_queue_cond") != 0) {
312 return SET_ERRNO(ERROR_THREAD, "Audio queue condition initialization failed");
313 }
314 connection->audio_queue_cond_initialized = true;
315 lifecycle_init(&connection->audio_send_queue_lc, "audio_queue");
316 if (client_connection_checkpoint("audio-queue") != ASCIICHAT_OK)
317 return ERROR_THREAD;
318 acip_transport_t *transport = atomic_ptr_load(&connection->transport);
319 if (transport && transport->outbound) {
320 // Admission copies bounded audio into the transport queue without socket I/O.
321 connection->audio_send_direct = true;
322 atomic_store_bool(&connection->audio_sender_exited, true);
323 return client_connection_checkpoint("audio-sender");
324 }
325 asciichat_error_t result = thread_pool_spawn(connection->client_worker_pool, audio_sender_thread_func, connection, 5, "audio_sender");
326 if (result != ASCIICHAT_OK)
327 return result;
328 connection->audio_sender_thread_created = true;
329 return client_connection_checkpoint("audio-sender");
330}
331
336 if (!connection)
337 return;
338 // Signal thread to exit
339 if (lifecycle_is_initialized(&connection->audio_send_queue_lc)) {
341 mutex_lock(&connection->audio_send_queue_mutex);
342 cond_signal(&connection->audio_send_queue_cond);
343 mutex_unlock(&connection->audio_send_queue_mutex);
344 }
345
346 // The worker is joined by thread_pool_stop_all() in client_connection_stop().
347 // Keep the queue synchronization objects alive until that join completes.
349 log_debug("Audio sender thread will be joined by thread pool");
350 }
351}
352
354 if (!connection)
355 return;
356 if (connection->audio_queue_cond_initialized) {
357 cond_destroy(&connection->audio_send_queue_cond);
358 connection->audio_queue_cond_initialized = false;
359 }
360 if (connection->audio_queue_mutex_initialized) {
361 mutex_destroy(&connection->audio_send_queue_mutex);
362 connection->audio_queue_mutex_initialized = false;
363 }
364 lifecycle_shutdown(&connection->audio_send_queue_lc);
365}
366
367/* ============================================================================
368 * Audio Processing Constants
369 * ============================================================================ */
370
372#define AUDIO_VOLUME_BOOST 1.0f // No boost/attenuation
373
374/* ============================================================================
375 * Audio Processing Functions
376 * ============================================================================ */
377
392void audio_process_received_samples(client_connection_t *connection, const float *samples, int num_samples) {
393 // Validate parameters
394 if (!connection || !connection->audio_context || !samples || num_samples <= 0) {
395 SET_ERRNO(ERROR_INVALID_PARAM, "Invalid audio samples: samples=%p, num_samples=%d", (void *)samples, num_samples);
396 return;
397 }
398
399 if (!GET_OPTION(audio_enabled)) {
400 log_warn_every(NS_PER_MS_INT, "Received audio samples but audio is disabled");
401 return;
402 }
403
404 // Allow both single packets and batched packets
405 if (num_samples > AUDIO_OPUS_MAX_DECODE_SAMPLES * AUDIO_CHANNELS || num_samples % AUDIO_CHANNELS) {
406 log_warn("Invalid audio sample count: %d (maximum %d)", num_samples, AUDIO_OPUS_MAX_DECODE_SAMPLES * AUDIO_CHANNELS);
407 return;
408 }
409
410 // Calculate RMS energy of received samples
411 float sum_squares = 0.0f;
412 for (int i = 0; i < num_samples; i++) {
413 sum_squares += samples[i] * samples[i];
414 }
415 float received_rms = sqrtf(sum_squares / num_samples);
416
417 // DUMP: Received audio from server (before playback processing)
418 if (g_wav_playback_received) {
419 wav_writer_write(g_wav_playback_received, samples, num_samples);
420 }
421
422 // Track samples for analysis
423 if (GET_OPTION(audio_analysis_enabled)) {
424 for (int i = 0; i < num_samples; i += AUDIO_CHANNELS) {
425 audio_analysis_track_received_sample((samples[i] + samples[i + 1]) * 0.5f);
426 }
427 }
428
429 // Copy samples to playback buffer (no processing needed - mixer already handled clipping)
430 float audio_buffer[AUDIO_OPUS_MAX_DECODE_SAMPLES * AUDIO_CHANNELS];
431 memcpy(audio_buffer, samples, (size_t)num_samples * sizeof(float));
432
433 // DEBUG: Log what we're writing to playback buffer (with first 4 samples to verify audio integrity)
434 int recv_count = ++connection->audio_receive_count;
435 if (recv_count <= 10 || recv_count % 50 == 0) {
436 float peak = 0.0f;
437 for (int i = 0; i < num_samples; i++) {
438 float abs_val = fabsf(samples[i]);
439 if (abs_val > peak)
440 peak = abs_val;
441 }
442 log_debug("CLIENT AUDIO RECV #%d: %d samples, RMS=%.6f, Peak=%.6f, first4=[%.4f,%.4f,%.4f,%.4f]", recv_count, num_samples, received_rms, peak, num_samples > 0 ? samples[0] : 0.0f,
443 num_samples > 1 ? samples[1] : 0.0f, num_samples > 2 ? samples[2] : 0.0f, num_samples > 3 ? samples[3] : 0.0f);
444 }
445
446 // Submit to playback system (goes to jitter buffer and speakers)
447 // NOTE: AEC3's AnalyzeRender is called in output_callback() when audio actually plays,
448 // NOT here. The jitter buffer adds 50-100ms delay, so calling AnalyzeRender here
449 // would give AEC3 the wrong timing and break echo cancellation.
450 audio_write_samples(connection->audio_context, audio_buffer, num_samples);
451
452 // Log latency after writing to playback buffer
453 if (connection->audio_context->playback_buffer) {
454 size_t buffer_samples = audio_ring_buffer_available_read(connection->audio_context->playback_buffer);
455 float buffer_latency_ms = (float)buffer_samples * 1000.0f / (AUDIO_SAMPLE_RATE * AUDIO_CHANNELS);
456 log_dev_every(500 * US_PER_MS_INT, "LATENCY: Client playback buffer after recv: %.1fms (%zu samples)", buffer_latency_ms, buffer_samples);
457 }
458
459#ifdef DEBUG_AUDIO
460 log_debug_every(US_PER_SEC_INT, "Processed %d received audio samples", num_samples);
461#endif
462}
463
464/* ============================================================================
465 * Audio Capture Thread Implementation
466 * ============================================================================ */
467
489static void *audio_capture_thread_func(void *arg) {
490 client_connection_t *connection = arg;
491
492 log_debug("Audio capture thread started");
493
494 // FPS tracking for audio capture thread (tracking Opus frames, ~50 FPS at 20ms per frame)
495 fps_t fps_tracker = {0};
496 bool fps_tracker_initialized = false;
497 if (!fps_tracker_initialized) {
498 fps_init(&fps_tracker, 25, "AUDIO_TX"); // Two 20 ms Opus frames per network batch.
499 fps_tracker_initialized = true;
500 }
501
502 // Detailed timing stats
503 double total_loop_ns = 0;
504 double total_read_ns = 0;
505 double total_encode_ns = 0;
506 double total_queue_ns = 0;
507 double max_loop_ns = 0;
508 double max_read_ns = 0;
509 double max_encode_ns = 0;
510 double max_queue_ns = 0;
511 uint64_t timing_loop_count = 0;
512 int total_reads = 0;
513 int norm_count = 0;
514 int read_count = 0;
515 int encode_count = 0;
516 int batch_send_count = 0;
517 int timeout_flush_count = 0;
518
519// Opus frame size: OPUS_FRAME_SAMPLES samples = 20ms @ 48kHz (must match pipeline config)
520#define OPUS_MAX_PACKET_SIZE 500 // Max Opus packet size
521
522 // Read enough samples per iteration to drain faster than we fill
523 // Buffer holds multiple Opus frames worth to prevent overflow
524 // 4 frames = 3840 samples = 80ms, but we'll read what's available up to this
525#define CAPTURE_READ_SIZE ((OPUS_FRAME_SAMPLES * AUDIO_CHANNELS) * 4)
526
527 float audio_buffer[CAPTURE_READ_SIZE];
528 // Accumulator for building complete Opus frames
529 float opus_frame_buffer[(OPUS_FRAME_SAMPLES * AUDIO_CHANNELS)];
530 int opus_frame_samples_collected = 0;
531
532 // Batch buffer for multiple Opus frames - send all at once to reduce blocking
533#define BATCH_TIMEOUT_NS (320LL * NS_PER_MS_INT) // Flush at most sixteen 20 ms Opus frames (bounded 320 ms latency)
535 uint16_t batch_frame_sizes[MAX_BATCH_FRAMES];
536 int batch_frame_count = 0;
537 size_t batch_total_size = 0;
538 uint64_t batch_start_time_ns = 0;
539 bool batch_has_data = false;
540
541 while (!should_exit() && !server_connection_is_lost(connection)) {
542 uint64_t audio_capture_loop_iteration_start_ns = time_get_ns();
543 timing_loop_count++;
544
545 if (!server_connection_is_active(connection)) {
546 platform_sleep_us(100 * US_PER_MS_INT); // Wait for connection
547 continue;
548 }
549
550 // Check if pipeline is ready
551 if (!connection->audio_pipeline) {
552 platform_sleep_us(100 * US_PER_MS_INT);
553 continue;
554 }
555
556 // Check how many samples are available in the ring buffer
558 if (available <= 0) {
559 // Flush partial batch before sleeping (prevent starvation during idle periods)
560 if (batch_has_data && batch_frame_count > 0) {
561 uint64_t now_ns = time_get_ns();
562 uint64_t elapsed_ns = time_elapsed_ns(batch_start_time_ns, now_ns);
563
564 if (elapsed_ns >= BATCH_TIMEOUT_NS) {
565 ASCII_OTEL_SCOPE(CLIENT_AUDIO_CAPTURE);
566 long elapsed_ms = (long)time_ns_to_ms(elapsed_ns);
567 log_debug_every(LOG_RATE_FAST, "Idle timeout flush: %d frames (%zu bytes) after %ld ms", batch_frame_count, batch_total_size, elapsed_ms);
568 (void)audio_queue_packet(connection, batch_buffer, batch_total_size, batch_frame_sizes, batch_frame_count);
569 batch_frame_count = 0;
570 batch_total_size = 0;
571 batch_has_data = false;
572 }
573 }
574
575 // Sleep briefly to reduce CPU usage when idle
576 // 5ms polling = 200 times/sec, fast enough to catch audio promptly
577 // Note: 50ms was causing 872ms gaps in audio transmission!
578 platform_sleep_us(5 * US_PER_MS_INT); // 5ms (was 50ms - caused huge gaps!)
579 continue;
580 }
581
582 ASCII_OTEL_SCOPE(CLIENT_AUDIO_CAPTURE);
583 // Read as many samples as possible (up to CAPTURE_READ_SIZE) to drain faster
584 // This prevents buffer overflow when processing is slower than capture
585 int to_read = (available < CAPTURE_READ_SIZE) ? available : CAPTURE_READ_SIZE;
586
587 uint64_t audio_read_samples_start_ns = time_get_ns();
588 asciichat_error_t read_result = audio_read_samples(connection->audio_context, audio_buffer, to_read);
589 double read_time_ns = (double)time_elapsed_ns(audio_read_samples_start_ns, time_get_ns());
590
591 total_read_ns += read_time_ns;
592 if (read_time_ns > max_read_ns)
593 max_read_ns = read_time_ns;
594
595 if (read_result != ASCIICHAT_OK) {
596 log_error("Failed to read audio samples from ring buffer");
597 platform_sleep_us(5 * US_PER_MS_INT); // 5ms (error path - was 50ms, caused gaps!)
598 continue;
599 }
600
601 int samples_read = to_read;
602
603 // Log every 10 reads to see if we're getting samples
604 total_reads++;
605 if (total_reads % 10 == 0) {
606 log_debug("Audio capture loop iteration #%d: available=%d, samples_read=%d", total_reads, available, samples_read);
607 }
608
609 if (samples_read > 0) {
610 // Normalize input to prevent clipping: bring peak to ยฑ0.99
611 // Calculate peak level first
612 float peak = 0.0f;
613 for (int i = 0; i < samples_read; i++) {
614 float abs_val = fabsf(audio_buffer[i]);
615 if (abs_val > peak)
616 peak = abs_val;
617 }
618
619 // Apply normalization if peak exceeds 1.0
620 // Use 0.99 to leave headroom for processing
621 if (peak > 1.0f) {
622 float gain = 0.99f / peak;
623 for (int i = 0; i < samples_read; i++) {
624 audio_buffer[i] *= gain;
625 }
626 norm_count++;
627 if (norm_count <= 5 || norm_count % 100 == 0) {
628 log_debug("Input normalization #%d: peak=%.4f, gain=%.4f", norm_count, peak, gain);
629 }
630 }
631
632 // DUMP: Raw captured audio (before any processing)
633 if (g_wav_capture_raw) {
634 wav_writer_write(g_wav_capture_raw, audio_buffer, samples_read);
635 }
636
637 // DEBUG: Log EVERY read to see what we're getting from the ring buffer
638 read_count++;
639 float sum_squares = 0.0f;
640 for (int i = 0; i < samples_read && i < 10; i++) {
641 sum_squares += audio_buffer[i] * audio_buffer[i];
642 }
643 float rms = sqrtf(sum_squares / (samples_read > 10 ? 10 : samples_read));
644 if (read_count <= 5 || read_count % 20 == 0) {
645 log_debug("Audio capture read #%d: available=%d, samples_read=%d, first=[%.6f,%.6f,%.6f], RMS=%.6f", read_count, available, samples_read, samples_read > 0 ? audio_buffer[0] : 0.0f,
646 samples_read > 1 ? audio_buffer[1] : 0.0f, samples_read > 2 ? audio_buffer[2] : 0.0f, rms);
647 }
648
649 // Track sent samples for analysis
650 if (GET_OPTION(audio_analysis_enabled)) {
651 for (int i = 0; i < samples_read; i += AUDIO_CHANNELS) {
652 audio_analysis_track_sent_sample((audio_buffer[i] + audio_buffer[i + 1]) * 0.5f);
653 }
654 }
655
656 // Accumulate samples into Opus frame buffer
657 int samples_to_process = samples_read;
658 int sample_offset = 0;
659
660 while (samples_to_process > 0) {
661 // How many samples can we add to current frame?
662 int space_in_frame = (OPUS_FRAME_SAMPLES * AUDIO_CHANNELS) - opus_frame_samples_collected;
663 int samples_to_copy = (samples_to_process < space_in_frame) ? samples_to_process : space_in_frame;
664
665 // Copy samples to frame buffer
666 memcpy(&opus_frame_buffer[opus_frame_samples_collected], &audio_buffer[sample_offset], (size_t)samples_to_copy * sizeof(float));
667
668 opus_frame_samples_collected += samples_to_copy;
669 sample_offset += samples_to_copy;
670 samples_to_process -= samples_to_copy;
671
672 // Do we have a complete frame?
673 if (opus_frame_samples_collected >= (OPUS_FRAME_SAMPLES * AUDIO_CHANNELS)) {
674 // Process through pipeline: AEC, filters, AGC, noise gate, Opus encode
675 uint8_t opus_packet[OPUS_MAX_PACKET_SIZE];
676
677 uint64_t opus_encode_start_ns = time_get_ns();
678 int opus_len = client_audio_pipeline_capture(connection->audio_pipeline, opus_frame_buffer, (OPUS_FRAME_SAMPLES * AUDIO_CHANNELS), opus_packet, OPUS_MAX_PACKET_SIZE);
679
680 encode_count++;
681 double opus_elapsed_ns = (double)time_elapsed_ns(opus_encode_start_ns, time_get_ns());
682 if (encode_count % 50 == 0) {
683 if (opus_elapsed_ns >= 0.0) {
684 char _duration_str[TIME_DURATION_BUFFER_SIZE];
685 time_pretty((uint64_t)(opus_elapsed_ns), -1, _duration_str, sizeof(_duration_str));
686 log_dev("Opus encode #%d: %d samples -> %d bytes in %s", encode_count, (OPUS_FRAME_SAMPLES * AUDIO_CHANNELS), opus_len, _duration_str);
687 }
688 }
689
690 double encode_time_ns = 0; // Timing already logged
691
692 total_encode_ns += encode_time_ns;
693 if (encode_time_ns > max_encode_ns)
694 max_encode_ns = encode_time_ns;
695
696 if (opus_len > 0) {
697
698 log_debug_every(LOG_RATE_VERY_FAST, "Pipeline encoded: %d samples -> %d bytes (compression: %.1fx)", (OPUS_FRAME_SAMPLES * AUDIO_CHANNELS), opus_len,
699 (float)((OPUS_FRAME_SAMPLES * AUDIO_CHANNELS) * sizeof(float)) / (float)opus_len);
700
701 // Add to batch buffer
702 if (batch_frame_count < MAX_BATCH_FRAMES && batch_total_size + (size_t)opus_len <= sizeof(batch_buffer)) {
703 // Mark batch start time on first frame
704 if (batch_frame_count == 0) {
705 batch_start_time_ns = time_get_ns();
706 batch_has_data = true;
707 }
708
709 memcpy(batch_buffer + batch_total_size, opus_packet, (size_t)opus_len);
710 batch_frame_sizes[batch_frame_count] = (uint16_t)opus_len;
711 batch_total_size += (size_t)opus_len;
712 batch_frame_count++;
713
714 if (GET_OPTION(audio_analysis_enabled)) {
715 audio_analysis_track_sent_packet((size_t)opus_len);
716 }
717 }
718 } else if (opus_len == 0) {
719 // DTX frame (silence) - no data to send
720 log_debug_every(LOG_RATE_VERY_FAST, "Pipeline DTX frame (silence detected)");
721 }
722
723 // Reset frame buffer
724 opus_frame_samples_collected = 0;
725 }
726 }
727
728 // Queue batch for async sending (non-blocking - sender thread handles network I/O)
729 if (batch_frame_count >= MAX_BATCH_FRAMES || (batch_frame_count > 0 && time_get_ns() - batch_start_time_ns >= BATCH_TIMEOUT_NS)) {
730 batch_send_count++;
731
732 uint64_t audio_queue_packet_start_ns = time_get_ns();
733 int queue_result = audio_queue_packet(connection, batch_buffer, batch_total_size, batch_frame_sizes, batch_frame_count);
734 double queue_time_ns = (double)time_elapsed_ns(audio_queue_packet_start_ns, time_get_ns());
735
736 total_queue_ns += queue_time_ns;
737 if (queue_time_ns > max_queue_ns)
738 max_queue_ns = queue_time_ns;
739
740 if (queue_result < 0) {
741 log_debug_every(LOG_RATE_VERY_FAST, "Failed to queue audio batch (queue full)");
742 } else {
743 if (batch_send_count <= 10 || batch_send_count % 50 == 0) {
744 char queue_duration_str[TIME_DURATION_BUFFER_SIZE];
745 time_pretty((uint64_t)(queue_time_ns), -1, queue_duration_str, sizeof(queue_duration_str));
746 log_debug("CLIENT: Queued Opus batch #%d (%d frames, %zu bytes) in %s", batch_send_count, batch_frame_count, batch_total_size, queue_duration_str);
747 }
748 // Track audio frame for FPS reporting
749 fps_frame_ns(&fps_tracker, time_get_ns(), "audio batch queued");
750 }
751
752 // Reset batch
753 batch_frame_count = 0;
754 batch_total_size = 0;
755 batch_has_data = false;
756 }
757
758 // Log overall loop iteration time periodically
759 double loop_time_ns = (double)time_elapsed_ns(audio_capture_loop_iteration_start_ns, time_get_ns());
760 total_loop_ns += loop_time_ns;
761 if (loop_time_ns > max_loop_ns)
762 max_loop_ns = loop_time_ns;
763
764 // Comprehensive timing report every 100 iterations (~2 seconds)
765 if (timing_loop_count % 100 == 0) {
766 char avg_loop_str[TIME_DURATION_BUFFER_SIZE], max_loop_str[TIME_DURATION_BUFFER_SIZE];
767 char avg_read_str[TIME_DURATION_BUFFER_SIZE], max_read_str[TIME_DURATION_BUFFER_SIZE];
768 char avg_encode_str[TIME_DURATION_BUFFER_SIZE], max_encode_str[TIME_DURATION_BUFFER_SIZE];
769 char avg_queue_str[TIME_DURATION_BUFFER_SIZE], max_queue_str[TIME_DURATION_BUFFER_SIZE];
770
771 time_pretty((uint64_t)(total_loop_ns / timing_loop_count), -1, avg_loop_str, sizeof(avg_loop_str));
772 time_pretty((uint64_t)(max_loop_ns), -1, max_loop_str, sizeof(max_loop_str));
773 time_pretty((uint64_t)(total_read_ns / timing_loop_count), -1, avg_read_str, sizeof(avg_read_str));
774 time_pretty((uint64_t)(max_read_ns), -1, max_read_str, sizeof(max_read_str));
775 time_pretty((uint64_t)(total_encode_ns / timing_loop_count), -1, avg_encode_str, sizeof(avg_encode_str));
776 time_pretty((uint64_t)(max_encode_ns), -1, max_encode_str, sizeof(max_encode_str));
777 time_pretty((uint64_t)(total_queue_ns / timing_loop_count), -1, avg_queue_str, sizeof(avg_queue_str));
778 time_pretty((uint64_t)(max_queue_ns), -1, max_queue_str, sizeof(max_queue_str));
779
780 log_debug("CAPTURE TIMING #%lu: loop avg=%s max=%s, read avg=%s max=%s", timing_loop_count, avg_loop_str, max_loop_str, avg_read_str, max_read_str);
781 log_debug(" encode avg=%s max=%s, queue avg=%s max=%s", avg_encode_str, max_encode_str, avg_queue_str, max_queue_str);
782 }
783
784 // Check if we have a partial batch that's been waiting too long (time-based flush)
785 // This prevents batches from sitting indefinitely when audio capture is irregular
786 if (batch_has_data && batch_frame_count > 0) {
787 uint64_t now_ns = time_get_ns();
788 uint64_t elapsed_ns = time_elapsed_ns(batch_start_time_ns, now_ns);
789
790 if (elapsed_ns >= BATCH_TIMEOUT_NS) {
791 timeout_flush_count++;
792
793 long elapsed_ms = (long)time_ns_to_ms(elapsed_ns);
794 log_debug_every(LOG_RATE_FAST, "Timeout flush #%d: %d frames (%zu bytes) after %ld ms", timeout_flush_count, batch_frame_count, batch_total_size, elapsed_ms);
795
796 // Queue partial batch
797 int queue_result = audio_queue_packet(connection, batch_buffer, batch_total_size, batch_frame_sizes, batch_frame_count);
798 if (queue_result == 0) {
799 // Track audio frame for FPS reporting
800 fps_frame_ns(&fps_tracker, time_get_ns(), "audio batch timeout flush");
801 }
802
803 // Reset batch
804 batch_frame_count = 0;
805 batch_total_size = 0;
806 batch_has_data = false;
807 }
808 }
809
810 // Yield to reduce CPU usage - audio arrives at ~20ms per Opus frame (OPUS_FRAME_SAMPLES samples @ 48kHz)
811 // Without sleep, thread spins at 90-100% CPU constantly checking for new samples
812 // Even 1ms sleep reduces CPU usage from 90% to <10% with minimal latency impact
813 platform_sleep_us(1 * US_PER_MS_INT); // 1ms
814 } else {
815 // Track loop time even when no samples processed
816 double loop_time_ns = (double)time_elapsed_ns(audio_capture_loop_iteration_start_ns, time_get_ns());
817 total_loop_ns += loop_time_ns;
818 if (loop_time_ns > max_loop_ns)
819 max_loop_ns = loop_time_ns;
820
821 // Flush partial batch before sleeping on error path (prevent starvation)
822 if (batch_has_data && batch_frame_count > 0) {
823 uint64_t now_ns = time_get_ns();
824 uint64_t elapsed_ns = time_elapsed_ns(batch_start_time_ns, now_ns);
825
826 if (elapsed_ns >= BATCH_TIMEOUT_NS) {
827 long elapsed_ms = (long)time_ns_to_ms(elapsed_ns);
828 log_debug_every(LOG_RATE_FAST, "Error path timeout flush: %d frames (%zu bytes) after %ld ms", batch_frame_count, batch_total_size, elapsed_ms);
829 (void)audio_queue_packet(connection, batch_buffer, batch_total_size, batch_frame_sizes, batch_frame_count);
830 batch_frame_count = 0;
831 batch_total_size = 0;
832 batch_has_data = false;
833 }
834 }
835
836 platform_sleep_us(5 * US_PER_MS_INT); // 5ms (error path - was 50ms, caused gaps!)
837 }
838 }
839
840 log_debug("Audio capture thread stopped");
842
843 // Clean up thread-local error context before exit
844
845 return NULL;
846}
847
848/* ============================================================================
849 * Public Interface Functions
850 * ============================================================================ */
851
863 if (!GET_OPTION(audio_enabled)) {
864 return 0; // Audio disabled - not an error
865 }
866
867 // Use lifecycle to ensure only one thread initializes audio at a time
868 // This prevents race conditions in worker thread creation
869 if (!lifecycle_init_once(&g_audio_client_init_lc)) {
870 // Another thread is already initializing, wait for it to complete
871 while (!lifecycle_is_initialized(&g_audio_client_init_lc)) {
872 platform_sleep_us(1000); // 1ms
873 }
874 return 0; // Already initialized by another thread
875 }
876
877 // Clean up previous audio context if it was already initialized
878 // This prevents leaks and thread creation issues on reconnection
879 if (g_audio_context.initialized) {
880 log_debug("Cleaning up previous audio context before reinitializing");
881 audio_destroy(&g_audio_context);
882 // NOTE: Do NOT reset lifecycle - keep it INITIALIZED so only ONE global
883 // audio context is ever created. Subsequent calls will return early.
884 }
885
886 // Initialize WAV dumper for received audio if debugging enabled
887 if (wav_dump_enabled()) {
888 char tmp[PLATFORM_MAX_PATH_LENGTH];
889 if (audio_dump_directory(tmp, sizeof(tmp))) {
890 char path[PLATFORM_MAX_PATH_LENGTH];
891 safe_snprintf(path, sizeof(path), "%s/audio_playback_received.wav", tmp);
892 g_wav_playback_received = wav_writer_open(path, AUDIO_SAMPLE_RATE, AUDIO_CHANNELS);
893 if (g_wav_playback_received) {
894 log_debug("Audio debugging enabled: dumping received audio to %s", path);
895 }
896 }
897 }
898
899 // Initialize PortAudio context using library function
900 log_debug("About to call audio_init()...");
901 if (audio_init(&g_audio_context) != ASCIICHAT_OK) {
902 log_error("Failed to initialize audio system");
903 // Clean up WAV writer if it was opened
904 if (g_wav_playback_received) {
905 wav_writer_close(g_wav_playback_received);
906 g_wav_playback_received = NULL;
907 }
908 lifecycle_init_abort(&g_audio_client_init_lc);
909 return -1;
910 }
911 log_debug("audio_init() completed successfully");
912
913 // Initialize WAV dumpers only once (file handles persist)
914 if (wav_dump_enabled()) {
915 char tmp[PLATFORM_MAX_PATH_LENGTH];
916 if (audio_dump_directory(tmp, sizeof(tmp))) {
917 char path[PLATFORM_MAX_PATH_LENGTH];
918 safe_snprintf(path, sizeof(path), "%s/audio_capture_raw.wav", tmp);
919 g_wav_capture_raw = wav_writer_open(path, AUDIO_SAMPLE_RATE, AUDIO_CHANNELS);
920 safe_snprintf(path, sizeof(path), "%s/audio_capture_processed.wav", tmp);
921 g_wav_capture_processed = wav_writer_open(path, AUDIO_SAMPLE_RATE, AUDIO_CHANNELS);
922 log_debug("Audio debugging enabled: dumping to %s/audio_capture_*.wav", tmp);
923 }
924 }
925
926 lifecycle_init_commit(&g_audio_client_init_lc);
927
928 return 0;
929}
930
932 if (!connection->audio_context || !connection->audio_context->initialized)
933 return ASCIICHAT_OK;
934 // Create unified audio pipeline (handles AEC, AGC, noise suppression, Opus)
937
938 // Enable echo cancellation, AGC, and essential processing for clear audio
939 // Noise suppression and VAD can destroy music quality, so keep them disabled
940 pipeline_config.flags.echo_cancel = true; // ENABLE: removes echo
941 pipeline_config.flags.jitter_buffer = true; // ENABLE: needed for AEC sync
942 pipeline_config.flags.noise_suppress = false; // DISABLED: destroys music quality
943 pipeline_config.flags.agc = true; // ENABLE: boost quiet microphones (35 dB gain)
944 pipeline_config.flags.vad = false; // DISABLED: destroys music quality
945 pipeline_config.flags.compressor = true; // ENABLE: prevent clipping from AGC boost
946 pipeline_config.flags.noise_gate = false; // DISABLED: would cut quiet music passages
947 pipeline_config.flags.highpass = true; // ENABLE: remove rumble and low-frequency feedback
948 pipeline_config.flags.lowpass = false; // DISABLED: preserve high-frequency content
949
950 // Applied before streams start; actual underflows increase this bounded target.
952
953 log_debug("About to create audio pipeline...");
954 connection->audio_pipeline = client_audio_pipeline_create(&pipeline_config);
955 log_debug("client_audio_pipeline_create() returned");
956 if (!connection->audio_pipeline) {
957 return SET_ERRNO(ERROR_AUDIO, "Failed to create connection audio pipeline");
958 }
959
960 log_debug("Audio pipeline created: %d Hz sample rate, %d bps bitrate", pipeline_config.sample_rate, pipeline_config.opus_bitrate);
961
962 // Associate pipeline with audio context for echo cancellation
963 // The audio output callback will feed playback samples directly to AEC3 from the speaker output,
964 // ensuring proper timing synchronization (not from the decode path 50-100ms earlier)
965 audio_set_pipeline(connection->audio_context, (void *)connection->audio_pipeline);
966
967 // Audio streams start from protocol_start_connection() after the network
968 // connection succeeds, so local media playback does not advance while dialing.
969
970 return 0;
971}
972
984 if (!connection->audio_context)
985 return 0;
986 log_debug("audio_enabled=%d", GET_OPTION(audio_enabled));
987
988 if (!GET_OPTION(audio_enabled)) {
989 log_debug("Audio is disabled, skipping audio capture thread creation");
990 return 0; // Audio disabled - not an error
991 }
992
993 // Check if thread is already running (not just created flag)
995 log_warn("Audio capture thread already running");
996 return 0;
997 }
998
999 // Note: STREAM_START for audio was already sent by protocol_start_connection()
1000 // No need to send it again here - the protocol layer handles sending combined stream types
1001
1002 // Start audio capture thread
1004 if (thread_pool_spawn(connection->client_worker_pool, audio_capture_thread_func, connection, 4, "audio_capture") != ASCIICHAT_OK) {
1005 log_error("Failed to spawn audio capture thread in worker pool");
1006 LOG_ERRNO_IF_SET("Audio capture thread creation failed");
1007 return -1;
1008 }
1009
1010 connection->audio_capture_thread_created = true;
1011
1012 return 0;
1013}
1014
1024 if (!connection || !connection->audio_context)
1025 return;
1026 // If audio was never initialized (e.g., in snapshot mode), nothing to stop
1027 if (!connection->audio_context->initialized) {
1028 log_debug("[AUDIO_STOP] Audio never initialized, nothing to stop");
1029 return;
1030 }
1031
1032 // Signal audio worker thread to exit first (stop processing)
1033 log_debug("[AUDIO_STOP] Signaling audio worker thread to stop");
1035 // Wake up the worker thread if it's waiting on condition variable
1036 mutex_lock(&connection->audio_context->audio_processing_wait_guard);
1037 cond_signal(&connection->audio_context->audio_processing_wakeup);
1038 mutex_unlock(&connection->audio_context->audio_processing_wait_guard);
1039 log_debug("[AUDIO_STOP] Worker thread stop signal sent");
1040
1041 // Signal audio sender thread to exit first.
1042 // This must happen before thread_pool_stop_all() is called, otherwise the sender
1043 // thread will be stuck in cond_wait() and thread_pool_stop_all() will hang forever.
1044 // The sender thread uses a condition variable to wait for packets - we must wake it up.
1045 // NOTE: Only do this if audio_sender_init() was actually called
1046 if (lifecycle_is_initialized(&connection->audio_send_queue_lc)) {
1047 log_debug("[AUDIO_STOP] Signaling audio sender thread to exit");
1048 atomic_store_bool(&connection->audio_sender_should_exit, true);
1049 mutex_lock(&connection->audio_send_queue_mutex);
1050 cond_signal(&connection->audio_send_queue_cond);
1051 mutex_unlock(&connection->audio_send_queue_mutex);
1052 log_debug("[AUDIO_STOP] Signal sent to sender thread");
1053
1054 // Wait for audio sender thread to exit gracefully
1055 // This must complete before thread_pool_stop_all() to prevent deadlock
1056 // CRITICAL: Longer timeout to ensure thread completes any in-flight send operations
1057 // Network operations can block for 10-50ms, so we need to wait longer
1058 int wait_count = 0;
1059 log_debug("[AUDIO_STOP] Waiting for sender thread to exit, max 500ms");
1060 while (wait_count < 50 && !atomic_load_bool(&connection->audio_sender_exited)) {
1061 platform_sleep_us(10 * US_PER_MS_INT); // 10ms * 50 = 500ms max wait
1062 wait_count++;
1063 }
1064
1065 if (!atomic_load_bool(&connection->audio_sender_exited)) {
1066 log_warn("[AUDIO_STOP] Audio sender thread not responding after 500ms - will be joined by thread pool");
1067 } else {
1068 log_debug("[AUDIO_STOP] Sender thread exited successfully");
1069 }
1070 } else {
1071 log_debug("[AUDIO_STOP] Audio sender was never initialized (connection failed), skipping sender shutdown");
1072 }
1073
1074 log_debug("[AUDIO_STOP] Checking if capture thread was created: g_audio_capture_thread_created=%d", connection->audio_capture_thread_created);
1076 log_debug("[AUDIO_STOP] Capture thread was not created, committing lifecycle and returning");
1077 log_debug("[AUDIO_STOP] Lifecycle committed, returning");
1078 return;
1079 }
1080 log_debug("[AUDIO_STOP] Capture thread was created, waiting for it to exit");
1081
1082 // Note: We don't call signal_exit() here because that's for global shutdown only
1083 // The audio capture thread checks server_connection_is_active() to detect connection loss
1084
1085 // Wait for thread to exit gracefully
1086 // Threads check should_exit() every 1-5ms, so timeout can be much shorter than 500ms
1087 int wait_count2 = 0;
1088 log_debug("[AUDIO_STOP] Waiting for capture thread to exit, max 50ms");
1089 while (wait_count2 < 5 && !atomic_load_bool(&connection->audio_capture_thread_exited)) {
1090 platform_sleep_us(10 * US_PER_MS_INT); // 10ms * 5 = 50ms max wait (threads respond in ~1-5ms)
1091 wait_count2++;
1092 }
1093
1094 if (!atomic_load_bool(&connection->audio_capture_thread_exited)) {
1095 log_warn("[AUDIO_STOP] Audio capture thread not responding - will be joined by thread pool");
1096 } else {
1097 log_debug("[AUDIO_STOP] Capture thread exited successfully");
1098 }
1099
1100 // Thread will be joined by thread_pool_stop_all() in client_connection_stop()
1101 connection->audio_capture_thread_created = false;
1102
1103 log_debug("Audio capture thread stopped");
1104}
1105
1116
1126 if (!GET_OPTION(audio_enabled)) {
1127 return;
1128 }
1129
1130 // Connection workers have joined before application audio cleanup.
1131
1132 // Stop audio stream before destroying pipeline to prevent race condition.
1133 // PortAudio may invoke the callback one more time after we request stop.
1134 // We need to clear the pipeline pointer first so the callback can't access freed memory.
1135 if (g_audio_context.initialized) {
1136 audio_stop_duplex(&g_audio_context);
1137 }
1138
1139 // Clear the pipeline pointer from audio context BEFORE destroying pipeline
1140 // This prevents any lingering PortAudio callbacks from trying to access freed memory
1141 audio_set_pipeline(&g_audio_context, NULL);
1143
1144 // Sleep to allow CoreAudio threads to finish executing callbacks.
1145 // On macOS, CoreAudio's internal threads may continue running after Pa_StopStream() returns.
1146 // The duplex_callback may still be in-flight on other threads. Even after we set the pipeline
1147 // pointer to NULL, a CoreAudio thread may have already cached the pointer before the assignment.
1148 // This sleep ensures all in-flight callbacks have fully completed before we destroy the pipeline.
1149 // WORKAROUND: Skip sleep during shutdown (process is exiting anyway)
1150 // 500ms is sufficient on macOS for CoreAudio's internal thread pool to completely wind down.
1151 // platform_sleep_us(500 * US_PER_MS_INT); // 500ms - macOS CoreAudio needs time to shut down all threads
1152
1153 // Close WAV dumpers
1154 if (g_wav_capture_raw) {
1155 wav_writer_close(g_wav_capture_raw);
1156 g_wav_capture_raw = NULL;
1157 log_debug("Closed audio capture raw dump");
1158 }
1159 if (g_wav_capture_processed) {
1160 wav_writer_close(g_wav_capture_processed);
1161 g_wav_capture_processed = NULL;
1162 log_debug("Closed audio capture processed dump");
1163 }
1164 if (g_wav_playback_received) {
1165 wav_writer_close(g_wav_playback_received);
1166 g_wav_playback_received = NULL;
1167 log_debug("Closed audio playback received dump");
1168 }
1169
1170 // Finally destroy the audio context
1171 if (g_audio_context.initialized) {
1172 audio_destroy(&g_audio_context);
1173 }
1174}
1175
1183 return connection ? connection->audio_pipeline : NULL;
1184}
1185
1196int audio_decode_opus(client_connection_t *connection, const uint8_t *opus_data, size_t opus_len, float *output, int max_samples) {
1197 if (!connection->audio_pipeline || !output || max_samples <= 0) {
1198 return -1;
1199 }
1200
1201 return client_audio_pipeline_playback(connection->audio_pipeline, opus_data, (int)opus_len, output, max_samples);
1202}
1203
1211 return &g_audio_context;
1212}
void audio_analysis_track_received_sample(float sample)
Track received audio sample.
Definition analysis.c:279
void audio_analysis_track_sent_sample(float sample)
Track sent audio sample.
Definition analysis.c:181
void audio_analysis_track_sent_packet(size_t size)
Track sent packet.
Definition analysis.c:252
Audio Analysis and Debugging Interface.
void errno_scope_end(asciichat_errno_scope_t scope, asciichat_errno_outcome_t outcome)
asciichat_errno_scope_t errno_scope_begin(void)
#define SET_ERRNO(code,...)
#define ASSERT_NO_ERRNO_SINCE(scope)
@ ASCIICHAT_ERRNO_HANDLED
#define LOG_ERRNO_IF_SET(message)
void atomic_store_bool(atomic_t *a, bool value)
Atomically store a boolean value.
bool atomic_load_bool(atomic_t *a)
Atomic pointer wrapper.
void * atomic_ptr_load(atomic_ptr_t *a)
Atomically load a pointer.
#define OPUS_FRAME_SAMPLES
#define AUDIO_OPUS_MAX_DECODE_SAMPLES
#define AUDIO_SAMPLE_RATE
Definition audio/limits.h:8
#define AUDIO_CHANNELS
Definition audio/limits.h:9
ascii-chat Client Media Capture Management Interface
ascii-chat Client Mode Entry Point Header
int client_audio_pipeline_playback(client_audio_pipeline_t *pipeline, const uint8_t *opus_in, int opus_len, float *output, int num_samples)
Decode Opus packet and process for playback.
client_audio_pipeline_t * client_audio_pipeline_create(const client_audio_pipeline_config_t *config)
Create and initialize a client audio pipeline.
client_audio_pipeline_config_t client_audio_pipeline_config_for_source(bool has_media_audio)
int client_audio_pipeline_capture(client_audio_pipeline_t *pipeline, const float *input, int num_samples, uint8_t *opus_out, int max_opus_len)
Process captured audio and encode to Opus.
Unified client-side audio processing pipeline.
Platform initialization and static synchronization helpers.
#define MAX_BATCH_FRAMES
#define AUDIO_SEND_QUEUE_SIZE
Named object registry for debugging โ€” log identifiable resource names.
โฑ๏ธ FPS tracking utility for monitoring frame throughput across all threads
asciichat_error_t audio_init(audio_context_t *ctx)
Initialize audio context and PortAudio.
size_t audio_ring_buffer_available_read(audio_ring_buffer_t *rb)
Get number of samples available for reading.
void audio_destroy(audio_context_t *ctx)
Destroy audio context and clean up resources.
asciichat_error_t audio_stop_duplex(audio_context_t *ctx)
Stop full-duplex audio.
asciichat_error_t audio_write_samples(audio_context_t *ctx, const float *buffer, int num_samples)
Write audio samples to playback buffer.
void audio_terminate_portaudio_final(void)
Terminate PortAudio and free all device resources.
void audio_set_pipeline(audio_context_t *ctx, void *pipeline)
Set audio pipeline for echo cancellation.
asciichat_error_t audio_read_samples(audio_context_t *ctx, float *buffer, int num_samples)
Read captured audio samples from capture buffer.
unsigned short uint16_t
unsigned int uint32_t
#define SAFE_GETENV(name)
void fps_frame_ns(fps_t *tracker, uint64_t current_time_ns, const char *context)
Track a frame and detect lag conditions (nanosecond version - PRIMARY).
Definition fps.c:49
#define PLATFORM_MAX_PATH_LENGTH
unsigned long long uint64_t
unsigned char uint8_t
void fps_init(fps_t *tracker, int expected_fps, const char *name)
Initialize FPS tracker.
Definition fps.c:29
asciichat_error_t
Error and exit codes - unified status values (0-255).
Definition error_codes.h:48
@ ERROR_AUDIO
Definition error_codes.h:66
@ ASCIICHAT_OK
Definition error_codes.h:50
@ ERROR_INVALID_PARAM
@ ERROR_THREAD
#define LOG_RATE_VERY_FAST
Log rate limit: 0.1 seconds (100,000 microseconds) - for very high frequency operations.
Definition log_rates.h:27
#define LOG_RATE_FAST
Log rate limit: 1 second (1,000,000 microseconds).
Definition log_rates.h:30
#define log_warn(...)
Log a WARN message.
Definition log/log.h:615
#define log_dev(...)
Log a DEV message (most verbose, development only).
Definition log/log.h:575
#define log_error(...)
Log an ERROR message.
Definition log/log.h:628
#define log_debug(...)
Log a DEBUG message.
Definition log/log.h:589
bool media_source_has_audio(media_source_t *source)
Check if media source has audio stream.
Definition source.c:774
int time_pretty(uint64_t nanoseconds, int decimals, char *buffer, size_t buffer_size)
Format nanoseconds as a compact duration with configurable sub-minute precision.
Definition time.c:368
#define TIME_DURATION_BUFFER_SIZE
Definition time.h:40
#define NS_PER_MS_INT
Definition time.h:172
uint64_t time_elapsed_ns(uint64_t start_ns, uint64_t end_ns)
Calculate elapsed time with wraparound safety.
Definition time.c:117
#define US_PER_MS_INT
Definition time.h:176
#define US_PER_SEC_INT
Definition time.h:177
#define GET_OPTION(field)
Definition options.h:1333
struct acip_transport acip_transport_t
Forward declaration of ACIP transport.
Definition host.h:508
void * session_capture_get_media_source(session_capture_ctx_t *ctx)
Get the underlying media source from capture context.
๐Ÿ”Š Audio Capture and Playback Interface for ascii-chat
โš™๏ธ Common definitions, error codes, macros, and types shared throughout the application
Network error handling utilities.
void audio_ring_buffer_set_jitter_target(audio_ring_buffer_t *rb, uint32_t samples)
bool network_error_is_local_rejection(asciichat_error_t code)
#define log_debug_every(interval_us, fmt,...)
Rate-limited DEBUG logging.
Definition log/log.h:743
#define log_warn_every(interval_us, fmt,...)
Rate-limited WARN logging.
Definition log/log.h:749
#define log_dev_every(interval_us, fmt,...)
Rate-limited DEV logging.
Definition log/log.h:740
bool should_exit(void)
Definition main.c:248
Global application exit and signal handling API.
๐Ÿ”ข Mathematical Utility Functions
โš™๏ธ Unified options parsing system for ascii-chat with builder pattern and lock-free access
gotel_context_t ascii_otel_context(void)
Definition otel.c:704
#define ASCII_OTEL_SCOPE(id)
Definition otel.h:77
#define ASCII_OTEL_SCOPE_PARENT(id, parent)
Definition otel.h:74
๐ŸŽฌ Unified media source abstraction for webcam, files, and stdin
void audio_cleanup()
Cleanup audio subsystem.
void audio_sender_cleanup(client_connection_t *connection)
Cleanup async audio sender.
asciichat_error_t audio_connection_init(client_connection_t *connection)
int audio_client_init()
Initialize audio subsystem.
void audio_stop_thread(client_connection_t *connection)
Stop audio capture thread.
int audio_decode_opus(client_connection_t *connection, const uint8_t *opus_data, size_t opus_len, float *output, int max_samples)
Decode Opus packet using the audio pipeline.
int audio_start_thread(client_connection_t *connection)
Start audio capture thread.
audio_context_t * audio_get_context(void)
Get the global audio context for use by other subsystems.
#define CAPTURE_READ_SIZE
client_audio_pipeline_t * audio_get_pipeline(client_connection_t *connection)
Get the audio pipeline (for advanced usage).
#define BATCH_TIMEOUT_NS
void audio_process_received_samples(client_connection_t *connection, const float *samples, int num_samples)
Process received audio samples from server.
bool audio_thread_exited(client_connection_t *connection)
Check if audio capture thread has exited.
asciichat_error_t audio_sender_init(client_connection_t *connection)
Initialize async audio sender queue and thread.
void audio_sender_finalize(client_connection_t *connection)
#define OPUS_MAX_PACKET_SIZE
ascii-chat Client Audio Processing Management Interface
asciichat_error_t client_connection_checkpoint(const char *stage)
struct client_connection client_connection_t
bool server_connection_is_active(client_connection_t *connection)
Establish connection to ascii-chat server.
asciichat_error_t threaded_send_audio_opus_batch(client_connection_t *connection, const uint8_t *opus_data, size_t opus_size, const uint16_t *frame_sizes, int frame_count)
Thread-safe Opus audio batch packet transmission.
bool server_connection_is_lost(client_connection_t *connection)
Check if connection loss has been detected.
ascii-chat Client Server Connection Management Interface
๐Ÿ”ค String Manipulation and Shell Escaping Utilities
struct acip_outbound * outbound
Per-connection plaintext admission and scheduling.
When code needs to work with asciichat errno snapshot, it keeps total, copied here instead of scatter...
When code needs to work with audio device info, it keeps index, max_input_channels,...
bool initialized
True if context has been initialized.
audio_ring_buffer_t * playback_buffer
OLD: Network โ†’ Worker (will become network_playback_rb).
atomic_t worker_should_stop
Signal worker thread to exit.
audio_ring_buffer_t * capture_buffer
OLD: Worker โ†’ Network (will become processed_capture_rb).
mutex_t audio_processing_wait_guard
Coordinates device-worker sleep with shutdown.
cond_t audio_processing_wakeup
Capture/AEC work, monitor refill, or shutdown notification.
uint8_t data[16 *CLIENT_AUDIO_PIPELINE_MAX_OPUS_PACKET]
gotel_context_t telemetry_context
Pipeline configuration parameters.
client_audio_pipeline_flags_t flags
Client audio pipeline state.
client_audio_pipeline_t * audio_pipeline
thread_pool_t * client_worker_pool
audio_send_packet_t audio_send_queue[32]
session_capture_ctx_t * capture_context
atomic_t audio_capture_thread_exited
lifecycle_t audio_send_queue_lc
audio_context_t * audio_context
Counters and timestamps used to calculate and report frame rate.
Definition fps.h:57
File handle and sample counters needed to finalize a WAV recording.
Definition wav_writer.h:28
๐Ÿงต Thread lifecycle management helpers
#define THREAD_IS_CREATED(created_flag)
Definition thread.h:66
asciichat_error_t thread_pool_spawn(thread_pool_t *pool, void *(*thread_func)(void *), void *thread_arg, int stop_id, const char *thread_name)
Spawn a worker thread in the pool.
๐Ÿงต Generic thread pool abstraction for managing worker threads
โฑ๏ธ High-precision timing utilities using sokol_time.h and uthash
bool platform_get_temp_dir(char *temp_dir, size_t path_size)
Definition util.c:81
bool wav_dump_enabled(void)
Check if audio dumping is enabled via environment.
Definition wav_writer.c:142
wav_writer_t * wav_writer_open(const char *filepath, int sample_rate, int channels)
Open WAV file for writing.
Definition wav_writer.c:52
int wav_writer_write(wav_writer_t *writer, const float *samples, int num_samples)
Write audio samples to WAV file.
Definition wav_writer.c:98
void wav_writer_close(wav_writer_t *writer)
Close WAV file and finalize header.
Definition wav_writer.c:116
Simple WAV file writer for audio debugging.