오디오재생함수추가

This commit is contained in:
ChiKyun Kim
2025-12-10 13:36:35 +09:00
parent 868fa2deec
commit af280d7b27
7 changed files with 112 additions and 24 deletions

Submodule Cs_HMI/SubProject/CommUtil updated: ed05439991...632b087c5b

Submodule Cs_HMI/SubProject/EnigProtocol updated: 4f360f33a7...8877cb1a9d

View File

@@ -0,0 +1,3 @@
{
"dotnet.preferCSharpExtension": true
}

View File

@@ -2,6 +2,7 @@ using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
namespace Supertonic
{
@@ -11,13 +12,14 @@ namespace Supertonic
{
public bool UseGpu { get; set; } = false;
public string OnnxDir { get; set; } = "assets/onnx";
public string BaseDir { get; set; } = "";
public int TotalStep { get; set; } = 5;
public float Speed { get; set; } = 1.05f;
public int NTest { get; set; } = 4;
public List<string> VoiceStyle { get; set; } = new List<string> { "assets/voice_styles/M1.json" };
public List<string> VoiceStyle { get; set; } = new List<string> { "assets/voice_styles/M2.json" };
public List<string> Text { get; set; } = new List<string>
{
"This morning, I took a walk in the park, and the sound of the birds and the breeze was so pleasant that I stopped for a long time just to listen."
"hello , 안녕하세요"
};
public string SaveDir { get; set; } = "results";
public bool Batch { get; set; } = false;
@@ -58,13 +60,16 @@ namespace Supertonic
case "--save-dir" when i + 1 < args.Length:
result.SaveDir = args[++i];
break;
case "--base-dir" when i + 1 < args.Length:
result.BaseDir = args[++i];
break;
}
}
return result;
}
static void Main(string[] args)
static async Task Main(string[] args)
{
Console.WriteLine("=== TTS Inference with ONNX Runtime (C#) ===\n");
@@ -86,10 +91,15 @@ namespace Supertonic
int bsz = voiceStylePaths.Count;
// --- 2. Load Text to Speech --- //
var textToSpeech = Helper.LoadTextToSpeech(parsedArgs.OnnxDir, parsedArgs.UseGpu);
var onnxdir = System.IO.Path.Combine(parsedArgs.BaseDir, parsedArgs.OnnxDir);
var textToSpeech = Helper.LoadTextToSpeech(onnxdir, parsedArgs.UseGpu);
Console.WriteLine();
// --- 3. Load Voice Style --- //
for(int i = 0; i < voiceStylePaths.Count;i++)
{
voiceStylePaths[i] = Path.Combine(parsedArgs.BaseDir, voiceStylePaths[i]);
}
var style = Helper.LoadVoiceStyle(voiceStylePaths, verbose: true);
// --- 4. Synthesize speech --- //
@@ -114,6 +124,8 @@ namespace Supertonic
Directory.CreateDirectory(saveDir);
}
var playbackTasks = new List<Task>();
for (int b = 0; b < bsz; b++)
{
string fname = $"{Helper.SanitizeFilename(textList[b], 20)}_{n + 1}.wav";
@@ -125,7 +137,16 @@ namespace Supertonic
string outputPath = Path.Combine(saveDir, fname);
Helper.WriteWavFile(outputPath, wavOut, textToSpeech.SampleRate);
Console.WriteLine($"Saved: {outputPath}");
// 비동기로 오디오 재생 (파일 저장과 동시에)
Console.WriteLine($"Playing audio [{b + 1}/{bsz}]...");
var playTask = Helper.PlayAudioAsync(wavOut, textToSpeech.SampleRate);
playbackTasks.Add(playTask);
}
// 모든 재생이 완료될 때까지 대기
await Task.WhenAll(playbackTasks);
Console.WriteLine("Playback completed.");
}
Console.WriteLine("\n=== Synthesis completed successfully! ===");

View File

