captions-handler.cpp 1.1 KB

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