Skip to content

Commit

Permalink
feat(chatlog): Add chatlog mode.
Browse files Browse the repository at this point in the history
With chatlog mode, you can hold asr output history.

Signed-off-by: Yibai Zhang <xm1994@gmail.com>
  • Loading branch information
summershrimp committed Dec 6, 2024
1 parent 7fdde3d commit 26ef1b1
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 11 deletions.
2 changes: 1 addition & 1 deletion .clang-format
Original file line number Diff line number Diff line change
Expand Up @@ -105,4 +105,4 @@ TabWidth: 8
# - 'DARRAY'
UseTab: ForContinuationAndIndentation
---
Language: ObjC
Language: ObjC
2 changes: 2 additions & 0 deletions data/locale/en-US.ini
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,5 @@ AutoSub.MSSAPI.CurrentSystemLanguage="Current System Language (%1)"
AutoSub.Language="Language"
AutoSub.SP.FunASR="FunASR"
AutoSub.Endpoint="Endpoint URL"
AutoSub.EnableChatlog="Chatlog Mode"
AutoSub.ChatLogLine="Chatlog Lines"
2 changes: 2 additions & 0 deletions data/locale/zh-CN.ini
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,5 @@ AutoSub.MSSAPI.CurrentSystemLanguage="当前系统语言 (%1)"
AutoSub.Language="语言"
AutoSub.SP.FunASR="FunASR"
AutoSub.Endpoint="服务URL"
AutoSub.EnableChatlog="聊天模式"
AutoSub.ChatLogLine="聊天行限制"
89 changes: 79 additions & 10 deletions src/obs-autosub-filter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,25 @@ static bool translate_enable_modified(void *priv, obs_properties_t *props,
return true;
}

static bool chatlogmode_enable_modified(void *priv, obs_properties_t *props,
obs_property_t *property,
obs_data_t *settings)
{
(void)property;
int enabled = obs_data_get_bool(settings, PROP_CHATLOG_ENABLED);
(void)priv;
if (!enabled) {
PROPERTY_SET_UNVISIBLE(props, PROP_CHATLOG_LINES);
PROPERTY_SET_VISIBLE(props, PROP_MAX_COUNT);
PROPERTY_SET_VISIBLE(props, PROP_CLEAR_TIMEOUT);
} else {
PROPERTY_SET_VISIBLE(props, PROP_CHATLOG_LINES);
PROPERTY_SET_UNVISIBLE(props, PROP_MAX_COUNT);
PROPERTY_SET_UNVISIBLE(props, PROP_CLEAR_TIMEOUT);
}
return true;
}

#undef PROPERTY_SET_UNVISIBLE
#undef PROPERTY_SET_VISIBLE

Expand All @@ -134,6 +153,15 @@ obs_properties_t *autosub_filter_getproperties(void *data)
props, PROP_TARGET_TEXT_SOURCE, T_TARGET_TEXT_SOURCE,
OBS_COMBO_TYPE_LIST, OBS_COMBO_FORMAT_STRING);

obs_property_t *cl = obs_properties_add_bool(
props, PROP_CHATLOG_ENABLED, T_CHATLOG_ENABLE);

obs_property_set_modified_callback2(cl, chatlogmode_enable_modified,
data);

obs_properties_add_int(props, PROP_CHATLOG_LINES, T_CHATLOG_LINES, 2,
50, 1);

