Audacity  2.3.1
Public Member Functions | Private Types | Private Member Functions | Private Attributes | List of all members
LyricsPanel Class Referencefinal

LyricsPanel is a panel that paints the bouncing ball and the lyrics text. More...

#include <Lyrics.h>

Inheritance diagram for LyricsPanel:
wxPanelWrapper wxTabTraversalWrapper< wxPanel >

Public Member Functions

 LyricsPanel (wxWindow *parent, wxWindowID id, AudacityProject *project, const wxPoint &pos=wxDefaultPosition, const wxSize &size=wxDefaultSize)
 
virtual ~LyricsPanel ()
 
int FindSyllable (long startChar)
 
int GetCurrentSyllableIndex ()
 
SyllableGetSyllable (int nSyl)
 
void SetCurrentSyllableIndex (int nSyl)
 
LyricsStyle GetLyricsStyle ()
 
void SetLyricsStyle (const LyricsStyle newLyricsStyle)
 
void Update (double t)
 
void UpdateLyrics (wxEvent &e)
 
void OnShow (wxShowEvent &e)
 
void OnStartStop (wxCommandEvent &e)
 
void OnKeyEvent (wxKeyEvent &event)
 
void DoPaint (wxDC &dc)
 
void OnPaint (wxPaintEvent &evt)
 
void OnSize (wxSizeEvent &evt)
 
void HandlePaint (wxDC &dc)
 
void HandlePaint_BouncingBall (wxDC &dc)
 
void HandleLayout ()
 
- Public Member Functions inherited from wxPanelWrapper
 wxPanelWrapper ()
 
 wxPanelWrapper (wxWindow *parent, wxWindowID winid=wxID_ANY, const wxPoint &pos=wxDefaultPosition, const wxSize &size=wxDefaultSize, long style=wxTAB_TRAVERSAL|wxNO_BORDER, const wxString &name=_("Panel"))
 
bool Create (wxWindow *parent, wxWindowID winid=wxID_ANY, const wxPoint &pos=wxDefaultPosition, const wxSize &size=wxDefaultSize, long style=wxTAB_TRAVERSAL|wxNO_BORDER, const wxString &name=_("Panel"))
 
- Public Member Functions inherited from wxTabTraversalWrapper< wxPanel >
 wxTabTraversalWrapper (Args &&...args)
 
 wxTabTraversalWrapper (const wxTabTraversalWrapper &)=delete
 
 wxTabTraversalWrapper (wxTabTraversalWrapper &&)=delete
 
wxTabTraversalWrapperoperator= (const wxTabTraversalWrapper &)=delete
 
wxTabTraversalWrapperoperator= (wxTabTraversalWrapper &&)=delete
 

Private Types

enum  LyricsStyle { kBouncingBallLyrics, kHighlightLyrics }
 

Private Member Functions

void Clear ()
 
void AddLabels (const LabelTrack *pLT)
 
void Finish (double finalT)
 
void Add (double t, const wxString &syllable, wxString &highlightText)
 
unsigned int GetDefaultFontSize () const
 
void SetDrawnFont (wxDC *dc)
 
void SetHighlightFont ()
 
void Measure (wxDC *dc)
 
int FindSyllable (double t)
 
void GetKaraokePosition (double t, int *outX, double *outY)
 

Private Attributes

int mWidth
 
int mHeight
 
int mKaraokeHeight
 
unsigned int mKaraokeFontSize
 
LyricsStyle mLyricsStyle
 
HighlightTextCtrlmHighlightTextCtrl
 
double mT
 
int mCurrentSyllable
 
std::vector< SyllablemSyllables
 
wxString mText
 
int mTextHeight
 
bool mMeasurementsDone
 
wxWeakRef< AudacityProjectmProject
 
bool mDelayedUpdate { false }
 

Detailed Description

LyricsPanel is a panel that paints the bouncing ball and the lyrics text.

Definition at line 74 of file Lyrics.h.

Member Enumeration Documentation

Enumerator
kBouncingBallLyrics 
kHighlightLyrics 

Definition at line 78 of file Lyrics.h.

78  {
79  kBouncingBallLyrics, // Lyrics move from right to left with bouncing ball.
80  // kGuitarTab, //v <<future>> Guitar Tablature moves from right to left.
81  kHighlightLyrics, // Lyrics show in scrolling page and syllables highlight successively.
82  };

Constructor & Destructor Documentation

LyricsPanel::LyricsPanel ( wxWindow *  parent,
wxWindowID  id,
AudacityProject project,
const wxPoint &  pos = wxDefaultPosition,
const wxSize &  size = wxDefaultSize 
)

Definition at line 84 of file Lyrics.cpp.

References OnShow(), OnSize(), OnStartStop(), safenew, and UpdateLyrics().

