captions-handler.hpp 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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 \
  21. std::string(obs_module_text("Captions.Error.GenericFail"))
  22. /* ------------------------------------------------------------------------- */
  23. class captions_handler {
  24. captions_cb cb;
  25. resampler_obj resampler;
  26. protected:
  27. inline void callback(const std::string &text) { cb(text); }
  28. virtual void pcm_data(const void *data, size_t frames) = 0;
  29. /* always resamples to 1 channel */
  30. bool reset_resampler(enum audio_format format, uint32_t sample_rate);
  31. public:
  32. /* throw std::string for errors shown to users */
  33. captions_handler(captions_cb callback, enum audio_format format,
  34. uint32_t sample_rate);
  35. virtual ~captions_handler() {}
  36. void push_audio(const audio_data *audio);
  37. };
  38. /* ------------------------------------------------------------------------- */
  39. struct captions_handler_info {
  40. std::string (*name)(void);
  41. captions_handler *(*create)(captions_cb cb, const std::string &lang);
  42. };