obs_properties_add_int(props, PROP_MAX_COUNT, T_MAX_CHAR_COUNT, 0,
100000, 1);
obs_properties_add_int(props, PROP_CLEAR_TIMEOUT, T_CLEAR_TIMEOUT, 0,
Expand Down Expand Up @@ -205,6 +233,14 @@ void autosub_filter_getdefaults(obs_data_t *settings)
struct resample_info resample_output = {16000, AUDIO_FORMAT_16BIT,
SPEAKERS_MONO};

void chatlog_filter(QStringList &lines, int cl_lines)
{
auto rl = lines.size();
if (rl > cl_lines) {
lines.remove(0, lines.size() - cl_lines);
}
}

void autosub_filter_update(void *data, obs_data_t *settings)
{
autosub_filter *s = (autosub_filter *)data;
Expand Down Expand Up @@ -252,8 +288,16 @@ void autosub_filter_update(void *data, obs_data_t *settings)
obs_weak_source_release(old_weak_text_source);
}

s->max_count = (int)obs_data_get_int(settings, PROP_MAX_COUNT);
s->clear_timeout = (int)obs_data_get_int(settings, PROP_CLEAR_TIMEOUT);
s->chatlog_mode = obs_data_get_bool(settings, PROP_CHATLOG_ENABLED);
s->chatlog_lines = (int)obs_data_get_int(settings, PROP_CHATLOG_LINES);
if (!s->chatlog_mode) {
s->max_count = (int)obs_data_get_int(settings, PROP_MAX_COUNT);
s->clear_timeout =
(int)obs_data_get_int(settings, PROP_CLEAR_TIMEOUT);
} else {
s->max_count = 0;
s->clear_timeout = 0;
}

int provider = (int)obs_data_get_int(settings, PROP_PROVIDER);
if (provider != s->provider) {
Expand Down Expand Up @@ -286,8 +330,9 @@ void autosub_filter_update(void *data, obs_data_t *settings)
return;
}
auto text_settings = obs_source_get_settings(target);
obs_data_set_string(text_settings, "text",
str.toUtf8().toStdString().c_str());
auto stdstr = str.toUtf8().toStdString();
const char *out_str = stdstr.c_str();
obs_data_set_string(text_settings, "text", out_str);
obs_source_update(target, text_settings);
obs_source_release(target);
});
Expand Down Expand Up @@ -399,8 +444,18 @@ void autosub_filter_update(void *data, obs_data_t *settings)
});

if (s->translator) {
s->translator->setResultCallback(
[=](QString data) { setTransText(data); });
s->translator->setResultCallback([=](QString data) {
QString out_str;
if (s->chatlog_mode) {
s->chatlog_trans.push_back(data);
chatlog_filter(s->chatlog_trans,
s->chatlog_lines);
out_str = s->chatlog_trans.join("\n");
} else {
out_str = data;
}
setTransText(out_str);
});

s->translator->setErrorCallback(
[=](QString data) { setTransText(data); });
Expand All @@ -425,11 +480,25 @@ void autosub_filter_update(void *data, obs_data_t *settings)
str.toStdString().c_str());
s->last_update_time = os_gettime_ns();
}
int t = s->max_count;
if (t != 0 && str.length() > t) {
str = str.right(t);
QString out_str;
if (s->chatlog_mode) {
QStringList t = s->chatlog_asr;
t.append(str);
chatlog_filter(t, s->chatlog_lines);
out_str = t.join("\n");
if (typ == 0) {
s->chatlog_asr.push_back(str);
chatlog_filter(s->chatlog_asr,
s->chatlog_lines);
}
} else {
out_str = str;
int t = s->max_count;
if (t != 0 && str.length() > t) {
str = str.right(t);
}
}
setText(str);
setText(out_str);
});
}
s->lock_asr.unlock();
Expand Down
10 changes: 10 additions & 0 deletions src/obs-autosub-filter.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,12 @@ along with this program; If not, see <https://www.gnu.org/licenses/>

#define PROP_TRANS_TARGET_TEXT_SOURCE "autosub_filter_trans_target_source"

#define PROP_CHATLOG_ENABLED "autosub_filter_enable_chatlog"
#define T_CHATLOG_ENABLE obs_module_text("AutoSub.EnableChatlog")

#define PROP_CHATLOG_LINES "autosub_filter_chatlog_lines"
#define T_CHATLOG_LINES obs_module_text("AutoSub.ChatLogLine")

using namespace std::placeholders;

struct autosub_filter {
Expand All @@ -52,6 +58,10 @@ struct autosub_filter {
uint32_t channels = 2;
int max_count = 0;
int clear_timeout = 0;
bool chatlog_mode = false;
int chatlog_lines = 0;
QStringList chatlog_asr;
QStringList chatlog_trans;
bool running = false;

int provider;
Expand Down

0 comments on commit 26ef1b1

Please sign in to comment.