### Build and Install libfvad Source: https://github.com/dpirch/libfvad/blob/master/README.md Standard commands to configure, build, and install the libfvad library using autoconf/automake. ```bash ./configure make sudo make install ``` -------------------------------- ### Enable Examples During Configuration Source: https://github.com/dpirch/libfvad/blob/master/README.md Optional configuration flag to enable examples during the build process. Requires libsndfile development files. ```bash ./configure --enable-examples ``` -------------------------------- ### Build fvadwav Command-Line Tool Source: https://context7.com/dpirch/libfvad/llms.txt This command builds the `fvadwav` example tool, which processes WAV files from the command line for voice/non-voice frame separation and logging. Ensure you have enabled examples during configuration. ```bash # Build with examples enabled ./configure --enable-examples make ``` -------------------------------- ### Combined Options Example Source: https://context7.com/dpirch/libfvad/llms.txt Demonstrates combining multiple options: setting aggressive mode, frame length, outputting voice frames, and logging detection results. ```bash ./examples/fvadwav -m 3 -f 20 -o speech.wav -l vad_log.txt recording.wav ``` -------------------------------- ### Run Tests Source: https://github.com/dpirch/libfvad/blob/master/README.md Command to execute the library's unit tests using the automake test suite. ```bash make check ``` -------------------------------- ### Basic WAV File Analysis Source: https://context7.com/dpirch/libfvad/llms.txt Analyze a WAV file for voice activity using default settings. ```bash ./examples/fvadwav input.wav ``` -------------------------------- ### Create VAD Instance with fvad_new Source: https://context7.com/dpirch/libfvad/llms.txt Initializes a new VAD instance with default settings. The returned pointer must be freed using fvad_free to avoid memory leaks. ```c #include #include int main(void) { // Create a new VAD instance Fvad *vad = fvad_new(); if (!vad) { fprintf(stderr, "Failed to create VAD instance: out of memory\n"); return 1; } printf("VAD instance created successfully\n"); // Instance is ready with defaults: mode=0, sample_rate=8000 // Always free when done fvad_free(vad); return 0; } ``` -------------------------------- ### Generate Configure Script Source: https://github.com/dpirch/libfvad/blob/master/README.md Command to generate the configure script when building from a git repository. Requires autoconf, libtool, and pkg-config. ```bash autoreconf -i ``` -------------------------------- ### fvad_new Source: https://context7.com/dpirch/libfvad/llms.txt Creates and initializes a new VAD instance with default settings. ```APIDOC ## fvad_new ### Description Creates and initializes a new VAD instance with default settings (mode 0, 8kHz sample rate). Returns a pointer to the VAD instance that must be freed with fvad_free(), or NULL if memory allocation fails. ### Response - **Fvad*** (pointer) - A pointer to the newly created VAD instance or NULL on failure. ``` -------------------------------- ### Set Frame Length Source: https://context7.com/dpirch/libfvad/llms.txt Analyze a WAV file with a specified frame length (10, 20, or 30 ms). ```bash ./examples/fvadwav -f 20 input.wav ``` -------------------------------- ### Import Upstream Changes Source: https://github.com/dpirch/libfvad/blob/master/README.md Script to import changes from the WebRTC Native Code package into the upstream-import branch of libfvad. ```bash tools/import.sh ``` -------------------------------- ### Set Aggressiveness Mode Source: https://context7.com/dpirch/libfvad/llms.txt Analyze a WAV file with a specified aggressiveness mode (0-3). Higher modes are more aggressive in detecting voice. ```bash ./examples/fvadwav -m 2 input.wav ``` -------------------------------- ### Recommended CFLAGS for Warnings Source: https://github.com/dpirch/libfvad/blob/master/README.md Compiler flags recommended for enabling comprehensive warnings during development to catch potential issues. ```c -std=c11 -Wall -Wextra -Wpedantic ``` -------------------------------- ### Configure Input Sample Rate Source: https://context7.com/dpirch/libfvad/llms.txt Sets the input sample rate for the VAD instance. Supported rates are 8000, 16000, 32000, and 48000 Hz. ```c #include #include int configure_for_audio_source(Fvad *vad, int source_sample_rate) { if (fvad_set_sample_rate(vad, source_sample_rate) < 0) { fprintf(stderr, "Unsupported sample rate: %d Hz\n", source_sample_rate); fprintf(stderr, "Supported rates: 8000, 16000, 32000, 48000 Hz\n"); return -1; } printf("Sample rate set to %d Hz\n", source_sample_rate); return 0; } int main(void) { Fvad *vad = fvad_new(); // Configure for different audio sources configure_for_audio_source(vad, 8000); // Telephone quality configure_for_audio_source(vad, 16000); // Wideband audio configure_for_audio_source(vad, 32000); // Super-wideband configure_for_audio_source(vad, 48000); // Full-band (CD quality) // Invalid sample rate configure_for_audio_source(vad, 44100); // CD audio - not supported fvad_free(vad); return 0; } ``` -------------------------------- ### Log Per-Frame Detection Results Source: https://context7.com/dpirch/libfvad/llms.txt Write the voice/non-voice detection result for each processed frame to a specified text file. ```bash ./examples/fvadwav -l results.txt input.wav ``` -------------------------------- ### Complete WAV File Processing with libfvad Source: https://context7.com/dpirch/libfvad/llms.txt This C code processes a WAV file to detect voice activity. It requires a 16-bit mono WAV input. Ensure the file is opened in binary read mode. ```c #include #include #include #include #include // Simplified WAV header structure (for 16-bit mono PCM) typedef struct { uint32_t sample_rate; uint16_t bits_per_sample; uint16_t num_channels; uint32_t data_size; } WavInfo; // Parse basic WAV header (simplified, assumes standard format) int read_wav_header(FILE *f, WavInfo *info) { char chunk_id[4]; uint32_t chunk_size; uint16_t audio_format; // Read RIFF header fread(chunk_id, 1, 4, f); if (memcmp(chunk_id, "RIFF", 4) != 0) return -1; fread(&chunk_size, 4, 1, f); fread(chunk_id, 1, 4, f); if (memcmp(chunk_id, "WAVE", 4) != 0) return -1; // Read fmt chunk fread(chunk_id, 1, 4, f); fread(&chunk_size, 4, 1, f); fread(&audio_format, 2, 1, f); fread(&info->num_channels, 2, 1, f); fread(&info->sample_rate, 4, 1, f); fseek(f, 6, SEEK_CUR); // Skip byte rate and block align fread(&info->bits_per_sample, 2, 1, f); fseek(f, chunk_size - 16, SEEK_CUR); // Skip any extra format bytes // Find data chunk while (fread(chunk_id, 1, 4, f) == 4) { fread(&chunk_size, 4, 1, f); if (memcmp(chunk_id, "data", 4) == 0) { info->data_size = chunk_size; return 0; } fseek(f, chunk_size, SEEK_CUR); } return -1; } int main(int argc, char *argv[]) { if (argc < 2) { printf("Usage: %s [mode]\n", argv[0]); printf(" mode: 0=quality, 1=low-bitrate, 2=aggressive, 3=very-aggressive\n"); return 1; } const char *filename = argv[1]; int mode = (argc > 2) ? atoi(argv[2]) : 0; // Open WAV file FILE *f = fopen(filename, "rb"); if (!f) { fprintf(stderr, "Cannot open file: %s\n", filename); return 1; } // Read WAV header WavInfo wav_info; if (read_wav_header(f, &wav_info) < 0) { fprintf(stderr, "Invalid WAV file\n"); fclose(f); return 1; } printf("WAV Info: %u Hz, %u-bit, %u channel(s)\n", wav_info.sample_rate, wav_info.bits_per_sample, wav_info.num_channels); if (wav_info.num_channels != 1 || wav_info.bits_per_sample != 16) { fprintf(stderr, "Only 16-bit mono WAV files are supported\n"); fclose(f); return 1; } // Initialize VAD Fvad *vad = fvad_new(); if (!vad) { fprintf(stderr, "Failed to create VAD instance\n"); fclose(f); return 1; } if (fvad_set_sample_rate(vad, wav_info.sample_rate) < 0) { fprintf(stderr, "Unsupported sample rate: %u Hz\n", wav_info.sample_rate); fvad_free(vad); fclose(f); return 1; } if (fvad_set_mode(vad, mode) < 0) { fprintf(stderr, "Invalid mode: %d\n", mode); fvad_free(vad); fclose(f); return 1; } printf("VAD configured: mode=%d, sample_rate=%u\n", mode, wav_info.sample_rate); // Process in 20ms frames size_t frame_samples = wav_info.sample_rate / 1000 * 20; int16_t *frame = malloc(frame_samples * sizeof(int16_t)); long voice_frames = 0, total_frames = 0; int prev_result = -1; long voice_segments = 0; while (fread(frame, sizeof(int16_t), frame_samples, f) == frame_samples) { int result = fvad_process(vad, frame, frame_samples); if (result < 0) { fprintf(stderr, "VAD processing error\n"); break; } total_frames++; if (result == 1) { voice_frames++; if (prev_result != 1) { voice_segments++; } } prev_result = result; } // Print results printf("\n--- VAD Results ---\n"); printf("Total frames: %ld (%.2f seconds)\n", total_frames, total_frames * 0.02); printf("Voice frames: %ld (%.2f%%)\n", voice_frames, 100.0 * voice_frames / total_frames); printf("Voice segments: %ld\n", voice_segments); printf("Avg segment len: %.2f frames\n", voice_segments > 0 ? (double)voice_frames / voice_segments : 0); // Cleanup free(frame); fvad_free(vad); fclose(f); return 0; } /* Expected output for a speech file: * WAV Info: 16000 Hz, 16-bit, 1 channel(s) * VAD configured: mode=0, sample_rate=16000 * * --- VAD Results --- * Total frames: 1500 (30.00 seconds) * Voice frames: 892 (59.47%) * Voice segments: 23 * Avg segment len: 38.78 frames */ ``` -------------------------------- ### Configure CMake for fvadwav Source: https://github.com/dpirch/libfvad/blob/master/examples/CMakeLists.txt Defines the build process for the fvadwav executable, including linking sndfile and fvad libraries. ```cmake cmake_minimum_required(VERSION 3.5) add_executable(fvadwav fvadwav.c) target_link_libraries(fvadwav sndfile fvad) install(TARGETS fvadwav DESTINATION bin) ``` -------------------------------- ### Process Audio Frame Source: https://context7.com/dpirch/libfvad/llms.txt Processes a 16-bit PCM audio frame to determine voice activity. Frames must correspond to 10, 20, or 30 ms durations. ```c #include #include #include #include // Frame length calculation: // 10ms at 8kHz = 80 samples // 20ms at 8kHz = 160 samples // 30ms at 8kHz = 240 samples // 10ms at 16kHz = 160 samples // 20ms at 16kHz = 320 samples // 30ms at 16kHz = 480 samples // 10ms at 48kHz = 480 samples // 20ms at 48kHz = 960 samples // 30ms at 48kHz = 1440 samples int main(void) { Fvad *vad = fvad_new(); // Configure for 16kHz audio fvad_set_sample_rate(vad, 16000); fvad_set_mode(vad, 2); // Aggressive mode // Allocate buffer for 20ms frame at 16kHz = 320 samples const size_t frame_length = 320; int16_t frame[320]; // Simulate silence (all zeros) memset(frame, 0, sizeof(frame)); int result = fvad_process(vad, frame, frame_length); printf("Silence frame: %s\n", result == 1 ? "VOICE" : result == 0 ? "NO VOICE" : "ERROR"); // Simulate some audio data (normally read from file/microphone) // This is just synthetic data for demonstration for (size_t i = 0; i < frame_length; i++) { frame[i] = (int16_t)((i % 100) * 100 - 5000); // Simple wave pattern } result = fvad_process(vad, frame, frame_length); printf("Audio frame: %s\n", result == 1 ? "VOICE" : result == 0 ? "NO VOICE" : "ERROR"); // Invalid frame length returns -1 result = fvad_process(vad, frame, 100); // Invalid: not 10/20/30ms if (result == -1) { printf("Invalid frame length rejected as expected\n"); } fvad_free(vad); return 0; } ``` -------------------------------- ### Reset VAD State with fvad_reset Source: https://context7.com/dpirch/libfvad/llms.txt Clears internal state and reverts mode and sample rate to defaults. Useful for reusing instances across different audio streams. ```c #include #include int main(void) { Fvad *vad = fvad_new(); // Configure for first audio stream fvad_set_mode(vad, 2); fvad_set_sample_rate(vad, 16000); // ... process first audio stream ... // Reset for processing a new audio stream fvad_reset(vad); // VAD is now back to defaults: mode=0, sample_rate=8000 printf("VAD reset to defaults\n"); // Reconfigure for second stream fvad_set_mode(vad, 1); fvad_set_sample_rate(vad, 48000); // ... process second audio stream ... fvad_free(vad); return 0; } ``` -------------------------------- ### Set Aggressiveness Mode with fvad_set_mode Source: https://context7.com/dpirch/libfvad/llms.txt Configures the sensitivity of the VAD instance. Higher modes reduce false positives but may increase missed speech. ```c #include #include int main(void) { Fvad *vad = fvad_new(); // Mode 0: Quality mode (default) // - Highest sensitivity, may have more false positives // - Best for high-quality audio with low background noise if (fvad_set_mode(vad, 0) == 0) { printf("Mode 0 (quality) set successfully\n"); } // Mode 1: Low bitrate mode // - Balanced sensitivity // - Good for VoIP and streaming applications if (fvad_set_mode(vad, 1) == 0) { printf("Mode 1 (low bitrate) set successfully\n"); } // Mode 2: Aggressive mode // - More restrictive, fewer false positives // - Good for noisy environments if (fvad_set_mode(vad, 2) == 0) { printf("Mode 2 (aggressive) set successfully\n"); } // Mode 3: Very aggressive mode // - Most restrictive, may miss some speech // - Best for very noisy environments if (fvad_set_mode(vad, 3) == 0) { printf("Mode 3 (very aggressive) set successfully\n"); } // Invalid mode returns -1 if (fvad_set_mode(vad, 5) == -1) { printf("Invalid mode rejected as expected\n"); } fvad_free(vad); return 0; } ``` -------------------------------- ### Free VAD Instance with fvad_free Source: https://context7.com/dpirch/libfvad/llms.txt Releases memory allocated for a VAD instance. Ensure the pointer is not accessed after calling this function. ```c #include void cleanup_vad(Fvad *vad) { if (vad) { fvad_free(vad); // vad pointer is now invalid, do not use } } int main(void) { Fvad *vad = fvad_new(); // ... use vad for processing ... // Clean up fvad_free(vad); return 0; } ``` -------------------------------- ### fvad_set_sample_rate Source: https://context7.com/dpirch/libfvad/llms.txt Configures the input sample rate for the VAD instance. ```APIDOC ## fvad_set_sample_rate ### Description Sets the input sample rate in Hz. Valid values are 8000, 16000, 32000, and 48000. Higher sample rates are internally downsampled to 8kHz for processing. ### Parameters - **vad** (Fvad*) - Required - The VAD instance pointer. - **sample_rate** (int) - Required - The sample rate in Hz (8000, 16000, 32000, or 48000). ### Response - **Return Value** (int) - Returns 0 on success, -1 on invalid sample rate. ``` -------------------------------- ### fvad_process Source: https://context7.com/dpirch/libfvad/llms.txt Processes an audio frame to determine if it contains active voice. ```APIDOC ## fvad_process ### Description Processes an audio frame and returns a VAD decision. The frame must be 10, 20, or 30 ms of signed 16-bit PCM samples. ### Parameters - **vad** (Fvad*) - Required - The VAD instance pointer. - **frame** (int16_t*) - Required - Pointer to the audio frame buffer. - **length** (size_t) - Required - The number of samples in the frame. ### Response - **Return Value** (int) - Returns 1 for active voice, 0 for non-voice, or -1 for invalid frame length. ``` -------------------------------- ### Output Voice Frames Source: https://context7.com/dpirch/libfvad/llms.txt Extract and save only the detected voice frames from an input WAV file to a new WAV file. ```bash ./examples/fvadwav -o voice_only.wav input.wav ``` -------------------------------- ### fvad_reset Source: https://context7.com/dpirch/libfvad/llms.txt Reinitializes a VAD instance to default state. ```APIDOC ## fvad_reset ### Description Reinitializes a VAD instance, clearing all internal state and resetting mode and sample rate to their default values. ### Parameters - **vad** (Fvad*) - Required - The VAD instance to reset. ``` -------------------------------- ### Output Non-Voice Frames Source: https://context7.com/dpirch/libfvad/llms.txt Extract and save only the detected non-voice (silence) frames from an input WAV file to a new WAV file. ```bash ./examples/fvadwav -n silence.wav input.wav ``` -------------------------------- ### fvad_free Source: https://context7.com/dpirch/libfvad/llms.txt Frees the dynamic memory allocated for a VAD instance. ```APIDOC ## fvad_free ### Description Frees the dynamic memory allocated for a VAD instance. Must be called when the VAD instance is no longer needed to prevent memory leaks. ### Parameters - **vad** (Fvad*) - Required - The VAD instance to free. ``` -------------------------------- ### fvad_set_mode Source: https://context7.com/dpirch/libfvad/llms.txt Sets the VAD operating mode (aggressiveness level). ```APIDOC ## fvad_set_mode ### Description Sets the VAD operating mode (aggressiveness level). Higher modes are more aggressive, resulting in fewer false positives but potentially more missed speech. ### Parameters - **vad** (Fvad*) - Required - The VAD instance. - **mode** (int) - Required - Valid modes: 0 (quality), 1 (low bitrate), 2 (aggressive), 3 (very aggressive). ### Response - **int** - Returns 0 on success, -1 on invalid mode. ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.