87  :
88  wxPanelWrapper(parent, id, pos, size, wxWANTS_CHARS),
89  mWidth(size.x), mHeight(size.y)
90  , mProject(project)
91 {
93  mLyricsStyle = kBouncingBallLyrics; // default
94  mKaraokeFontSize = this->GetDefaultFontSize(); // Call only after mLyricsPanelStyle is set.
95 
96  this->SetBackgroundColour(*wxWHITE);
97 
99  safenew HighlightTextCtrl(this, -1, // wxWindow* parent, wxWindowID id,
100  wxT(""), // const wxString& value = wxT(""),
101  wxPoint(0, 0), // const wxPoint& pos = wxDefaultPosition,
102  size); // const wxSize& size = wxDefaultSize
103  this->SetHighlightFont();
104  mHighlightTextCtrl->Show(mLyricsStyle == kHighlightLyrics); // test, in case we conditionalize the default, above
105 
106 
107  mT = 0.0;
108 
109  Clear();
110  Finish(0.0);
111 
112  #ifdef __WXMAC__
113  wxSizeEvent dummyEvent;
114  OnSize(dummyEvent);
115  #endif
116 
117  parent->Bind(wxEVT_SHOW, &LyricsPanel::OnShow, this);
118 
119  auto undoManager = project->GetUndoManager();
120  undoManager->Bind(EVT_UNDO_PUSHED, &LyricsPanel::UpdateLyrics, this);
121  undoManager->Bind(EVT_UNDO_MODIFIED, &LyricsPanel::UpdateLyrics, this);
122  undoManager->Bind(EVT_UNDO_RESET, &LyricsPanel::UpdateLyrics, this);
123 
124  wxTheApp->Bind(EVT_AUDIOIO_PLAYBACK, &LyricsPanel::OnStartStop, this);
125  wxTheApp->Bind(EVT_AUDIOIO_CAPTURE, &LyricsPanel::OnStartStop, this);
126 }
void OnStartStop(wxCommandEvent &e)
Definition: Lyrics.cpp:506
int mHeight
Definition: Lyrics.h:139
void SetHighlightFont()
Definition: Lyrics.cpp:266
double mT
Definition: Lyrics.h:147
void Finish(double finalT)
Definition: Lyrics.cpp:203
#define safenew
Definition: Audacity.h:230
unsigned int GetDefaultFontSize() const
Definition: Lyrics.cpp:256
int mKaraokeHeight
Definition: Lyrics.h:141
void OnSize(wxSizeEvent &evt)
Definition: Lyrics.cpp:564
void OnShow(wxShowEvent &e)
Definition: Lyrics.cpp:515
int mWidth
Definition: Lyrics.h:138
UndoManager * GetUndoManager()
Definition: Project.h:212
unsigned int mKaraokeFontSize
Definition: Lyrics.h:142
wxWeakRef< AudacityProject > mProject
Definition: Lyrics.h:156
void UpdateLyrics(wxEvent &e)
Definition: Lyrics.cpp:473
LyricsStyle mLyricsStyle
Definition: Lyrics.h:144
void Clear()
Definition: Lyrics.cpp:134
HighlightTextCtrl * mHighlightTextCtrl
Definition: Lyrics.h:145
LyricsPanel::~LyricsPanel ( )
virtual

Definition at line 128 of file Lyrics.cpp.

129 {
130 }

Member Function Documentation

void LyricsPanel::Add ( double  t,
const wxString &  syllable,
wxString &  highlightText 
)
private

Definition at line 159 of file Lyrics.cpp.

References Syllable::char0, Syllable::char1, mSyllables, mText, Syllable::t, Syllable::text, and Syllable::textWithSpace.

Referenced by AddLabels().

160 {
161  int i = mSyllables.size();
162 
163  {
164  Syllable &prevSyllable = mSyllables[i - 1];
165 
166  if (prevSyllable.t == t) {
167  // We can't have two syllables with the same time, so append
168  // this to the end of the previous one if they're at the
169  // same time.
170  prevSyllable.text += syllable;
171  prevSyllable.textWithSpace += syllable;
172  prevSyllable.char1 += syllable.Length();
173  return;
174  }
175  }
176 
177  mSyllables.push_back(Syllable());
178  Syllable &thisSyllable = mSyllables[i];
179  thisSyllable.t = t;
180  thisSyllable.text = syllable;
181 
182  thisSyllable.char0 = mText.Length();
183 
184  // Put a space between syllables unless the previous one
185  // ended in a hyphen
186  if (i > 0 &&
187  // mSyllables[i-1].text.Length() > 0 &&
188  mSyllables[i - 1].text.Right(1) != wxT("-"))
189  thisSyllable.textWithSpace = wxT(" ") + syllable;
190  else
191  thisSyllable.textWithSpace = syllable;
192 
193  mText += thisSyllable.textWithSpace;
194  thisSyllable.char1 = mText.Length();
195 
196  int nTextLen = thisSyllable.textWithSpace.Length();
197  if ((nTextLen > 0) && (thisSyllable.textWithSpace.Right(1) == wxT("_")))
198  highlightText += (thisSyllable.textWithSpace.Left(nTextLen - 1) + wxT("\n"));
199  else
200  highlightText += thisSyllable.textWithSpace;
201 }
std::vector< Syllable > mSyllables
Definition: Lyrics.h:150
int char0
Definition: Lyrics.h:40
int char1
Definition: Lyrics.h:41
double t
Definition: Lyrics.h:37
wxString text
Definition: Lyrics.h:38
wxString textWithSpace
Definition: Lyrics.h:39
used in LyricsPanel, a Syllable gives positional information to be used with the bouncing ball effect...
Definition: Lyrics.h:30
wxString mText
Definition: Lyrics.h:151
void LyricsPanel::AddLabels ( const LabelTrack pLT)
private

