2026-07-24 10:33:45 +02:00
|
|
|
internal import CPlaydate
|
|
|
|
|
|
2026-07-24 11:10:25 +02:00
|
|
|
extension Sound {
|
2026-07-24 10:33:45 +02:00
|
|
|
/// A mixer channel holding sources and effects. Wraps `SoundChannel`.
|
|
|
|
|
public final class Channel {
|
2026-07-25 10:43:32 +02:00
|
|
|
private static var api: UnsafePointer<playdate_sound_channel> { Playdate.channelAPI.unsafelyUnwrapped }
|
2026-07-24 10:33:45 +02:00
|
|
|
|
|
|
|
|
let pointer: OpaquePointer
|
|
|
|
|
let isOwned: Bool
|
|
|
|
|
private var retainedSources: [Source] = []
|
|
|
|
|
private var retainedEffects: [Effect] = []
|
|
|
|
|
private var retainedModulators: [SignalValue] = []
|
|
|
|
|
|
|
|
|
|
init(pointer: OpaquePointer, isOwned: Bool) {
|
|
|
|
|
self.pointer = pointer
|
|
|
|
|
self.isOwned = isOwned
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Creates a new channel. Add it to the sound engine with `add()`.
|
|
|
|
|
public convenience init() {
|
2026-07-25 07:23:19 +02:00
|
|
|
self.init(pointer: Channel.api.pointee.newChannel.unsafelyUnwrapped().unsafelyUnwrapped,
|
2026-07-24 10:33:45 +02:00
|
|
|
isOwned: true)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
deinit {
|
|
|
|
|
if isOwned {
|
2026-07-25 07:23:19 +02:00
|
|
|
Channel.api.pointee.freeChannel.unsafelyUnwrapped(pointer)
|
2026-07-25 07:09:30 +02:00
|
|
|
// The freed channel no longer pulls its callback sources, so
|
|
|
|
|
// their trampoline registrations can be released too.
|
|
|
|
|
for source in retainedSources where source is CallbackSource {
|
|
|
|
|
CallbackSource.release(source)
|
|
|
|
|
}
|
2026-07-24 10:33:45 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// The default channel, which sources are added to unless otherwise
|
2026-07-25 10:28:24 +02:00
|
|
|
/// specified. A single shared wrapper, so resources retained through
|
|
|
|
|
/// it (sources, effects, modulators) stay alive.
|
|
|
|
|
public static var `default`: Channel { defaultChannel }
|
|
|
|
|
|
|
|
|
|
nonisolated(unsafe) private static let defaultChannel =
|
2026-07-25 07:23:19 +02:00
|
|
|
Channel(pointer: snd.pointee.getDefaultChannel.unsafelyUnwrapped().unsafelyUnwrapped,
|
2026-07-24 10:33:45 +02:00
|
|
|
isOwned: false)
|
|
|
|
|
|
|
|
|
|
nonisolated(unsafe) private static var addedChannels: [Channel] = []
|
|
|
|
|
|
|
|
|
|
/// Adds the channel to the sound engine.
|
|
|
|
|
@discardableResult
|
|
|
|
|
public func add() -> Bool {
|
2026-07-25 07:23:19 +02:00
|
|
|
let added = snd.pointee.addChannel.unsafelyUnwrapped(pointer) != 0
|
2026-07-24 10:33:45 +02:00
|
|
|
if added, !Channel.addedChannels.contains(where: { $0 === self }) {
|
|
|
|
|
Channel.addedChannels.append(self)
|
|
|
|
|
}
|
|
|
|
|
return added
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Removes the channel from the sound engine.
|
|
|
|
|
@discardableResult
|
|
|
|
|
public func remove() -> Bool {
|
2026-07-25 07:23:19 +02:00
|
|
|
let removed = snd.pointee.removeChannel.unsafelyUnwrapped(pointer) != 0
|
2026-07-24 10:33:45 +02:00
|
|
|
Channel.addedChannels.removeAll { $0 === self }
|
|
|
|
|
return removed
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Adds a source to the channel. A source can only be on one channel.
|
|
|
|
|
@discardableResult
|
|
|
|
|
public func addSource(_ source: Source) -> Bool {
|
2026-07-25 07:23:19 +02:00
|
|
|
let added = Channel.api.pointee.addSource.unsafelyUnwrapped(pointer, source.pointer) != 0
|
2026-07-24 10:33:45 +02:00
|
|
|
if added, !retainedSources.contains(where: { $0 === source }) {
|
|
|
|
|
retainedSources.append(source)
|
|
|
|
|
}
|
|
|
|
|
return added
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@discardableResult
|
|
|
|
|
public func removeSource(_ source: Source) -> Bool {
|
2026-07-25 07:23:19 +02:00
|
|
|
let removed = Channel.api.pointee.removeSource.unsafelyUnwrapped(pointer, source.pointer) != 0
|
2026-07-25 10:28:24 +02:00
|
|
|
// Only drop the retentions if the source was actually on this
|
|
|
|
|
// channel; otherwise another channel may still be pulling it.
|
|
|
|
|
if removed {
|
|
|
|
|
retainedSources.removeAll { $0 === source }
|
|
|
|
|
CallbackSource.release(source)
|
|
|
|
|
}
|
2026-07-24 10:33:45 +02:00
|
|
|
return removed
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Adds a callback-based source to the channel. The callback fills
|
|
|
|
|
/// the sample buffers and returns `true` if it produced output.
|
|
|
|
|
public func addCallbackSource(stereo: Bool,
|
|
|
|
|
_ callback: @escaping CallbackSource.Callback) -> CallbackSource {
|
|
|
|
|
let source = CallbackSource(callback: callback)
|
2026-07-25 07:23:19 +02:00
|
|
|
let pointer = Channel.api.pointee.addCallbackSource.unsafelyUnwrapped(
|
2026-07-24 10:33:45 +02:00
|
|
|
self.pointer, CallbackSource.trampoline, source.contextPointer, stereo ? 1 : 0)
|
|
|
|
|
source.adopt(pointer: pointer.unsafelyUnwrapped)
|
|
|
|
|
retainedSources.append(source)
|
|
|
|
|
return source
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@discardableResult
|
|
|
|
|
public func addEffect(_ effect: Effect) -> Bool {
|
2026-07-25 07:23:19 +02:00
|
|
|
let added = Channel.api.pointee.addEffect.unsafelyUnwrapped(pointer, effect.pointer) != 0
|
2026-07-24 10:33:45 +02:00
|
|
|
if added, !retainedEffects.contains(where: { $0 === effect }) {
|
|
|
|
|
retainedEffects.append(effect)
|
|
|
|
|
}
|
|
|
|
|
return added
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@discardableResult
|
|
|
|
|
public func removeEffect(_ effect: Effect) -> Bool {
|
2026-07-25 07:23:19 +02:00
|
|
|
let removed = Channel.api.pointee.removeEffect.unsafelyUnwrapped(pointer, effect.pointer) != 0
|
2026-07-24 10:33:45 +02:00
|
|
|
retainedEffects.removeAll { $0 === effect }
|
|
|
|
|
return removed
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// The channel's volume, 0...1.
|
|
|
|
|
public var volume: Float {
|
2026-07-25 07:23:19 +02:00
|
|
|
get { Channel.api.pointee.getVolume.unsafelyUnwrapped(pointer) }
|
|
|
|
|
set { Channel.api.pointee.setVolume.unsafelyUnwrapped(pointer, newValue) }
|
2026-07-24 10:33:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Modulates the channel's volume.
|
|
|
|
|
public var volumeModulator: SignalValue? {
|
2026-07-26 00:20:19 +02:00
|
|
|
get { SignalValue.wrap(Channel.api.pointee.getVolumeModulator.unsafelyUnwrapped(pointer)) }
|
|
|
|
|
set {
|
|
|
|
|
retain(newValue)
|
|
|
|
|
Channel.api.pointee.setVolumeModulator.unsafelyUnwrapped(pointer, newValue?.pointer)
|
|
|
|
|
}
|
2026-07-24 10:33:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// The channel's stereo pan: -1 (left) to 1 (right).
|
|
|
|
|
public func setPan(_ pan: Float) {
|
2026-07-25 07:23:19 +02:00
|
|
|
Channel.api.pointee.setPan.unsafelyUnwrapped(pointer, pan)
|
2026-07-24 10:33:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Modulates the channel's pan. The signal's range 0...1 maps to
|
|
|
|
|
/// left...right.
|
|
|
|
|
public var panModulator: SignalValue? {
|
2026-07-26 00:20:19 +02:00
|
|
|
get { SignalValue.wrap(Channel.api.pointee.getPanModulator.unsafelyUnwrapped(pointer)) }
|
|
|
|
|
set {
|
|
|
|
|
retain(newValue)
|
|
|
|
|
Channel.api.pointee.setPanModulator.unsafelyUnwrapped(pointer, newValue?.pointer)
|
|
|
|
|
}
|
2026-07-24 10:33:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// A signal following the channel's dry (unprocessed) level.
|
|
|
|
|
public var dryLevelSignal: SignalValue? {
|
2026-07-25 07:23:19 +02:00
|
|
|
SignalValue.wrap(Channel.api.pointee.getDryLevelSignal.unsafelyUnwrapped(pointer))
|
2026-07-24 10:33:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// A signal following the channel's wet (processed) level.
|
|
|
|
|
public var wetLevelSignal: SignalValue? {
|
2026-07-25 07:23:19 +02:00
|
|
|
SignalValue.wrap(Channel.api.pointee.getWetLevelSignal.unsafelyUnwrapped(pointer))
|
2026-07-24 10:33:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// The channel's output as a source, for feeding into another channel.
|
2026-07-25 10:28:24 +02:00
|
|
|
/// The same wrapper is returned on every access, so callbacks
|
|
|
|
|
/// registered on it stay valid for the channel's lifetime.
|
2026-07-24 10:33:45 +02:00
|
|
|
public var outputAsSource: Source? {
|
2026-07-25 07:23:19 +02:00
|
|
|
guard let source = Channel.api.pointee.getOutputAsSource.unsafelyUnwrapped(pointer) else {
|
2026-07-24 10:33:45 +02:00
|
|
|
return nil
|
|
|
|
|
}
|
2026-07-25 10:28:24 +02:00
|
|
|
if let cached = cachedOutputSource, cached.pointer == source {
|
|
|
|
|
return cached
|
|
|
|
|
}
|
|
|
|
|
let wrapper = Source(pointer: source, isOwned: false)
|
|
|
|
|
cachedOutputSource = wrapper
|
|
|
|
|
return wrapper
|
2026-07-24 10:33:45 +02:00
|
|
|
}
|
|
|
|
|
|
2026-07-25 10:28:24 +02:00
|
|
|
private var cachedOutputSource: Source?
|
|
|
|
|
|
2026-07-24 10:33:45 +02:00
|
|
|
private func retain(_ modulator: SignalValue?) {
|
|
|
|
|
if let modulator, !retainedModulators.contains(where: { $0 === modulator }) {
|
|
|
|
|
retainedModulators.append(modulator)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|