gpt4 book ai didi

swift - 如何快速编写 pow( ) 并修复错误?

转载 作者:可可西里 更新时间:2023-11-01 01:05:38 25 4
gpt4 key购买 nike

我正在尝试用 swift 编写一个 pow 函数,这是我的代码:

var recorder: AVAudioRecorder!
var levelTimer = NSTimer()
var lowPassResults: Double = 0.0

override func viewDidLoad() {
super.viewDidLoad()
let url = NSURL.fileURLWithPath("dev/null")
//numbers are automatically wrapped into NSNumber objects, so I simplified that to [NSString : NSNumber]
var settings : [NSString : NSNumber ] = [AVSampleRateKey: 44100.0, AVFormatIDKey: kAudioFormatAppleLossless, AVNumberOfChannelsKey: 1, AVEncoderAudioQualityKey: AVAudioQuality.Max.rawValue]
var error: NSError?
self.recorder = [[AVAudioRecorder alloc] initWithURL:soundFileURL, settings:recordSettings, error:&error];
if((recorder) != nil){
recorder.prepareToRecord()
recorder.meteringEnabled = true
recorder.record()
levelTimer = NSTimer.scheduledTimerWithTimeInterval(0.03, target: self, selector: Selector("listenForBlow"), userInfo: nil, repeats: true)
}
else{
NSLog("%@", "Fehler an dieser Stelle");
}
}
func levelTimerCallback(timer:NSTimer) {
recorder.updateMeters()
let ALPHA: Double = 0.05
var peakPowerForChannel: Double = pow(10, (0.05 * [self.recorder.peakPowerForChannel(0)]))
lowPassResults = ALPHA * peakPowerForChannel + (1.0 - ALPHA) * lowPassResults;

NSLog("@Average input: %f Peak input: %f Low pass results: %f", [recorder.averagePowerForChannel(0)], [recorder.peakPowerForChannel(0)], lowPassResults);
}

我在两行中遇到错误,但我不知道如何修复它们。这是这一行中的第一个错误:(error: "Expected ',' separator)。是否存在我忽略的语法错误?

self.recorder = [[AVAudioRecorder alloc] initWithURL:soundFileURL, settings:recordSettings, error:&error];

这是另一个错误:“找不到接受所提供参数的 '*' 的重载”。我知道类型必须相同 (Double * Double)。但究竟错在哪里呢?

var peakPowerForChannel: Double = pow(10, (0.05 * [self.recorder.peakPowerForChannel(0)]))

P.S.:我知道有很多这样的问题,但我无法快速找到答案。先谢谢了!!

最佳答案

使用 Swift 而不是 Objective-C 语法来初始化您的录音机:

recorder = try AVAudioRecorder(URL:…settings:…)

为了计算 peakPowerForChannel,知道 10 将是一个 Int 并且 peakPowerForChannel(_:) 返回 Float,而不是 Double。你需要显式地转换这些:

var peakPowerForChannel : Double = pow(Double(10), (0.05 * Double(recorder.peakPowerForChannel(0))))

此外,由于 pow(_: _:) 返回 Double,您可以删除 peakPowerForChannel 的类型说明:

var peakPowerForChannel = pow(Double(10), (0.05 * Double(recorder.peakPowerForChannel(0))))

关于swift - 如何快速编写 pow( ) 并修复错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31223314/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com