Skip to content

Commit 98c629d

Browse files
committed
UTF-8文字列と勘違いしていたため、VoiceModelIdが壊れていた
uuidみたいに他の人が扱っていなければinterface上変わりないのでbreakingにはならない…はず…
1 parent ead894e commit 98c629d

File tree

5 files changed

+49
-34
lines changed

5 files changed

+49
-34
lines changed

src/VoicevoxCoreSharp.Core.Unity/Runtime/Script/Synthesizer.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ public ResultCode UnloadVoiceModel(string modelId)
7272
{
7373
unsafe
7474
{
75-
fixed (byte* ptr = System.Text.Encoding.UTF8.GetBytes(modelId))
75+
fixed (byte* ptr = NativeUuid.ToUUIDv4ByteArray(modelId))
7676
{
7777
return CoreUnsafe.voicevox_synthesizer_unload_voice_model((VoicevoxSynthesizer*)Handle, ptr).FromNative();
7878
}
@@ -94,7 +94,7 @@ public bool IsLoadedVoiceModel(string modelId)
9494
{
9595
unsafe
9696
{
97-
fixed (byte* ptr = System.Text.Encoding.UTF8.GetBytes(modelId))
97+
fixed (byte* ptr = NativeUuid.ToUUIDv4ByteArray(modelId))
9898
{
9999
return CoreUnsafe.voicevox_synthesizer_is_loaded_voice_model((VoicevoxSynthesizer*)Handle, ptr);
100100
}

src/VoicevoxCoreSharp.Core.Unity/Runtime/Script/VoiceModel.cs

Lines changed: 24 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,22 @@ public class VoiceModelFile : IDisposable
3434
internal VoiceModelFileHandle Handle { get; private set; }
3535
private bool _disposed = false;
3636

37-
private unsafe VoiceModelFile(VoicevoxVoiceModelFile* modelHandle)
37+
public string Id { get; private set; }
38+
39+
public string MetasJson { get; private set; }
40+
41+
private VoiceModelFile()
42+
{
43+
Handle = new VoiceModelFileHandle(IntPtr.Zero);
44+
Id = string.Empty;
45+
MetasJson = string.Empty;
46+
}
47+
48+
private unsafe VoiceModelFile(VoicevoxVoiceModelFile* modelHandle, string id, string metasJson)
3849
{
3950
Handle = new VoiceModelFileHandle(new IntPtr(modelHandle));
51+
Id = id;
52+
MetasJson = metasJson;
4053
}
4154

4255
[Obsolete("Use VoiceModelFile.Open(string modelPath, out VoiceModelFile voiceModel) instead.")]
@@ -55,44 +68,26 @@ public static ResultCode Open(string modelPath, out VoiceModelFile voiceModel)
5568
var result = CoreUnsafe.voicevox_voice_model_file_open(ptr, &p).FromNative();
5669
if (result == ResultCode.RESULT_OK)
5770
{
58-
voiceModel = new VoiceModelFile(p);
71+
byte* idPtr = stackalloc byte[16];
72+
CoreUnsafe.voicevox_voice_model_file_id(p, idPtr);
73+
var id = NativeUuid.ToUUIDv4(idPtr);
74+
75+
var jsonPtr = CoreUnsafe.voicevox_voice_model_file_create_metas_json(p);
76+
var json = StringConvertCompat.ToUTF8String(jsonPtr);
77+
CoreUnsafe.voicevox_json_free(jsonPtr);
78+
79+
voiceModel = new VoiceModelFile(p, id, json);
5980
}
6081
else
6182
{
62-
voiceModel = new VoiceModelFile(null);
83+
voiceModel = new VoiceModelFile();
6384
}
6485

6586
return result;
6687
}
6788
}
6889
}
6990

70-
public string Id
71-
{
72-
get
73-
{
74-
unsafe
75-
{
76-
byte* ptr = stackalloc byte[16];
77-
CoreUnsafe.voicevox_voice_model_file_id((VoicevoxVoiceModelFile*)Handle, ptr);
78-
return StringConvertCompat.ToUTF8String(ptr);
79-
}
80-
}
81-
}
82-
83-
public string MetasJson
84-
{
85-
get
86-
{
87-
unsafe
88-
{
89-
var ptr = CoreUnsafe.voicevox_voice_model_file_create_metas_json((VoicevoxVoiceModelFile*)Handle);
90-
var json = StringConvertCompat.ToUTF8String(ptr);
91-
CoreUnsafe.voicevox_json_free(ptr);
92-
return json;
93-
}
94-
}
95-
}
9691

9792
public void Dispose()
9893
{

src/VoicevoxCoreSharp.Core/Synthesizer.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ public ResultCode UnloadVoiceModel(string modelId)
7272
{
7373
unsafe
7474
{
75-
fixed (byte* ptr = System.Text.Encoding.UTF8.GetBytes(modelId))
75+
fixed (byte* ptr = NativeUuid.ToUUIDv4ByteArray(modelId))
7676
{
7777
return CoreUnsafe.voicevox_synthesizer_unload_voice_model((VoicevoxSynthesizer*)Handle, ptr).FromNative();
7878
}
@@ -94,7 +94,7 @@ public bool IsLoadedVoiceModel(string modelId)
9494
{
9595
unsafe
9696
{
97-
fixed (byte* ptr = System.Text.Encoding.UTF8.GetBytes(modelId))
97+
fixed (byte* ptr = NativeUuid.ToUUIDv4ByteArray(modelId))
9898
{
9999
return CoreUnsafe.voicevox_synthesizer_is_loaded_voice_model((VoicevoxSynthesizer*)Handle, ptr);
100100
}

src/VoicevoxCoreSharp.Core/VoiceModel.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ public string Id
7575
{
7676
byte* ptr = stackalloc byte[16];
7777
CoreUnsafe.voicevox_voice_model_file_id((VoicevoxVoiceModelFile*)Handle, ptr);
78-
return StringConvertCompat.ToUTF8String(ptr);
78+
return NativeUuid.ToUUIDv4(ptr);
7979
}
8080
}
8181
}

tests/VoicevoxCoreSharp.Core.Tests/SynthesizerTest.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,5 +33,25 @@ public void Tts()
3333
Assert.True(wavLength > 0);
3434
Assert.NotNull(wav);
3535
}
36+
37+
[Fact]
38+
public void VoiceModelLoaded()
39+
{
40+
OpenJtalk.New(Consts.OpenJTalkDictDir, out var openJtalk);
41+
var initializeOptions = InitializeOptions.Default();
42+
var onnxruntimeOptions = new LoadOnnxruntimeOptions(Path.Join(AppContext.BaseDirectory, Helper.GetOnnxruntimeAssemblyName()));
43+
if (Onnxruntime.LoadOnce(onnxruntimeOptions, out var onnruntime) != ResultCode.RESULT_OK)
44+
{
45+
Assert.Fail("Failed to initialize onnxruntime");
46+
}
47+
Synthesizer.New(onnruntime, openJtalk, initializeOptions, out var synthesizer);
48+
49+
VoiceModelFile.Open(Consts.SampleVoiceModel, out var voiceModel);
50+
synthesizer.LoadVoiceModel(voiceModel);
51+
52+
Assert.True(synthesizer.IsLoadedVoiceModel(voiceModel.Id));
53+
synthesizer.UnloadVoiceModel(voiceModel.Id);
54+
Assert.False(synthesizer.IsLoadedVoiceModel(voiceModel.Id));
55+
}
3656
}
3757
}

0 commit comments

Comments
 (0)