AVSpeechSynthesizer - 强调某些词

时间:2017-12-07 13:17:48

标签: ios avfoundation

试图强调文本到语音中的特定单词。

import SpriteKit
import PlaygroundSupport
import AVFoundation

let bounds = CGRect(x: 0, y: 0, width: 400, height: 200)
let skview = SKView(frame: bounds)

PlaygroundPage.current.liveView = skview

var voices = [
   (volume: 0.5, rate: 0.5, text: "This is a "),
   (volume: 1.0, rate: 0.2, text: "HUGE"), 
   (volume: 0.5, rate: 0.5, text: "deal") ]

   var utterances: [AVSpeechUtterance] = []
   for voice in voices {
       let utterance = AVSpeechUtterance(string: voice.text)
       utterance.voice = AVSpeechSynthesisVoice(language: "en-US")
       utterance.volume = Float(voice.volume)
       utterance.rate = Float(voice.rate)
       utterance.preUtteranceDelay = 0
       utterance.postUtteranceDelay = 0
       utterances.append(utterance)
   }
   let synth = AVSpeechSynthesizer()
   for utterance in utterances {
       synth.speak(utterance)
   }

我期待话语之间不要停顿。上面的代码应该导致 "这是一个......巨大的......交易"。

然而,暂停时间太长而且语音听起来脱节(即使我将前后延迟设置为0)。

"这是......巨大的......交易"。

我做错了吗?是否有更好的技巧来强调语音?

这篇文章on word stress建议使用`符号。然而,正在寻找更多的效果。

1 个答案:

答案 0 :(得分:0)

MacOS Cocoa语音合成器API支持TUNE格式,即带有符号的文本编码,用于强调,音量变化,速率变化和其他很酷的事物。因此,我将使用Cocoa API将我的文本语音合成,保存为音频文件,并将音频文件作为资源包含在我的iOS项目中。如果要在iOS应用程序中动态生成要说出的文本,这当然不起作用。

这是一个可重复录制音频的Cocoa Playground。

import Cocoa
import PlaygroundSupport
PlaygroundPage.current.needsIndefiniteExecution = true
let synth = NSSpeechSynthesizer(voice:         NSSpeechSynthesizer.VoiceName(rawValue: "com.apple.speech.synthesis.voice.samantha"))
synth?.volume = 0.5;
let saveURL = NSURL(fileURLWithPath: NSString(string: "~/Desktop/SpokenText.aiff").expandingTildeInPath);
synth?.startSpeaking( "This is a [[rate -100; volm + 0.5]]HUGE[[rate +100; volm - 0.5]]  deal", to:  saveURL as URL)

这是更新的Swift游乐场,用于播放iOS中保存的音频文件。感谢Bill Chan在discussion on playing AV within playground的回应。 (IMP:首先将录制的文件作为资源导入游乐场)

import SpriteKit
import PlaygroundSupport
import AVFoundation
let bounds = CGRect(x: 0, y: 0, width: 400, height: 200)
let skview = SKView(frame: bounds)
PlaygroundPage.current.liveView = skview
PlaygroundPage.current.needsIndefiniteExecution = true

let path = Bundle.main.path(forResource: "SpokenText", ofType: "aiff")!
let url = NSURL(fileURLWithPath: path)
var playerItem = AVPlayerItem(url: url as URL);
var player = AVPlayer(playerItem: playerItem)
var playerLayer = AVPlayerLayer(player: player)
playerLayer.frame = bounds;
skview.layer.addSublayer(playerLayer)
player.play()