ascii-chat 0.11.33
Video chat in your terminal
Loading...
Searching...
No Matches
audio.h File Reference

Stub audio types for WASM builds (uses Web Audio API instead of PortAudio) More...

Go to the source code of this file.

Data Structures

struct  audio_batch_info_t
 Parsed audio batch packet header information. More...
 
struct  audio_frame_t
 

Functions

asciichat_error_t audio_parse_batch_header (const void *data, size_t len, audio_batch_info_t *out_batch)
 
asciichat_error_t audio_validate_batch_params (const audio_batch_info_t *batch)
 
bool audio_is_supported_sample_rate (uint32_t sample_rate)
 

Detailed Description

Stub audio types for WASM builds (uses Web Audio API instead of PortAudio)

This header provides minimal audio type definitions for WASM builds. In WASM, audio is handled by Web Audio API via AudioPipeline.ts instead of PortAudio. This stub provides just enough definitions for network packet parsing to compile.

Definition in file lib/platform/wasm/stubs/audio.h.

Function Documentation

◆ audio_is_supported_sample_rate()

bool audio_is_supported_sample_rate ( uint32_t  sample_rate)

Definition at line 2335 of file lib/audio/audio.c.

2335 {
2336 // List of commonly supported audio sample rates
2337 static const uint32_t supported_rates[] = {
2338 8000, // Telephone quality
2339 16000, // Wideband telephony
2340 24000, // High quality speech
2341 32000, // Good for video
2342 44100, // CD quality (less common in VoIP)
2343 48000, // Standard professional
2344 96000, // High-end professional
2345 192000, // Ultra-high-end mastering
2346 };
2347
2348 const size_t rate_count = sizeof(supported_rates) / sizeof(supported_rates[0]);
2349 for (size_t i = 0; i < rate_count; i++) {
2350 if (sample_rate == supported_rates[i]) {
2351 return true;
2352 }
2353 }
2354
2355 return false;
2356}
unsigned int uint32_t
Definition common.h:58

◆ audio_parse_batch_header()

asciichat_error_t audio_parse_batch_header ( const void *  data,
size_t  len,
audio_batch_info_t *  out_batch 
)

Definition at line 2269 of file lib/audio/audio.c.

2269 {
2270 if (!data) {
2271 return SET_ERRNO(ERROR_INVALID_PARAM, "Audio batch header data pointer is NULL");
2272 }
2273
2274 if (!out_batch) {
2275 return SET_ERRNO(ERROR_INVALID_PARAM, "Audio batch info output pointer is NULL");
2276 }
2277
2278 if (len < sizeof(audio_batch_packet_t)) {
2279 return SET_ERRNO(ERROR_INVALID_PARAM, "Audio batch header too small (len=%zu, expected=%zu)", len,
2280 sizeof(audio_batch_packet_t));
2281 }
2282
2283 const audio_batch_packet_t *batch_header = (const audio_batch_packet_t *)data;
2284
2285 // Unpack network byte order values to host byte order
2286 out_batch->batch_count = ntohl(batch_header->batch_count);
2287 out_batch->total_samples = ntohl(batch_header->total_samples);
2288 out_batch->sample_rate = ntohl(batch_header->sample_rate);
2289 out_batch->channels = ntohl(batch_header->channels);
2290
2291 return ASCIICHAT_OK;
2292}
#define SET_ERRNO(code, context_msg,...)
Set error code with custom context message and log it, returning the error code.
@ ASCIICHAT_OK
Definition error_codes.h:51
@ ERROR_INVALID_PARAM
uint32_t batch_count
Number of audio frames in this batch.
uint32_t channels
Number of channels (1=mono, 2=stereo)
uint32_t sample_rate
Sample rate in Hz (e.g., 48000)
uint32_t total_samples
Total number of samples across all frames.
Audio batch packet structure (Packet Type 28)
Definition packet.h:904
uint32_t channels
Number of audio channels (1=mono, 2=stereo)
Definition packet.h:912
uint32_t batch_count
Number of audio chunks in this batch (usually AUDIO_BATCH_COUNT = 32)
Definition packet.h:906
uint32_t sample_rate
Sample rate in Hz (e.g., 44100, 48000)
Definition packet.h:910
uint32_t total_samples
Total audio samples across all chunks (typically 8192)
Definition packet.h:908

◆ audio_validate_batch_params()

asciichat_error_t audio_validate_batch_params ( const audio_batch_info_t *  batch)

Definition at line 2294 of file lib/audio/audio.c.

2294 {
2295 if (!batch) {
2296 return SET_ERRNO(ERROR_INVALID_PARAM, "Audio batch info pointer is NULL");
2297 }
2298
2299 // Validate batch_count
2300 if (batch->batch_count == 0) {
2301 return SET_ERRNO(ERROR_INVALID_PARAM, "Audio batch count cannot be zero");
2302 }
2303
2304 // Check for reasonable max (256 frames per batch is very generous)
2305 if (batch->batch_count > 256) {
2306 return SET_ERRNO(ERROR_INVALID_PARAM, "Audio batch count too large (batch_count=%u, max=256)", batch->batch_count);
2307 }
2308
2309 // Validate channels (1=mono, 2=stereo, max 8 for multi-channel)
2310 if (batch->channels == 0 || batch->channels > 8) {
2311 return SET_ERRNO(ERROR_INVALID_PARAM, "Invalid channel count (channels=%u, valid=1-8)", batch->channels);
2312 }
2313
2314 // Validate sample rate
2316 return SET_ERRNO(ERROR_INVALID_PARAM, "Unsupported sample rate (sample_rate=%u)", batch->sample_rate);
2317 }
2318
2319 // Check for reasonable sample counts
2320 if (batch->total_samples == 0) {
2321 return SET_ERRNO(ERROR_INVALID_PARAM, "Audio batch has zero samples");
2322 }
2323
2324 // Each batch typically has samples_per_frame worth of samples
2325 // For 48kHz at 20ms per frame: 48000 * 0.02 = 960 samples per frame
2326 // With max 256 frames, that's up to ~245k samples per batch
2327 if (batch->total_samples > 1000000) {
2328 return SET_ERRNO(ERROR_INVALID_PARAM, "Audio batch sample count suspiciously large (total_samples=%u)",
2329 batch->total_samples);
2330 }
2331
2332 return ASCIICHAT_OK;
2333}
bool audio_is_supported_sample_rate(uint32_t sample_rate)
Check if a sample rate is a standard/supported rate.