File size: 8,167 Bytes
1e05caf d75605b 1e05caf |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 |
//
// This file is part of TEN Framework, an open source project.
// Licensed under the Apache License, Version 2.0.
// See the LICENSE file for more information.
//
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <time.h>
#include <inttypes.h>
#include <string.h> // memcmp
#ifdef _WIN32
#include <windows.h>
#endif
#include "ten_vad.h"
#if defined(__APPLE__)
#include <TargetConditionals.h>
#if TARGET_OS_IPHONE
#include "sample_array.h"
#endif
#endif
const int hop_size = 256; // 16 ms per frame
uint64_t get_timestamp_ms()
{
#ifdef _WIN32
LARGE_INTEGER frequency;
LARGE_INTEGER counter;
QueryPerformanceFrequency(&frequency);
QueryPerformanceCounter(&counter);
return (uint64_t)(counter.QuadPart * 1000 / frequency.QuadPart);
#else
struct timespec ts;
uint64_t millis;
clock_gettime(CLOCK_MONOTONIC, &ts);
millis = ts.tv_sec * 1000 + ts.tv_nsec / 1000000;
return millis;
#endif
}
// define RIFF header
#pragma pack(push, 1)
typedef struct
{
char chunk_id[4]; // should be "RIFF"
uint32_t chunk_size; // file total size - 8
char format[4]; // should be "WAVE"
} riff_header_t;
// define each sub chunk header
typedef struct
{
char id[4]; // should be "fmt " or "data"
uint32_t size; // chunk data size
} chunk_header_t;
#pragma pack(pop)
// define WAV file info we care about
typedef struct
{
uint16_t audio_format; // audio format (e.g. PCM=1)
uint16_t num_channels; // number of channels
uint32_t sample_rate; // sample rate
uint32_t byte_rate; // byte rate
uint16_t block_align; // block align
uint16_t bits_per_sample; // bits per sample
uint32_t data_size; // data size
long data_offset; // data offset in file
} wav_info_t;
int read_wav_file(FILE *fp, wav_info_t *info);
int vad_process(int16_t *input_buf, uint32_t frame_num,
float *out_probs, int32_t *out_flags,
float *use_time)
{
printf("tenvadsrc version: %s\n", ten_vad_get_version());
void *ten_vad_handle = NULL;
float voice_threshold = 0.5f;
ten_vad_create(&ten_vad_handle, hop_size, voice_threshold);
uint64_t start = get_timestamp_ms();
for (int i = 0; i < frame_num; ++i)
{
int16_t *audio_data = input_buf + i * hop_size;
ten_vad_process(ten_vad_handle, audio_data, hop_size,
&out_probs[i], &out_flags[i]);
printf("[%d] %0.6f, %d\n", i, out_probs[i], out_flags[i]);
}
uint64_t end = get_timestamp_ms();
*use_time = (float)(end - start);
ten_vad_destroy(&ten_vad_handle);
ten_vad_handle = NULL;
return 0;
}
int test_with_wav(int argc, char *argv[])
{
if (argc < 3)
{
printf("Warning: Test.exe input.wav output.txt\n");
return 0;
}
char *input_file = argv[1];
char *out_file = argv[2];
FILE *fp = fopen(input_file, "rb");
if (fp == NULL)
{
printf("Failed to open input file: %s\n", input_file);
return 1;
}
fseek(fp, 0, SEEK_SET);
wav_info_t info;
if (read_wav_file(fp, &info) != 0)
{
printf("Failed to read WAV file header\n");
fclose(fp);
return 1;
}
uint32_t byte_num = info.data_size;
printf("WAV file byte num: %d\n", byte_num);
char *input_buf = (char *)malloc(byte_num);
fseek(fp, info.data_offset, SEEK_SET);
fread(input_buf, 1, byte_num, fp);
fclose(fp);
fp = NULL;
uint32_t sample_num = byte_num / sizeof(int16_t);
float total_audio_time = (float)sample_num / 16.0;
printf("total_audio_time: %.2f(ms)\n", total_audio_time);
uint32_t frame_num = sample_num / hop_size;
printf("Audio frame Num: %d\n", frame_num);
float *out_probs = (float *)malloc(frame_num * sizeof(float));
int32_t *out_flags = (int32_t *)malloc(frame_num * sizeof(int32_t)); // Output flags are binary speech indicators (0 for non-speech signal, 1 for speech signal)
float use_time = .0;
vad_process((int16_t *)input_buf, frame_num,
out_probs, out_flags,
&use_time);
float rtf = use_time / total_audio_time;
printf("Consuming time: %f(ms), audio-time: %.2f(ms), =====> RTF: %0.6f\n",
use_time, total_audio_time, rtf);
FILE *fout = fopen(out_file, "w");
if (fout != NULL)
{
for (int i = 0; i < frame_num; i++)
{
fprintf(fout, "[%d] %0.6f, %d\n", i, out_probs[i], out_flags[i]);
}
fclose(fout);
fout = NULL;
}
free(input_buf);
free(out_probs);
free(out_flags);
return 0;
}
#if TARGET_OS_IPHONE
// Used for iOS APP demo
int test_with_array()
{
char *input_buf = (char *)sample_array;
uint32_t byte_num = sizeof(sample_array) / sizeof(sample_array[0]);
printf("WAV file byte num: %d\n", byte_num);
uint32_t sample_num = byte_num / sizeof(int16_t);
float total_audio_time = (float)sample_num / 16.0;
printf("total_audio_time: %.2f(ms)\n", total_audio_time);
uint32_t frame_num = sample_num / hop_size;
printf("Audio frame Num: %d\n", frame_num);
float *out_probs = (float *)malloc(frame_num * sizeof(float));
int32_t *out_flags = (int32_t *)malloc(frame_num * sizeof(int32_t));
float use_time = .0;
vad_process((int16_t *)input_buf, frame_num,
out_probs, out_flags,
&use_time);
float rtf = use_time / total_audio_time;
printf("Consuming time: %f(ms), audio-time: %.2f(ms), =====> RTF: %0.6f\n",
use_time, total_audio_time, rtf);
return 0;
}
#endif
int main(int argc, char *argv[])
{
#if TARGET_OS_IPHONE
return test_with_array();
#else
return test_with_wav(argc, argv);
#endif
}
// function to read WAV file info
int read_wav_file(FILE *fp, wav_info_t *info)
{
if (fp == NULL || info == NULL)
return -1;
// save current file position
long orig_pos = ftell(fp);
fseek(fp, 0, SEEK_SET);
// read RIFF header
riff_header_t riff;
if (fread(&riff, sizeof(riff_header_t), 1, fp) != 1)
{
fprintf(stderr, "Can not read RIFF head\n");
fseek(fp, orig_pos, SEEK_SET);
return -1;
}
// verify RIFF/WAVE format
if (memcmp(riff.chunk_id, "RIFF", 4) != 0 ||
memcmp(riff.format, "WAVE", 4) != 0)
{
fprintf(stderr, "not a valid RIFF/WAVE file\n");
fseek(fp, orig_pos, SEEK_SET);
return -1;
}
// initialize, mark chunks not found yet
int fmt_found = 0, data_found = 0;
memset(info, 0, sizeof(wav_info_t));
// iterate all chunks
while (!feof(fp))
{
chunk_header_t chunk;
if (fread(&chunk, sizeof(chunk_header_t), 1, fp) != 1)
{
break; // read failed, maybe end of file
}
// check if it's fmt chunk
if (memcmp(chunk.id, "fmt ", 4) == 0)
{
// read fmt data
fmt_found = 1;
if (chunk.size < 16)
{
fprintf(stderr, "fmt chunk size is abnormal\n");
fseek(fp, orig_pos, SEEK_SET);
return -1;
}
// read fmt parameters
if (fread(&info->audio_format, 2, 1, fp) != 1 ||
fread(&info->num_channels, 2, 1, fp) != 1 ||
fread(&info->sample_rate, 4, 1, fp) != 1 ||
fread(&info->byte_rate, 4, 1, fp) != 1 ||
fread(&info->block_align, 2, 1, fp) != 1 ||
fread(&info->bits_per_sample, 2, 1, fp) != 1)
{
fprintf(stderr, "failed to read fmt data\n");
fseek(fp, orig_pos, SEEK_SET);
return -1;
}
// skip fmt extension data
if (chunk.size > 16)
{
fseek(fp, chunk.size - 16, SEEK_CUR);
}
}
// check if it's data chunk
else if (memcmp(chunk.id, "data", 4) == 0)
{
data_found = 1;
info->data_size = chunk.size;
info->data_offset = ftell(fp); // record data start position
break; // found data chunk, can exit loop
}
// other chunks, skip
else
{
// consider byte alignment, pad odd size
fseek(fp, (chunk.size + (chunk.size % 2)), SEEK_CUR);
}
}
// check if necessary chunks are found
if (!fmt_found)
{
fprintf(stderr, "fmt chunk not found\n");
fseek(fp, orig_pos, SEEK_SET);
return -1;
}
if (!data_found)
{
fprintf(stderr, "data chunk not found\n");
fseek(fp, orig_pos, SEEK_SET);
return -1;
}
// restore original file position
fseek(fp, orig_pos, SEEK_SET);
return 0;
} |