Skip to content
This repository has been archived by the owner on Mar 23, 2024. It is now read-only.

Commit

Permalink
Merge pull request #28 from kc3hack/feat/#27
Browse files Browse the repository at this point in the history
音声合成周りのインターフェース・モック作成
  • Loading branch information
miyaji255 authored Feb 22, 2024
2 parents f6b55e6 + 0bd0341 commit 31e6266
Show file tree
Hide file tree
Showing 12 changed files with 120 additions and 11 deletions.
13 changes: 13 additions & 0 deletions KoeBook.Core/Contracts/Services/ISoundGenerationSelectorService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using KoeBook.Core.Models;

namespace KoeBook.Core.Contracts.Services;

public interface ISoundGenerationSelectorService
{
/// <summary>
/// サウンドモデル・スタイルの一覧
/// </summary>
public IReadOnlyList<SoundModel> Models { get; }

public ValueTask InitializeAsync(CancellationToken cancellationToken);
}
15 changes: 15 additions & 0 deletions KoeBook.Core/Contracts/Services/ISoundGenerationService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using KoeBook.Core.Models;

namespace KoeBook.Core.Contracts.Services;

public interface ISoundGenerationService
{
/// <summary>
/// 1文の音声を生成します
/// </summary>
/// <param name="scriptLine"></param>
/// <param name="bookOptions"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
ValueTask<byte[]> GenerateLineSoundAsync(ScriptLine scriptLine, BookOptions bookOptions, CancellationToken cancellationToken);
}
2 changes: 1 addition & 1 deletion KoeBook.Core/KoeBook.Core.csproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<RootNamespace>KoeBook.Core</RootNamespace>
Expand Down
5 changes: 5 additions & 0 deletions KoeBook.Core/Models/SoundModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
namespace KoeBook.Core.Models;

public record SoundModel(
string name,
IReadOnlyList<string> styles);
22 changes: 22 additions & 0 deletions KoeBook.Core/Services/Mocks/SoundGenerationSelectorServiceMock.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using KoeBook.Core.Contracts.Services;
using KoeBook.Core.Models;

namespace KoeBook.Core.Services.Mocks;

internal class SoundGenerationSelectorServiceMock : ISoundGenerationSelectorService
{
public IReadOnlyList<SoundModel> Models { get; private set; } = [];

public async ValueTask InitializeAsync(CancellationToken cancellationToken)
{
await Task.Delay(1000, cancellationToken).ConfigureAwait(false);
Models = [
new SoundModel("青年1", ["neutral", "laughing", "happy", "sad", "cry", "surprised", "angry"]),
new SoundModel("青年2", ["neutral", "laughing", "happy", "sad", "cry", "surprised", "angry"]),
new SoundModel("女性1", ["neutral", "laughing", "happy", "sad", "cry", "surprised", "angry"]),
new SoundModel("女性2", ["neutral", "laughing", "happy", "sad", "cry", "surprised", "angry"]),
new SoundModel("ナレーション (男性)", ["narration"]),
new SoundModel("ナレーション (女性)", ["narration"]),
];
}
}
17 changes: 17 additions & 0 deletions KoeBook.Core/Services/Mocks/SoundGenerationServiceMock.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using KoeBook.Core.Contracts.Services;
using KoeBook.Core.Models;

namespace KoeBook.Core.Services.Mocks;