Definition at line 148 of file Lyrics.cpp.

References Add(), LabelTrack::GetLabel(), LabelTrack::GetNumLabels(), LabelStruct::getT0(), mHighlightTextCtrl, and LabelStruct::title.

Referenced by UpdateLyrics().

149 {
150  const size_t numLabels = pLT->GetNumLabels();
151  wxString highlightText;
152  for (size_t ii = 0; ii < numLabels; ++ii) {
153  const LabelStruct *const pLabel = pLT->GetLabel(ii);
154  Add(pLabel->getT0(), pLabel->title, highlightText);
155  }
156  mHighlightTextCtrl->AppendText(highlightText);
157 }
void Add(double t, const wxString &syllable, wxString &highlightText)
Definition: Lyrics.cpp:159
wxString title
Definition: LabelTrack.h:92
int GetNumLabels() const
A LabelStruct holds information for ONE label in a LabelTrack.
Definition: LabelTrack.h:44
const LabelStruct * GetLabel(int index) const
double getT0() const
Definition: LabelTrack.h:61
HighlightTextCtrl * mHighlightTextCtrl
Definition: Lyrics.h:145
void LyricsPanel::Clear ( )
private

Definition at line 134 of file Lyrics.cpp.

References mHighlightTextCtrl, mSyllables, and mText.

Referenced by UpdateLyrics().

135 {
136  mSyllables.clear();
137  mText = wxT("");
138 
139  // Add two dummy syllables at the beginning
140  mSyllables.push_back(Syllable());
141  mSyllables[0].t = -2.0;
142  mSyllables.push_back(Syllable());
143  mSyllables[1].t = -1.0;
144 
145  mHighlightTextCtrl->Clear();
146 }
std::vector< Syllable > mSyllables
Definition: Lyrics.h:150
used in LyricsPanel, a Syllable gives positional information to be used with the bouncing ball effect...
Definition: Lyrics.h:30
wxString mText
Definition: Lyrics.h:151
HighlightTextCtrl * mHighlightTextCtrl
Definition: Lyrics.h:145
void LyricsPanel::DoPaint ( wxDC &  dc)

Definition at line 535 of file Lyrics.cpp.

References HandlePaint(), kBouncingBallLyrics, Measure(), mKaraokeHeight, mLyricsStyle, mMeasurementsDone, and mWidth.

Referenced by OnPaint(), and OnSize().

536 {
537  if (!this->GetParent()->IsShown())
538  return;
539 
541  {
542  if (!mMeasurementsDone)
543  Measure(&dc);
544 
545  #ifdef __WXMAC__
546  // Mac OS X automatically double-buffers the screen for you,
547  // so our bitmap is unneccessary
548  HandlePaint(dc);
549  #else
550  wxBitmap bitmap(mWidth, mKaraokeHeight);
551  wxMemoryDC memDC;
552  memDC.SelectObject(bitmap);
553  HandlePaint(memDC);
554  dc.Blit(0, 0, mWidth, mKaraokeHeight, &memDC, 0, 0, wxCOPY, FALSE);
555  #endif
556  }
557  else // (mLyricsStyle == kHighlightLyrics)
558  {
559  //v causes flicker in ported version
560  // this->SetHighlightFont();
561  }
562 }
void HandlePaint(wxDC &dc)
Definition: Lyrics.cpp:600
int mKaraokeHeight
Definition: Lyrics.h:141
void Measure(wxDC *dc)
Definition: Lyrics.cpp:274
int mWidth
Definition: Lyrics.h:138
bool mMeasurementsDone
Definition: Lyrics.h:154
LyricsStyle mLyricsStyle
Definition: Lyrics.h:144
int LyricsPanel::FindSyllable ( long  startChar)

Definition at line 221 of file Lyrics.cpp.

References mSyllables.

Referenced by GetKaraokePosition(), HighlightTextCtrl::OnMouseEvent(), and Update().

222 {
223  int i1, i2;
224 
225  i1 = 0;
226  i2 = mSyllables.size();
227  while (i2 > i1+1) {
228  int pmid = (i1+i2)/2;
229  if (mSyllables[pmid].char0 > startChar)
230  i2 = pmid;
231  else
232  i1 = pmid;
233  }
234 
235  if (i1 < 2)
236  i1 = 2;
237  if (i1 > (int)(mSyllables.size()) - 3)
238  i1 = mSyllables.size() - 3;
239 
240  return i1;
241 }
std::vector< Syllable > mSyllables
Definition: Lyrics.h:150
int LyricsPanel::FindSyllable ( double  t)
private

Definition at line 329 of file Lyrics.cpp.

References mSyllables.

