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