MCPcopy Create free account
hub / github.com/PABannier/bark.cpp / bark_generate_audio

Function bark_generate_audio

bark.cpp:2180–2227  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

2178}
2179
2180bool bark_generate_audio(struct bark_context* bctx, const char* text, int n_threads) {
2181 if (!bctx) {
2182 fprintf(stderr, "%s: invalid bark context\n", __func__);
2183 return false;
2184 }
2185
2186 bark_reset_statistics(bctx);
2187
2188 int64_t t_start_eval_us = ggml_time_us();
2189
2190 std::string text_str(text);
2191 bark_tokenize_input(bctx, text_str);
2192
2193 if (!bark_forward_eval(bctx, n_threads)) {
2194 fprintf(stderr, "%s: failed to forward eval\n", __func__);
2195 return false;
2196 }
2197
2198 auto& params = bctx->params;
2199
2200 int32_t target_bandwidth = params.target_bandwidth;
2201 int32_t sample_rate = params.sample_rate;
2202
2203 encodec_set_target_bandwidth(bctx->encodec_ctx, target_bandwidth);
2204 encodec_set_sample_rate(bctx->encodec_ctx, sample_rate);
2205
2206 // current shape fine_tokens: [seq_length][n_channels], n_channels are contiguous
2207 // encodec expects shape fine_tokens: [n_channels][seq_length], time steps are contiguous
2208 std::vector<bark_vocab::id> encodec_tokens;
2209
2210 for (int i = 0; i < (int)bctx->fine_tokens[0].size(); i++) {
2211 for (int j = 0; j < (int)bctx->fine_tokens.size(); j++) {
2212 encodec_tokens.push_back(bctx->fine_tokens[j][i]);
2213 }
2214 }
2215
2216 if (!encodec_decompress_audio(bctx->encodec_ctx, encodec_tokens.data(), encodec_tokens.size(), n_threads)) {
2217 printf("%s: Could not generate waveform from tokens with Encodec\n", __func__);
2218 return false;
2219 }
2220
2221 bctx->generated_audio = encodec_get_audio(bctx->encodec_ctx);
2222 bctx->n_generated_samples = encodec_get_audio_size(bctx->encodec_ctx);
2223
2224 bctx->stats.t_eval_us = ggml_time_us() - t_start_eval_us;
2225
2226 return true;
2227}
2228
2229static void bark_free_model(struct gpt_model* model) {
2230 if (!model) {

Callers

nothing calls this directly

Calls 5

dataMethod · 0.80
bark_reset_statisticsFunction · 0.70
bark_tokenize_inputFunction · 0.70
bark_forward_evalFunction · 0.70
sizeMethod · 0.45

Tested by

no test coverage detected