From b00c5c16c67701d5db76b45132e575af9c43243c Mon Sep 17 00:00:00 2001 From: Hamid Reza Mohammadi Date: Thu, 5 Nov 2020 10:14:59 +0330 Subject: [PATCH] #26 api to return narration verse sync --- .../Controllers/AudioNarrationController.cs | 18 ++++++++ .../ViewModels/NarrationVerseSync.cs | 23 ++++++++++ RMuseum/RMuseum.xml | 41 ++++++++++++++++++ RMuseum/Services/IAudioNarrationService.cs | 7 +++ .../Implementation/AudioNarrationService.cs | 43 +++++++++++++++++++ 5 files changed, 132 insertions(+) create mode 100644 RMuseum/Models/GanjoorAudio/ViewModels/NarrationVerseSync.cs diff --git a/RMuseum/Controllers/AudioNarrationController.cs b/RMuseum/Controllers/AudioNarrationController.cs index 1c7d31cc..556fc440 100644 --- a/RMuseum/Controllers/AudioNarrationController.cs +++ b/RMuseum/Controllers/AudioNarrationController.cs @@ -142,6 +142,24 @@ namespace RMuseum.Controllers return Ok(System.IO.File.ReadAllText(narration.Result.LocalXmlFilePath)); } + /// + /// Gets Verse Sync Information + /// + /// narration id + /// + [HttpGet("verses/{id}")] + [AllowAnonymous] + [ProducesResponseType((int)HttpStatusCode.OK, Type = typeof(string))] + [ProducesResponseType((int)HttpStatusCode.BadRequest, Type = typeof(string))] + public async Task GetPoemNarrationVerseSyncArray(int id) + { + RServiceResult verses = + await _audioService.GetPoemNarrationVerseSyncArray(id); + if (!string.IsNullOrEmpty(verses.ExceptionString)) + return BadRequest(verses.ExceptionString); + return Ok(verses.Result); + } + /// /// updates narration metadata /// diff --git a/RMuseum/Models/GanjoorAudio/ViewModels/NarrationVerseSync.cs b/RMuseum/Models/GanjoorAudio/ViewModels/NarrationVerseSync.cs new file mode 100644 index 00000000..f89413f7 --- /dev/null +++ b/RMuseum/Models/GanjoorAudio/ViewModels/NarrationVerseSync.cs @@ -0,0 +1,23 @@ +namespace RMuseum.Models.GanjoorAudio.ViewModels +{ + /// + /// Verse Sync Range + /// + public class NarrationVerseSync + { + /// + /// Verse Order + /// + public int VerseOrder { get; set; } + + /// + /// Verse Text + /// + public string VerseText { get; set; } + + /// + /// Audio Start in Milliseconds + /// + public int AudioStartMilliseconds { get; set; } + } +} diff --git a/RMuseum/RMuseum.xml b/RMuseum/RMuseum.xml index 8b56059e..6e529ca6 100644 --- a/RMuseum/RMuseum.xml +++ b/RMuseum/RMuseum.xml @@ -657,6 +657,13 @@ + + + Gets Verse Sync Information + + narration id + + updates narration metadata @@ -1989,6 +1996,26 @@ is default + + + Verse Sync Range + + + + + Verse Order + + + + + Verse Text + + + + + Audio Start in Milliseconds + + Moderation Result @@ -3959,6 +3986,13 @@ + + + Gets Verse Sync Range Information + + narration id + + updates metadata for narration @@ -4763,6 +4797,13 @@ + + + Gets Verse Sync Range Information + + narration id + + updates metadata for narration diff --git a/RMuseum/Services/IAudioNarrationService.cs b/RMuseum/Services/IAudioNarrationService.cs index 790cf9ae..4708df65 100644 --- a/RMuseum/Services/IAudioNarrationService.cs +++ b/RMuseum/Services/IAudioNarrationService.cs @@ -30,6 +30,13 @@ namespace RMuseum.Services /// Task> Get(int id); + /// + /// Gets Verse Sync Range Information + /// + /// narration id + /// + Task> GetPoemNarrationVerseSyncArray(int id); + /// /// updates metadata for narration /// diff --git a/RMuseum/Services/Implementation/AudioNarrationService.cs b/RMuseum/Services/Implementation/AudioNarrationService.cs index 99c0ee4d..24ac422d 100644 --- a/RMuseum/Services/Implementation/AudioNarrationService.cs +++ b/RMuseum/Services/Implementation/AudioNarrationService.cs @@ -19,6 +19,7 @@ using System.Data; using System.IO; using System.Linq; using System.Threading.Tasks; +using System.Xml.Linq; namespace RMuseum.Services.Implementation { @@ -91,6 +92,48 @@ namespace RMuseum.Services.Implementation } } + /// + /// Gets Verse Sync Range Information + /// + /// narration id + /// + public async Task> GetPoemNarrationVerseSyncArray(int id) + { + try + { + var narration = await _context.AudioFiles.Where(a => a.Id == id).SingleOrDefaultAsync(); + var verses = await _context.GanjoorVerses.Where(v => v.PoemId == narration.GanjoorPostId).ToListAsync(); + + string xml = File.ReadAllText(narration.LocalXmlFilePath); + + List verseSyncs = new List(); + + XElement elObject = XDocument.Parse(xml).Root; + foreach (var syncInfo in elObject.Element("PoemAudio").Element("SyncArray").Elements("SyncInfo")) + { + int verseOrder = int.Parse(syncInfo.Element("VerseOrder").Value); + if (verseOrder < 0) //this happens, seems to be a bug, I did not trace it yet + verseOrder = 0; + var verse = verses.Where(v => v.VOrder == verseOrder).Single(); + if(verse != null) + { + verseSyncs.Add(new NarrationVerseSync() + { + VerseOrder = verseOrder, + VerseText = verse.Text, + AudioStartMilliseconds = int.Parse(syncInfo.Element("AudioMiliseconds").Value) + }); + } + } + + return new RServiceResult(verseSyncs.ToArray()); + } + catch (Exception exp) + { + return new RServiceResult(null, exp.ToString()); + } + } + /// /// updates metadata for narration ///