SpeziSpeech

Build and Test codecov DOI

识别和合成自然语言语音。

概述

Spezi Speech 组件提供了一种简单便捷的方式来识别(语音转文本)和合成(文本转语音)自然语言内容,从而促进与应用程序的无缝交互。它构建于 Apple 的 SpeechAVFoundation 框架之上。

设置

1. 添加 Spezi Speech 作为依赖项

您需要将 Spezi Speech Swift 包添加到 Xcode 中的应用程序Swift 包

重要提示

如果您的应用程序尚未配置为使用 Spezi,请按照Spezi 设置文章设置 Spezi 核心基础架构。

2. 在 Spezi Configuration 中配置 SpeechRecognizerSpeechSynthesizer

SpeechRecognizerSpeechSynthesizer 模块需要在基于 Spezi 的应用程序中使用 configurationSpeziAppDelegate 中注册。

class ExampleAppDelegate: SpeziAppDelegate {
    override var configuration: Configuration {
        Configuration {
            SpeechRecognizer()
            SpeechSynthesizer()
            // ...
        }
    }
}

注意

您可以在 Spezi 文档中了解有关 Module 的更多信息。

3. 配置目标属性

为了确保您的应用程序具有麦克风访问和语音识别的必要权限,请按照以下步骤配置 Xcode 项目中的目标属性

这些条目对于使用麦克风和语音识别功能的应用程序是强制性的。 如果未能提供这些条目,将导致您的应用程序无法访问这些功能。

示例

SpeechTestView 演示了 Spezi Speech 的功能。 它展示了与 SpeechRecognizer 的交互,以提供语音转文本功能,以及与 SpeechSynthesizer 的交互,以从文本生成语音。

struct SpeechTestView: View {
   // Get the `SpeechRecognizer` and `SpeechSynthesizer` from the SwiftUI `Environment`.
   @Environment(SpeechRecognizer.self) private var speechRecognizer
   @Environment(SpeechSynthesizer.self) private var speechSynthesizer
   // The transcribed message from the user's voice input.
   @State private var message = ""


   var body: some View {
      VStack {
         // Button used to start and stop recording by triggering the `microphoneButtonPressed()` function.
         Button("Record") {
             microphoneButtonPressed()
         }
             .padding(.bottom)

         // Button used to start and stop playback of the transcribed message by triggering the `playbackButtonPressed()` function.
         Button("Playback") {
             playbackButtonPressed()
         }
             .padding(.bottom)
         
         Text(message)
      }
   }


   private func microphoneButtonPressed() {
      if speechRecognizer.isRecording {
         // If speech is currently recognized, stop the transcribing.
         speechRecognizer.stop()
      } else {
         // If the recognizer is idle, start a new recording.
         Task {
            do {
               // The `speechRecognizer.start()` function returns an `AsyncThrowingStream` that yields the transcribed text.
               for try await result in speechRecognizer.start() {
                  // Access the string-based result of the transcribed result.
                  message = result.bestTranscription.formattedString
               }
            }
         }
      }
   }
    
   private func playbackButtonPressed() {
      if speechSynthesizer.isSpeaking {
         // If speech is currently synthezized, pause the playback.
         speechSynthesizer.pause()
      } else {
         // If synthesizer is idle, start with the text-to-speech functionality.
         speechSynthesizer.speak(message)
      }
   }
}

SpeziSpeech 还支持选择声音,包括个人声音

以下示例显示了如何让用户在他们当前的语言环境中选择声音,并且所选的声音可以用于合成语音。

struct SpeechVoiceSelectionExample: View {
   @Environment(SpeechSynthesizer.self) private var speechSynthesizer
   @State private var selectedVoiceIndex = 0
   @State private var message = ""


   var body: some View {
      VStack {
         TextField("Enter text to be spoken", text: $message)
            .textFieldStyle(RoundedBorderTextFieldStyle())
            .padding()
          Picker("Voice", selection: $selectedVoiceIndex) {
              ForEach(speechSynthesizer.voices.indices, id: \.self) { index in
                  Text(speechSynthesizer.voices[index].name)
                      .tag(index)
              }
          }
              .pickerStyle(.inline)
              .accessibilityIdentifier("voicePicker")
              .padding()
         Button("Speak") {
            speechSynthesizer.speak(
                message,
                voice: speechSynthesizer.voices[selectedVoiceIndex]
            )
         }
      }
         .padding()
   }
}

iOS 17 及更高版本支持个人声音。 用户必须首先创建个人声音。 使用个人声音还需要获得用户的授权。 要请求访问任何可用的个人声音,您可以使用 SpeechSynthesizergetPersonalVoices() 方法。 然后,个人声音将与系统声音一起提供。

有关更多信息,请参阅API 文档

贡献

欢迎对此项目做出贡献。 请务必先阅读贡献指南贡献者盟约行为准则

许可证

本项目根据 MIT 许可证获得许可。 有关更多信息,请参阅许可证

Spezi Footer Spezi Footer