Swift 6.4 migration and exhancements (#1)
This PR contains the work done to update the library and the attached example project to use the Swift 6.4 computer as a minimum supported version and also, to use the latest features introduced in it. Reviewed-on: #1 Co-authored-by: Javier Cicchelli <javier@rock-n-code.com>
This commit was merged in pull request #1.
This commit is contained in:
@@ -13,7 +13,7 @@ extension Sound {
|
||||
self.isOwned = isOwned
|
||||
}
|
||||
|
||||
/// Allocates a sample buffer with room for `byteCount` bytes.
|
||||
/// An empty buffer sized for a `byteCount`-byte file; fill it with `load(path:)`.
|
||||
public convenience init(byteCount: Int) {
|
||||
self.init(pointer: AudioSample.api.pointee.newSampleBuffer.unsafelyUnwrapped(
|
||||
Int32(byteCount)).unsafelyUnwrapped, isOwned: true)
|
||||
@@ -21,17 +21,15 @@ extension Sound {
|
||||
|
||||
/// Loads the wav or aiff file at `path`.
|
||||
public convenience init(path: String) throws(PlaydateError) {
|
||||
let pointer = path.withPlaydateCString { AudioSample.api.pointee.load.unsafelyUnwrapped($0) }
|
||||
let pointer = path.withCString { AudioSample.api.pointee.load.unsafelyUnwrapped($0) }
|
||||
guard let pointer else {
|
||||
throw PlaydateError(message: "unable to load sample: \(path)")
|
||||
}
|
||||
self.init(pointer: pointer, isOwned: true)
|
||||
}
|
||||
|
||||
/// Creates a sample referencing existing sample data. If
|
||||
/// `freeWhenDone` is `true`, the OS frees `data` when the sample is
|
||||
/// freed; otherwise the caller must keep `data` valid for the
|
||||
/// sample's lifetime.
|
||||
/// References `data` without copying; it must outlive the sample, which frees it
|
||||
/// if `freeWhenDone`. Returns `nil` on failure.
|
||||
public convenience init?(data: UnsafeMutablePointer<UInt8>, format: Format,
|
||||
sampleRate: UInt32, byteCount: Int, freeWhenDone: Bool) {
|
||||
guard let pointer = AudioSample.api.pointee.newSampleFromData.unsafelyUnwrapped(
|
||||
@@ -47,9 +45,8 @@ extension Sound {
|
||||
}
|
||||
}
|
||||
|
||||
/// Loads the file at `path` into this sample's buffer.
|
||||
public func load(path: String) throws(PlaydateError) {
|
||||
let loaded = path.withPlaydateCString {
|
||||
let loaded = path.withCString {
|
||||
AudioSample.api.pointee.loadIntoSample.unsafelyUnwrapped(pointer, $0) != 0
|
||||
}
|
||||
if !loaded {
|
||||
@@ -57,7 +54,7 @@ extension Sound {
|
||||
}
|
||||
}
|
||||
|
||||
/// The sample's raw data, format, and rate.
|
||||
/// Data pointer (owned by the sample), format, rate in Hz, and length in bytes.
|
||||
public var data: (data: UnsafeMutablePointer<UInt8>?, format: Format,
|
||||
sampleRate: UInt32, byteLength: UInt32) {
|
||||
var data: UnsafeMutablePointer<UInt8>?
|
||||
@@ -67,13 +64,13 @@ extension Sound {
|
||||
return (data, Format(format), sampleRate, byteLength)
|
||||
}
|
||||
|
||||
/// The sample's length in seconds.
|
||||
/// Length in seconds.
|
||||
public var length: Float {
|
||||
AudioSample.api.pointee.getLength.unsafelyUnwrapped(pointer)
|
||||
}
|
||||
|
||||
/// Decompresses an ADPCM sample to 16-bit PCM so it can be used in a
|
||||
/// synth. Returns `false` if there is not enough memory.
|
||||
/// Decompresses ADPCM to 16-bit PCM (4x memory), needed for synths and reverse
|
||||
/// play. Returns `false` if out of memory.
|
||||
@discardableResult
|
||||
public func decompress() -> Bool {
|
||||
AudioSample.api.pointee.decompress.unsafelyUnwrapped(pointer) != 0
|
||||
|
||||
@@ -1,15 +1,14 @@
|
||||
extension Sound {
|
||||
/// A source that produces audio by calling back into Swift.
|
||||
/// A source rendered by a Swift callback every audio cycle. Create with
|
||||
/// `Sound.addSource(stereo:_:)`; it stays alive until removed with `removeSource`.
|
||||
public final class CallbackSource: Source {
|
||||
let callback: Callback
|
||||
|
||||
/// Every callback source is kept alive here while the C side may
|
||||
/// still invoke its trampoline: from creation until it is removed
|
||||
/// with `Sound.removeSource`/`Channel.removeSource`, or until its
|
||||
/// owning channel is freed.
|
||||
/// Keeps sources alive for the C trampoline until removed (`Sound`/`Channel`
|
||||
/// `.removeSource`) or their channel is freed.
|
||||
nonisolated(unsafe) static var live: [CallbackSource] = []
|
||||
|
||||
/// Releases the registration added by `adopt(pointer:)`.
|
||||
/// Drops the reference added by `adopt(pointer:)`.
|
||||
static func release(_ source: Source) {
|
||||
live.removeAll { $0 === source }
|
||||
}
|
||||
@@ -27,9 +26,9 @@ extension Sound {
|
||||
UnsafeMutablePointer<Int16>?, Int32) -> Int32 = { context, left, right, length in
|
||||
guard let context, let left else { return 0 }
|
||||
let source = Unmanaged<CallbackSource>.fromOpaque(context).takeUnretainedValue()
|
||||
let leftBuffer = UnsafeMutableBufferPointer(start: left, count: Int(length))
|
||||
let rightBuffer = right.map { UnsafeMutableBufferPointer(start: $0, count: Int(length)) }
|
||||
return source.callback(leftBuffer, rightBuffer) ? 1 : 0
|
||||
var leftSpan = UnsafeMutableBufferPointer(start: left, count: Int(length)).mutableSpan
|
||||
var rightSpan = UnsafeMutableBufferPointer(start: right, count: right == nil ? 0 : Int(length)).mutableSpan
|
||||
return source.callback(&leftSpan, &rightSpan) ? 1 : 0
|
||||
}
|
||||
|
||||
/// Attaches the C object created for this source.
|
||||
|
||||
@@ -7,7 +7,7 @@ extension Sound {
|
||||
|
||||
var loopCallback: ((FilePlayer) -> Void)?
|
||||
var fadeCallback: ((FilePlayer) -> Void)?
|
||||
var mp3DataSource: ((UnsafeMutableBufferPointer<UInt8>) -> Int)?
|
||||
var mp3DataSource: ((inout MutableSpan<UInt8>) -> Int)?
|
||||
private var retainedRateModulator: SignalValue?
|
||||
|
||||
override init(pointer: OpaquePointer?, isOwned: Bool) {
|
||||
@@ -19,7 +19,6 @@ extension Sound {
|
||||
isOwned: true)
|
||||
}
|
||||
|
||||
/// Creates a player and loads the audio file at `path`.
|
||||
public convenience init(path: String) throws(PlaydateError) {
|
||||
self.init()
|
||||
try load(path: path)
|
||||
@@ -31,9 +30,8 @@ extension Sound {
|
||||
}
|
||||
}
|
||||
|
||||
/// Prepares the player to stream the file at `path`.
|
||||
public func load(path: String) throws(PlaydateError) {
|
||||
let loaded = path.withPlaydateCString {
|
||||
let loaded = path.withCString {
|
||||
FilePlayer.api.pointee.loadIntoPlayer.unsafelyUnwrapped(pointer, $0) != 0
|
||||
}
|
||||
if !loaded {
|
||||
@@ -41,63 +39,60 @@ extension Sound {
|
||||
}
|
||||
}
|
||||
|
||||
/// Sets the length of the stream buffer, in seconds. Default 0.25.
|
||||
/// Stream buffer length, in seconds; default 0.25.
|
||||
public func setBufferLength(_ seconds: Float) {
|
||||
FilePlayer.api.pointee.setBufferLength.unsafelyUnwrapped(pointer, seconds)
|
||||
}
|
||||
|
||||
/// Starts playback, looping `repeat` times; 0 loops endlessly.
|
||||
/// Plays `repeat` times (0 loops forever); `false` if buffer allocation failed.
|
||||
@discardableResult
|
||||
public func play(repeat repeatCount: Int = 1) -> Bool {
|
||||
FilePlayer.api.pointee.play.unsafelyUnwrapped(pointer, Int32(repeatCount)) != 0
|
||||
}
|
||||
|
||||
/// Pauses playback.
|
||||
public func pause() {
|
||||
FilePlayer.api.pointee.pause.unsafelyUnwrapped(pointer)
|
||||
}
|
||||
|
||||
/// Stops playback.
|
||||
public func stop() {
|
||||
FilePlayer.api.pointee.stop.unsafelyUnwrapped(pointer)
|
||||
}
|
||||
|
||||
/// The file's length in seconds.
|
||||
/// Length in seconds.
|
||||
public var length: Float {
|
||||
FilePlayer.api.pointee.getLength.unsafelyUnwrapped(pointer)
|
||||
}
|
||||
|
||||
/// The playback position in seconds.
|
||||
/// Playback position, in seconds.
|
||||
public var offset: Float {
|
||||
get { FilePlayer.api.pointee.getOffset.unsafelyUnwrapped(pointer) }
|
||||
set { FilePlayer.api.pointee.setOffset.unsafelyUnwrapped(pointer, newValue) }
|
||||
}
|
||||
|
||||
/// The playback rate; 1 is normal speed, negative values are not
|
||||
/// supported.
|
||||
/// Playback rate; 1 is normal. Negative (reverse) is unsupported.
|
||||
public var rate: Float {
|
||||
get { FilePlayer.api.pointee.getRate.unsafelyUnwrapped(pointer) }
|
||||
set { FilePlayer.api.pointee.setRate.unsafelyUnwrapped(pointer, newValue) }
|
||||
}
|
||||
|
||||
/// Loops playback between `start` and `end` (seconds) while playing
|
||||
/// with `repeat` 0. An `end` of 0 means the end of the file.
|
||||
/// Loop region, in seconds; `end` 0 means end of file. Loops only if played
|
||||
/// with `repeat` 0 or ≥ 2.
|
||||
public func setLoopRange(start: Float, end: Float) {
|
||||
FilePlayer.api.pointee.setLoopRange.unsafelyUnwrapped(pointer, start, end)
|
||||
}
|
||||
|
||||
/// Whether playback underran because the file could not be read fast
|
||||
/// enough.
|
||||
/// Whether playback underran because the file couldn't be read fast enough.
|
||||
public var didUnderrun: Bool {
|
||||
FilePlayer.api.pointee.didUnderrun.unsafelyUnwrapped(pointer) != 0
|
||||
}
|
||||
|
||||
/// Stops playback (instead of looping the buffer) on underrun.
|
||||
/// If `true`, an underrun stops playback and calls the finish callback; by
|
||||
/// default playback resumes after a stutter once data arrives.
|
||||
public func setStopOnUnderrun(_ flag: Bool) {
|
||||
FilePlayer.api.pointee.setStopOnUnderrun.unsafelyUnwrapped(pointer, flag ? 1 : 0)
|
||||
}
|
||||
|
||||
/// Sets a function called every time playback loops.
|
||||
/// Called each time playback loops; `nil` removes it.
|
||||
public func setLoopCallback(_ callback: ((FilePlayer) -> Void)?) {
|
||||
loopCallback = callback
|
||||
if callback != nil {
|
||||
@@ -111,8 +106,8 @@ extension Sound {
|
||||
}
|
||||
}
|
||||
|
||||
/// Fades the volume to the given levels over `length` sample frames,
|
||||
/// then calls `completion`.
|
||||
/// Fades to `left`/`right` (0–1) over `length` sample frames, then calls
|
||||
/// `completion`.
|
||||
public func fadeVolume(left: Float, right: Float, length: Int32,
|
||||
completion: ((FilePlayer) -> Void)? = nil) {
|
||||
fadeCallback = completion
|
||||
@@ -127,21 +122,20 @@ extension Sound {
|
||||
}
|
||||
}
|
||||
|
||||
/// Streams MP3 data from a callback instead of a file. The callback
|
||||
/// fills the buffer and returns the number of bytes written; return 0
|
||||
/// to signal the end of the stream.
|
||||
/// Streams MP3 from `dataSource`, buffering `bufferLength` seconds. `dataSource`
|
||||
/// fills the span and returns bytes written; 0 ends the stream.
|
||||
public func setMP3StreamSource(bufferLength: Float,
|
||||
_ dataSource: @escaping (UnsafeMutableBufferPointer<UInt8>) -> Int) {
|
||||
_ dataSource: @escaping (inout MutableSpan<UInt8>) -> Int) {
|
||||
mp3DataSource = dataSource
|
||||
FilePlayer.api.pointee.setMP3StreamSource.unsafelyUnwrapped(pointer, { data, bytes, userdata in
|
||||
guard let userdata, let data else { return 0 }
|
||||
let player = Unmanaged<FilePlayer>.fromOpaque(userdata).takeUnretainedValue()
|
||||
let buffer = UnsafeMutableBufferPointer(start: data, count: Int(bytes))
|
||||
return Int32(player.mp3DataSource?(buffer) ?? 0)
|
||||
var buffer = UnsafeMutableBufferPointer(start: data, count: Int(bytes)).mutableSpan
|
||||
return Int32(player.mp3DataSource?(&buffer) ?? 0)
|
||||
}, Unmanaged.passUnretained(self).toOpaque(), bufferLength)
|
||||
}
|
||||
|
||||
/// Modulates the playback rate.
|
||||
/// A signal added to `rate`; `nil` clears it. The player retains it.
|
||||
public var rateModulator: SignalValue? {
|
||||
get { SignalValue.wrap(FilePlayer.api.pointee.getRateModulator.unsafelyUnwrapped(pointer)) }
|
||||
set {
|
||||
|
||||
@@ -18,7 +18,6 @@ extension Sound {
|
||||
isOwned: true)
|
||||
}
|
||||
|
||||
/// Creates a player for the sample at `path`.
|
||||
public convenience init(path: String) throws(PlaydateError) {
|
||||
self.init()
|
||||
sample = try AudioSample(path: path)
|
||||
@@ -30,7 +29,7 @@ extension Sound {
|
||||
}
|
||||
}
|
||||
|
||||
/// The sample to play.
|
||||
/// Retained by the player.
|
||||
public var sample: AudioSample? {
|
||||
get { retainedSample }
|
||||
set {
|
||||
@@ -39,46 +38,43 @@ extension Sound {
|
||||
}
|
||||
}
|
||||
|
||||
/// Starts playback at `rate`, looping `repeat` times; 0 loops
|
||||
/// endlessly, -1 loops ping-pong.
|
||||
/// Plays `repeat` times at `rate` (1 is normal); 0 loops forever, -1 ping-pongs.
|
||||
@discardableResult
|
||||
public func play(repeat repeatCount: Int = 1, rate: Float = 1) -> Bool {
|
||||
SamplePlayer.api.pointee.play.unsafelyUnwrapped(pointer, Int32(repeatCount), rate) != 0
|
||||
}
|
||||
|
||||
/// Stops playback.
|
||||
public func stop() {
|
||||
SamplePlayer.api.pointee.stop.unsafelyUnwrapped(pointer)
|
||||
}
|
||||
|
||||
/// Pauses or resumes playback.
|
||||
public func setPaused(_ paused: Bool) {
|
||||
SamplePlayer.api.pointee.setPaused.unsafelyUnwrapped(pointer, paused ? 1 : 0)
|
||||
}
|
||||
|
||||
/// The sample's length in seconds.
|
||||
/// Length in seconds.
|
||||
public var length: Float {
|
||||
SamplePlayer.api.pointee.getLength.unsafelyUnwrapped(pointer)
|
||||
}
|
||||
|
||||
/// The playback position in seconds.
|
||||
/// Playback position, in seconds.
|
||||
public var offset: Float {
|
||||
get { SamplePlayer.api.pointee.getOffset.unsafelyUnwrapped(pointer) }
|
||||
set { SamplePlayer.api.pointee.setOffset.unsafelyUnwrapped(pointer, newValue) }
|
||||
}
|
||||
|
||||
/// The playback rate; 1 is normal speed, negative plays backward.
|
||||
/// Playback rate; 1 is normal. Negative plays backward (PCM only, not ADPCM).
|
||||
public var rate: Float {
|
||||
get { SamplePlayer.api.pointee.getRate.unsafelyUnwrapped(pointer) }
|
||||
set { SamplePlayer.api.pointee.setRate.unsafelyUnwrapped(pointer, newValue) }
|
||||
}
|
||||
|
||||
/// Restricts playback to the given range of sample frames.
|
||||
/// Restricts playback to `start`–`end`, in sample frames.
|
||||
public func setPlayRange(start: Int, end: Int) {
|
||||
SamplePlayer.api.pointee.setPlayRange.unsafelyUnwrapped(pointer, Int32(start), Int32(end))
|
||||
}
|
||||
|
||||
/// Sets a function called every time playback loops.
|
||||
/// Called each time playback loops; `nil` removes it.
|
||||
public func setLoopCallback(_ callback: ((SamplePlayer) -> Void)?) {
|
||||
loopCallback = callback
|
||||
if callback != nil {
|
||||
@@ -92,7 +88,7 @@ extension Sound {
|
||||
}
|
||||
}
|
||||
|
||||
/// Modulates the playback rate.
|
||||
/// A signal added to `rate`; `nil` clears it. The player retains it.
|
||||
public var rateModulator: SignalValue? {
|
||||
get { SignalValue.wrap(SamplePlayer.api.pointee.getRateModulator.unsafelyUnwrapped(pointer)) }
|
||||
set {
|
||||
|
||||
@@ -1,12 +1,12 @@
|
||||
internal import CPlaydate
|
||||
|
||||
extension Sound {
|
||||
/// A source of audio: the base class of `FilePlayer`, `SamplePlayer`,
|
||||
/// `Synth`, `DelayLineTap`, and `CallbackSource`. Wraps `SoundSource`.
|
||||
/// Base class of `FilePlayer`, `SamplePlayer`, `Synth`, `DelayLineTap`, and
|
||||
/// `CallbackSource`. Wraps `SoundSource`.
|
||||
public class Source {
|
||||
private static var api: UnsafePointer<playdate_sound_source> { Playdate.sourceAPI.unsafelyUnwrapped }
|
||||
|
||||
/// The underlying C object. Set once, immediately after creation.
|
||||
/// Set once, right after creation.
|
||||
var pointer: OpaquePointer!
|
||||
let isOwned: Bool
|
||||
var finishCallback: ((Source) -> Void)?
|
||||
@@ -16,7 +16,7 @@ extension Sound {
|
||||
self.isOwned = isOwned
|
||||
}
|
||||
|
||||
/// The playback volume of the left and right channels, 0...1.
|
||||
/// Per-channel volume, 0–1.
|
||||
public var volume: (left: Float, right: Float) {
|
||||
get {
|
||||
var left: Float = 0, right: Float = 0
|
||||
@@ -26,7 +26,6 @@ extension Sound {
|
||||
set { Source.api.pointee.setVolume.unsafelyUnwrapped(pointer, newValue.left, newValue.right) }
|
||||
}
|
||||
|
||||
/// Sets the playback volume of both channels.
|
||||
public func setVolume(_ volume: Float) {
|
||||
self.volume = (volume, volume)
|
||||
}
|
||||
@@ -35,7 +34,7 @@ extension Sound {
|
||||
Source.api.pointee.isPlaying.unsafelyUnwrapped(pointer) != 0
|
||||
}
|
||||
|
||||
/// Sets a function called when the source finishes playing.
|
||||
/// Called when the source finishes playing; `nil` removes it.
|
||||
public func setFinishCallback(_ callback: ((Source) -> Void)?) {
|
||||
finishCallback = callback
|
||||
if callback != nil {
|
||||
|
||||
Reference in New Issue
Block a user