330 {
331  int i1, i2;
332 
333  i1 = 0;
334  i2 = mSyllables.size();
335  while (i2 > i1+1) {
336  int pmid = (i1+i2)/2;
337  if (mSyllables[pmid].t > t)
338  i2 = pmid;
339  else
340  i1 = pmid;
341  }
342 
343  if (i1 < 2)
344  i1 = 2;
345  if (i1 > (int)(mSyllables.size()) - 3)
346  i1 = mSyllables.size() - 3;
347 
348  return i1;
349 }
std::vector< Syllable > mSyllables
Definition: Lyrics.h:150
void LyricsPanel::Finish ( double  finalT)
private

Definition at line 203 of file Lyrics.cpp.

References mCurrentSyllable, mHighlightTextCtrl, mMeasurementsDone, and mSyllables.

Referenced by UpdateLyrics().

204 {
205  // Add 3 dummy syllables at the end
206  int i = mSyllables.size();
207  mSyllables.push_back(Syllable());
208  mSyllables[i].t = finalT + 1.0;
209  mSyllables.push_back(Syllable());
210  mSyllables[i+1].t = finalT + 2.0;
211  mSyllables.push_back(Syllable());
212  mSyllables[i+2].t = finalT + 3.0;
213 
214  // Mark measurements as invalid
215  mMeasurementsDone = false; // only for drawn text
216  mCurrentSyllable = 0;
217  mHighlightTextCtrl->ShowPosition(0);
218 }
std::vector< Syllable > mSyllables
Definition: Lyrics.h:150
int mCurrentSyllable
Definition: Lyrics.h:149
bool mMeasurementsDone
Definition: Lyrics.h:154
used in LyricsPanel, a Syllable gives positional information to be used with the bouncing ball effect...
Definition: Lyrics.h:30
HighlightTextCtrl * mHighlightTextCtrl
Definition: Lyrics.h:145
int LyricsPanel::GetCurrentSyllableIndex ( )
inline

Definition at line 92 of file Lyrics.h.

References mCurrentSyllable.

Referenced by HighlightTextCtrl::OnMouseEvent().

92 { return mCurrentSyllable; };
int mCurrentSyllable
Definition: Lyrics.h:149
unsigned int LyricsPanel::GetDefaultFontSize ( ) const
private

Definition at line 256 of file Lyrics.cpp.

References kBouncingBallLyrics, and mLyricsStyle.

Referenced by OnSize().

257 {
258  return (mLyricsStyle == kBouncingBallLyrics) ? 48 : 10;
259 }
LyricsStyle mLyricsStyle
Definition: Lyrics.h:144
void LyricsPanel::GetKaraokePosition ( double  t,
int *  outX,
double *  outY 
)
private

Definition at line 358 of file Lyrics.cpp.

References FindSyllable(), I_FIRST_REAL_SYLLABLE, M_PI, and mSyllables.

Referenced by HandlePaint_BouncingBall().

360 {
361  *outX = 0;
362  *outY = 0;
363 
364  if (t < mSyllables[I_FIRST_REAL_SYLLABLE].t || t > mSyllables[mSyllables.size() - 3].t)
365  return;
366 
367  int i0, i1, i2, i3;
368  int x0, x1, x2, x3;
369  double t0, t1, t2, t3;
370  i1 = FindSyllable(t);
371  i2 = i1 + 1;
372 
373  // Because we've padded the syllables with two dummies at the beginning
374  // and end, we know that i0...i3 will always exist. Also, we've made
375  // sure that we don't ever have two of the same time, so t2>t1 strictly.
376  //
377  // t
378  // \/
379  // time: t0 t1 t2 t3
380  // pos: x0 x1 x2 x3
381  // index: i0 i1 i2 i3
382  // vel: vel1 vel2
383 
384  i0 = i1 - 1;
385  i3 = i2 + 1;
386 
387  x0 = mSyllables[i0].x;
388  x1 = mSyllables[i1].x;
389  x2 = mSyllables[i2].x;
390  x3 = mSyllables[i3].x;
391 
392  t0 = mSyllables[i0].t;
393  t1 = mSyllables[i1].t;
394  t2 = mSyllables[i2].t;
395  t3 = mSyllables[i3].t;
396 
397  double linear_vel0 = (x1 - x0) / (t1 - t0);
398  double linear_vel1 = (x2 - x1) / (t2 - t1);
399  double linear_vel2 = (x3 - x2) / (t3 - t2);
400 
401  // average velocities
402  double v1 = (linear_vel0 + linear_vel1) / 2;
403  double v2 = (linear_vel1 + linear_vel2) / 2;
404 
405  // Solve a cubic equation f(t) = at^3 + bt^2 + ct + d
406  // which gives the position x as a function of
407  // (t - t1), by constraining f(0), f'(0), f(t2-t1), f'(t2-t1)
408  double delta_t = t2 - t1;
409  double delta_x = x2 - x1;
410  v1 *= delta_t;
411  v2 *= delta_t;
412  double a = v1 + v2 - 2*delta_x;
413  double b = 3*delta_x - 2*v1 - v2;
414  double c = v1;
415  double d = x1;
416 
417  t = (t - t1) / (t2 - t1);
418  double xx = a*t*t*t + b*t*t + c*t + d;
419 
420  // Unfortunately sometimes our cubic goes backwards. This is a quick
421  // hack to stop that from happening.
422  if (xx < x1)
423  xx = x1;
424 
425  *outX = (int)xx;
426 
427  // The y position is a simple cosine curve; the max height is a
428  // function of the time.
429  double height = t2 - t1 > 4.0? 1.0: sqrt((t2-t1)/4.0);
430  *outY = height * sin(M_PI * t);
431 }
std::vector< Syllable > mSyllables
Definition: Lyrics.h:150
#define I_FIRST_REAL_SYLLABLE
Definition: Lyrics.cpp:132
int FindSyllable(long startChar)
Definition: Lyrics.cpp:221
#define M_PI
Definition: Distortion.cpp:28
LyricsStyle LyricsPanel::GetLyricsStyle ( )
inline

