0

我正在为 TTS 使用 Windows.Media.SpeechSynthesis 并在我的 WPF 应用程序中播放音频信号工作正常。我想保存音频信号,但如果我调用 StorageFolder,我会收到错误消息:HRESULT: 0x80073D54 - 该进程没有包标识。如何解决?整个代码如下。我还想知道使用 Win 10 TTS 中的音频文件有哪些权利?我在任何地方都没有找到它 - 但我并没有寻找那么多......

private async void Talk(string text)
{
    var stream = await speechSynthesizer.SynthesizeTextToStreamAsync(text);

    StorageFolder localfolder = ApplicationData.Current.LocalFolder;
    StorageFile sampleFile = await localfolder.CreateFileAsync("sample.wav", CreationCollisionOption.ReplaceExisting);
    using (var reader = new DataReader(stream))
    {
        await reader.LoadAsync((uint)stream.Size);
        IBuffer buffer = reader.ReadBuffer((uint)stream.Size);
        await FileIO.WriteBufferAsync(sampleFile, buffer);
    }
}
4

1 回答 1

0

最后,我找到了一个解决方案,如何将流从 tts 保存到 *.wav 或 Naudio 的 *.mp3 中。我不知道它是否清楚,但它是功能性的:

public bool ByteArrayToFile(string fileName, byte[] byteArray)
{
    try
    {
        using (var fs = new FileStream(fileName, FileMode.Create, FileAccess.Write))
        {
            fs.Write(byteArray, 0, byteArray.Length);
            return true;
        }
    }
    catch (Exception ex)
    {
        Console.WriteLine("Exception caught in process: {0}", ex);
        return false;
    }
}

private async Task SaveAudio(string text)
{
    var stream = await speechSynthesizer.SynthesizeTextToStreamAsync(text);

    using (var reader = new DataReader(stream))
    {
        await reader.LoadAsync((uint)stream.Size);
        IBuffer buffer = reader.ReadBuffer((uint)stream.Size);
        DataReader dataReader = DataReader.FromBuffer(buffer);
        byte[] bytes = new byte[buffer.Length];
        dataReader.ReadBytes(bytes);
        //ByteArrayToFile("sample.wav", bytes);
        ConvertWavStreamToMp3File(bytes, TB_File.Text);
        MessageBox.Show("Audio was saved to file: " + TB_File.Text, "Info");
    }
}

public static void ConvertWavStreamToMp3File(byte[] wavFile, string savetofilename)
{
    using (var retMs = new MemoryStream())
    using (var ms = new MemoryStream(wavFile))
    using (var rdr = new WaveFileReader(ms))

    using (var wtr = new LameMP3FileWriter(savetofilename, rdr.WaveFormat, LAMEPreset.VBR_90))
    {
        rdr.CopyTo(wtr);
    }
}

于 2021-02-16T14:59:04.643 回答