| 1 |
/* -*- c-basic-offset: 4; indent-tabs-mode: nil -*- */ |
| 2 |
/** |
| 3 |
* Example of using external timestamp sources with ps_endpointer_t |
| 4 |
* to avoid timestamp drift between audio and system clocks. |
| 5 |
*/ |
| 6 |
|
| 7 |
#include <stdio.h> |
| 8 |
#include <time.h> |
| 9 |
#ifdef __APPLE__ |
| 10 |
#include <sys/time.h> |
| 11 |
#endif |
| 12 |
#include <pocketsphinx.h> |
| 13 |
|
| 14 |
/* Get system time in seconds (monotonic clock) */ |
| 15 |
static double |
| 16 |
get_system_time(void *user_data) |
| 17 |
{ |
| 18 |
struct timespec ts; |
| 19 |
(void)user_data; /* unused */ |
| 20 |
|
| 21 |
#ifdef __APPLE__ |
| 22 |
/* macOS doesn't have clock_gettime before 10.12 */ |
| 23 |
struct timeval tv; |
| 24 |
gettimeofday(&tv, NULL); |
| 25 |
return tv.tv_sec + tv.tv_usec / 1000000.0; |
| 26 |
#else |
| 27 |
clock_gettime(CLOCK_MONOTONIC, &ts); |
| 28 |
return ts.tv_sec + ts.tv_nsec / 1000000000.0; |
| 29 |
#endif |
| 30 |
} |
| 31 |
|
| 32 |
int |
| 33 |
main(int argc, char *argv[]) |
| 34 |
{ |
| 35 |
ps_endpointer_t *ep; |
| 36 |
int16 frame[480]; /* Frame buffer - size determined by endpointer */ |
| 37 |
int frame_size; |
| 38 |
double start_time; |
| 39 |
int i; |
| 40 |
|
| 41 |
(void)argc; |
| 42 |
(void)argv; |
| 43 |
|
| 44 |
/* Initialize endpointer with default settings */ |
| 45 |
ep = ps_endpointer_init(0, 0, PS_VAD_LOOSE, 16000, 0.0); |
| 46 |
if (ep == NULL) { |
| 47 |
fprintf(stderr, "Failed to initialize endpointer\n"); |
| 48 |
return 1; |
| 49 |
} |
| 50 |
|
| 51 |
frame_size = ps_endpointer_frame_size(ep); |
| 52 |
printf("Endpointer frame size: %d samples\n", frame_size); |
| 53 |
|
| 54 |
/* Set external timestamp callback to use system time */ |
| 55 |
if (ps_endpointer_set_timestamp_func(ep, get_system_time, NULL) < 0) { |
| 56 |
fprintf(stderr, "Failed to set timestamp callback\n"); |
| 57 |
ps_endpointer_free(ep); |
| 58 |
return 1; |
| 59 |
} |
| 60 |
|
| 61 |
start_time = get_system_time(NULL); |
| 62 |
printf("Starting at system time: %.3f\n", start_time); |
| 63 |
|
| 64 |
/* Simulate processing audio frames */ |
| 65 |
printf("\nProcessing frames with system timestamps:\n"); |
| 66 |
for (i = 0; i < 100; i++) { |
| 67 |
const int16 *speech; |
| 68 |
|
| 69 |
/* Generate dummy audio data (silence in this example) */ |
| 70 |
int j; |
| 71 |
for (j = 0; j < frame_size; j++) |
| 72 |
frame[j] = 0; |
| 73 |
|
| 74 |
/* Process frame */ |
| 75 |
speech = ps_endpointer_process(ep, frame); |
| 76 |
|
| 77 |
/* Every 10 frames, print timestamp */ |
| 78 |
if (i % 10 == 9) { |
| 79 |
double current_time = ps_endpointer_timestamp(ep); |
| 80 |
double elapsed = current_time - start_time; |
| 81 |
printf("Frame %3d: System time: %.3f, Elapsed: %.3f sec\n", |
| 82 |
i + 1, current_time, elapsed); |
| 83 |
} |
| 84 |
|
| 85 |
/* If speech is detected (won't happen with silence) */ |
| 86 |
if (speech != NULL) { |
| 87 |
printf("Speech detected at: %.3f\n", |
| 88 |
ps_endpointer_speech_start(ep)); |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
/* Demonstrate reverting to audio timestamps */ |
| 93 |
printf("\nReverting to audio-based timestamps:\n"); |
| 94 |
ps_endpointer_set_timestamp_func(ep, NULL, NULL); |
| 95 |
|
| 96 |
for (i = 0; i < 20; i++) { |
| 97 |
const int16 *speech; |
| 98 |
int j; |
| 99 |
|
| 100 |
for (j = 0; j < frame_size; j++) |
| 101 |
frame[j] = 0; |
| 102 |
|
| 103 |
speech = ps_endpointer_process(ep, frame); |
| 104 |
(void)speech; /* unused */ |
| 105 |
} |
| 106 |
|
| 107 |
/* Audio timestamp continues from where it was */ |
| 108 |
printf("Audio timestamp after 20 more frames: %.3f\n", |
| 109 |
ps_endpointer_timestamp(ep)); |
| 110 |
|
| 111 |
ps_endpointer_free(ep); |
| 112 |
printf("\nExample completed successfully.\n"); |
| 113 |
|
| 114 |
return 0; |
| 115 |
} |