0

So I am trying to implement the Superpowered library in Swift, and am getting stuck around initialisation with a callback. How would I convert that line:

    __unsafe_unretained Superpowered *self = (__bridge Superpowered *)clientdata;

into Swift?

Here is the simplified Objective C implementation:

@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;
}

And here is the start of my Swift version:

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
    }
rmaddy
  • 314,917
  • 42
  • 532
  • 579
Josh Kopecek
  • 143
  • 10
  • There's no need for it to be `__unsafe_unretained` there. If you just let it be the default (`__strong`), it will retain and then release at the end of the function, which doesn't cause any problems. – newacct Sep 12 '18 at 02:04
  • Thanks for the comment @newacct - but I was really asking how to implement that in Swift! The bridge() fns did the trick there. – Josh Kopecek Sep 12 '18 at 10:52

1 Answers1

0

So I discovered that in this context the self I was trying to cast to is actually a new variable providing an __unsafe_unretained reference to self, as passed to the initialiser for SuperpoweredIOSAudioIO.

This answer provided me with almost everything I needed. Swift 3.3 changes a couple of things, and this is what I ended up with:

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) // ***
}
Josh Kopecek
  • 143
  • 10