最近試著做即時翻譯這件事,透過安裝naudio。把麥克風聲音錄下後,每10秒轉成一個檔案上傳至open AI whisper做即時翻譯或逐字稿:
private const string API_KEY = "sk-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
private const string API_URL = "https://api.openai.com/v1/audio/transcriptions";
var waveIn = new WaveInEvent();
waveIn.WaveFormat = new WaveFormat(16000, 1);
var buffer = new MemoryStream();
var writer = new WaveFileWriter(buffer, waveIn.WaveFormat);
//var writer = new WaveFileWriter(new DisposeStream(buffer), waveIn.WaveFormat);
waveIn.DataAvailable += async (sender, e) =>
{
writer.Write(e.Buffer, 0, e.BytesRecorded);
if (buffer.Length > 16000 * 2 * 10) // 每10秒
{
var audioData = buffer.ToArray();
buffer.SetLength(0);
buffer.Position = 0;
await SaveAudioChunkAsMp3(audioData, waveIn.WaveFormat);
await SendAudioChunk(audioData);
}
};
Console.WriteLine(“開始錄音。按任意鍵停止…”);
waveIn.StartRecording();
Console.ReadKey();
waveIn.StopRecording();
writer.Dispose();
buffer.Dispose();
static async Task SendAudioChunk(byte[] audioData)
{
using var httpClient = new HttpClient();
httpClient.DefaultRequestHeaders.Add(“Authorization”, $”Bearer {API_KEY}”);
using var content = new MultipartFormDataContent();
content.Add(new ByteArrayContent(audioData), “file”, “audio.wav”);
content.Add(new StringContent(“whisper-1”), “model”);
content.Add(new StringContent(“language”), “zh-hant”);
var response = await httpClient.PostAsync(API_URL, content);
var result = await response.Content.ReadAsStringAsync();
Console.WriteLine($”Transcription: {result}”);
}