Definition at line 96 of file Lyrics.h.

References mLyricsStyle.

96 { return mLyricsStyle; };
LyricsStyle mLyricsStyle
Definition: Lyrics.h:144
Syllable* LyricsPanel::GetSyllable ( int  nSyl)
inline

Definition at line 93 of file Lyrics.h.

References mSyllables.

Referenced by HighlightTextCtrl::OnMouseEvent().

93 { return &(mSyllables[nSyl]); };
std::vector< Syllable > mSyllables
Definition: Lyrics.h:150
void LyricsPanel::HandleLayout ( )
void LyricsPanel::HandlePaint ( wxDC &  dc)

Definition at line 600 of file Lyrics.cpp.

References HandlePaint_BouncingBall(), kBouncingBallLyrics, mKaraokeHeight, mLyricsStyle, and mWidth.

Referenced by DoPaint().

601 {
602  wxASSERT(mLyricsStyle == kBouncingBallLyrics);
603  dc.SetBrush(*wxWHITE_BRUSH);
604  dc.DrawRectangle(0, 0, mWidth, mKaraokeHeight);
605 
606  this->HandlePaint_BouncingBall(dc);
607 }
void HandlePaint_BouncingBall(wxDC &dc)
Definition: Lyrics.cpp:609
int mKaraokeHeight
Definition: Lyrics.h:141
int mWidth
Definition: Lyrics.h:138
LyricsStyle mLyricsStyle
Definition: Lyrics.h:144
void LyricsPanel::HandlePaint_BouncingBall ( wxDC &  dc)

Definition at line 609 of file Lyrics.cpp.

References GetKaraokePosition(), mKaraokeHeight, mSyllables, mT, mTextHeight, mWidth, and SetDrawnFont().

Referenced by HandlePaint().

610 {
611  int ctr = mWidth / 2;
612  int x;
613  double y;
614  GetKaraokePosition(mT, &x, &y);
615 
616  dc.SetTextForeground(wxColour(238, 0, 102));
617  bool changedColor = false;
618 
619  SetDrawnFont(&dc);
620  unsigned int i;
621  wxCoord yTextTop = mKaraokeHeight - mTextHeight - 4;
622  for(i = 0; i < mSyllables.size(); i++) {
623  if (mSyllables[i].x + mSyllables[i].width < (x - ctr))
624  continue;
625  if (mSyllables[i].x > x + ctr)
626  continue;
627 
628  if (!changedColor && mSyllables[i].x >= x) {
629  dc.SetTextForeground(*wxBLACK);
630  changedColor = true;
631  }
632 
633  wxString text = mSyllables[i].text;
634  if (text.Length() > 0 && text.Right(1) == wxT("_")) {
635  text = text.Left(text.Length() - 1);
636  }
637 
638  dc.DrawText(text,
639  mSyllables[i].leftX + ctr - x,
640  yTextTop);
641  }
642 
643  int ballRadius = (int)(mTextHeight / 8.0);
644  int bounceTop = ballRadius * 2;
645  int bounceHeight = yTextTop - bounceTop;
646  int yi = (int)(yTextTop - 4 - (y * bounceHeight));
647 
648  if (mT >= 0.0) {
649  wxRect ball(ctr - ballRadius, yi - ballRadius, 2 * ballRadius, 2 * ballRadius);
650  dc.SetBrush(wxBrush(wxColour(238, 0, 102), wxBRUSHSTYLE_SOLID));
651  dc.DrawEllipse(ball);
652  }
653 }
std::vector< Syllable > mSyllables
Definition: Lyrics.h:150
double mT
Definition: Lyrics.h:147
int mKaraokeHeight
Definition: Lyrics.h:141
void SetDrawnFont(wxDC *dc)
Definition: Lyrics.cpp:261
int mTextHeight
Definition: Lyrics.h:153
int mWidth
Definition: Lyrics.h:138
void GetKaraokePosition(double t, int *outX, double *outY)
Definition: Lyrics.cpp:358
void LyricsPanel::Measure ( wxDC *  dc)
private

Definition at line 274 of file Lyrics.cpp.

References I_FIRST_REAL_SYLLABLE, mMeasurementsDone, mSyllables, mTextHeight, and SetDrawnFont().

Referenced by DoPaint().

