출력단자
for (int i = 0; i< NAudio.Wave.WaveOut.DeviceCount; i++)
{
var caps = NAudio.Wave.WaveOut.GetCapabilities(i);
comboBox2_Output.Items.Add(caps.ProductName);
}
if (comboBox2_Output.Items.Count > 0)
{
comboBox2_Output.SelectedIndex = 0;
outputDevice = new WaveOutEvent() { DeviceNumber = 0 };
}
void comboBox2_Output_SelectedIndexChanged(object sender, EventArgs e)
{
outputDevice.DeviceNumber = comboBox2_Output.SelectedIndex;
}
void Speech(string str)
{
//synth.Speak(str);
if (outputDevice != null)
{
outputDevice.Stop();
}
var waveProvider = new SpeechToWaveProvider(synth, (string)str);
outputDevice.Init(waveProvider);
outputDevice.Play();
}
public class SpeechToWaveProvider : IWaveProvider
{
private readonly BufferedWaveProvider waveProvider;
public SpeechToWaveProvider(SpeechSynthesizer synth, string text)
{
var memoryStream = new MemoryStream();
synth.SetOutputToWaveStream(memoryStream);
synth.Speak(text);
synth.SetOutputToDefaultAudioDevice();
var waveFormat = new WaveFormat(22000, 1);
waveProvider = new BufferedWaveProvider(waveFormat)
{
BufferLength = (int)memoryStream.Length
};
memoryStream.Position = 0;
var buffer = new byte[memoryStream.Length];
memoryStream.Read(buffer, 0, buffer.Length);
waveProvider.AddSamples(buffer, 0, buffer.Length);
}
public WaveFormat WaveFormat => waveProvider.WaveFormat;
public int Read(byte[] buffer, int offset, int count)
{
return waveProvider.Read(buffer, offset, count);
}
}
입력단자
for (int i = 0; i < NAudio.Wave.WaveIn.DeviceCount; i++)
{
var caps = NAudio.Wave.WaveIn.GetCapabilities(i);
comboBox_Input.Items.Add(caps.ProductName);
}
if (comboBox_Input.Items.Count > 0)
{
LoadRecognize();
inputDevice = new WaveInEvent() ;
inputDevice.DataAvailable += OnDataAvailable;
//inputDevice.BufferMilliseconds =100;
comboBox_Input.SelectedIndex = 0;
//inputDevice.StartRecording();
}
private void comboBox_Input_SelectedIndexChanged(object sender, EventArgs e)
{
inputDevice.StopRecording();
inputDevice.DeviceNumber = comboBox_Input.SelectedIndex;
inputDevice.StartRecording();
}
void OnDataAvailable(object s, WaveInEventArgs e)
{
// audioStream에 데이터 추가
if (e.Buffer.Length==0)
{
return;
}
var waveFormat = inputDevice.WaveFormat;
var audioStreamMax = 0;
if (audioStream.Length>0)
{
//audioStreamMax = audioStream.ToArray().Max();
audioStreamMax = GetSamples(audioStream.ToArray(), waveFormat).Max();
}
/*
var arr = e.Buffer;
for (int i = 0;i< arr.Length;i++)
{
arr[i] = (byte)(i % 255);
}
audioStream.Write(arr, 0, e.BytesRecorded);
*/
var cutout = 32;
{
var waveStream = new RawSourceWaveStream(new MemoryStream(e.Buffer), waveFormat) as WaveStream;
waveViewer_Input.WaveStream = waveStream;
if ((audioStreamMax > cutout) || (GetSamples(e.Buffer, waveFormat).Max() > cutout))
{
audioStream.Write(e.Buffer, 0, e.BytesRecorded);
}
}
if (audioStream.Length > inputDevice.WaveFormat.SampleRate*2)
{
var array = audioStream.ToArray();
var samples = GetSamples(array, waveFormat);
var startIndex = System.Array.FindIndex(samples, x => x > cutout);
var lastIndex = System.Array.FindLastIndex(samples, x => x > cutout);
var length = lastIndex - startIndex + 1;
var sliceArray = array.Skip(startIndex * 2).Take(length * 2).ToArray();
var max = (float)samples.Max();
var amp = 32767f / max/2;
amp = Math.Max(1, amp);
amp = Math.Min(16, amp);
sliceArray = Amplify(sliceArray, amp);
//var stream = new RawSourceWaveStream(new MemoryStream((byte[])audioStream.ToArray().Clone()), waveFormat);
var memoryStream = new MemoryStream(sliceArray);
var waveStream = new RawSourceWaveStream(memoryStream, waveFormat) as WaveStream;
waveViewer_Text.WaveStream = waveStream;
//recognizer.SetInputToAudioStream(memoryStream, new SpeechAudioFormatInfo(waveFormat.SampleRate, AudioBitsPerSample.Sixteen, AudioChannel.Stereo));
recognizer.SetInputToAudioStream(memoryStream, new SpeechAudioFormatInfo(
waveFormat.SampleRate,
(AudioBitsPerSample)waveFormat.BitsPerSample,
(AudioChannel)waveFormat.Channels
));
//recognizer.SetInputToWaveStream(waveStream);
recognizer.RecognizeAsync(RecognizeMode.Multiple);
audioStream.SetLength(0);
//Console.WriteLine("전달");
}
}
'윈도우폼' 카테고리의 다른 글
윈폼 TTS (0) | 2024.07.31 |
---|---|
윈폼 차트 (0) | 2024.01.15 |
윈도우용 대화형 프로그램 설정 (0) | 2023.09.26 |