오디오재생함수추가
This commit is contained in:
@@ -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}");
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user