【发布时间】:2014-08-22 08:04:53
【问题描述】:
这次我有一个逻辑问题。希望你们中的某个人可以帮助我。使用 `NSSpeechSynthesizer' 你可以设置速率,即每分钟 235 个单词,每分钟 100 个单词等等......
我发现通常每分钟的平均单词数是使用每个单词 5 个字符的标准化单词长度来计算的,同时计算空格和符号。
我需要自动将长文本细分为具有预选持续时间的曲目,比如每首曲目 15 分钟。
我们如何计算每个“拆分”到语音引擎的正确字符数?
我的解决方法如下:
// duration is the number of minutes per track
numberOfWordsPerTrack = [rateSlider floatValue] * duration;
splits = [[NSMutableArray alloc] init];
finished = NO;
NSUInteger position = 0;
while( !finished ) {
NSRange range;
// the idea is: I take 5*numberOfWordsPerTrack characters
// until the text allows me to select them
range = NSMakeRange( position, 5*numberOfWordsPerTrack );
if( range.location+range.length > mainTextView.string.length ) {
// If there are not another full character track we get
// the tail of the remaining text
finished = YES;
range = NSMakeRange( position, mainTextView.string.length-position );
}
// Here we get the track and add it to the split list
if( range.location+range.length <= mainTextView.string.length ) {
currentSplit = [mainTextView.string substringWithRange:range];
[splits addObject:currentSplit];
}
position += range.length;
}
此解决方案的问题是轨道持续时间不正确。它与期望值相差不远,但它是不正确的。例如,使用每分钟 235 个单词,持续时间为 50 分钟,我每首曲目有 40 分钟。如果我设置每首曲目 120 分钟,我每首曲目有 1 小时:39 米……等等……
你认为逻辑错误在哪里?
在 JanX2 回复后编辑
好吧,虽然我随机想到了以下假设,你能告诉我你在实施之前是怎么想的,因为这不是我代码中的轻微变化
如果我使用 speechSynthesizer:willSpeakWord:ofString: 代表成员,我可以经常测试 .aiff 文件大小,即在说出下一个单词之前(真实单词,未标准化)。因为我们知道这些文件是由合成器创建的 Hz、比特和通道,并且因为我们知道它们没有被压缩,所以我们可以对轨道的当前长度进行一些猜测。
此解决方案的最大缺点可能是连续磁盘访问,这会大大降低性能。
你怎么看?
【问题讨论】:
标签: objective-c macos cocoa text-to-speech