Skip to content

Commit

Permalink
Format
Browse files Browse the repository at this point in the history
  • Loading branch information
finnvoor committed Apr 7, 2024
1 parent 5dfc852 commit 76923cc
Showing 1 changed file with 37 additions and 34 deletions.
71 changes: 37 additions & 34 deletions Sources/PlaydateKit/Core/Sound.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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<CChar>) -> 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<CallbackData>?

/// Installs a closure that will be called when playback has completed.
public var finishCallback: (() -> Void)? {
get {
Expand All @@ -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()
}
Expand All @@ -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<CChar>) -> 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.
Expand Down Expand Up @@ -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<CallbackData>?

private let pointer: OpaquePointer
}

Expand Down

0 comments on commit 76923cc

Please sign in to comment.