-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvadc.c
1287 lines (1067 loc) · 40.4 KB
/
vadc.c
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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "vadc.h"
#include <inttypes.h>
#define WIN32_LEAN_AND_MEAN
#include <windows.h> // GetModuleFileNameW
#include <Shlwapi.h> // PathRemoveFileSpecW, PathAppendW
#include "string8.c"
#include <tracy\TracyC.h>
#include "utils.h"
#if ONNX_INFERENCE_ENABLED
#include "onnx_helpers.c"
#else
#include "silero.h"
#endif // ONNX_INFERENCE_ENABLED
#define MEMORY_IMPLEMENTATION
#include "memory.h"
#ifndef DEBUG_WRITE_STATE_TO_FILE
#define DEBUG_WRITE_STATE_TO_FILE 0
#endif
// TODO(irwin):
// - move win32-specific stuff to separate file
#if DEBUG_WRITE_STATE_TO_FILE
typedef struct DEBUG_Silero_State DEBUG_Silero_State;
struct DEBUG_Silero_State
{
float samples[1536];
float state_h[128];
float state_c[128];
};
static FILE *getDebugFile()
{
static FILE *debug_file = NULL;
if ( debug_file == NULL )
{
debug_file = fopen( "debug_state.out", "wb" );
}
return debug_file;
}
#endif
void process_chunks( MemoryArena *arena, VADC_Context context, Silero_Config config,
const size_t buffered_samples_count,
const float *samples_buffer_float32,
float *probabilities_buffer)
{
VAR_UNUSED(arena);
{
int stride = (int)context.buffers.window_size_samples * config.batch_size;
for (size_t offset = 0;
offset < buffered_samples_count;
offset += stride)
{
// NOTE(irwin): copy a slice of the buffered samples
size_t samples_count_left = buffered_samples_count - offset;
size_t samples_count = samples_count_left > stride ? stride : samples_count_left;
// TODO(irwin): memset to 0 the entire tensor for simplicity, to avoid manual error-prone padding offset calculations
memset( context.buffers.input_samples, 0, stride * sizeof( context.buffers.input_samples[0] ) );
memmove( context.buffers.input_samples, samples_buffer_float32 + offset, samples_count * sizeof( context.buffers.input_samples[0] ) );
// NOTE(irwin): pad chunks with not enough samples
// for ( size_t pad_index = samples_count; pad_index < stride; ++pad_index )
// {
// context.buffers.input_samples[pad_index] = 0.0f;
// }
memmove( context.buffers.lstm_h, context.buffers.lstm_h_out, context.buffers.lstm_count * sizeof( context.buffers.lstm_h[0] ) );
memmove( context.buffers.lstm_c, context.buffers.lstm_c_out, context.buffers.lstm_count * sizeof( context.buffers.lstm_c[0] ) );
int output_stride = config.output_stride;
// NOTE(irwin): if there aren't enough samples for a full batch it can only be the very last batch
// otherwise we don't tell the backend that the batch size is lower, and so if one
// batch in the middle of the stream comes shorter, the lstm state will get screwed up
// from processing null samples in the middle of the stream.
backend_run(arena, &context, config);
// NOTE(irwin): will copy stale probabilities from previous batch if not enough samples for a full batch
for (int i = 0; i < config.batch_size; ++i)
{
float result_probability = context.buffers.output[i * output_stride + config.silero_probability_out_index];
*probabilities_buffer++ = result_probability;
}
}
}
}
void process_chunks_v5( MemoryArena *arena, VADC_Context context, Silero_Config config,
const size_t buffered_samples_count,
const float *samples_buffer_float32,
float *probabilities_buffer)
{
VAR_UNUSED(arena);
{
s32 context_size = config.context_size;
s32 window_size = config.input_count;
s32 total_sequence_count = context_size + window_size;
int input_buffer_size = ((int)context.buffers.window_size_samples + context_size) * config.batch_size;
int stride = (int)context.buffers.window_size_samples * config.batch_size;
for (size_t offset = 0;
offset < buffered_samples_count;
offset += stride)
{
// NOTE(irwin): copy context from previous batch, should be zeroes for first batch due to ZII
memmove(context.buffers.input_samples, context.buffers.input_samples + input_buffer_size - context_size, context_size * sizeof(float));
// NOTE(irwin): copy a slice of the buffered samples
// size_t samples_count_left = buffered_samples_count - offset;
// size_t samples_count = samples_count_left > stride ? stride : samples_count_left;
memmove(context.buffers.input_samples + context_size, samples_buffer_float32 + offset, window_size * sizeof(float));
for (int batch_index = 1; batch_index < config.batch_size; ++batch_index)
{
memmove(context.buffers.input_samples + (batch_index * total_sequence_count),
(samples_buffer_float32 + offset) + (batch_index * window_size) - context_size,
64 * sizeof(float));
memmove(context.buffers.input_samples + (batch_index * total_sequence_count) + context_size,
(samples_buffer_float32 + offset) + (batch_index * window_size),
512 * sizeof(float));
}
memmove( context.buffers.lstm_h, context.buffers.lstm_h_out, context.buffers.lstm_count * sizeof( context.buffers.lstm_h[0] ) );
memmove( context.buffers.lstm_c, context.buffers.lstm_c_out, context.buffers.lstm_count * sizeof( context.buffers.lstm_c[0] ) );
int output_stride = config.output_stride;
// NOTE(irwin): if there aren't enough samples for a full batch it can only be the very last batch
// otherwise we don't tell the backend that the batch size is lower, and so if one
// batch in the middle of the stream comes shorter, the lstm state will get screwed up
// from processing null samples in the middle of the stream.
backend_run(arena, &context, config);
// NOTE(irwin): will copy stale probabilities from previous batch if not enough samples for a full batch
for (int i = 0; i < config.batch_size; ++i)
{
float result_probability = context.buffers.output[i * output_stride + config.silero_probability_out_index];
*probabilities_buffer++ = result_probability;
}
}
}
}
FeedProbabilityResult feed_probability(FeedState *state,
int min_silence_duration_chunks,
int min_speech_duration_chunks,
float probability,
float threshold,
float neg_threshold,
int global_chunk_index
)
{
FeedProbabilityResult result = {0};
if (probability >= threshold && state->temp_end > 0)
{
state->temp_end = 0;
}
if (!state->triggered)
{
if (probability >= threshold)
{
state->triggered = 1;
state->current_speech_start = global_chunk_index;
}
}
else
{
if (probability < neg_threshold)
{
if (state->temp_end == 0)
{
state->temp_end = global_chunk_index;
}
if (global_chunk_index - state->temp_end < min_silence_duration_chunks)
{
}
else
{
if (state->temp_end - state->current_speech_start >= min_speech_duration_chunks)
{
result.speech_start = state->current_speech_start;
result.speech_end = state->temp_end;
result.is_valid = 1;
}
state->current_speech_start = 0;
state->temp_end = 0;
state->triggered = 0;
}
}
}
return result;
}
void emit_speech_segment(FeedProbabilityResult segment,
float speech_pad_ms,
Segment_Output_Format output_format,
VADC_Stats *stats,
float seconds_per_chunk)
{
const float spc = seconds_per_chunk;
const float speech_pad_s = speech_pad_ms / 1000.0f;
float speech_end_padded = (segment.speech_end * spc) + speech_pad_s;
// NOTE(irwin): print previous start/end times padded in seconds
float speech_start_padded = (segment.speech_start * spc) - speech_pad_s;
if (speech_start_padded < 0.0f)
{
speech_start_padded = 0.0f;
}
stats->total_speech += (double)speech_end_padded - (double)speech_start_padded;
switch (output_format)
{
case Segment_Output_Format_Seconds:
{
fprintf(stdout, "%.2f,%.2f\n", speech_start_padded, speech_end_padded);
} break;
case Segment_Output_Format_CentiSeconds:
{
s64 start_centi = (s64)((double)speech_start_padded * 100.0 + 0.5);
s64 end_centi = (s64)((double)speech_end_padded * 100.0 + 0.5);
fprintf(stdout, "%" PRId64 "," "%" PRId64 "\n", start_centi, end_centi);
} break;
}
fflush(stdout);
print_speech_stats(*stats);
}
FeedProbabilityResult combine_or_emit_speech_segment(FeedProbabilityResult buffered, FeedProbabilityResult feed_result,
float speech_pad_ms, Segment_Output_Format output_format, VADC_Stats *stats,
float seconds_per_chunk)
{
FeedProbabilityResult result = buffered;
const float spc = seconds_per_chunk;
const float speech_pad_s = speech_pad_ms / 1000.0f;
float current_speech_start_padded = (feed_result.speech_start * spc) - speech_pad_s;
if (current_speech_start_padded < 0.0f)
{
current_speech_start_padded = 0.0f;
}
if (result.is_valid)
{
float buffered_speech_end_padded = (result.speech_end * spc) + speech_pad_s;
if (buffered_speech_end_padded >= current_speech_start_padded)
{
result.speech_end = feed_result.speech_end;
}
else
{
emit_speech_segment(result, speech_pad_ms, output_format, stats, spc);
result = feed_result;
}
}
else
{
result = feed_result;
}
return result;
}
#if 0
void read_wav_ffmpeg( const char *fname_inp )
{
const wchar_t ffmpeg_to_s16le[] = L"ffmpeg -hide_banner -loglevel error -stats -i \"%s\" -map 0:a:0 -vn -sn -dn -ac 1 -ar 16k -f s16le -";
wchar_t *fname_widechar = nullptr;
if ( UTF8_ToWidechar( &fname_widechar, fname_inp, 0 ) )
{
wchar_t ffmpeg_final[4096];
swprintf( ffmpeg_final, 4096, ffmpeg_to_s16le, fname_widechar );
free( fname_widechar );
// Create the pipe
SECURITY_ATTRIBUTES saAttr = {sizeof( SECURITY_ATTRIBUTES )};
saAttr.bInheritHandle = FALSE;
HANDLE ffmpeg_stdout_read, ffmpeg_stdout_write;
if ( !CreatePipe( &ffmpeg_stdout_read, &ffmpeg_stdout_write, &saAttr, 0 ) )
{
fprintf( stderr, "Error creating ffmpeg pipe\n" );
return false;
}
// NOTE(irwin): ffmpeg does inherit the write handle to its output
SetHandleInformation( ffmpeg_stdout_write, HANDLE_FLAG_INHERIT, 1 );
// Launch ffmpeg and redirect its output to the pipe
STARTUPINFOW startup_info_ffmpeg = {sizeof( STARTUPINFO )};
// NOTE(irwin): hStdInput is 0, we don't want ffmpeg to inherit our stdin
startup_info_ffmpeg.hStdOutput = ffmpeg_stdout_write;
startup_info_ffmpeg.hStdError = GetStdHandle( STD_ERROR_HANDLE );
startup_info_ffmpeg.dwFlags |= STARTF_USESTDHANDLES;
PROCESS_INFORMATION ffmpeg_process_info = {};
if ( !CreateProcessW( NULL, ffmpeg_final, NULL, NULL, TRUE, 0, NULL, NULL, &startup_info_ffmpeg, &ffmpeg_process_info ) )
{
fprintf( stderr, "Error launching ffmpeg\n" );
return false;
}
// Close the write end of the pipe, as we're not writing to it
CloseHandle( ffmpeg_stdout_write );
// NOTE(irwin): restore non-inheritable status
SetHandleInformation( ffmpeg_stdout_write, HANDLE_FLAG_INHERIT, 0 );
// we can close the handles early if we're not going to use them
CloseHandle( ffmpeg_process_info.hProcess );
CloseHandle( ffmpeg_process_info.hThread );
if ( ffmpeg_stdout_read != INVALID_HANDLE_VALUE )
{
const int BUFSIZE = 4096 * 2 * 2;
// Read ffmpeg's output
unsigned char buffer[BUFSIZE];
int leftover = 0;
DWORD dwRead = 0;
unsigned char *buffer_dst = buffer + leftover;
auto byte_count_to_read = sizeof( buffer ) - leftover;
while ( ReadFile( ffmpeg_stdout_read, buffer_dst, byte_count_to_read, &dwRead, NULL ) )
{
if ( dwRead == 0 )
{
// fflush(stdout);
// NOTE(irwin): we ignore any leftover bytes in buffer in this case
break;
}
DWORD bytes_in_buffer = dwRead + leftover;
DWORD remainder = bytes_in_buffer % sizeof( int16_t );
int16_t *from = (int16_t *)buffer;
int16_t *to = (int16_t *)(buffer + (bytes_in_buffer - remainder));
//-----------------------------------------------------------------------------
// got bytes, do something with them here
//-----------------------------------------------------------------------------
if ( remainder != 0 )
{
memmove( buffer, to, remainder );
}
leftover = remainder;
//printf( "%.*s", (int)dwRead, buffer );
// printf("\n%d\n", (int)dwRead);
// fflush(stdout);
// WriteFile(GetStdHandle(STD_OUTPUT_HANDLE), buffer, dwRead, NULL, NULL);
// FlushFileBuffers(GetStdHandle(STD_OUTPUT_HANDLE));
// FlushFileBuffers(GetStdHandle(STD_ERROR_HANDLE));
}
}
}
}
#endif
typedef enum BS_Error BS_Error;
enum BS_Error
{
BS_Error_NoError = 0,
BS_Error_Error,
BS_Error_EndOfFile,
BS_Error_Memory,
BS_Error_CantOpenFile,
BS_Error_COUNT
};
typedef struct Buffered_Stream Buffered_Stream;
typedef BS_Error ( *Refill_Function ) (Buffered_Stream *);
struct Buffered_Stream
{
u8 *start;
u8 *cursor;
u8 *end;
Refill_Function refill;
BS_Error error_code;
// NOTE(irwin): win32
HANDLE read_handle_internal;
// NOTE(irwin): crt
FILE *file_handle_internal;
u8 *buffer_internal;
size_t buffer_internal_size;
};
BS_Error refill_zeros(Buffered_Stream *s)
{
static u8 zeros[256] = {0};
s->start = zeros;
s->cursor = zeros;
s->end = zeros + sizeof( zeros );
return s->error_code;
}
static BS_Error fail_buffered_stream(Buffered_Stream *s, BS_Error error_code)
{
s->error_code = error_code;
s->refill = refill_zeros;
s->refill( s );
return s->error_code;
}
BS_Error refill_FILE( Buffered_Stream *s )
{
if (s->cursor == s->end)
{
size_t values_read = fread( s->buffer_internal, 1, s->buffer_internal_size, s->file_handle_internal );
if (values_read == s->buffer_internal_size)
{
s->start = s->buffer_internal;
s->cursor = s->buffer_internal;
s->end = s->start + values_read;
}
else if ( values_read > 0 )
{
s->start = s->buffer_internal;
s->cursor = s->buffer_internal;
s->end = s->start + values_read;
}
else
{
if (feof(s->file_handle_internal))
{
return fail_buffered_stream( s, BS_Error_EndOfFile );
}
else if (ferror(s->file_handle_internal))
{
return fail_buffered_stream( s, BS_Error_Error );
}
}
}
return s->error_code;
}
BS_Error refill_HANDLE( Buffered_Stream *s )
{
if ( s->cursor == s->end )
{
DWORD byte_count_read = 0;
DWORD byte_count_read_total = 0;
BOOL read_file_result = 0;
do
{
// NOTE(irwin): keep calling ReadFile until we've filled our internal buffer or until ReadFile returns 0
u8 *destination = s->buffer_internal + byte_count_read_total;
DWORD max_byte_count_to_read = (DWORD)s->buffer_internal_size - byte_count_read_total;
read_file_result = ReadFile( s->read_handle_internal, destination, max_byte_count_to_read, &byte_count_read, NULL );
byte_count_read_total += byte_count_read;
} while (read_file_result && byte_count_read > 0 && byte_count_read_total < (DWORD)s->buffer_internal_size);
if ( byte_count_read_total > 0 )
{
s->start = s->buffer_internal;
s->cursor = s->buffer_internal;
s->end = s->start + byte_count_read_total;
}
else
{
if ( !read_file_result )
{
return fail_buffered_stream( s, BS_Error_EndOfFile );
}
else // read_file_result && bytes_read == 0
{
return fail_buffered_stream( s, BS_Error_Error );
}
}
}
return s->error_code;
}
static void init_buffered_stream_ffmpeg(MemoryArena *arena, Buffered_Stream *s, String8 fname_inp, size_t buffer_size,
int audio_source,
float start_seconds)
{
memset( s, 0, sizeof( *s ) );
const char *ffmpeg_to_s16le = "ffmpeg -hide_banner -loglevel error -nostats -ss %f -i \"%.*s\" -map 0:a:%d -vn -sn -dn -ac 1 -ar 16k -f s16le -";
String8 ffmpeg_command = String8_pushf(arena, ffmpeg_to_s16le, start_seconds, fname_inp.size, fname_inp.begin, audio_source);
wchar_t *ffmpeg_command_wide = NULL;
String8_ToWidechar(arena, &ffmpeg_command_wide, ffmpeg_command);
{
// Create the pipe
SECURITY_ATTRIBUTES saAttr = {sizeof( SECURITY_ATTRIBUTES )};
saAttr.bInheritHandle = FALSE;
HANDLE ffmpeg_stdout_read, ffmpeg_stdout_write;
if ( !CreatePipe( &ffmpeg_stdout_read, &ffmpeg_stdout_write, &saAttr, 0 ) )
{
fprintf( stderr, "Error creating ffmpeg pipe\n" );
return;
}
// NOTE(irwin): ffmpeg does inherit the write handle to its output
SetHandleInformation( ffmpeg_stdout_write, HANDLE_FLAG_INHERIT, 1 );
// Launch ffmpeg and redirect its output to the pipe
STARTUPINFOW startup_info_ffmpeg = {sizeof( STARTUPINFO )};
// NOTE(irwin): hStdInput is 0, we don't want ffmpeg to inherit our stdin
startup_info_ffmpeg.hStdOutput = ffmpeg_stdout_write;
startup_info_ffmpeg.hStdError = GetStdHandle( STD_ERROR_HANDLE );
startup_info_ffmpeg.dwFlags |= STARTF_USESTDHANDLES;
PROCESS_INFORMATION ffmpeg_process_info = {0};
if ( !CreateProcessW( NULL, ffmpeg_command_wide, NULL, NULL, TRUE, 0, NULL, NULL, &startup_info_ffmpeg, &ffmpeg_process_info ) )
{
fprintf( stderr, "Error launching ffmpeg\n" );
return;
}
// Close the write end of the pipe, as we're not writing to it
CloseHandle( ffmpeg_stdout_write );
// NOTE(irwin): restore non-inheritable status
SetHandleInformation( ffmpeg_stdout_write, HANDLE_FLAG_INHERIT, 0 );
// we can close the handles early if we're not going to use them
CloseHandle( ffmpeg_process_info.hProcess );
CloseHandle( ffmpeg_process_info.hThread );
if ( ffmpeg_stdout_read != INVALID_HANDLE_VALUE )
{
// s->buffer_internal = malloc( buffer_size );
s->buffer_internal = pushSizeZeroed( arena, buffer_size, TEMP_DEFAULT_ALIGNMENT );
if ( s->buffer_internal )
{
// memset( s->buffer_internal, 0, buffer_size );
s->read_handle_internal = ffmpeg_stdout_read;
s->refill = refill_HANDLE;
s->buffer_internal_size = buffer_size;
s->error_code = BS_Error_NoError;
s->refill( s );
}
else
{
fail_buffered_stream( s, BS_Error_Memory );
}
}
else
{
// TODO(irwin):
fail_buffered_stream( s, BS_Error_Error );
}
}
}
static void init_buffered_stream_stdin(MemoryArena *arena, Buffered_Stream *s, size_t buffer_size)
{
memset( s, 0, sizeof( *s ) );
s->buffer_internal = pushSizeZeroed( arena, buffer_size, TEMP_DEFAULT_ALIGNMENT );
if ( s->buffer_internal )
{
s->read_handle_internal = GetStdHandle(STD_INPUT_HANDLE);
s->refill = refill_HANDLE;
s->buffer_internal_size = buffer_size;
s->error_code = BS_Error_NoError;
s->refill( s );
}
else
{
fail_buffered_stream( s, BS_Error_Memory );
}
}
static void init_buffered_stream_file(MemoryArena *arena, Buffered_Stream *s, FILE *f, size_t buffer_size)
{
memset( s, 0, sizeof( *s ) );
if (f)
{
// s->buffer_internal = malloc( buffer_size );
s->buffer_internal = pushSizeZeroed( arena, buffer_size, TEMP_DEFAULT_ALIGNMENT );
if ( s->buffer_internal )
{
// memset( s->buffer_internal, 0, buffer_size );
s->file_handle_internal = f;
s->refill = refill_FILE;
s->buffer_internal_size = buffer_size;
s->error_code = BS_Error_NoError;
s->refill( s );
}
else
{
fail_buffered_stream( s, BS_Error_Memory );
}
}
else
{
fail_buffered_stream( s, BS_Error_CantOpenFile );
}
}
static void deinit_buffered_stream_file( Buffered_Stream *s )
{
if ( s->file_handle_internal )
{
s->file_handle_internal = NULL;
}
if ( s->buffer_internal )
{
// free( s->buffer_internal );
s->buffer_internal = NULL;
s->buffer_internal_size = 0;
}
}
int run_inference(String8 model_path_arg,
MemoryArena *arena,
float min_silence_duration_ms,
float min_speech_duration_ms,
float threshold,
float neg_threshold,
float speech_pad_ms,
float desired_sequence_count,
b32 raw_probabilities,
Segment_Output_Format output_format,
String8 filename,
b32 stats_output_enabled,
s32 preferred_batch_size,
int audio_source,
float start_seconds )
{
Silero_Config config = {0};
config.batch_size_restriction = 1;
config.batch_size = 1;
void *backend = backend_init( arena, model_path_arg, &config );
if ( !backend )
{
return -1;
}
b32 is_silero_v5 = config.is_silero_v5;
if (is_silero_v5)
{
fprintf(stderr, "%s", "Model arch is Silero v5\n");
config.context_size = SILERO_V5_CONTEXT_SIZE;
}
if (config.output_dims == 3)
{
config.silero_probability_out_index = 1;
config.output_stride = 2;
}
else
{
config.silero_probability_out_index = 0;
config.output_stride = 1;
}
config.batch_size = (config.batch_size_restriction == -1) ? preferred_batch_size : config.batch_size_restriction;
fprintf(stderr, "Running with batch size %d\n", config.batch_size);
{
Assert(config.output_dims == 2 || config.output_dims == 3);
if (config.output_dims == 2)
{
config.prob_shape_count = 2;
config.prob_shape[0] = config.batch_size;
config.prob_shape[1] = 1;
}
else
{
config.prob_shape_count = 3;
config.prob_shape[0] = config.batch_size;
config.prob_shape[1] = 2;
config.prob_shape[2] = 1;
}
size_t prob_tensor_element_count = 1;
for (int i = 0; i < config.prob_shape_count; ++i)
{
prob_tensor_element_count *= config.prob_shape[i];
}
config.prob_tensor_element_count = prob_tensor_element_count;
}
{
int sequence_count = (int)desired_sequence_count;
if (sequence_count < config.input_size_min)
{
sequence_count = config.input_size_min;
}
if (sequence_count > config.input_size_max)
{
sequence_count = config.input_size_max;
}
config.input_count = (s32)sequence_count;
fprintf(stderr, "Running with sequence count %d\n", config.input_count);
}
const float HARDCODED_CHUNK_DURATION_MS = config.input_count / (float)HARDCODED_SAMPLE_RATE * 1000.0f;
int min_speech_duration_chunks = (int)(min_speech_duration_ms / HARDCODED_CHUNK_DURATION_MS + 0.5f);
if (min_speech_duration_chunks < 1)
{
min_speech_duration_chunks = 1;
}
int min_silence_duration_chunks = (int)(min_silence_duration_ms / HARDCODED_CHUNK_DURATION_MS + 0.5f);
if (min_silence_duration_chunks < 1)
{
min_silence_duration_chunks = 1;
}
// NOTE(irwin): create tensors and allocate tensors backing memory buffers
Tensor_Buffers buffers = {0};
buffers.window_size_samples = (int)config.input_count;
if (is_silero_v5)
{
buffers.input_samples = pushArray(arena, (buffers.window_size_samples + config.context_size) * config.batch_size, float);
}
else
{
buffers.input_samples = pushArray(arena, buffers.window_size_samples * config.batch_size, float);
}
buffers.output = pushArray(arena, config.prob_tensor_element_count, float);
buffers.lstm_count = 128;
buffers.lstm_h = pushArray(arena, buffers.lstm_count, float);
buffers.lstm_c = pushArray(arena, buffers.lstm_count, float);
buffers.lstm_h_out = pushArray(arena, buffers.lstm_count, float);
buffers.lstm_c_out = pushArray(arena, buffers.lstm_count, float);
backend_create_tensors(config, backend, buffers);
// NOTE(irwin): read samples from a file or stdin and run inference
// NOTE(irwin): at 16000 sampling rate, one chunk is 96 ms or 1536 samples
// NOTE(irwin): chunks count being 96, the same as one chunk's length in milliseconds,
// is purely coincidental
const int chunks_count = 96;
// NOTE(irwin): buffered_samples_count is the normalization window size
const size_t buffered_samples_count = buffers.window_size_samples * chunks_count;
short *samples_buffer_s16 = pushArray(arena, buffered_samples_count, short);
float *samples_buffer_float32 = pushArray(arena, buffered_samples_count, float);
float *probabilities_buffer = pushArray(arena, chunks_count, float);
Buffered_Stream read_stream = {0};
size_t buffered_samples_size_in_bytes = sizeof( short ) * buffered_samples_count;
if (filename.size)
{
init_buffered_stream_ffmpeg(arena, &read_stream, filename, buffered_samples_size_in_bytes,
audio_source,
start_seconds );
}
else
{
init_buffered_stream_stdin(arena, &read_stream, buffered_samples_size_in_bytes );
}
VADC_Context context =
{
.backend = backend,
.buffers = buffers,
};
FeedState state = {0};
int global_chunk_index = 0;
FeedProbabilityResult buffered = {0};
VADC_Stats stats = {0};
stats.output_enabled = stats_output_enabled;
{
LARGE_INTEGER frequency = {0};
LARGE_INTEGER first_timestamp = {0};
QueryPerformanceFrequency(&frequency);
QueryPerformanceCounter(&first_timestamp);
stats.first_call_timestamp = first_timestamp.QuadPart;
stats.timer_frequency = frequency.QuadPart;
}
const float HARDCODED_SECONDS_PER_CHUNK = (float)config.input_count / HARDCODED_SAMPLE_RATE;
s64 total_samples_read = 0;
// NOTE(irwin): values_read is only accessed inside the for loop
size_t values_read = 0;
for(;;)
{
BS_Error read_error_code = 0;
// TODO(irwin): what do we do about errors that arose in refilling the buffered stream
// but some data was still read? Like EOF, or closed pipe?
read_error_code = read_stream.refill( &read_stream );
values_read = (read_stream.end - read_stream.start) / sizeof(short);
total_samples_read += values_read;
stats.total_samples = total_samples_read;
stats.total_duration = (double)total_samples_read / HARDCODED_SAMPLE_RATE;
// values_read = fread(samples_buffer_s16, sizeof(short), buffered_samples_count, read_source);
// fprintf(stderr, "%zu\n", values_read);
//if (values_read > 0)
if ( read_error_code == BS_Error_NoError )
{
memmove( samples_buffer_s16, read_stream.start, read_stream.end - read_stream.start );
float max_value = 0.0f;
for (size_t i = 0; i < values_read; ++i)
{
float value = samples_buffer_s16[i];
float abs_value = value > 0.0f ? value : value * -1.0f;
if (abs_value > max_value)
{
max_value = abs_value;
}
samples_buffer_float32[i] = value;
}
read_stream.cursor = read_stream.end;
#if 0
if (max_value > 0.0f)
{
for (size_t i = 0; i < values_read; ++i)
{
samples_buffer_float32[i] /= max_value;
}
}
#else
{
for (size_t i = 0; i < values_read; ++i)
{
samples_buffer_float32[i] /= 32768.0f;
}
}
#endif
size_t leftover = buffered_samples_count - values_read;
if (leftover > 0)
{
for (size_t i = values_read; i < buffered_samples_count; ++i)
{
samples_buffer_float32[i] = 0.0f;
}
}
}
else
{
switch (read_stream.error_code)
{
case BS_Error_CantOpenFile:
{
fprintf( stderr, "Error: BS_Error_CantOpenFile\n" );
} break;
case BS_Error_EndOfFile:
{
fprintf( stderr, "Error: BS_Error_EndOfFile\n" );
} break;
case BS_Error_Error:
{
fprintf( stderr, "Error: BS_Error_Error\n" );
} break;
case BS_Error_Memory:
{
fprintf( stderr, "Error: BS_Error_Memory\n" );
} break;
case BS_Error_NoError:
{
fprintf( stderr, "Error: BS_Error_NoError\n" );
} break;
default:
{
fprintf( stderr, "Error: Unreachable switch case\n" );
} break;
}
break;
}
if (is_silero_v5)
{
process_chunks_v5( arena, context, config,
values_read,
samples_buffer_float32,
probabilities_buffer);
}
else
{
process_chunks( arena, context, config,
values_read,
samples_buffer_float32,
probabilities_buffer);
}
int probabilities_count = (int)(values_read / (float)config.input_count);
if (!raw_probabilities)
{
for (int i = 0; i < probabilities_count; ++i)
{
float probability = probabilities_buffer[i];
FeedProbabilityResult feed_result = feed_probability(&state,
min_silence_duration_chunks,
min_speech_duration_chunks,
probability,
threshold,
neg_threshold,
global_chunk_index
);
if (feed_result.is_valid)
{
buffered = combine_or_emit_speech_segment(buffered, feed_result,
speech_pad_ms, output_format, &stats, HARDCODED_SECONDS_PER_CHUNK);
}
// printf("%f\n", probability);
++global_chunk_index;
}
}
else
{
for (int i = 0; i < probabilities_count; ++i)
{
float probability = probabilities_buffer[i];
printf("%f\n", probability);
++global_chunk_index;
}
}
}