@@ -5,6 +5,8 @@ using System.Linq;
using System.Text;
using System.Text.Json;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Media;
using Microsoft.ML.OnnxRuntime;
using Microsoft.ML.OnnxRuntime.Tensors;
@@ -877,5 +879,66 @@ namespace Supertonic
return chunks;
}
// ============================================================================
// Audio Playback
// ============================================================================
/// <summary>
/// 오디오를 스피커로 비동기 재생합니다.
/// </summary>
/// <param name="audioData">float 배열 형태의 오디오 데이터 (-1.0 ~ 1.0)</param>
/// <param name="sampleRate">샘플레이트 (예: 16000, 22050, 44100)</param>
public static async Task PlayAudioAsync(float[] audioData, int sampleRate)
{
await Task.Run(() =>
{
try
{
// 임시 WAV 파일 생성
string tempWavFile = Path.Combine(Path.GetTempPath(), $"temp_audio_{Guid.NewGuid()}.wav");
WriteWavFile(tempWavFile, audioData, sampleRate);
// SoundPlayer로 재생
using (var player = new SoundPlayer(tempWavFile))
{
player.PlaySync(); // 재생이 끝날 때까지 대기
}
// 임시 파일 삭제
try
{
File.Delete(tempWavFile);
}
catch { /* 임시 파일 삭제 실패 무시 */ }
}
catch (Exception ex)
{
Console.WriteLine($"[Audio Playback Error] {ex.Message}");
}
});
}
/// <summary>
/// 오디오 파일을 스피커로 비동기 재생합니다.
/// </summary>
/// <param name="wavFilePath">WAV 파일 경로</param>
public static async Task PlayWavFileAsync(string wavFilePath)
{
await Task.Run(() =>
{
try
{
using (var player = new SoundPlayer(wavFilePath))
{
player.PlaySync(); // 재생이 끝날 때까지 대기
}
}
catch (Exception ex)
{
Console.WriteLine($"[WAV Playback Error] {ex.Message}");
}
});
}
}
}

View File

