Итак, я пытаюсь реализовать библиотеку Superpowered в Swift и застреваю вокруг инициализации с помощью обратного вызова. Как бы я преобразовал эту строку:
__unsafe_unretained Superpowered *self = (__bridge Superpowered *)clientdata;
в Свифт?
Вот упрощенная реализация Objective C:
@implementation Superpowered {
SuperpoweredIOSAudioIO *audioIO;
SuperpoweredBandpassFilterbank *filters;
unsigned int samplerate;
}
static bool audioProcessing(void *clientdata, float **buffers, unsigned int inputChannels, unsigned int outputChannels, unsigned int numberOfSamples, unsigned int samplerate, uint64_t hostTime) {
__unsafe_unretained Superpowered *self = (__bridge Superpowered *)clientdata;
if (samplerate != self->samplerate) {
self->samplerate = samplerate;
};
// Update position.
self->lastNumberOfSamples = numberOfSamples;
return false;
}
- (id)init {
self = [super init];
if (!self) return nil;
samplerate = 44100;
audioIO = [[SuperpoweredIOSAudioIO alloc] initWithDelegate:(id<SuperpoweredIOSAudioIODelegate>)self preferredBufferSize:12 preferredMinimumSamplerate:44100 audioSessionCategory:AVAudioSessionCategoryRecord channels:2 audioProcessingCallback:audioProcessing clientdata:(__bridge void *)self];
[audioIO start];
return self;
}
И вот начало моей версии Swift:
func bridge<T : AnyObject>(obj : T) -> UnsafeMutableRawPointer {
return UnsafeMutableRawPointer(Unmanaged.passUnretained(obj).toOpaque())
// return unsafeAddressOf(obj) // ***
}
func bridge<T : AnyObject>(ptr : UnsafeMutableRawPointer) -> T {
return Unmanaged<T>.fromOpaque(ptr).takeUnretainedValue()
// return unsafeBitCast(ptr, T.self) // ***
}
open class EchoesEngine: NSObject, CLLocationManagerDelegate, SuperpoweredIOSAudioIODelegate {
public var audioIO:SuperpoweredIOSAudioIO
static var lastNumberOfSamples:UInt32!
static var samplerate:UInt32!
override init() {
super.init()
audioIO = SuperpoweredIOSAudioIO.init(delegate: self, preferredBufferSize: 12, preferredMinimumSamplerate: 44100, audioSessionCategory: AVAudioSessionCategoryPlayAndRecord, channels: 2, audioProcessingCallback: EchoesEngine.audioProcessingCallback, clientdata: bridge(obj: self))
…
}
@objc static let audioProcessingCallback : @convention(c) (UnsafeMutableRawPointer?, UnsafeMutablePointer<UnsafeMutablePointer<Float>?>?, UInt32, UInt32, UInt32, UInt32, UInt64) -> Bool = {
(clientdata, buffers, inputChannels, outputChannels, numberOfSamples, _samplerate, hostTime) in
/*
let unsafePointer = Unmanaged<EchoesEngine>.fromOpaque(clientdata!).takeUnretainedValue()
let pointer = AutoreleasingUnsafeMutablePointer<EchoesEngine>(unsafePointer)
*/
self = bridge(ptr: clientdata!)
if samplerate != _samplerate {
samplerate = _samplerate
}
lastNumberOfSamples = numberOfSamples
return false
}