Skip to content

Commit

Permalink
#3 merging synchronized files
Browse files Browse the repository at this point in the history
  • Loading branch information
hrmoh committed Mar 22, 2023
1 parent c87dd90 commit fb35c1a
Show file tree
Hide file tree
Showing 3 changed files with 291 additions and 8 deletions.
237 changes: 237 additions & 0 deletions gsync2vid/PoemAudioListProcessor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,237 @@
using System;
using System.Collections.Generic;
using System.Xml;
using System.Reflection;
using System.IO;

namespace ganjoor
{
/// <summary>
/// پردازشگر ذخیره و بارگذاری لیستهای اشعار
/// </summary>
public class PoemAudioListProcessor
{

/// <summary>
/// ذخیره در فایل
/// </summary>
/// <param name="FileName"></param>
/// <param name="poemAudio"></param>
/// <param name="oldVersion"></param>
/// <returns></returns>
public static bool Save(string FileName, PoemAudio poemAudio, bool oldVersion)
{
List<PoemAudio> lst = new List<PoemAudio>();
lst.Add(poemAudio);
return Save(FileName, lst, oldVersion);
}

/// <summary>
/// ذخیره در فایل
/// </summary>
/// <param name="FileName"></param>
/// <param name="List"></param>
/// <param name="oldVersion"></param>
/// <returns></returns>
public static bool Save(string FileName, List<PoemAudio> List, bool oldVersion)
{
XmlDocument doc = new XmlDocument();
XmlNode poemAudioRootNode = doc.CreateNode(XmlNodeType.Element, "DesktopGanjoorPoemAudioList", "");
doc.AppendChild(poemAudioRootNode);
foreach (PoemAudio audio in List)
{
XmlNode poemAudioNode = doc.CreateNode(XmlNodeType.Element, "PoemAudio", "");
foreach (PropertyInfo prop in typeof(PoemAudio).GetProperties())
{
if (!prop.CanWrite)
continue;
bool ignoreProp = false;
XmlNode propNode = doc.CreateNode(XmlNodeType.Element, prop.Name, "");
if (prop.PropertyType == typeof(string))
{
string value = prop.GetValue(audio, null) == null ? "" : prop.GetValue(audio, null).ToString();
if (string.IsNullOrEmpty(value))
{
ignoreProp = true;
}
else
propNode.InnerText = value;
}
else
if (prop.PropertyType == typeof(Int32))
{
int value = Convert.ToInt32(prop.GetValue(audio, null));
if (value == 0)
{
ignoreProp = true;
}
else
propNode.InnerText = value.ToString();
}
else
if (prop.PropertyType == typeof(bool))
{
bool value = Convert.ToBoolean(prop.GetValue(audio, null));
if (!value)
{
ignoreProp = true;
}
else
propNode.InnerText = value.ToString();
}
else
if (prop.PropertyType == typeof(Guid))
{
string value = prop.GetValue(audio, null).ToString();
if (string.IsNullOrEmpty(value))
{
ignoreProp = true;
}
else
propNode.InnerText = value;
}
else
{
ignoreProp = true;
}

if (!ignoreProp)
poemAudioNode.AppendChild(propNode);
}
//رفع اشکال نسخه قدیمی NAudio
XmlNode bugfixNode = doc.CreateNode(XmlNodeType.Element, "OneSecondBugFix", "");
bugfixNode.InnerText = "1000";
poemAudioNode.AppendChild(bugfixNode);

XmlNode syncInfoArrayNode = doc.CreateNode(XmlNodeType.Element, "SyncArray", "");
if (audio.SyncArray != null)
{

foreach (PoemAudio.SyncInfo info in audio.SyncArray)
{
XmlNode syncInfoNode = doc.CreateNode(XmlNodeType.Element, "SyncInfo", "");

XmlNode vOrderNode = doc.CreateNode(XmlNodeType.Element, "VerseOrder", "");
vOrderNode.InnerText = info.VerseOrder.ToString();
syncInfoNode.AppendChild(vOrderNode);

XmlNode vAudioMiliseconds = doc.CreateNode(XmlNodeType.Element, "AudioMiliseconds", "");
if (oldVersion)
{
vAudioMiliseconds.InnerText = (info.AudioMiliseconds/2).ToString();
}
else
vAudioMiliseconds.InnerText = info.AudioMiliseconds.ToString();
syncInfoNode.AppendChild(vAudioMiliseconds);


syncInfoArrayNode.AppendChild(syncInfoNode);
}
}

poemAudioNode.AppendChild(syncInfoArrayNode);
poemAudioRootNode.AppendChild(poemAudioNode);
}
try
{
doc.Save(FileName);
return true;
}
catch
{

return false;
}
}


/// <summary>
/// بارگذاری از فایل
/// </summary>
/// <param name="FileName"></param>
/// <returns></returns>
public static List<PoemAudio> Load(string FileName)
{
List<PoemAudio> lstPoemAudio = new List<PoemAudio>();

XmlDocument doc = new XmlDocument();
doc.LoadXml(File.ReadAllText(FileName));

//Collect List:
XmlNodeList poemAudioNodes = doc.GetElementsByTagName("PoemAudio");
foreach (XmlNode poemAudioNode in poemAudioNodes)
{
PoemAudio poemAudioInfo = new PoemAudio();
poemAudioInfo.IsDirectlyDownloadable = false;
poemAudioInfo.IsUploaded = false;
foreach (XmlNode Node in poemAudioNode.ChildNodes)
{
switch (Node.Name)
{
case "PoemId":
poemAudioInfo.PoemId = Convert.ToInt32(Node.InnerText);
break;
case "Id":
poemAudioInfo.Id = Convert.ToInt32(Node.InnerText);
break;
case "DownloadUrl":
poemAudioInfo.DownloadUrl = Node.InnerText;
break;
case "FilePath":
poemAudioInfo.FilePath = Node.InnerText;
break;
case "Description":
poemAudioInfo.Description = Node.InnerText;
break;
case "SyncGuid":
poemAudioInfo.SyncGuid = Guid.Parse(Node.InnerText);
break;
case "FileCheckSum":
poemAudioInfo.FileCheckSum = Node.InnerText;
break;
case "IsDirectlyDownloadable":
poemAudioInfo.IsDirectlyDownloadable = Convert.ToBoolean(Node.InnerText);
break;
case "IsUploaded":
poemAudioInfo.IsDirectlyDownloadable = Convert.ToBoolean(Node.InnerText);
break;
case "SyncArray":
{
XmlNodeList syncInfoNodeList = Node.SelectNodes("SyncInfo");
if (syncInfoNodeList != null)
{
List<PoemAudio.SyncInfo> lstSyncInfo = new List<PoemAudio.SyncInfo>();

foreach (XmlNode nodeSyncIndo in syncInfoNodeList)
{

lstSyncInfo.Add(
new PoemAudio.SyncInfo()
{
VerseOrder = Convert.ToInt32(nodeSyncIndo.SelectSingleNode("VerseOrder").InnerText),
AudioMiliseconds = Convert.ToInt32(nodeSyncIndo.SelectSingleNode("AudioMiliseconds").InnerText),
}
);
}

if (lstSyncInfo.Count > 0)
{
poemAudioInfo.SyncArray = lstSyncInfo.ToArray();
}
}
}
break;

}

}
lstPoemAudio.Add(poemAudioInfo);
}


return lstPoemAudio;
}


}
}
61 changes: 53 additions & 8 deletions gsync2vid/RecitationsMerger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using System;
using System.IO;
using gsync2vid.Properties;
using DocumentFormat.OpenXml.Presentation;