@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="..\csharp\packages\Microsoft.ML.OnnxRuntime.1.23.2\build\netstandard2.0\Microsoft.ML.OnnxRuntime.props" Condition="Exists('..\csharp\packages\Microsoft.ML.OnnxRuntime.1.23.2\build\netstandard2.0\Microsoft.ML.OnnxRuntime.props')" />
<Import Project=".\packages\Microsoft.ML.OnnxRuntime.1.23.2\build\netstandard2.0\Microsoft.ML.OnnxRuntime.props" Condition="Exists('.\packages\Microsoft.ML.OnnxRuntime.1.23.2\build\netstandard2.0\Microsoft.ML.OnnxRuntime.props')" />
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
@@ -62,37 +62,37 @@
</PropertyGroup>
<ItemGroup>
<Reference Include="Microsoft.Bcl.AsyncInterfaces, Version=10.0.0.1, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
<HintPath>..\csharp\packages\Microsoft.Bcl.AsyncInterfaces.10.0.1\lib\net462\Microsoft.Bcl.AsyncInterfaces.dll</HintPath>
<HintPath>.\packages\Microsoft.Bcl.AsyncInterfaces.10.0.1\lib\net462\Microsoft.Bcl.AsyncInterfaces.dll</HintPath>
</Reference>
<Reference Include="Microsoft.ML.OnnxRuntime, Version=1.23.2.0, Culture=neutral, PublicKeyToken=f27f157f0a5b7bb6, processorArchitecture=MSIL">
<HintPath>..\csharp\packages\Microsoft.ML.OnnxRuntime.Managed.1.23.2\lib\netstandard2.0\Microsoft.ML.OnnxRuntime.dll</HintPath>
<HintPath>.\packages\Microsoft.ML.OnnxRuntime.Managed.1.23.2\lib\netstandard2.0\Microsoft.ML.OnnxRuntime.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Buffers, Version=4.0.5.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
<HintPath>..\csharp\packages\System.Buffers.4.6.1\lib\net462\System.Buffers.dll</HintPath>
<HintPath>.\packages\System.Buffers.4.6.1\lib\net462\System.Buffers.dll</HintPath>
</Reference>
<Reference Include="System.Core" />
<Reference Include="System.IO.Pipelines, Version=10.0.0.1, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
<HintPath>..\csharp\packages\System.IO.Pipelines.10.0.1\lib\net462\System.IO.Pipelines.dll</HintPath>
<HintPath>.\packages\System.IO.Pipelines.10.0.1\lib\net462\System.IO.Pipelines.dll</HintPath>
</Reference>
<Reference Include="System.Memory, Version=4.0.5.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
<HintPath>..\csharp\packages\System.Memory.4.6.3\lib\net462\System.Memory.dll</HintPath>
<HintPath>.\packages\System.Memory.4.6.3\lib\net462\System.Memory.dll</HintPath>
</Reference>
<Reference Include="System.Numerics" />
<Reference Include="System.Numerics.Vectors, Version=4.1.6.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\csharp\packages\System.Numerics.Vectors.4.6.1\lib\net462\System.Numerics.Vectors.dll</HintPath>
<HintPath>.\packages\System.Numerics.Vectors.4.6.1\lib\net462\System.Numerics.Vectors.dll</HintPath>
</Reference>
<Reference Include="System.Runtime.CompilerServices.Unsafe, Version=6.0.3.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\csharp\packages\System.Runtime.CompilerServices.Unsafe.6.1.2\lib\net462\System.Runtime.CompilerServices.Unsafe.dll</HintPath>
<HintPath>.\packages\System.Runtime.CompilerServices.Unsafe.6.1.2\lib\net462\System.Runtime.CompilerServices.Unsafe.dll</HintPath>
</Reference>
<Reference Include="System.Text.Encodings.Web, Version=10.0.0.1, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
<HintPath>..\csharp\packages\System.Text.Encodings.Web.10.0.1\lib\net462\System.Text.Encodings.Web.dll</HintPath>
<HintPath>.\packages\System.Text.Encodings.Web.10.0.1\lib\net462\System.Text.Encodings.Web.dll</HintPath>
</Reference>
<Reference Include="System.Text.Json, Version=10.0.0.1, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
<HintPath>..\csharp\packages\System.Text.Json.10.0.1\lib\net462\System.Text.Json.dll</HintPath>
<HintPath>.\packages\System.Text.Json.10.0.1\lib\net462\System.Text.Json.dll</HintPath>
</Reference>
<Reference Include="System.Threading.Tasks.Extensions, Version=4.2.4.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
<HintPath>..\csharp\packages\System.Threading.Tasks.Extensions.4.6.3\lib\net462\System.Threading.Tasks.Extensions.dll</HintPath>
<HintPath>.\packages\System.Threading.Tasks.Extensions.4.6.3\lib\net462\System.Threading.Tasks.Extensions.dll</HintPath>
</Reference>
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
@@ -111,16 +111,16 @@
<None Include="packages.config" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<Import Project="..\csharp\packages\Microsoft.ML.OnnxRuntime.Managed.1.23.2\build\netstandard2.0\Microsoft.ML.OnnxRuntime.Managed.targets" Condition="Exists('..\csharp\packages\Microsoft.ML.OnnxRuntime.Managed.1.23.2\build\netstandard2.0\Microsoft.ML.OnnxRuntime.Managed.targets')" />
<Import Project=".\packages\Microsoft.ML.OnnxRuntime.Managed.1.23.2\build\netstandard2.0\Microsoft.ML.OnnxRuntime.Managed.targets" Condition="Exists('.\packages\Microsoft.ML.OnnxRuntime.Managed.1.23.2\build\netstandard2.0\Microsoft.ML.OnnxRuntime.Managed.targets')" />
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
<PropertyGroup>
<ErrorText>이 프로젝트는 이 컴퓨터에 없는 NuGet 패키지를 참조합니다. 해당 패키지를 다운로드하려면 NuGet 패키지 복원을 사용하십시오. 자세한 내용은 http://go.microsoft.com/fwlink/?LinkID=322105를 참조하십시오. 누락된 파일은 {0}입니다.</ErrorText>
</PropertyGroup>
<Error Condition="!Exists('..\csharp\packages\Microsoft.ML.OnnxRuntime.Managed.1.23.2\build\netstandard2.0\Microsoft.ML.OnnxRuntime.Managed.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\csharp\packages\Microsoft.ML.OnnxRuntime.Managed.1.23.2\build\netstandard2.0\Microsoft.ML.OnnxRuntime.Managed.targets'))" />
<Error Condition="!Exists('..\csharp\packages\Microsoft.ML.OnnxRuntime.1.23.2\build\netstandard2.0\Microsoft.ML.OnnxRuntime.props')" Text="$([System.String]::Format('$(ErrorText)', '..\csharp\packages\Microsoft.ML.OnnxRuntime.1.23.2\build\netstandard2.0\Microsoft.ML.OnnxRuntime.props'))" />
<Error Condition="!Exists('..\csharp\packages\Microsoft.ML.OnnxRuntime.1.23.2\build\netstandard2.0\Microsoft.ML.OnnxRuntime.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\csharp\packages\Microsoft.ML.OnnxRuntime.1.23.2\build\netstandard2.0\Microsoft.ML.OnnxRuntime.targets'))" />
<Error Condition="!Exists('..\csharp\packages\System.ValueTuple.4.6.1\build\net471\System.ValueTuple.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\csharp\packages\System.ValueTuple.4.6.1\build\net471\System.ValueTuple.targets'))" />
<Error Condition="!Exists('.\packages\Microsoft.ML.OnnxRuntime.Managed.1.23.2\build\netstandard2.0\Microsoft.ML.OnnxRuntime.Managed.targets')" Text="$([System.String]::Format('$(ErrorText)', '.\packages\Microsoft.ML.OnnxRuntime.Managed.1.23.2\build\netstandard2.0\Microsoft.ML.OnnxRuntime.Managed.targets'))" />
<Error Condition="!Exists('.\packages\Microsoft.ML.OnnxRuntime.1.23.2\build\netstandard2.0\Microsoft.ML.OnnxRuntime.props')" Text="$([System.String]::Format('$(ErrorText)', '.\packages\Microsoft.ML.OnnxRuntime.1.23.2\build\netstandard2.0\Microsoft.ML.OnnxRuntime.props'))" />
<Error Condition="!Exists('.\packages\Microsoft.ML.OnnxRuntime.1.23.2\build\netstandard2.0\Microsoft.ML.OnnxRuntime.targets')" Text="$([System.String]::Format('$(ErrorText)', '.\packages\Microsoft.ML.OnnxRuntime.1.23.2\build\netstandard2.0\Microsoft.ML.OnnxRuntime.targets'))" />
<Error Condition="!Exists('.\packages\System.ValueTuple.4.6.1\build\net471\System.ValueTuple.targets')" Text="$([System.String]::Format('$(ErrorText)', '.\packages\System.ValueTuple.4.6.1\build\net471\System.ValueTuple.targets'))" />
</Target>
<Import Project="..\csharp\packages\Microsoft.ML.OnnxRuntime.1.23.2\build\netstandard2.0\Microsoft.ML.OnnxRuntime.targets" Condition="Exists('..\csharp\packages\Microsoft.ML.OnnxRuntime.1.23.2\build\netstandard2.0\Microsoft.ML.OnnxRuntime.targets')" />
<Import Project="..\csharp\packages\System.ValueTuple.4.6.1\build\net471\System.ValueTuple.targets" Condition="Exists('..\csharp\packages\System.ValueTuple.4.6.1\build\net471\System.ValueTuple.targets')" />
<Import Project=".\packages\Microsoft.ML.OnnxRuntime.1.23.2\build\netstandard2.0\Microsoft.ML.OnnxRuntime.targets" Condition="Exists('.\packages\Microsoft.ML.OnnxRuntime.1.23.2\build\netstandard2.0\Microsoft.ML.OnnxRuntime.targets')" />
<Import Project=".\packages\System.ValueTuple.4.6.1\build\net471\System.ValueTuple.targets" Condition="Exists('.\packages\System.ValueTuple.4.6.1\build\net471\System.ValueTuple.targets')" />
</Project>

View File

@@ -3,6 +3,7 @@
<package id="Microsoft.Bcl.AsyncInterfaces" version="10.0.1" targetFramework="net48" />
<package id="Microsoft.ML.OnnxRuntime" version="1.23.2" targetFramework="net48" />
<package id="Microsoft.ML.OnnxRuntime.Managed" version="1.23.2" targetFramework="net48" />
<package id="NAudio" version="2.2.1" targetFramework="net48" />
<package id="System.Buffers" version="4.6.1" targetFramework="net48" />
<package id="System.IO.Pipelines" version="10.0.1" targetFramework="net48" />
<package id="System.Memory" version="4.6.3" targetFramework="net48" />