captions-handler.cpp 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. #include "captions-handler.hpp"
  2. captions_handler::captions_handler(captions_cb callback, enum audio_format format, uint32_t sample_rate) : cb(callback)
  3. {
  4. if (!reset_resampler(format, sample_rate))
  5. throw CAPTIONS_ERROR_GENERIC_FAIL;
  6. }
  7. bool captions_handler::reset_resampler(enum audio_format format, uint32_t sample_rate)
  8. try {
  9. obs_audio_info ai;
  10. if (!obs_get_audio_info(&ai))
  11. throw std::string("Failed to get OBS audio info");
  12. resample_info src = {ai.samples_per_sec, AUDIO_FORMAT_FLOAT_PLANAR, ai.speakers};
  13. resample_info dst = {sample_rate, format, SPEAKERS_MONO};
  14. if (!resampler.reset(dst, src))
  15. throw std::string("Failed to create audio resampler");
  16. return true;
  17. } catch (std::string text) {
  18. blog(LOG_WARNING, "%s: %s", __FUNCTION__, text.c_str());
  19. return false;
  20. }
  21. void captions_handler::push_audio(const audio_data *audio)
  22. {
  23. uint8_t *out[MAX_AV_PLANES];
  24. uint32_t frames;
  25. uint64_t ts_offset;
  26. bool success;
  27. success = audio_resampler_resample(resampler, out, &frames, &ts_offset, (const uint8_t *const *)audio->data,
  28. audio->frames);
  29. if (success)
  30. pcm_data(out[0], frames);
  31. }