captions-handler.hpp 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. #pragma once
  2. #include <media-io/audio-resampler.h>
  3. #include <obs-module.h>
  4. #include <functional>
  5. #include <string>
  6. class resampler_obj {
  7. audio_resampler_t *resampler = nullptr;
  8. public:
  9. inline ~resampler_obj() { audio_resampler_destroy(resampler); }
  10. inline bool reset(const resample_info &dst, const resample_info &src)
  11. {
  12. audio_resampler_destroy(resampler);
  13. resampler = audio_resampler_create(&dst, &src);
  14. return !!resampler;
  15. }
  16. inline operator audio_resampler_t *() { return resampler; }
  17. };
  18. /* ------------------------------------------------------------------------- */
  19. typedef std::function<void(const std::string &)> captions_cb;
  20. #define CAPTIONS_ERROR_GENERIC_FAIL std::string(obs_module_text("Captions.Error.GenericFail"))
  21. /* ------------------------------------------------------------------------- */
  22. class captions_handler {
  23. captions_cb cb;
  24. resampler_obj resampler;
  25. protected:
  26. inline void callback(const std::string &text) { cb(text); }
  27. virtual void pcm_data(const void *data, size_t frames) = 0;
  28. /* always resamples to 1 channel */
  29. bool reset_resampler(enum audio_format format, uint32_t sample_rate);
  30. public:
  31. /* throw std::string for errors shown to users */
  32. captions_handler(captions_cb callback, enum audio_format format, uint32_t sample_rate);
  33. virtual ~captions_handler() {}
  34. void push_audio(const audio_data *audio);
  35. };
  36. /* ------------------------------------------------------------------------- */
  37. struct captions_handler_info {
  38. std::string (*name)(void);
  39. captions_handler *(*create)(captions_cb cb, const std::string &lang);
  40. };