[英]AudioKit, AKPlayer: How to play from Samples, not time
I am currently using FDWaveFormView to great success to display waveforms representing audio I record from AKMicrophone
or AKAudioFile
. 我目前正在使用FDWaveFormView来成功地显示代表我从AKMicrophone
或AKAudioFile
记录的音频的波形。
I am successfully able to highlight specific regions in the waveform and FDwaveForm
gives back a range of the samples from the audiofile. 我能够成功突出显示波形中的特定区域,并且FDwaveForm
会从FDwaveForm
返回一定范围的样本。
My problem now is I cannot find an appropriate method in AKPlayer
that would let me play from a start sample to an end sample. 我现在的问题是我无法在AKPlayer
中找到合适的方法来让我从开始样本播放到结束样本。
I noticed that AKSamplePlayer
is now deprecated, but it did have a method: play(from: Sample, to: Sample)
我注意到现在不推荐使用AKSamplePlayer
,但是它确实有一个方法: play(from: Sample, to: Sample)
My guess is that I would be able to do some math to translate Sample position to time (as a Double
as prescribed in AKPlayer
), however I have not found the appropriate math or functions to do this, any hints? 我的猜测是,我将能够做一些数学到样品位置转换为时间(作为Double
在规定AKPlayer
),但我还没有找到合适的数学或函数来做到这一点,任何提示?
To be very explicit in what I am trying to do, please refer to the image below: 要明确说明我要执行的操作,请参考下图:
note for any AudioKit core members who may see this question, I know there are a variety of AudioKitUI components that may of made this easier, however only FDWaveFormView
has given me the functionality I need for this particular app, i'm happy to discuss further offline, thanks again so much. 对于可能会看到此问题的所有AudioKit核心成员的说明 ,我知道可以通过多种AudioKitUI组件简化此操作,但是只有FDWaveFormView
为我提供了此特定应用程序所需的功能,我很乐意进一步讨论离线,再次感谢。
EDIT 编辑
I've come up with some code that I believe has solved it: 我想出了一些我认为可以解决的代码:
let startingSampleIndex = self.waveformPlot.highlightedSamples!.min()
let endingSampleIndex = self.waveformPlot.highlightedSamples!.max()
let millisecondsPerSample : Double = 1000 / 44100
let startingDuration : Double = (startingSampleIndex! * millisecondsPerSample) / 1000
let endingDuration : Double = (endingSampleIndex! * millisecondsPerSample) / 1000
print("StartSample:\(startingSampleIndex!) | EndSample:\(endingSampleIndex!) | milliPerSample:\(millisecondsPerSample) | StartDuration:\(startingDuration) | EndDuration:\(endingDuration)")
player.play(from: startingDuration, to: endingDuration)
The main equation being numberOfSamples * millisecondsPerSample = timeInMilliseconds
by dividing by 1000 I can normalize everything to seconds which is what AKPlayer
wants. 主要公式为numberOfSamples * millisecondsPerSample = timeInMilliseconds
除以1000,我可以将所有内容归一化为AKPlayer
想要的秒数。 If anyone sees something problematic here I'd love the advice but I think this has done it! 如果有人在这里发现问题,我很乐意提供建议,但我认为这样做已成功! Sorry I am still new to DSP and so thankful for AudioKit being an incredible Shepard into this world! 抱歉,我还是DSP新手,非常感谢AudioKit成为这个世界不可思议的Shepard!
To convert from frames to seconds you should divide by the sample rate of the audio file, not a hardcoded 44100 value: 要将帧转换为秒,应除以音频文件的采样率,而不是硬编码的44100值:
guard let frameRange = self.waveformPlot.highlightedSamples else { return }
let startTime = frameRange.min() / audioFile.fileFormat.sampleRate
let endTime = frameRange.max() / audioFile.fileFormat.sampleRate
player.play(from: startTime, to: endTime)
I found the solution, essentially RTFM on DSP 101 and samples 😅: 我找到了解决方案,本质上是DSP 101上的RTFM和示例😅:
let startingSampleIndex = self.waveformPlot.highlightedSamples!.min()
let endingSampleIndex = self.waveformPlot.highlightedSamples!.max()
let millisecondsPerSample : Double = 1000 / 44100
let startingDuration : Double = (startingSampleIndex! * millisecondsPerSample) / 1000
let endingDuration : Double = (endingSampleIndex! * millisecondsPerSample) / 1000
print("StartSample:\(startingSampleIndex!) | EndSample:\(endingSampleIndex!) | milliPerSample:\(millisecondsPerSample) | StartDuration:\(startingDuration) | EndDuration:\(endingDuration)")
player.play(from: startingDuration, to: endingDuration)
This is working excellently, thanks again to both FDWaveFormView
and AudioKit
! 再次非常感谢FDWaveFormView
和AudioKit
,它的运行效果非常AudioKit
!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.