2011-09-28 21:44:07 +02:00
|
|
|
// Copyright (c) 2011, Thomas Goyne <plorkyeran@aegisub.org>
|
2006-01-16 22:02:54 +01: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.
|
2006-01-16 22:02:54 +01: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.
|
2006-01-16 22:02:54 +01:00
|
|
|
//
|
2009-07-29 07:43:02 +02:00
|
|
|
// Aegisub Project http://www.aegisub.org/
|
2006-01-16 22:02:54 +01:00
|
|
|
//
|
2009-07-29 07:43:02 +02:00
|
|
|
// $Id$
|
|
|
|
|
|
|
|
/// @file audio_karaoke.cpp
|
|
|
|
/// @brief Karaoke table UI in audio box (not in audio display)
|
|
|
|
/// @ingroup audio_ui
|
|
|
|
///
|
2006-01-16 22:02:54 +01:00
|
|
|
|
2009-01-04 07:31:48 +01:00
|
|
|
#include "config.h"
|
|
|
|
|
2011-09-28 21:44:07 +02:00
|
|
|
#include "audio_karaoke.h"
|
|
|
|
|
2009-09-10 15:06:40 +02:00
|
|
|
#ifndef AGI_PRE
|
|
|
|
#include <algorithm>
|
2011-09-28 21:44:07 +02:00
|
|
|
#include <numeric>
|
2009-09-10 15:06:40 +02:00
|
|
|
|
2011-09-28 21:44:07 +02:00
|
|
|
#include <wx/bmpbuttn.h>
|
|
|
|
#include <wx/button.h>
|
2007-09-12 01:22:26 +02:00
|
|
|
#include <wx/dcclient.h>
|
2011-09-28 21:44:07 +02:00
|
|
|
#include <wx/dcmemory.h>
|
2009-09-10 15:06:40 +02:00
|
|
|
#include <wx/menu.h>
|
2011-09-28 21:44:07 +02:00
|
|
|
#include <wx/panel.h>
|
2007-09-12 01:22:26 +02:00
|
|
|
#include <wx/settings.h>
|
2011-09-28 21:44:07 +02:00
|
|
|
#include <wx/sizer.h>
|
2009-09-10 15:06:40 +02:00
|
|
|
#include <wx/tokenzr.h>
|
|
|
|
#endif
|
2006-01-16 22:02:54 +01:00
|
|
|
|
2011-09-28 21:44:07 +02:00
|
|
|
#include "include/aegisub/context.h"
|
2010-06-01 10:21:30 +02:00
|
|
|
|
2011-09-28 21:44:07 +02:00
|
|
|
#include "ass_dialogue.h"
|
|
|
|
#include "ass_file.h"
|
|
|
|
#include "ass_karaoke.h"
|
2009-09-10 15:06:40 +02:00
|
|
|
#include "ass_override.h"
|
2011-09-29 07:33:10 +02:00
|
|
|
#include "audio_box.h"
|
|
|
|
#include "audio_timing.h"
|
2011-09-28 21:44:07 +02:00
|
|
|
#include "libresrc/libresrc.h"
|
|
|
|
#include "main.h"
|
2011-07-15 06:04:13 +02:00
|
|
|
#include "selection_controller.h"
|
2006-01-16 22:02:54 +01:00
|
|
|
|
2011-09-28 21:44:07 +02:00
|
|
|
template<class Container, class Value>
|
|
|
|
static inline size_t last_lt_or_eq(Container const& c, Value const& v) {
|
|
|
|
typename Container::const_iterator it = lower_bound(c.begin(), c.end(), v);
|
|
|
|
// lower_bound gives first >=
|
|
|
|
if (it == c.end() || *it > v)
|
|
|
|
--it;
|
|
|
|
return distance(c.begin(), it);
|
|
|
|
};
|
|
|
|
|
|
|
|
AudioKaraoke::AudioKaraoke(wxWindow *parent, agi::Context *c)
|
|
|
|
: wxWindow(parent, -1, wxDefaultPosition, wxDefaultSize, wxTAB_TRAVERSAL | wxBORDER_SUNKEN)
|
|
|
|
, c(c)
|
|
|
|
, file_changed(c->ass->AddCommitListener(&AudioKaraoke::OnFileChanged, this))
|
2011-11-18 23:58:22 +01:00
|
|
|
, audio_opened(c->audioController->AddAudioOpenListener(&AudioKaraoke::OnAudioOpened, this))
|
|
|
|
, audio_closed(c->audioController->AddAudioCloseListener(&AudioKaraoke::OnAudioClosed, this))
|
2011-09-28 21:44:07 +02:00
|
|
|
, active_line(0)
|
|
|
|
, kara(new AssKaraoke)
|
|
|
|
, enabled(false)
|
2007-06-23 02:21:20 +02:00
|
|
|
{
|
2011-09-28 21:44:07 +02:00
|
|
|
using std::tr1::bind;
|
2007-06-23 02:21:20 +02:00
|
|
|
|
2011-09-28 21:44:07 +02:00
|
|
|
wxSizer *main_sizer = new wxBoxSizer(wxHORIZONTAL);
|
2006-01-16 22:02:54 +01:00
|
|
|
|
2011-09-28 21:44:07 +02:00
|
|
|
cancel_button = new wxBitmapButton(this, -1, GETIMAGE(kara_split_cancel_16));
|
|
|
|
cancel_button->SetToolTip(_("Discard all uncommitted splits"));
|
|
|
|
cancel_button->Bind(wxEVT_COMMAND_BUTTON_CLICKED, bind(&AudioKaraoke::CancelSplit, this));
|
|
|
|
main_sizer->Add(cancel_button);
|
2006-02-23 22:56:46 +01:00
|
|
|
|
2011-09-28 21:44:07 +02:00
|
|
|
accept_button = new wxBitmapButton(this, -1, GETIMAGE(kara_split_accept_16));
|
|
|
|
accept_button->SetToolTip(_("Commit splits"));
|
|
|
|
accept_button->Bind(wxEVT_COMMAND_BUTTON_CLICKED, bind(&AudioKaraoke::AcceptSplit, this));
|
|
|
|
main_sizer->Add(accept_button);
|
2006-01-16 22:02:54 +01:00
|
|
|
|
2011-09-28 21:44:07 +02:00
|
|
|
split_area = new wxPanel(this);
|
|
|
|
main_sizer->Add(split_area, wxSizerFlags(1).Expand());
|
2006-01-27 01:48:59 +01:00
|
|
|
|
2011-09-28 21:44:07 +02:00
|
|
|
SetSizerAndFit(main_sizer);
|
2006-01-16 22:02:54 +01:00
|
|
|
|
2011-09-28 21:44:07 +02:00
|
|
|
/// @todo subscribe
|
|
|
|
split_font.SetFaceName(OPT_GET("Audio/Karaoke/Font Face")->GetString());
|
|
|
|
split_font.SetPointSize(OPT_GET("Audio/Karaoke/Font Size")->GetInt());
|
|
|
|
|
|
|
|
Bind(wxEVT_SIZE, bind(&AudioKaraoke::Refresh, this, false, (const wxRect*)0));
|
|
|
|
split_area->Bind(wxEVT_PAINT, &AudioKaraoke::OnPaint, this);
|
|
|
|
split_area->Bind(wxEVT_LEFT_DOWN, &AudioKaraoke::OnMouse, this);
|
|
|
|
split_area->Bind(wxEVT_LEFT_UP, &AudioKaraoke::OnMouse, this);
|
|
|
|
split_area->Bind(wxEVT_MOTION, &AudioKaraoke::OnMouse, this);
|
|
|
|
split_area->Bind(wxEVT_LEAVE_WINDOW, &AudioKaraoke::OnMouse, this);
|
|
|
|
split_area->Bind(wxEVT_CONTEXT_MENU, &AudioKaraoke::OnContextMenu, this);
|
2006-01-16 22:02:54 +01:00
|
|
|
|
2011-09-28 21:44:07 +02:00
|
|
|
c->selectionController->AddSelectionListener(this);
|
|
|
|
|
2011-09-29 07:33:10 +02:00
|
|
|
accept_button->Enable(false);
|
|
|
|
cancel_button->Enable(false);
|
|
|
|
enabled = false;
|
2006-01-16 22:02:54 +01:00
|
|
|
}
|
|
|
|
|
2011-09-28 21:44:07 +02:00
|
|
|
AudioKaraoke::~AudioKaraoke() {
|
|
|
|
c->selectionController->RemoveSelectionListener(this);
|
2006-01-16 22:02:54 +01:00
|
|
|
}
|
|
|
|
|
2011-09-28 21:44:07 +02:00
|
|
|
void AudioKaraoke::OnActiveLineChanged(AssDialogue *new_line) {
|
|
|
|
active_line = new_line;
|
|
|
|
if (enabled) {
|
|
|
|
LoadFromLine();
|
|
|
|
split_area->Refresh(false);
|
2008-01-19 01:48:47 +01:00
|
|
|
}
|
2011-09-28 21:44:07 +02:00
|
|
|
}
|
2008-01-19 01:48:47 +01:00
|
|
|
|
2011-09-28 21:44:07 +02:00
|
|
|
void AudioKaraoke::OnFileChanged(int type) {
|
|
|
|
if (enabled && (type & AssFile::COMMIT_DIAG_FULL)) {
|
|
|
|
LoadFromLine();
|
|
|
|
split_area->Refresh(false);
|
2006-01-16 22:02:54 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-11-18 23:58:22 +01:00
|
|
|
void AudioKaraoke::OnAudioOpened() {
|
|
|
|
SetEnabled(enabled);
|
|
|
|
}
|
|
|
|
|
|
|
|
void AudioKaraoke::OnAudioClosed() {
|
|
|
|
c->audioController->SetTimingController(0);
|
|
|
|
}
|
|
|
|
|
2011-09-28 21:44:07 +02:00
|
|
|
void AudioKaraoke::SetEnabled(bool en) {
|
2011-09-29 20:17:37 +02:00
|
|
|
enabled = en;
|
|
|
|
|
|
|
|
c->audioBox->ShowKaraokeBar(enabled);
|
|
|
|
split_area->SetSize(GetSize().GetWidth(), -1);
|
|
|
|
|
|
|
|
if (enabled) {
|
2011-09-28 21:44:07 +02:00
|
|
|
LoadFromLine();
|
|
|
|
c->audioController->SetTimingController(CreateKaraokeTimingController(c, kara.get(), file_changed));
|
2011-09-29 20:17:37 +02:00
|
|
|
Refresh(false);
|
2006-01-16 22:02:54 +01:00
|
|
|
}
|
2011-09-28 21:44:07 +02:00
|
|
|
else {
|
2011-09-28 21:44:44 +02:00
|
|
|
c->audioController->SetTimingController(CreateDialogueTimingController(c));
|
2006-01-16 22:02:54 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-09-28 21:44:07 +02:00
|
|
|
void AudioKaraoke::OnPaint(wxPaintEvent &evt) {
|
|
|
|
int w, h;
|
|
|
|
split_area->GetClientSize(&w, &h);
|
2006-01-16 22:02:54 +01:00
|
|
|
|
2011-09-28 21:44:07 +02:00
|
|
|
wxPaintDC dc(split_area);
|
2011-09-29 20:17:37 +02:00
|
|
|
wxMemoryDC bmp_dc(rendered_line);
|
|
|
|
|
|
|
|
// Draw the text and split lines
|
|
|
|
dc.Blit(0, 0, w, h, &bmp_dc, 0, 0);
|
|
|
|
|
|
|
|
// Draw the split line under the mouse
|
|
|
|
dc.SetPen(*wxRED);
|
|
|
|
dc.DrawLine(mouse_pos, 0, mouse_pos, h);
|
|
|
|
}
|
|
|
|
|
|
|
|
void AudioKaraoke::RenderText() {
|
|
|
|
int w, h;
|
|
|
|
split_area->GetClientSize(&w, &h);
|
|
|
|
|
2011-11-09 03:29:36 +01:00
|
|
|
if (!rendered_line.IsOk() || split_area->GetClientSize() != rendered_line.GetSize()) {
|
2011-09-29 20:17:37 +02:00
|
|
|
rendered_line = wxBitmap(w, h);
|
|
|
|
}
|
|
|
|
|
|
|
|
wxMemoryDC dc(rendered_line);
|
2006-01-16 22:02:54 +01:00
|
|
|
|
|
|
|
// Draw background
|
2011-09-29 20:17:37 +02:00
|
|
|
dc.SetBrush(wxBrush(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW)));
|
2006-01-16 22:02:54 +01:00
|
|
|
dc.SetPen(*wxTRANSPARENT_PEN);
|
2011-09-28 21:44:07 +02:00
|
|
|
dc.DrawRectangle(0, 0, w, h);
|
2006-01-16 22:02:54 +01:00
|
|
|
|
2011-09-28 21:44:07 +02:00
|
|
|
dc.SetFont(split_font);
|
|
|
|
dc.SetTextForeground(wxColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT)));
|
2006-01-16 22:02:54 +01:00
|
|
|
|
2011-09-28 21:44:07 +02:00
|
|
|
// Draw each character in the line
|
|
|
|
int y = (h - char_height) / 2;
|
|
|
|
for (size_t i = 0; i < spaced_text.size(); ++i) {
|
|
|
|
dc.DrawText(spaced_text[i], char_x[i], y);
|
2006-01-16 22:02:54 +01:00
|
|
|
}
|
|
|
|
|
2011-09-28 21:44:07 +02:00
|
|
|
// Draw the lines between each syllable
|
|
|
|
dc.SetPen(wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHT));
|
|
|
|
for (size_t i = 0; i < syl_lines.size(); ++i) {
|
|
|
|
dc.DrawLine(syl_lines[i], 0, syl_lines[i], h);
|
2006-01-16 22:02:54 +01:00
|
|
|
}
|
2011-09-28 21:44:07 +02:00
|
|
|
}
|
2006-01-16 22:02:54 +01:00
|
|
|
|
2011-09-28 21:44:07 +02:00
|
|
|
void AudioKaraoke::AddMenuItem(wxMenu &menu, wxString const& tag, wxString const& help, wxString const& selected) {
|
|
|
|
wxMenuItem *item = menu.AppendCheckItem(-1, tag, help);
|
|
|
|
menu.Bind(wxEVT_COMMAND_MENU_SELECTED, std::tr1::bind(&AudioKaraoke::SetTagType, this, tag), item->GetId());
|
|
|
|
item->Check(tag == selected);
|
2006-01-16 22:02:54 +01:00
|
|
|
}
|
|
|
|
|
2011-09-28 21:44:07 +02:00
|
|
|
void AudioKaraoke::OnContextMenu(wxContextMenuEvent&) {
|
|
|
|
if (!enabled) return;
|
2006-01-16 22:02:54 +01:00
|
|
|
|
2011-09-28 21:44:07 +02:00
|
|
|
wxMenu context_menu(_("Karaoke tag"));
|
|
|
|
wxString type = kara->GetTagType();
|
2006-01-16 22:02:54 +01:00
|
|
|
|
2011-09-28 21:44:07 +02:00
|
|
|
AddMenuItem(context_menu, "\\k", _("Change karaoke tag to \\k"), type);
|
|
|
|
AddMenuItem(context_menu, "\\kf", _("Change karaoke tag to \\kf"), type);
|
|
|
|
AddMenuItem(context_menu, "\\ko", _("Change karaoke tag to \\ko"), type);
|
2007-06-30 02:00:44 +02:00
|
|
|
|
2011-09-28 21:44:07 +02:00
|
|
|
PopupMenu(&context_menu);
|
2006-01-16 22:02:54 +01:00
|
|
|
}
|
|
|
|
|
2011-09-28 21:44:07 +02:00
|
|
|
void AudioKaraoke::OnMouse(wxMouseEvent &event) {
|
|
|
|
if (!enabled) return;
|
2006-01-16 22:02:54 +01:00
|
|
|
|
2011-09-28 21:44:07 +02:00
|
|
|
mouse_pos = event.GetX();
|
2006-01-16 22:02:54 +01:00
|
|
|
|
2011-09-28 21:44:07 +02:00
|
|
|
if (event.Leaving())
|
|
|
|
mouse_pos = -1;
|
|
|
|
|
|
|
|
if (!event.LeftDown()) {
|
|
|
|
// Erase the old line and draw the new one
|
2011-09-29 20:17:37 +02:00
|
|
|
split_area->Refresh(false);
|
2011-09-28 21:44:07 +02:00
|
|
|
return;
|
2006-01-16 22:02:54 +01:00
|
|
|
}
|
2007-06-20 00:37:25 +02:00
|
|
|
|
2011-09-28 21:44:07 +02:00
|
|
|
// Character to insert the new split point before
|
|
|
|
int split_pos = std::min<int>((mouse_pos - char_width / 2) / char_width, spaced_text.size());
|
2006-01-16 22:02:54 +01:00
|
|
|
|
2011-09-28 21:44:07 +02:00
|
|
|
// Syllable this character is in
|
|
|
|
int syl = last_lt_or_eq(syl_start_points, split_pos);
|
2006-01-16 22:02:54 +01:00
|
|
|
|
2011-09-28 21:44:07 +02:00
|
|
|
// If the click is sufficiently close to a line of a syllable split,
|
|
|
|
// remove that split rather than adding a new one
|
|
|
|
if ((syl > 0 && mouse_pos <= syl_lines[syl - 1] + 2) || (syl < (int)syl_lines.size() && mouse_pos >= syl_lines[syl] - 2)) {
|
|
|
|
kara->RemoveSplit(syl);
|
2006-01-27 01:48:59 +01:00
|
|
|
}
|
2011-09-28 21:44:07 +02:00
|
|
|
else {
|
|
|
|
kara->AddSplit(syl, split_pos - syl_start_points[syl]);
|
2006-01-16 22:02:54 +01:00
|
|
|
}
|
|
|
|
|
2011-09-28 21:44:07 +02:00
|
|
|
SetDisplayText();
|
|
|
|
accept_button->Enable(true);
|
|
|
|
cancel_button->Enable(true);
|
|
|
|
split_area->Refresh(false);
|
2006-01-16 22:02:54 +01:00
|
|
|
}
|
|
|
|
|
2011-09-28 21:44:07 +02:00
|
|
|
void AudioKaraoke::LoadFromLine() {
|
|
|
|
kara->SetLine(active_line, true);
|
|
|
|
SetDisplayText();
|
|
|
|
accept_button->Enable(false);
|
|
|
|
cancel_button->Enable(false);
|
2009-05-14 23:44:43 +02:00
|
|
|
}
|
|
|
|
|
2011-09-28 21:44:07 +02:00
|
|
|
void AudioKaraoke::SetDisplayText() {
|
|
|
|
// Insert spaces between each syllable to avoid crowding
|
|
|
|
spaced_text.clear();
|
|
|
|
syl_start_points.clear();
|
|
|
|
syl_start_points.reserve(kara->size());
|
|
|
|
for (AssKaraoke::iterator it = kara->begin(); it != kara->end(); ++it) {
|
|
|
|
syl_start_points.push_back(spaced_text.size());
|
|
|
|
spaced_text += " " + it->text;
|
2006-06-27 06:04:40 +02:00
|
|
|
}
|
|
|
|
|
2011-09-28 21:44:07 +02:00
|
|
|
// Get the x-coordinates of the right edge of each character
|
|
|
|
wxMemoryDC dc;
|
|
|
|
dc.SetFont(split_font);
|
|
|
|
wxArrayInt p_char_x;
|
|
|
|
dc.GetPartialTextExtents(spaced_text, p_char_x);
|
2006-06-27 06:04:40 +02:00
|
|
|
|
2011-09-28 21:44:07 +02:00
|
|
|
// Convert the partial sub to the the width of each character
|
2011-09-29 04:33:22 +02:00
|
|
|
std::adjacent_difference(p_char_x.begin(), p_char_x.end(), p_char_x.begin());
|
2006-06-27 06:04:40 +02:00
|
|
|
|
2011-09-28 21:44:07 +02:00
|
|
|
// Get the maximum character width
|
2011-09-29 04:33:22 +02:00
|
|
|
char_width = *std::max_element(p_char_x.begin(), p_char_x.end());
|
2011-09-28 21:44:07 +02:00
|
|
|
|
|
|
|
// Center each character within the space available to it
|
|
|
|
char_x.resize(p_char_x.size());
|
|
|
|
for (size_t i = 0; i < p_char_x.size(); ++i) {
|
|
|
|
char_x[i] = i * char_width + (char_width - p_char_x[i]) / 2;
|
2006-06-27 06:04:40 +02:00
|
|
|
}
|
|
|
|
|
2011-09-28 21:44:07 +02:00
|
|
|
// Calculate the positions of the syllable divider lines
|
|
|
|
syl_lines.resize(syl_start_points.size() - 1);
|
|
|
|
for (size_t i = 1; i < syl_start_points.size(); ++i) {
|
|
|
|
syl_lines[i - 1] = syl_start_points[i] * char_width + char_width / 2;
|
2006-06-27 06:04:40 +02:00
|
|
|
}
|
|
|
|
|
2011-09-28 21:44:07 +02:00
|
|
|
// Get line height
|
|
|
|
wxSize extents = dc.GetTextExtent(spaced_text);
|
|
|
|
char_height = extents.GetHeight();
|
2011-09-29 20:17:37 +02:00
|
|
|
|
|
|
|
RenderText();
|
2006-06-27 06:04:40 +02:00
|
|
|
}
|
|
|
|
|
2011-09-28 21:44:07 +02:00
|
|
|
void AudioKaraoke::CancelSplit() {
|
|
|
|
LoadFromLine();
|
|
|
|
split_area->Refresh(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
void AudioKaraoke::AcceptSplit() {
|
|
|
|
active_line->Text = kara->GetText();
|
|
|
|
file_changed.Block();
|
|
|
|
c->ass->Commit(_("karaoke split"), AssFile::COMMIT_DIAG_TEXT);
|
|
|
|
file_changed.Unblock();
|
|
|
|
|
|
|
|
accept_button->Enable(false);
|
|
|
|
cancel_button->Enable(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
void AudioKaraoke::SetTagType(wxString new_tag) {
|
|
|
|
kara->SetTagType(new_tag);
|
|
|
|
AcceptSplit();
|
|
|
|
}
|