From acccb503d942b9a0ed09e81e40036834b6c180fd Mon Sep 17 00:00:00 2001 From: TakenPt Date: Thu, 2 May 2024 23:12:43 +0900 Subject: [PATCH] =?UTF-8?q?#51=20=E7=94=9F=E6=88=90=E3=83=95=E3=82=A1?= =?UTF-8?q?=E3=82=A4=E3=83=AB=E3=81=AE=E5=86=8D=E5=88=A9=E7=94=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Services/EpubGenerateService.cs | 7 ++++--- KoeBook.Core/Models/Audio.cs | 21 +++++-------------- 2 files changed, 9 insertions(+), 19 deletions(-) diff --git a/Epub/KoeBook.Epub/Services/EpubGenerateService.cs b/Epub/KoeBook.Epub/Services/EpubGenerateService.cs index 9b5f58a..4c91af4 100644 --- a/Epub/KoeBook.Epub/Services/EpubGenerateService.cs +++ b/Epub/KoeBook.Epub/Services/EpubGenerateService.cs @@ -18,17 +18,18 @@ public async ValueTask GenerateEpubAsync(BookScripts bookScripts, string cancellationToken.ThrowIfCancellationRequested(); var document = _documentStoreService.Documents.Single(d => d.Id == bookScripts.BookProperties.Id); - var tmpMp3Path = Path.Combine(tempDirectory, "temp.mp3"); - foreach (var scriptLine in bookScripts.ScriptLines) + for (var i = 0; i < bookScripts.ScriptLines.Length; i++) { + var scriptLine = bookScripts.ScriptLines[i]; var wavData = await _soundGenerationService.GenerateLineSoundAsync(scriptLine, bookScripts.Options, cancellationToken).ConfigureAwait(false); var ms = new MemoryStream(); ms.Write(wavData); ms.Position = 0; using var reader = new WaveFileReader(ms); + var tmpMp3Path = Path.Combine(tempDirectory, $"{document.Title}{i}.mp3"); MediaFoundationEncoder.EncodeToMp3(reader, tmpMp3Path); - scriptLine.Audio = new Audio(File.ReadAllBytes(tmpMp3Path)); + scriptLine.Audio = new Audio(reader.TotalTime, tmpMp3Path); } if (await _createService.TryCreateEpubAsync(document, tempDirectory, cancellationToken).ConfigureAwait(false)) diff --git a/KoeBook.Core/Models/Audio.cs b/KoeBook.Core/Models/Audio.cs index 1afda6d..d100060 100644 --- a/KoeBook.Core/Models/Audio.cs +++ b/KoeBook.Core/Models/Audio.cs @@ -2,24 +2,13 @@ namespace KoeBook.Epub.Models; -public sealed class Audio +public sealed class Audio(TimeSpan totalTIme, string tempFilePath) { - public TimeSpan TotalTime { get; } - private readonly byte[] _mp3Data; + public TimeSpan TotalTime { get; } = totalTIme; + public string TempFilePath { get; } = tempFilePath; - public Audio(byte[] mp3Data) + public FileStream GetStream() { - _mp3Data = mp3Data; - using var ms = new MemoryStream(); - ms.Write(_mp3Data.AsSpan()); - ms.Flush(); - ms.Position = 0; - using var reader = new Mp3FileReader(ms); - TotalTime = reader.TotalTime; - } - - public MemoryStream GetStream() - { - return new MemoryStream(_mp3Data); + return File.OpenRead(TempFilePath); } }