2011-09-28 21:44:07 +02:00
|
|
|
// Copyright (c) 2011, Thomas Goyne <plorkyeran@aegisub.org>
|
2007-06-23 02:21:20 +02:00
|
|
|
//
|
2011-09-28 21:44:07 +02:00
|
|
|
// Permission to use, copy, modify, and distribute this software for any
|
|
|
|
// purpose with or without fee is hereby granted, provided that the above
|
|
|
|
// copyright notice and this permission notice appear in all copies.
|
2007-06-23 02:21:20 +02:00
|
|
|
//
|
2011-09-28 21:44:07 +02:00
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
|
|
// ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
|
|
// ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
|
|
// OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
2007-06-23 02:21:20 +02:00
|
|
|
//
|
2009-07-29 07:43:02 +02:00
|
|
|
// Aegisub Project http://www.aegisub.org/
|
|
|
|
|
2007-06-23 02:21:20 +02:00
|
|
|
#include "ass_karaoke.h"
|
2011-09-28 21:44:07 +02:00
|
|
|
|
|
|
|
#include "ass_dialogue.h"
|
2007-06-23 02:21:20 +02:00
|
|
|
|
2014-05-29 00:19:05 +02:00
|
|
|
#include <libaegisub/format.h>
|
|
|
|
|
2012-12-30 00:53:56 +01:00
|
|
|
#include <boost/algorithm/string/predicate.hpp>
|
|
|
|
#include <boost/algorithm/string/trim.hpp>
|
Note: This was done using a script! it's far from perfect but 95% of the work has been done already formatting-wise.
Document all functions, class, struct, union, enum, macro, variable, typedefs. This isn't the actual document in itself but empty documentation using any old documentation if it was there.
This was done using exuberant ctags to get tag info, then a TCL script to parse/remove old comments and convert them into Doxygen-style.
Some notes:
* Anything labeled 'DOCME' needs to be documented, @param and @return have been left blank as it would be annoying to delete the 'DOCME' from every one of those.
* Some multiline comments may have been munged into single line comments
* Leave the /// comments above global variables with a space, if they're harder to read then we'll be less likey to use them.
* Enum comments can go after the enumeration itself '[value] /// comment'
* include/aegisub/*.h haven't been converted yet, this will be done in a later commit
* Some documentation blocks are in the wrong place, in the .h when it should be in the .cpp, or vice versa.
See http://devel.aegisub.org/wiki/Doxygen for some details on Doxygen and a 'style guide'.
Originally committed to SVN as r3312.
2009-07-30 00:59:22 +02:00
|
|
|
|
2012-12-30 00:53:56 +01:00
|
|
|
std::string AssKaraoke::Syllable::GetText(bool k_tag) const {
|
|
|
|
std::string ret;
|
2011-09-28 21:44:07 +02:00
|
|
|
|
|
|
|
if (k_tag)
|
2014-05-29 00:19:05 +02:00
|
|
|
ret = agi::format("{%s%d}", tag_type, ((duration + 5) / 10));
|
2011-09-28 21:44:07 +02:00
|
|
|
|
|
|
|
size_t idx = 0;
|
2012-11-04 04:53:03 +01:00
|
|
|
for (auto const& ovr : ovr_tags) {
|
2012-12-30 00:53:56 +01:00
|
|
|
ret += text.substr(idx, ovr.first - idx);
|
2012-11-04 04:53:03 +01:00
|
|
|
ret += ovr.second;
|
|
|
|
idx = ovr.first;
|
2011-09-28 21:44:07 +02:00
|
|
|
}
|
2012-12-30 00:53:56 +01:00
|
|
|
ret += text.substr(idx);
|
2011-09-28 21:44:07 +02:00
|
|
|
return ret;
|
2007-06-23 02:21:20 +02:00
|
|
|
}
|
|
|
|
|
2015-01-13 23:56:29 +01:00
|
|
|
AssKaraoke::AssKaraoke(const AssDialogue *line, bool auto_split, bool normalize) {
|
2012-02-16 06:21:00 +01:00
|
|
|
if (line) SetLine(line, auto_split, normalize);
|
2011-09-28 21:44:07 +02:00
|
|
|
}
|
|
|
|
|
2015-01-13 23:56:29 +01:00
|
|
|
void AssKaraoke::SetLine(const AssDialogue *line, bool auto_split, bool normalize) {
|
2011-09-28 21:44:07 +02:00
|
|
|
syls.clear();
|
|
|
|
Syllable syl;
|
2011-12-22 22:28:51 +01:00
|
|
|
syl.start_time = line->Start;
|
2011-09-28 21:44:07 +02:00
|
|
|
syl.duration = 0;
|
|
|
|
syl.tag_type = "\\k";
|
2007-06-23 02:21:20 +02:00
|
|
|
|
2012-12-01 01:40:00 +01:00
|
|
|
ParseSyllables(line, syl);
|
|
|
|
|
|
|
|
if (normalize) {
|
|
|
|
// Normalize the syllables so that the total duration is equal to the line length
|
2013-10-23 23:52:46 +02:00
|
|
|
int end_time = line->End;
|
2012-12-01 01:40:00 +01:00
|
|
|
int last_end = syl.start_time + syl.duration;
|
|
|
|
|
|
|
|
// Total duration is shorter than the line length so just extend the last
|
|
|
|
// syllable; this has no effect on rendering but is easier to work with
|
|
|
|
if (last_end < end_time)
|
|
|
|
syls.back().duration += end_time - last_end;
|
|
|
|
else if (last_end > end_time) {
|
|
|
|
// Truncate any syllables that extend past the end of the line
|
|
|
|
for (auto& syl : syls) {
|
|
|
|
if (syl.start_time > end_time) {
|
|
|
|
syl.start_time = end_time;
|
|
|
|
syl.duration = 0;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
syl.duration = std::min(syl.duration, end_time - syl.start_time);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add karaoke splits at each space
|
|
|
|
if (auto_split && syls.size() == 1) {
|
|
|
|
size_t pos;
|
|
|
|
no_announce = true;
|
2012-12-30 00:53:56 +01:00
|
|
|
while ((pos = syls.back().text.find(' ')) != std::string::npos)
|
2012-12-01 01:40:00 +01:00
|
|
|
AddSplit(syls.size() - 1, pos + 1);
|
|
|
|
no_announce = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
AnnounceSyllablesChanged();
|
|
|
|
}
|
|
|
|
|
2015-01-13 23:56:29 +01:00
|
|
|
void AssKaraoke::ParseSyllables(const AssDialogue *line, Syllable &syl) {
|
2014-04-14 19:58:46 +02:00
|
|
|
for (auto& block : line->ParseTags()) {
|
|
|
|
std::string text = block->GetText();
|
2012-12-01 01:40:00 +01:00
|
|
|
|
2014-12-26 17:51:14 +01:00
|
|
|
switch (block->GetType()) {
|
|
|
|
case AssBlockType::PLAIN:
|
2012-12-30 18:04:43 +01:00
|
|
|
syl.text += text;
|
2014-12-26 17:51:14 +01:00
|
|
|
break;
|
|
|
|
case AssBlockType::COMMENT:
|
|
|
|
// drawings aren't override tags but they shouldn't show up in the
|
|
|
|
// stripped text so pretend they are
|
|
|
|
case AssBlockType::DRAWING:
|
2012-12-30 18:04:43 +01:00
|
|
|
syl.ovr_tags[syl.text.size()] += text;
|
2014-12-26 17:51:14 +01:00
|
|
|
break;
|
|
|
|
case AssBlockType::OVERRIDE:
|
|
|
|
auto ovr = static_cast<AssDialogueBlockOverride*>(block.get());
|
2011-09-28 21:44:07 +02:00
|
|
|
bool in_tag = false;
|
2012-12-11 00:32:36 +01:00
|
|
|
for (auto& tag : ovr->Tags) {
|
2012-12-30 00:53:56 +01:00
|
|
|
if (tag.IsValid() && boost::istarts_with(tag.Name, "\\k")) {
|
2011-09-28 21:44:07 +02:00
|
|
|
if (in_tag) {
|
|
|
|
syl.ovr_tags[syl.text.size()] += "}";
|
|
|
|
in_tag = false;
|
|
|
|
}
|
2007-06-23 02:21:20 +02:00
|
|
|
|
2011-09-28 21:44:07 +02:00
|
|
|
// Dealing with both \K and \kf is mildly annoying so just
|
|
|
|
// convert them both to \kf
|
2012-12-11 00:32:36 +01:00
|
|
|
if (tag.Name == "\\K") tag.Name = "\\kf";
|
2007-06-23 02:21:20 +02:00
|
|
|
|
2011-09-28 21:44:07 +02:00
|
|
|
// Don't bother including zero duration zero length syls
|
|
|
|
if (syl.duration > 0 || !syl.text.empty()) {
|
|
|
|
syls.push_back(syl);
|
|
|
|
syl.text.clear();
|
|
|
|
syl.ovr_tags.clear();
|
2007-06-23 02:21:20 +02:00
|
|
|
}
|
|
|
|
|
2012-12-11 00:32:36 +01:00
|
|
|
syl.tag_type = tag.Name;
|
2011-09-28 21:44:07 +02:00
|
|
|
syl.start_time += syl.duration;
|
2012-12-11 00:32:36 +01:00
|
|
|
syl.duration = tag.Params[0].Get(0) * 10;
|
2007-06-23 02:21:20 +02:00
|
|
|
}
|
2011-09-28 21:44:07 +02:00
|
|
|
else {
|
2012-12-30 00:53:56 +01:00
|
|
|
std::string& otext = syl.ovr_tags[syl.text.size()];
|
2011-09-28 21:44:07 +02:00
|
|
|
// Merge adjacent override tags
|
2014-12-26 20:00:23 +01:00
|
|
|
boost::trim_right_if(text, [](char c) { return c == '}'; });
|
2012-12-30 00:53:56 +01:00
|
|
|
if (!in_tag)
|
2011-09-28 21:44:07 +02:00
|
|
|
otext += "{";
|
2007-06-23 02:21:20 +02:00
|
|
|
|
2011-09-28 21:44:07 +02:00
|
|
|
in_tag = true;
|
2012-12-11 00:32:36 +01:00
|
|
|
otext += tag;
|
2011-09-28 21:44:07 +02:00
|
|
|
}
|
2007-06-23 02:21:20 +02:00
|
|
|
}
|
2011-09-28 21:44:07 +02:00
|
|
|
|
|
|
|
if (in_tag)
|
|
|
|
syl.ovr_tags[syl.text.size()] += "}";
|
2014-12-26 17:51:14 +01:00
|
|
|
break;
|
2007-06-23 02:21:20 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
syls.push_back(syl);
|
2011-09-28 21:44:07 +02:00
|
|
|
}
|
|
|
|
|
2013-01-04 16:01:50 +01:00
|
|
|
std::string AssKaraoke::GetText() const {
|
|
|
|
std::string text;
|
2011-09-28 21:44:07 +02:00
|
|
|
text.reserve(size() * 10);
|
|
|
|
|
2012-11-04 04:53:03 +01:00
|
|
|
for (auto const& syl : syls)
|
2013-01-04 16:01:50 +01:00
|
|
|
text += syl.GetText(true);
|
2011-09-28 21:44:07 +02:00
|
|
|
|
|
|
|
return text;
|
|
|
|
}
|
|
|
|
|
2012-12-30 00:53:56 +01:00
|
|
|
std::string AssKaraoke::GetTagType() const {
|
2011-09-28 21:44:07 +02:00
|
|
|
return begin()->tag_type;
|
2007-06-23 02:21:20 +02:00
|
|
|
}
|
|
|
|
|
2012-12-30 00:53:56 +01:00
|
|
|
void AssKaraoke::SetTagType(std::string const& new_type) {
|
2012-11-04 04:53:03 +01:00
|
|
|
for (auto& syl : syls)
|
|
|
|
syl.tag_type = new_type;
|
2011-09-28 21:44:07 +02:00
|
|
|
}
|
2009-07-29 07:43:02 +02:00
|
|
|
|
2011-09-28 21:44:07 +02:00
|
|
|
void AssKaraoke::AddSplit(size_t syl_idx, size_t pos) {
|
|
|
|
syls.insert(syls.begin() + syl_idx + 1, Syllable());
|
|
|
|
Syllable &syl = syls[syl_idx];
|
|
|
|
Syllable &new_syl = syls[syl_idx + 1];
|
Note: This was done using a script! it's far from perfect but 95% of the work has been done already formatting-wise.
Document all functions, class, struct, union, enum, macro, variable, typedefs. This isn't the actual document in itself but empty documentation using any old documentation if it was there.
This was done using exuberant ctags to get tag info, then a TCL script to parse/remove old comments and convert them into Doxygen-style.
Some notes:
* Anything labeled 'DOCME' needs to be documented, @param and @return have been left blank as it would be annoying to delete the 'DOCME' from every one of those.
* Some multiline comments may have been munged into single line comments
* Leave the /// comments above global variables with a space, if they're harder to read then we'll be less likey to use them.
* Enum comments can go after the enumeration itself '[value] /// comment'
* include/aegisub/*.h haven't been converted yet, this will be done in a later commit
* Some documentation blocks are in the wrong place, in the .h when it should be in the .cpp, or vice versa.
See http://devel.aegisub.org/wiki/Doxygen for some details on Doxygen and a 'style guide'.
Originally committed to SVN as r3312.
2009-07-30 00:59:22 +02:00
|
|
|
|
2011-09-28 21:44:07 +02:00
|
|
|
// If the syl is empty or the user is adding a syllable past the last
|
|
|
|
// character then pos will be out of bounds. Doing this is a bit goofy,
|
|
|
|
// but it's sometimes required for complex karaoke scripts
|
|
|
|
if (pos < syl.text.size()) {
|
2012-12-30 00:53:56 +01:00
|
|
|
new_syl.text = syl.text.substr(pos);
|
|
|
|
syl.text = syl.text.substr(0, pos);
|
2011-09-28 21:44:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (new_syl.text.empty())
|
|
|
|
new_syl.duration = 0;
|
2012-10-15 06:37:14 +02:00
|
|
|
else if (syl.text.empty()) {
|
|
|
|
new_syl.duration = syl.duration;
|
|
|
|
syl.duration = 0;
|
|
|
|
}
|
2011-09-28 21:44:07 +02:00
|
|
|
else {
|
2012-12-01 01:25:20 +01:00
|
|
|
new_syl.duration = (syl.duration * new_syl.text.size() / (syl.text.size() + new_syl.text.size()) + 5) / 10 * 10;
|
2011-09-28 21:44:07 +02:00
|
|
|
syl.duration -= new_syl.duration;
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(syl.duration >= 0);
|
|
|
|
|
|
|
|
new_syl.start_time = syl.start_time + syl.duration;
|
|
|
|
new_syl.tag_type = syl.tag_type;
|
|
|
|
|
|
|
|
// Move all override tags after the split to the new syllable and fix the indices
|
|
|
|
size_t text_len = syl.text.size();
|
2012-12-23 00:35:13 +01:00
|
|
|
for (auto it = syl.ovr_tags.begin(); it != syl.ovr_tags.end(); ) {
|
2011-09-28 21:44:07 +02:00
|
|
|
if (it->first < text_len)
|
|
|
|
++it;
|
|
|
|
else {
|
|
|
|
new_syl.ovr_tags[it->first - text_len] = it->second;
|
|
|
|
syl.ovr_tags.erase(it++);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!no_announce) AnnounceSyllablesChanged();
|
|
|
|
}
|
|
|
|
|
|
|
|
void AssKaraoke::RemoveSplit(size_t syl_idx) {
|
|
|
|
// Don't allow removing the first syllable
|
|
|
|
if (syl_idx == 0) return;
|
|
|
|
|
|
|
|
Syllable &syl = syls[syl_idx];
|
|
|
|
Syllable &prev = syls[syl_idx - 1];
|
|
|
|
|
|
|
|
prev.duration += syl.duration;
|
2012-12-01 01:40:00 +01:00
|
|
|
for (auto const& tag : syl.ovr_tags)
|
|
|
|
prev.ovr_tags[tag.first + prev.text.size()] = tag.second;
|
2011-09-28 21:44:07 +02:00
|
|
|
prev.text += syl.text;
|
|
|
|
|
|
|
|
syls.erase(syls.begin() + syl_idx);
|
|
|
|
|
|
|
|
if (!no_announce) AnnounceSyllablesChanged();
|
|
|
|
}
|
|
|
|
|
|
|
|
void AssKaraoke::SetStartTime(size_t syl_idx, int time) {
|
|
|
|
// Don't allow moving the first syllable
|
|
|
|
if (syl_idx == 0) return;
|
|
|
|
|
|
|
|
Syllable &syl = syls[syl_idx];
|
|
|
|
Syllable &prev = syls[syl_idx - 1];
|
|
|
|
|
|
|
|
assert(time >= prev.start_time);
|
|
|
|
assert(time <= syl.start_time + syl.duration);
|
|
|
|
|
|
|
|
int delta = time - syl.start_time;
|
|
|
|
syl.start_time = time;
|
|
|
|
syl.duration -= delta;
|
|
|
|
prev.duration += delta;
|
|
|
|
}
|
|
|
|
|
2012-02-22 23:00:54 +01:00
|
|
|
void AssKaraoke::SetLineTimes(int start_time, int end_time) {
|
|
|
|
assert(end_time >= start_time);
|
|
|
|
|
|
|
|
size_t idx = 0;
|
2012-12-01 01:40:00 +01:00
|
|
|
// Chop off any portion of syllables starting before the new start_time
|
2012-02-22 23:00:54 +01:00
|
|
|
do {
|
|
|
|
int delta = start_time - syls[idx].start_time;
|
|
|
|
syls[idx].start_time = start_time;
|
|
|
|
syls[idx].duration = std::max(0, syls[idx].duration - delta);
|
|
|
|
} while (++idx < syls.size() && syls[idx].start_time < start_time);
|
|
|
|
|
2013-04-05 04:53:59 +02:00
|
|
|
// And truncate any syllables ending after the new end_time
|
2012-02-22 23:00:54 +01:00
|
|
|
idx = syls.size() - 1;
|
|
|
|
while (syls[idx].start_time > end_time) {
|
|
|
|
syls[idx].start_time = end_time;
|
|
|
|
syls[idx].duration = 0;
|
|
|
|
--idx;
|
|
|
|
}
|
|
|
|
syls[idx].duration = end_time - syls[idx].start_time;
|
|
|
|
}
|