Skip to content

Commit

Permalink
#51 生成ファイルの再利用
Browse files Browse the repository at this point in the history
  • Loading branch information
TakenPt committed May 2, 2024
1 parent cbbbb66 commit acccb50
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 19 deletions.
7 changes: 4 additions & 3 deletions Epub/KoeBook.Epub/Services/EpubGenerateService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,18 @@ public async ValueTask<string> 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))
Expand Down
21 changes: 5 additions & 16 deletions KoeBook.Core/Models/Audio.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

0 comments on commit acccb50

Please sign in to comment.