diff --git a/Sources/PlaydateKit/Core/Sound.swift b/Sources/PlaydateKit/Core/Sound.swift index ef230d8a..95d0f1fc 100644 --- a/Sources/PlaydateKit/Core/Sound.swift +++ b/Sources/PlaydateKit/Core/Sound.swift @@ -34,30 +34,6 @@ public enum Sound { fileplayer.isPlaying.unsafelyUnwrapped(pointer) == 1 } - /// Prepares player to stream the file at path. Returns `true` if the file exists, otherwise `false`. - @discardableResult public func load(path: StaticString) -> Bool { - fileplayer.loadIntoPlayer(pointer, path.utf8Start) == 1 - } - - /// Prepares player to stream the file at path. Returns `true` if the file exists, otherwise `false`. - @discardableResult public func load(path: UnsafePointer) -> Bool { - fileplayer.loadIntoPlayer(pointer, path) == 1 - } - - /// Starts playing the file player. If repeat is greater than one, it loops the given number of times. - /// If zero, it loops endlessly until it is stopped with ``stop()`` - /// Returns 1 on success, 0 if buffer allocation failed. - @discardableResult public func play(repeat: Int32 = 1) -> Int32 { - fileplayer.play.unsafelyUnwrapped(pointer, `repeat`) - } - - // `CallbackData` wraps the callback closure and is stored in a dynamically allocated block, which - // is passed as `userData` to `setFinishCallback` below. - private struct CallbackData { - var callback: (() -> Void)? - } - private var _callbackData: UnsafeMutablePointer? - /// Installs a closure that will be called when playback has completed. public var finishCallback: (() -> Void)? { get { @@ -71,7 +47,7 @@ public enum Sound { callbackData.initialize(to: .init(callback: newValue)) _callbackData = callbackData setFinishCallback( - callback: { sourceSource, userdata in + callback: { _, userdata in if let callback = userdata?.assumingMemoryBound(to: CallbackData.self).pointee.callback { callback() } @@ -82,15 +58,21 @@ public enum Sound { } } - /// Sets a function to be called when playback has completed. - func setFinishCallback( - callback: @convention(c) ( - _ soundSource: OpaquePointer?, - _ userdata: UnsafeMutableRawPointer? - ) -> Void, - soundUserdata: UnsafeMutableRawPointer? = nil - ) { - fileplayer.setFinishCallback.unsafelyUnwrapped(pointer, callback, soundUserdata) + /// Prepares player to stream the file at path. Returns `true` if the file exists, otherwise `false`. + @discardableResult public func load(path: StaticString) -> Bool { + fileplayer.loadIntoPlayer(pointer, path.utf8Start) == 1 + } + + /// Prepares player to stream the file at path. Returns `true` if the file exists, otherwise `false`. + @discardableResult public func load(path: UnsafePointer) -> Bool { + fileplayer.loadIntoPlayer(pointer, path) == 1 + } + + /// Starts playing the file player. If repeat is greater than one, it loops the given number of times. + /// If zero, it loops endlessly until it is stopped with ``stop()`` + /// Returns 1 on success, 0 if buffer allocation failed. + @discardableResult public func play(repeat: Int32 = 1) -> Int32 { + fileplayer.play.unsafelyUnwrapped(pointer, `repeat`) } /// Pauses the file player. @@ -181,8 +163,29 @@ public enum Sound { return (left, right) } + // MARK: Internal + + /// Sets a function to be called when playback has completed. + func setFinishCallback( + callback: @convention(c) ( + _ soundSource: OpaquePointer?, + _ userdata: UnsafeMutableRawPointer? + ) -> Void, + soundUserdata: UnsafeMutableRawPointer? = nil + ) { + fileplayer.setFinishCallback.unsafelyUnwrapped(pointer, callback, soundUserdata) + } + // MARK: Private + /// `CallbackData` wraps the callback closure and is stored in a dynamically allocated block, which + /// is passed as `userData` to `setFinishCallback` below. + private struct CallbackData { + var callback: (() -> Void)? + } + + private var _callbackData: UnsafeMutablePointer? + private let pointer: OpaquePointer }