Skip to content

Commit

Permalink
lrc cut lines
Browse files Browse the repository at this point in the history
  • Loading branch information
fabricelacharme committed Jan 29, 2025
1 parent d0e2bc6 commit 7b7a982
Show file tree
Hide file tree
Showing 5 changed files with 429 additions and 262 deletions.
48 changes: 40 additions & 8 deletions Karaboss/Lyrics/frmLrcOptions.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,38 @@
using System;
#region License

/* Copyright (c) 2025 Fabrice Lacharme
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to
* deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

#endregion

#region Contact

/*
* Fabrice Lacharme
* Email: fabrice.lacharme@gmail.com
*/

#endregion

using System;
using System.Windows.Forms;
using static Karaboss.Karaclass;

Expand Down Expand Up @@ -52,7 +86,9 @@ public bool bCutLines
#endregion properties


//public frmLrcOptions(Karaclass.LrcFormats LrcFormat, bool bRemoveAccents, bool bForceUpperCase, bool bRemoveNonAlphaNumeric)
/// <summary>
/// Constructor
/// </summary>
public frmLrcOptions()
{
InitializeComponent();
Expand Down Expand Up @@ -139,16 +175,12 @@ private void OptFormatSyllabes_CheckedChanged(object sender, EventArgs e)
chkCutLines.Visible = !OptFormatSyllabes.Checked;
UpdCutLines.Visible = !OptFormatSyllabes.Checked;
lblCutLines.Visible = !OptFormatSyllabes.Checked;
//UpdCutLines.Visible = chkCutLines.Visible && chkCutLines.Checked;
//lblCutLines.Visible = chkCutLines.Visible && chkCutLines.Checked;
}