275 {
276  this->SetDrawnFont(dc);
277  int width = 0, height = 0;
278 
279  const int kIndent = 4;
280  int x = 2*kIndent;
281 
282  unsigned int i;
283  for(i = 0; i < mSyllables.size(); i++) {
284  if ((i < I_FIRST_REAL_SYLLABLE) || // Clear() starts the list with I_FIRST_REAL_SYLLABLE dummies.
285  (i >= mSyllables.size() - 3)) // Finish() ends with 3 dummies.
286  {
287  dc->GetTextExtent(wxT("DUMMY"), &width, &height); // Get the correct height even if we're at i=0.
288  width = 0;
289  }
290  else {
291  dc->GetTextExtent(mSyllables[i].textWithSpace, &width, &height);
292  }
293 
294  // Add some space between words; the space is normally small but
295  // when there's a long pause relative to the previous word, insert
296  // extra space.
297  int extraWidth;
298  if (i >= I_FIRST_REAL_SYLLABLE && i < mSyllables.size() - 2)
299  {
300  double deltaThis = mSyllables[i+1].t - mSyllables[i].t;
301  double deltaPrev = mSyllables[i].t - mSyllables[i-1].t;
302 
303  double ratio;
304  if (deltaPrev > 0.0)
305  ratio = deltaThis / deltaPrev;
306  else
307  ratio = deltaThis;
308 
309  if (ratio > 2.0)
310  extraWidth = 15 + (int)(15.0 * ratio);
311  else
312  extraWidth = 15;
313  }
314  else
315  extraWidth = 20;
316 
317  mSyllables[i].width = width + extraWidth;
318  mSyllables[i].leftX = x;
319  mSyllables[i].x = x + width/2;
320  x += mSyllables[i].width;
321  }
322 
323  mTextHeight = height;
324 
325  mMeasurementsDone = true;
326 }
std::vector< Syllable > mSyllables
Definition: Lyrics.h:150
#define I_FIRST_REAL_SYLLABLE
Definition: Lyrics.cpp:132
void SetDrawnFont(wxDC *dc)
Definition: Lyrics.cpp:261
int mTextHeight
Definition: Lyrics.h:153
bool mMeasurementsDone
Definition: Lyrics.h:154
void LyricsPanel::OnKeyEvent ( wxKeyEvent &  event)

Definition at line 522 of file Lyrics.cpp.

References CommandManager::FilterKeyEvent(), GetActiveProject(), and AudacityProject::GetCommandManager().

523 {
524  AudacityProject *project = GetActiveProject();
525  project->GetCommandManager()->FilterKeyEvent(project, event, true);
526  event.Skip();
527 }
bool FilterKeyEvent(AudacityProject *project, const wxKeyEvent &evt, bool permit=false)
CommandManager * GetCommandManager()
Definition: Project.h:362
AudacityProject provides the main window, with tools and tracks contained within it.
Definition: Project.h:174
AUDACITY_DLL_API AudacityProject * GetActiveProject()
Definition: Project.cpp:311
void LyricsPanel::OnPaint ( wxPaintEvent &  evt)

Definition at line 529 of file Lyrics.cpp.

References DoPaint().

530 {
531  wxPaintDC dc(this);
532  DoPaint(dc);
533 }
void DoPaint(wxDC &dc)
Definition: Lyrics.cpp:535
void LyricsPanel::OnShow ( wxShowEvent &  e)

Definition at line 515 of file Lyrics.cpp.

References UpdateLyrics().

Referenced by LyricsPanel().

516 {
517  e.Skip();
518  if (e.IsShown())
519  UpdateLyrics(e);
520 }
void UpdateLyrics(wxEvent &e)
Definition: Lyrics.cpp:473
void LyricsPanel::OnSize ( wxSizeEvent &  evt)

Definition at line 564 of file Lyrics.cpp.

References DoPaint(), GetDefaultFontSize(), kBouncingBallLyrics, LYRICS_DEFAULT_HEIGHT, mHeight, mHighlightTextCtrl, mKaraokeFontSize, mKaraokeHeight, mLyricsStyle, mMeasurementsDone, mWidth, and SetHighlightFont().

Referenced by SetLyricsStyle().

565 {
566  GetClientSize(&mWidth, &mHeight);
567 
569 
571  (int)((float)(this->GetDefaultFontSize() * mHeight) / (float)LYRICS_DEFAULT_HEIGHT);
572  // Usually don't get the size window we want, usually less than
573  // LYRICS_DEFAULT_HEIGHT, so bump it a little.
574  mKaraokeFontSize += 2;
575 
577  {
578  mMeasurementsDone = false;
579  wxClientDC dc(this);
580  this->DoPaint(dc);
581  }
582  else // (mLyricsStyle == kHighlightLyrics)
583  {
585  this->SetHighlightFont();
586  }
587 
588  this->Refresh(false);
589 }
int mHeight
Definition: Lyrics.h:139
void SetHighlightFont()
Definition: Lyrics.cpp:266
unsigned int GetDefaultFontSize() const
Definition: Lyrics.cpp:256
int mKaraokeHeight
Definition: Lyrics.h:141
void DoPaint(wxDC &dc)
Definition: Lyrics.cpp:535
int mWidth
Definition: Lyrics.h:138
#define LYRICS_DEFAULT_HEIGHT
Definition: Lyrics.h:26
bool mMeasurementsDone
Definition: Lyrics.h:154
unsigned int mKaraokeFontSize
Definition: Lyrics.h:142
LyricsStyle mLyricsStyle
Definition: Lyrics.h:144
HighlightTextCtrl * mHighlightTextCtrl
Definition: Lyrics.h:145
void LyricsPanel::OnStartStop ( wxCommandEvent &  e)

