← Back to C-Kernel-Engine Docs Doxygen Source Documentation
 
Loading...
Searching...
No Matches
kokoro_shape_bounds.c
Go to the documentation of this file.
1#include "ckernel_tts.h"
2
3#include <stdint.h>
4#include <string.h>
5
6static int checked_add(size_t a, size_t b, size_t *out) {
7 if (b > SIZE_MAX - a) return 0;
8 *out = a + b;
9 return 1;
10}
11
12static int checked_mul(size_t a, size_t b, size_t *out) {
13 if (a && b > SIZE_MAX / a) return 0;
14 *out = a * b;
15 return 1;
16}
17
19 const int32_t *durations,
20 size_t phoneme_count,
21 const CKKokoroShapeLimitsV8 *limits,
22 CKKokoroShapeV8 *shape) {
23 if (!durations || !limits || !shape || !phoneme_count ||
24 !limits->max_phonemes || !limits->max_duration_per_phoneme ||
25 !limits->max_expanded_frames || !limits->max_generator_samples ||
26 !limits->max_output_samples || !limits->max_alignment_elements ||
27 !limits->alignment_channels || !limits->decoder_upsample_factor ||
29 !limits->istft_hop_size || !limits->istft_fft_size) {
31 }
32 memset(shape, 0, sizeof(*shape));
33 if (phoneme_count > limits->max_phonemes) return CK_AUDIO_EXTENT_LIMIT;
34
35 size_t frames = 0;
36 for (size_t i = 0; i < phoneme_count; ++i) {
37 if (durations[i] <= 0) return CK_AUDIO_EXTENT_INVALID;
38 if ((size_t)durations[i] > limits->max_duration_per_phoneme)
40 if (!checked_add(frames, (size_t)durations[i], &frames))
42 if (frames > limits->max_expanded_frames)
44 }
45
46 size_t align, decoder, generator, istft_frames, padded, output;
47 if (!checked_mul(frames, limits->alignment_channels, &align) ||
48 !checked_mul(frames, limits->decoder_upsample_factor, &decoder) ||
49 !checked_mul(decoder, limits->generator_upsample_rate_0, &generator) ||
50 !checked_mul(generator, limits->generator_upsample_rate_1, &generator) ||
51 !checked_add(generator / limits->istft_hop_size, 1, &istft_frames) ||
52 !checked_add(generator, limits->istft_fft_size, &padded) ||
53 !checked_add(generator, limits->istft_fft_size, &output)) {
55 }
56 if (align > SIZE_MAX / sizeof(float) ||
57 decoder > SIZE_MAX / sizeof(float) ||
58 generator > SIZE_MAX / sizeof(float) ||
59 padded > SIZE_MAX / sizeof(float) ||
60 output > SIZE_MAX / sizeof(float)) {
62 }
63 if (align > limits->max_alignment_elements ||
64 generator > limits->max_generator_samples ||
65 output > limits->max_output_samples) {
67 }
68
69 shape->phonemes = phoneme_count;
70 shape->expanded_frames = frames;
71 shape->alignment_elements = align;
72 shape->decoder_frames = decoder;
73 shape->generator_samples = generator;
74 shape->istft_frames = istft_frames;
75 shape->istft_padded_samples = padded;
76 shape->output_samples_upper_bound = output;
77 return CK_AUDIO_EXTENT_OK;
78}
@ CK_AUDIO_EXTENT_INVALID
Definition ckernel_tts.h:42
@ CK_AUDIO_EXTENT_LIMIT
Definition ckernel_tts.h:43
@ CK_AUDIO_EXTENT_OVERFLOW
Definition ckernel_tts.h:44
@ CK_AUDIO_EXTENT_OK
Definition ckernel_tts.h:41
int ck_kokoro_plan_shape_v8(const int32_t *durations, size_t phoneme_count, const CKKokoroShapeLimitsV8 *limits, CKKokoroShapeV8 *shape)
static int checked_add(size_t a, size_t b, size_t *out)
static int checked_mul(size_t a, size_t b, size_t *out)
size_t max_duration_per_phoneme
Definition ckernel_tts.h:16
size_t generator_upsample_rate_0
Definition ckernel_tts.h:23
size_t generator_upsample_rate_1
Definition ckernel_tts.h:24
size_t alignment_elements
Definition ckernel_tts.h:32
size_t output_samples_upper_bound
Definition ckernel_tts.h:37
size_t expanded_frames
Definition ckernel_tts.h:31
size_t istft_padded_samples
Definition ckernel_tts.h:36
size_t istft_frames
Definition ckernel_tts.h:35
size_t generator_samples
Definition ckernel_tts.h:34
size_t decoder_frames
Definition ckernel_tts.h:33