private void OptFormatLines_CheckedChanged(object sender, EventArgs e)
{
chkCutLines.Visible = OptFormatLines.Checked;
//UpdCutLines.Visible = chkCutLines.Visible && chkCutLines.Checked;
//lblCutLines.Visible = chkCutLines.Visible && chkCutLines.Checked;

chkCutLines.Visible = OptFormatLines.Checked;
}

}
}
257 changes: 11 additions & 246 deletions Karaboss/Lyrics/frmLyricsEdit.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2231,7 +2231,7 @@ private void SaveLRCLines(string File, bool bRemoveAccents, bool bUpperCase, boo
string cr = "\r\n";
string strSpaceBetween;
bool bSpaceBetwwen = false;
bool bStartLine;
//bool bStartLine;

//bool bControlLength = true;
//int MaxLength = 38;
Expand Down Expand Up @@ -2319,262 +2319,27 @@ private void SaveLRCLines(string File, bool bRemoveAccents, bool bUpperCase, boo
}
#endregion List of Lyrics


#region List of Lines

// Store lyrics in lines

// List to store lines
List<string> lstLines = new List<string>();
List<string> lstTimeLines = new List<string>();

bStartLine = true;
// sTime, sType, sLyric
for (int i = 0; i < lstLyricsItems.Count; i++)
{
sTime = lstLyricsItems[i].Item1;
sType = lstLyricsItems[i].Item2;
sLyric = lstLyricsItems[i].Item3;

if (sType == "text") // Do not add empty lyrics to a line ?
{

if (bStartLine)
{
if (sLyric.Length > 0 && sLyric.StartsWith(" "))
sLyric = sLyric.Remove(0, 1);
sLine = sTime + strSpaceBetween + sLyric; // time + lyric for the beginning of a line
sTimeLine = sTime + strSpaceBetween + sLyric;
bStartLine = false;
}
else
{
// Line continuation
sLine += sLyric; // only lyric for the continuation of a line
sTimeLine += sTime + strSpaceBetween + sLyric;
}
}
else
{

// Remove last space
if (sLine.Length > 0 && sLine.EndsWith(" "))
sLine = sLine.Remove(sLine.Length - 1, 1);

if (sTimeLine.Length > 0 && sTimeLine.EndsWith(" "))
sTimeLine = sTimeLine.Remove(sTimeLine.Length - 1, 1);


// Save current line
if (sLine != "")
{
// Add new line
lstLines.Add(sLine);
}

if (sTimeLine != "")
{
// Add new line
lstTimeLines.Add(sTimeLine);
}

// Reset all
bStartLine = true;
sLine = string.Empty;
sTimeLine = string.Empty;
}
}
// Save last line
if (sLine != "")
{
lstLines.Add(sLine);
}

// Save last line
if (sTimeLine != "")
{
// Remove last space
if (sTimeLine.Length > 0 && sTimeLine.EndsWith(" "))
sTimeLine = sTimeLine.Remove(sTimeLine.Length - 1, 1);
lstTimeLines.Add(sTimeLine);
}


#region deleteme
/*
// Store lyrics in lines with cuts
bStartLine = true;
// sTime, sType, sLyric
for (int i = 0; i < lstLyricsItems.Count; i++)
{
sTime = lstLyricsItems[i].Item1;
sType = lstLyricsItems[i].Item2;
sLyric = lstLyricsItems[i].Item3;
if (sType == "text") // Do not add empty lyrics to a line ?
{
// Length control: Can we add the next lyric to the current line ?
// Is it a cut inside a word ???????????????????????????????????????????????
if (bControlLength && (strPartialLine + sLyric).Length > MaxLength)
{
// if not: save current line and start a new one with this lyric
// Remove last space
if (sLine.Length > 0 && sLine.EndsWith(" "))
sLine = sLine.Remove(sLine.Length - 1, 1);
// Save current line
lrcs += sLine + cr;
// Start a new line
if (sLyric.Length > 0 && sLyric.StartsWith(" "))
sLyric = sLyric.Remove(0, 1);
sLine = sTime + strSpaceBetween + sLyric;
bStartLine = false;
strPartialLine = sLyric;
}
else
{
// No length control
strPartialLine += sLyric;
if (bStartLine)
{
if (sLyric.Length > 0 && sLyric.StartsWith(" "))
sLyric = sLyric.Remove(0, 1);
sLine = sTime + strSpaceBetween + sLyric; // time + lyric for the beginning of a line
bStartLine = false;
}
else
{
// Line continuation
sLine += sLyric; // only lyric for the continuation of a line
}
}
}
else
{
// Remove last space
if (sLine.Length > 0 && sLine.EndsWith(" "))
sLine = sLine.Remove(sLine.Length - 1, 1);
// Save current line
if (sLine != "")
{
// Add new line
lrcs += sLine + cr;
}
// Reset all
bStartLine = true;
sLine = string.Empty;
strPartialLine = string.Empty;
}
}
// Save last line
if (sLine != "")
{
lrcs += sLine + cr;
}
*/
#endregion deleteme

#endregion List of lines


#region List of lines cut

List<string[]> lstWords = new List<string[]>();
List<string[]> lstTimes = new List<string[]>();

string[] words;
string[] Times;
string removepattern = @"\[\d{2}[:]\d{2}[.]\d{3}\]";
string replace = @"";
for (int i = 0; i < lstTimeLines.Count; i++)
{
sTimeLine = lstTimeLines[i];
words = sTimeLine.Split(' ');
Times = new string[words.Length];
for (int j = 0; j < words.Length; j++)
{
Times[j] = words[j].Substring(0, 11);
words[j] = Regex.Replace(words[j], removepattern, replace);
}
lstWords.Add(words);
lstTimes.Add(Times);
}

// Store lyrics in lines
List<string> lstLines = Utilities.LyricsUtilities.GetLrcLines(lstLyricsItems, strSpaceBetween);

// Store timestamps + lyrics in lines
List<string> lstTimeLines = Utilities.LyricsUtilities.GetLrcTimeLines(lstLyricsItems, strSpaceBetween);

// Manage length
List<string> lstLinesCut = new List<string>();
strPartialLine = string.Empty;
sLine = string.Empty;
string[] ItemsW;
string[] ItemsT;
for (int i = 0; i < lstWords.Count; i++)
{
ItemsT = lstTimes[i];
ItemsW = lstWords[i];
sLine = string.Empty;

for (int j = 0; j < ItemsW.Count(); j++)
{
bStartLine = (j == 0);
sLyric = ItemsW[j];
sTime = ItemsT[j];

if ( !bStartLine && (strPartialLine + " " + sLyric).Length > MaxLength)
{
// Too long
// Remove last space
if (sLine.Length > 0 && sLine.EndsWith(" "))
sLine = sLine.Remove(sLine.Length - 1, 1);
lstLinesCut.Add(sLine);

// Restart a new line
sLine = sTime + sLyric + " ";
strPartialLine = sLyric + " ";

}
else
{
if (bStartLine)
{
sLine = sTime + sLyric + " ";
strPartialLine = sLyric + " ";
}
else
{
sLine += sLyric + " ";
strPartialLine += sLyric + " ";
}
}
}

// Remove last space
if (sLine.Length > 0 && sLine.EndsWith(" "))
sLine = sLine.Remove(sLine.Length - 1, 1);
lstLinesCut.Add(sLine);
sLine = string.Empty;
}

if (sLine != string.Empty)
{
// Remove last space
if (sLine.Length > 0 && sLine.EndsWith(" "))
sLine = sLine.Remove(sLine.Length - 1, 1);
lstLinesCut.Add(sLine);
}

#endregion List of lines cut
// Store lyrics by line and cut lines to MaxLength characters using lstTimeLines
List<string> lstLinesCut = Utilities.LyricsUtilities.GetLrcLinesCut(lstTimeLines, MaxLength);



#region send all to string
// Header
lrcs = string.Empty;
for (int i = 0; i < lstHeaderLines.Count; i++)
{
lrcs += lstHeaderLines[i] + cr;
}

// Lines
if (bControlLength)
{
for (int i = 0; i < lstLinesCut.Count; i++)
Expand Down
Loading

0 comments on commit 7b7a982

Please sign in to comment.