captions-handler.hpp 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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()
  10. {
  11. audio_resampler_destroy(resampler);
  12. }
  13. inline bool reset(const resample_info &dst, const resample_info &src)
  14. {
  15. audio_resampler_destroy(resampler);
  16. resampler = audio_resampler_create(&dst, &src);
  17. return !!resampler;
  18. }
  19. inline operator audio_resampler_t*() {return resampler;}
  20. };
  21. /* ------------------------------------------------------------------------- */
  22. typedef std::function<void (const std::string &)> captions_cb;
  23. #define captions_error(s) std::string(obs_module_text("Captions.Error." ## s))
  24. #define CAPTIONS_ERROR_GENERIC_FAIL captions_error("GenericFail")
  25. /* ------------------------------------------------------------------------- */
  26. class captions_handler {
  27. captions_cb cb;
  28. resampler_obj resampler;
  29. protected:
  30. inline void callback(const std::string &text)
  31. {
  32. cb(text);
  33. }
  34. virtual void pcm_data(const void *data, size_t frames)=0;
  35. /* always resamples to 1 channel */
  36. bool reset_resampler(enum audio_format format, uint32_t sample_rate);
  37. public:
  38. /* throw std::string for errors shown to users */
  39. captions_handler(
  40. captions_cb callback,
  41. enum audio_format format,
  42. uint32_t sample_rate);
  43. virtual ~captions_handler() {}
  44. void push_audio(const audio_data *audio);
  45. };
  46. /* ------------------------------------------------------------------------- */
  47. struct captions_handler_info {
  48. std::string (*name)(void);
  49. captions_handler *(*create)(captions_cb cb, const std::string &lang);
  50. };