Definition at line 506 of file Lyrics.cpp.

References mDelayedUpdate, and UpdateLyrics().

Referenced by LyricsPanel().

507 {
508  e.Skip();
509  if ( !e.GetInt() && mDelayedUpdate ) {
510  mDelayedUpdate = false;
511  UpdateLyrics( e );
512  }
513 }
bool mDelayedUpdate
Definition: Lyrics.h:157
void UpdateLyrics(wxEvent &e)
Definition: Lyrics.cpp:473
void LyricsPanel::SetCurrentSyllableIndex ( int  nSyl)
inline

Definition at line 94 of file Lyrics.h.

References mCurrentSyllable.

94 { mCurrentSyllable = nSyl; };
int mCurrentSyllable
Definition: Lyrics.h:149
void LyricsPanel::SetDrawnFont ( wxDC *  dc)
private

Definition at line 261 of file Lyrics.cpp.

References mKaraokeFontSize.

Referenced by HandlePaint_BouncingBall(), and Measure().

262 {
263  dc->SetFont(wxFont(mKaraokeFontSize, wxFONTFAMILY_SWISS, wxFONTSTYLE_NORMAL, wxFONTWEIGHT_NORMAL));
264 }
unsigned int mKaraokeFontSize
Definition: Lyrics.h:142
void LyricsPanel::SetHighlightFont ( )
private

Definition at line 266 of file Lyrics.cpp.

References mHighlightTextCtrl, and mKaraokeFontSize.

Referenced by OnSize().

267 {
268  wxFont newFont(mKaraokeFontSize, wxFONTFAMILY_SWISS, wxFONTSTYLE_NORMAL, wxFONTWEIGHT_NORMAL);
269  mHighlightTextCtrl->SetDefaultStyle(wxTextAttr(wxNullColour, wxNullColour, newFont));
270  mHighlightTextCtrl->SetStyle(0, mHighlightTextCtrl->GetLastPosition(),
271  wxTextAttr(wxNullColour, wxNullColour, newFont));
272 }
unsigned int mKaraokeFontSize
Definition: Lyrics.h:142
HighlightTextCtrl * mHighlightTextCtrl
Definition: Lyrics.h:145
void LyricsPanel::SetLyricsStyle ( const LyricsStyle  newLyricsStyle)

Definition at line 243 of file Lyrics.cpp.

References kHighlightLyrics, mHighlightTextCtrl, mLyricsStyle, and OnSize().

Referenced by LyricsWindow::OnStyle_BouncingBall(), and LyricsWindow::OnStyle_Highlight().

244 {
245  if (mLyricsStyle == newLyricsStyle)
246  return;
247 
248  mLyricsStyle = newLyricsStyle;
250 
251  wxSizeEvent ignore;
252  this->OnSize(ignore);
253 }
void OnSize(wxSizeEvent &evt)
Definition: Lyrics.cpp:564
LyricsStyle mLyricsStyle
Definition: Lyrics.h:144
HighlightTextCtrl * mHighlightTextCtrl
Definition: Lyrics.h:145
void LyricsPanel::Update ( double  t)

Definition at line 433 of file Lyrics.cpp.

References FindSyllable(), GetActiveProject(), AudacityProject::GetSel0(), kBouncingBallLyrics, kHighlightLyrics, mCurrentSyllable, mHighlightTextCtrl, mKaraokeHeight, mLyricsStyle, mSyllables, mT, and mWidth.

Referenced by LyricsWindow::OnTimer(), and UpdateLyrics().

434 {
435  if (t < 0.0)
436  {
437  // TrackPanel::OnTimer passes gAudioIO->GetStreamTime(), which is -DBL_MAX if !IsStreamActive().
438  // In that case, use the selection start time.
440  mT = pProj->GetSel0();
441  }
442  else
443  mT = t;
444 
446  {
447  wxRect karaokeRect(0, 0, mWidth, mKaraokeHeight);
448  this->Refresh(false, &karaokeRect);
449  }
450 
451  int i = FindSyllable(mT);
452  if (i == mCurrentSyllable)
453  return;
454 
455  mCurrentSyllable = i;
456 
458  {
459  mHighlightTextCtrl->SetSelection(mSyllables[i].char0, mSyllables[i].char1);
460 
461  //v No trail for now.
463  //if (i == I_FIRST_REAL_SYLLABLE)
464  // // Reset the trail to zero.
465  // mHighlightTextCtrl->SetStyle(0, mHighlightTextCtrl->GetLastPosition(), wxTextAttr(wxNullColour, *wxWHITE));
467  //mHighlightTextCtrl->SetStyle(mSyllables[i].char0, mSyllables[i].char1, wxTextAttr(wxNullColour, *wxLIGHT_GREY));
468 
469  //v Too much flicker: mHighlightTextCtrl->ShowPosition(mSyllables[i].char0);
470  }
471 }
std::vector< Syllable > mSyllables
Definition: Lyrics.h:150
int mCurrentSyllable
Definition: Lyrics.h:149
double GetSel0() const
Definition: Project.h:221
double mT
Definition: Lyrics.h:147
int FindSyllable(long startChar)
Definition: Lyrics.cpp:221
AudacityProject provides the main window, with tools and tracks contained within it.
Definition: Project.h:174
int mKaraokeHeight
Definition: Lyrics.h:141
int mWidth
Definition: Lyrics.h:138
AUDACITY_DLL_API AudacityProject * GetActiveProject()
Definition: Project.cpp:311
LyricsStyle mLyricsStyle
Definition: Lyrics.h:144
HighlightTextCtrl * mHighlightTextCtrl
Definition: Lyrics.h:145
void LyricsPanel::UpdateLyrics ( wxEvent &  e)

