1
0

captions-handler.cpp 1.1 KB

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