1 #include "captions-handler.hpp"
2 
captions_handler(captions_cb callback,enum audio_format format,uint32_t sample_rate)3 captions_handler::captions_handler(captions_cb callback,
4 				   enum audio_format format,
5 				   uint32_t sample_rate)
6 	: cb(callback)
7 {
8 	if (!reset_resampler(format, sample_rate))
9 		throw CAPTIONS_ERROR_GENERIC_FAIL;
10 }
11 
reset_resampler(enum audio_format format,uint32_t sample_rate)12 bool captions_handler::reset_resampler(enum audio_format format,
13 				       uint32_t sample_rate)
14 try {
15 	obs_audio_info ai;
16 	if (!obs_get_audio_info(&ai))
17 		throw std::string("Failed to get OBS audio info");
18 
19 	resample_info src = {ai.samples_per_sec, AUDIO_FORMAT_FLOAT_PLANAR,
20 			     ai.speakers};
21 	resample_info dst = {sample_rate, format, SPEAKERS_MONO};
22 
23 	if (!resampler.reset(dst, src))
24 		throw std::string("Failed to create audio resampler");
25 
26 	return true;
27 
28 } catch (std::string text) {
29 	blog(LOG_WARNING, "%s: %s", __FUNCTION__, text.c_str());
30 	return false;
31 }
32 
push_audio(const audio_data * audio)33 void captions_handler::push_audio(const audio_data *audio)
34 {
35 	uint8_t *out[MAX_AV_PLANES];
36 	uint32_t frames;
37 	uint64_t ts_offset;
38 	bool success;
39 
40 	success = audio_resampler_resample(resampler, out, &frames, &ts_offset,
41 					   (const uint8_t *const *)audio->data,
42 					   audio->frames);
43 	if (success)
44 		pcm_data(out[0], frames);
45 }
46