Definition at line 473 of file Lyrics.cpp.

References AddLabels(), Clear(), Finish(), gAudioIO, AudioIO::IsStreamActive(), mDelayedUpdate, mProject, and Update().

Referenced by LyricsPanel(), OnShow(), and OnStartStop().

474 {
475  e.Skip();
476 
477  // It's crucial to not do that repopulating during playback.
478  if (gAudioIO->IsStreamActive()) {
479  mDelayedUpdate = true;
480  return;
481  }
482 
483  Clear();
484 
485  if (!mProject)
486  return;
487 
488  // Lyrics come from only the first label track.
489  auto pLabelTrack = *mProject->GetTracks()->Any< const LabelTrack >().begin();
490  if (!pLabelTrack)
491  return;
492 
493  // The code that updates the lyrics is rather expensive when there
494  // are a lot of labels.
495  // So - bail out early if the lyrics window is not visible.
496  // We will later force an update when the lyrics window is made visible.
497  auto parent = dynamic_cast<wxFrame*>(GetParent());
498  if( !(parent && parent->IsVisible()) )
499  return;
500 
501  AddLabels(pLabelTrack);
502  Finish(pLabelTrack->GetEndTime());
503  Update(mProject->GetSel0());
504 }
bool mDelayedUpdate
Definition: Lyrics.h:157
bool IsStreamActive() const
Returns true if the audio i/o is running at all, but not during cleanup.
Definition: AudioIO.cpp:2749
void Finish(double finalT)
Definition: Lyrics.cpp:203
A LabelTrack is a Track that holds labels (LabelStruct).
Definition: LabelTrack.h:113
AudioIO * gAudioIO
Definition: AudioIO.cpp:491
void Update(double t)
Definition: Lyrics.cpp:433
wxWeakRef< AudacityProject > mProject
Definition: Lyrics.h:156
void Clear()
Definition: Lyrics.cpp:134
void AddLabels(const LabelTrack *pLT)
Definition: Lyrics.cpp:148

Member Data Documentation

int LyricsPanel::mCurrentSyllable
private

Definition at line 149 of file Lyrics.h.

Referenced by Finish(), GetCurrentSyllableIndex(), SetCurrentSyllableIndex(), and Update().

bool LyricsPanel::mDelayedUpdate { false }
private

Definition at line 157 of file Lyrics.h.

Referenced by OnStartStop(), and UpdateLyrics().

int LyricsPanel::mHeight
private

Definition at line 139 of file Lyrics.h.

Referenced by OnSize().

HighlightTextCtrl* LyricsPanel::mHighlightTextCtrl
private

Definition at line 145 of file Lyrics.h.

Referenced by AddLabels(), Clear(), Finish(), OnSize(), SetHighlightFont(), SetLyricsStyle(), and Update().

unsigned int LyricsPanel::mKaraokeFontSize
private

Definition at line 142 of file Lyrics.h.

Referenced by OnSize(), SetDrawnFont(), and SetHighlightFont().

int LyricsPanel::mKaraokeHeight
private

Definition at line 141 of file Lyrics.h.

Referenced by DoPaint(), HandlePaint(), HandlePaint_BouncingBall(), OnSize(), and Update().

LyricsStyle LyricsPanel::mLyricsStyle
private
bool LyricsPanel::mMeasurementsDone
private

Definition at line 154 of file Lyrics.h.

Referenced by DoPaint(), Finish(), Measure(), and OnSize().

wxWeakRef<AudacityProject> LyricsPanel::mProject
private

Definition at line 156 of file Lyrics.h.

Referenced by UpdateLyrics().

std::vector<Syllable> LyricsPanel::mSyllables
private
double LyricsPanel::mT
private

Definition at line 147 of file Lyrics.h.

Referenced by HandlePaint_BouncingBall(), and Update().

wxString LyricsPanel::mText
private

Definition at line 151 of file Lyrics.h.

Referenced by Add(), and Clear().

int LyricsPanel::mTextHeight
private

Definition at line 153 of file Lyrics.h.

Referenced by HandlePaint_BouncingBall(), and Measure().

int LyricsPanel::mWidth
private

Definition at line 138 of file Lyrics.h.

Referenced by DoPaint(), HandlePaint(), HandlePaint_BouncingBall(), OnSize(), and Update().


The documentation for this class was generated from the following files: