简体   繁体   English

我怎样才能返回void函数的值

[英]How can i return the values of a void function

I have a function that calculate the fft of the mic input. 我有一个计算麦克风输入fft的功能。 The target is to create a framework when i call the run function the i ged the float array with all bands. 目标是在我调用run函数时创建一个框架,我将浮点数组与所有band一起调用。

Now it work all fine, but i don't know how can i return the Array in the run function from the gotSomeAudio function. 现在它工作得很好,但我不知道如何从gotSomeAudio函数返回run函数中的Array。

Thank you very much for help 非常感谢你的帮助

@objc
public class FFT:NSObject{


    var audioInput: TempiAudioInput!

    @objc
    public func run() -> Array<Float>{

        let audioInputCallback: TempiAudioInputCallback = { (numberOfFrames, timeStamp, inout samples: [Float]) -> Void in
            self.gotSomeAudio(numberOfFrames, timeStamp: timeStamp, samples: samples)
        }


        audioInput = TempiAudioInput(audioInputCallback: audioInputCallback, sampleRate: 44100, numberOfChannels: 1)
        audioInput.startRecording()

        // how can i return the Array from the gotSomeAudio function? 
        return xyz
    }




    @objc
    public func gotSomeAudio(numberOfFrames: Int, timeStamp: Double, samples: [Float]) -> Array<Float> {

        let fft = TempiFFT(withSize: numberOfFrames, sampleRate: 44100)

        // Setting a window type reduces errors
        fft.windowType = TempiFFTWindowType.hanning

        // Perform the FFT
        fft.fftForward(samples)

        // Map FFT data to logical bands. This gives 4 bands per octave across 7 octaves = 28 bands.
        //fft.calculateLogarithmicBands(minFrequency: 100, maxFrequency: 11025, bandsPerOctave: 4)
        //fft.calculateLinearBands(minFrequency: 0, maxFrequency: fft.nyquistFrequency, numberOfBands: Int(screenWidth))
        fft.calculateLogarithmicBands(minFrequency: 100, maxFrequency: 11025, bandsPerOctave: 4)

        // Process some data
        return fft.bandFrequencies

    }

}

You can pass it to a callback function: 您可以将其传递给回调函数:

public func run(complete: Array<Float> -> Void) {

    let audioInputCallback: TempiAudioInputCallback = { (numberOfFrames, timeStamp, inout samples: [Float]) -> Void in
        complete(self.gotSomeAudio(numberOfFrames, timeStamp: timeStamp, samples: samples))
    }

...

myInstance.run() { floatArray in
    // Use floatArray here.
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM