yolobs-studio/UI/frontend-plugins/frontend-tools/captions-handler.hpp

60 lines
1.6 KiB
C++
Raw Permalink Normal View History

2017-06-29 19:01:10 +00:00
#pragma once
#include <media-io/audio-resampler.h>
#include <obs-module.h>
#include <functional>
#include <string>
class resampler_obj {
audio_resampler_t *resampler = nullptr;
public:
2019-09-22 21:19:10 +00:00
inline ~resampler_obj() { audio_resampler_destroy(resampler); }
2017-06-29 19:01:10 +00:00
inline bool reset(const resample_info &dst, const resample_info &src)
{
audio_resampler_destroy(resampler);
resampler = audio_resampler_create(&dst, &src);
return !!resampler;
}
2019-09-22 21:19:10 +00:00
inline operator audio_resampler_t *() { return resampler; }
2017-06-29 19:01:10 +00:00
};
/* ------------------------------------------------------------------------- */
2019-09-22 21:19:10 +00:00
typedef std::function<void(const std::string &)> captions_cb;
2017-06-29 19:01:10 +00:00
2019-09-22 21:19:10 +00:00
#define captions_error(s) std::string(obs_module_text("Captions.Error."##s))
#define CAPTIONS_ERROR_GENERIC_FAIL captions_error("GenericFail")
2017-06-29 19:01:10 +00:00
/* ------------------------------------------------------------------------- */
class captions_handler {
captions_cb cb;
resampler_obj resampler;
protected:
2019-09-22 21:19:10 +00:00
inline void callback(const std::string &text) { cb(text); }
2017-06-29 19:01:10 +00:00
2019-09-22 21:19:10 +00:00
virtual void pcm_data(const void *data, size_t frames) = 0;
2017-06-29 19:01:10 +00:00
/* always resamples to 1 channel */
bool reset_resampler(enum audio_format format, uint32_t sample_rate);
public:
/* throw std::string for errors shown to users */
2019-09-22 21:19:10 +00:00
captions_handler(captions_cb callback, enum audio_format format,
uint32_t sample_rate);
2017-06-29 19:01:10 +00:00
virtual ~captions_handler() {}
void push_audio(const audio_data *audio);
};
/* ------------------------------------------------------------------------- */
struct captions_handler_info {
2019-09-22 21:19:10 +00:00
std::string (*name)(void);
2017-06-29 19:01:10 +00:00
captions_handler *(*create)(captions_cb cb, const std::string &lang);
};