public class SoundGenerationServiceMock : ISoundGenerationService
{
public async ValueTask<byte[]> GenerateLineSoundAsync(ScriptLine scriptLine, BookOptions bookOptions, CancellationToken cancellationToken)
{
await Task.Delay(1000, cancellationToken).ConfigureAwait(false);
// 適当なバイト列を返す
var random = new Random();
var buffer = new byte[44100 * 2];
random.NextBytes(buffer);
return buffer;
}
}
5 changes: 4 additions & 1 deletion KoeBook.Core/Services/SercretSettingsService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@ public SecretSettingsService()
return Task.Run(async () =>
{
cancellationToken.ThrowIfCancellationRequested();
var data = await File.ReadAllBytesAsync(Path.Combine(folderPath, "alt"), cancellationToken).ConfigureAwait(false);
var path = Path.Combine(folderPath, "alt");
if (!File.Exists(path))
return null;
var data = await File.ReadAllBytesAsync(path, cancellationToken).ConfigureAwait(false);
if (data is null)
return null;

Expand Down
11 changes: 9 additions & 2 deletions KoeBook/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
using KoeBook.Contracts.Services;
using KoeBook.Core.Contracts.Services;
using KoeBook.Core.Services;
using KoeBook.Helpers;
using KoeBook.Core.Services.Mocks;
using KoeBook.Models;
using KoeBook.Notifications;
using KoeBook.Services;
using KoeBook.ViewModels;
using KoeBook.Views;

using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.UI.Xaml;
Expand Down Expand Up @@ -86,6 +86,13 @@ public App()

// Configuration
services.Configure<LocalSettingsOptions>(context.Configuration.GetSection(nameof(LocalSettingsOptions)));

// Core Services Mock
var mockOptions = context.Configuration.GetSection(nameof(MockOptions)).Get<MockOptions>();
if (mockOptions.ISoundGenerationSelectorService.HasValue && mockOptions.ISoundGenerationSelectorService.Value)
services.AddSingleton<ISoundGenerationSelectorService, SoundGenerationSelectorServiceMock>();
if (mockOptions.ISoundGenerationService.HasValue && mockOptions.ISoundGenerationService.Value)
services.AddSingleton<ISoundGenerationService, SoundGenerationServiceMock>();
})
.Build();

Expand Down
4 changes: 0 additions & 4 deletions KoeBook/KoeBook.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,6 @@
<ItemGroup Condition="'$(DisableMsixProjectCapabilityAddedByProject)'!='true' and '$(EnableMsixTooling)'=='true'">
<ProjectCapability Include="Msix" />
</ItemGroup>

<ItemGroup>
<Folder Include="Services\CoreMocks\" />
</ItemGroup>

<PropertyGroup Condition="'$(DisableHasPackageAndPublishMenuAddedByProject)'!='true' and '$(EnableMsixTooling)'=='true'">
<HasPackageAndPublishMenu>true</HasPackageAndPublishMenu>
Expand Down
8 changes: 8 additions & 0 deletions KoeBook/Models/MockOptions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace KoeBook.Models;

internal class MockOptions
{
public bool? ISoundGenerationSelectorService { get; set; }

public bool? ISoundGenerationService { get; set; }
}
19 changes: 19 additions & 0 deletions KoeBook/Services/CoreMocks/SoundGenerationSelectorServiceMock.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using KoeBook.Core.Contracts.Services;
using KoeBook.Core.Models;

namespace KoeBook.Core.Services.Mocks;

internal class SoundGenerationSelectorServiceMock : ISoundGenerationSelectorService
{
public IReadOnlyList<SoundModel> Models { get; private set; } = [];

public async ValueTask InitializeAsync(CancellationToken cancellationToken)
{
await Task.Delay(1000, cancellationToken).ConfigureAwait(false);
Models = [
new SoundModel("青年1", ["neutral", "laughing", "happy", "sad", "cry", "surprised", "angry"]),
new SoundModel("青年2", ["neutral", "laughing", "happy", "sad", "cry", "surprised", "angry"]),
new SoundModel("王", ["narration", "neutral", "laughing", "happy", "sad", "cry", "surprised", "angry"]),
];
}
}
10 changes: 7 additions & 3 deletions KoeBook/appsettings.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
{
{
"LocalSettingsOptions": {
"ApplicationDataFolder": "KoeBook/ApplicationData",
"LocalSettingsFile": "LocalSettings.json"
"ApplicationDataFolder": "KoeBook/ApplicationData",
"LocalSettingsFile": "LocalSettings.json"
},
"MockOptions": {
"ISoundGenerationSelectorService": false,
"ISoundGenerationService": false
}
}

0 comments on commit 31e6266

Please sign in to comment.