| 1344 | } |
| 1345 | |
| 1346 | struct encodec_context * encodec_load_model(const std::string & model_path) { |
| 1347 | int64_t t_start_load_us = ggml_time_us(); |
| 1348 | |
| 1349 | struct encodec_context * ectx = new encodec_context(); |
| 1350 | |
| 1351 | ectx->model = encodec_model(); |
| 1352 | if (!encodec_load_model_weights(model_path, ectx->model)) { |
| 1353 | fprintf(stderr, "%s: failed to load model weights from '%s'\n", __func__, model_path.c_str()); |
| 1354 | return {}; |
| 1355 | } |
| 1356 | |
| 1357 | // pre-compute the number of codebooks required |
| 1358 | int bandwidth = ectx->model.hparams.bandwidth; |
| 1359 | int sr = ectx->model.hparams.sr; |
| 1360 | |
| 1361 | int hop_length = 1; |
| 1362 | for (int i = 0; i < 4; i++) { |
| 1363 | hop_length *= ectx->model.hparams.ratios[i]; |
| 1364 | } |
| 1365 | ectx->model.hparams.hop_length = hop_length; |
| 1366 | |
| 1367 | ectx->model.hparams.n_q = get_num_codebooks(bandwidth, hop_length, sr); |
| 1368 | fprintf(stderr, "%s: n_q = %d\n", __func__, ectx->model.hparams.n_q); |
| 1369 | |
| 1370 | ectx->t_load_us = ggml_time_us() - t_start_load_us; |
| 1371 | |
| 1372 | return ectx; |
| 1373 | } |
| 1374 | |
| 1375 | void encodec_free(struct encodec_context * ectx) { |
| 1376 | if (!ectx) { |
no test coverage detected