namespace gsync2vid
{
Expand Down Expand Up @@ -65,7 +66,7 @@ private void btnAdd_Click(object sender, System.EventArgs e)
}
}

private void btnMerge_Click(object sender, System.EventArgs e)
private void btnMerge_Click(object sender, EventArgs e)
{
if(lstRecitations.Items.Count < 2)
{
Expand All @@ -77,10 +78,56 @@ private void btnMerge_Click(object sender, System.EventArgs e)
dlg.Filter = "MP3 Files (*.mp3)|*.mp3";
if(dlg.ShowDialog(this) == DialogResult.OK)
{
int addInMiliseconds = 0;
PoemAudio firstItem = lstRecitations.Items[0] as PoemAudio;
PoemAudio merged = new PoemAudio()
{
Id = firstItem.Id,
PoemId = PoemId,
FilePath = dlg.FileName,
SyncGuid = Guid.NewGuid(),
Description = firstItem.Description,
DownloadUrl = "",
IsDirectlyDownloadable = false,
IsUploaded = false
};
List<PoemAudio.SyncInfo> syncs = new List<PoemAudio.SyncInfo>();
foreach (PoemAudio item in lstRecitations.Items)
{
PoemAudioPlayer player = new PoemAudioPlayer();
if (!player.BeginPlayback(item))
{
GMessageBox.SayError($"تلاش برای دسترسی و پخش فایل صوتی موفق نبود - {item}");
return;
}
else
{
player.StopPlayBack();

int nLen = item.SyncArray.Length;
if (item.SyncArray[nLen - 1].AudioMiliseconds > player.TotalTimeInMiliseconds)
{
for (int i = 0; i < nLen; i++)
{
item.SyncArray[i].AudioMiliseconds = item.SyncArray[i].AudioMiliseconds / 2;
}
}

for (int i = 0; i < nLen; i++)
{
item.SyncArray[i].AudioMiliseconds += addInMiliseconds;
syncs.Add(item.SyncArray[i]);
}


addInMiliseconds += player.TotalTimeInMiliseconds;
}
}
merged.SyncArray = syncs.ToArray();
List<string> lines = new List<string>();
foreach (PoemAudio path in lstRecitations.Items)
foreach (PoemAudio item in lstRecitations.Items)
{
lines.Add($"file '{path.FilePath}'");
lines.Add($"file '{item.FilePath}'");
}

string strTempList = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString() + ".txt");
Expand Down Expand Up @@ -109,11 +156,9 @@ private void btnMerge_Click(object sender, System.EventArgs e)

if (File.Exists(outFileName))
{
if (MessageBox.Show("آیا مایلید فایل خروجی را مشاهده کنید؟", "تأییدیه", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1,
MessageBoxOptions.RtlReading | MessageBoxOptions.RightAlign) == DialogResult.Yes)
{
Process.Start(outFileName);
}
merged.FileCheckSum = PoemAudio.ComputeCheckSum(outFileName);
PoemAudioListProcessor.Save(Path.Combine(Path.GetDirectoryName(outFileName), Path.GetFileNameWithoutExtension(outFileName) + ".xml"), merged, false);
GMessageBox.Announce("ذخیره شد.");
}
else
{
Expand Down
1 change: 1 addition & 0 deletions gsync2vid/gsync2vid.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@
<Compile Include="ObjectPropertiesEditor.Designer.cs">
<DependentUpon>ObjectPropertiesEditor.cs</DependentUpon>
</Compile>
<Compile Include="PoemAudioListProcessor.cs" />
<Compile Include="PowerPointGenerator.cs" />
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
Expand Down

0 comments on commit fb35c1a

Please sign in to comment.