I am getting an error while trying to implement this VAD (Voice Activity Detection) in C using CCS.
I think fread function is not working properly because I am getting num_frames = 1 each time I run it and consequently only frame 0 is considered by the VAD.
Here is my code:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <errno.h>
#include <string.h>
#define FRAME_SIZE 256 // Frame size in samples
#define SAMPLE_RATE 16000 // Sample rate in Hz
#define THRESHOLD 1e7 // Energy threshold
double compute_energy(const short *frame, int frame_size)
{
double energy = 0.0;
int i;
for (i = 0; i < frame_size; ++i)
{
double val = frame[i];
energy += val * val;
}
return energy;
}
void vad(const short *audio_data, int num_samples)
{
int num_frames = num_samples / FRAME_SIZE;
printf("NUM FRAMES: %d", num_frames);
int i;
for (i = 0; i < num_frames; ++i)
{
printf("i VALUE: %d", i);
// Extract frame
const short *frame = &audio_data[i * FRAME_SIZE];
// Compute energy
double energy = compute_energy(frame, FRAME_SIZE);
// Make decision
if (energy > THRESHOLD)
{
printf("Frame %d: Speech (Energy: %f)n", i, energy);
}
else
{
printf("Frame %d: Silence (Energy: %f)n", i, energy);
}
}
}
int main()
{
// File path to the .raw audio file
const char *file_path = "../example.raw"; // Ensure this is the correct file name
// Open the file
FILE *file = fopen(file_path, "rb");
if (!file)
{
fprintf(stderr, "Failed to open file '%s': %sn", file_path, strerror(errno));
return 1;
}
// Seek to the end of the file
fseek(file, 0, SEEK_END);
// Get the current file position
long filesize = ftell(file);
// Print the file size
printf("Size of file: %ld bytesn", filesize);
// Seek back to the beginning of the file
fseek(file, 0, SEEK_SET);
// Allocate memory for one frame of audio data
char *audio_data = (char *)malloc(FRAME_SIZE * sizeof(char));
if (!audio_data)
{
perror("Failed to allocate memory");
fclose(file);
return 1;
}
int num_samples = fread(audio_data, sizeof(char), FRAME_SIZE, file);
if (num_samples == 0 && ferror(file))
{
perror("Failed to read from file");
free(audio_data);
fclose(file);
return 1;
}
while (num_samples > 0)
{
printf("TEST ENTER VADn");
// Perform VAD
vad(audio_data, num_samples);
}
fclose(file);
// Free memory
free(audio_data);
return 0;
}
I expect to see many frames being evaluated by VAD
4