36 lines
1.2 KiB
Swift
36 lines
1.2 KiB
Swift
import AVFoundation
|
|
|
|
/// Decoder for formats Apple doesn't handle (Ogg Vorbis/Opus, WMA, APE, WavPack, Musepack, DSD, trackers…).
|
|
/// Backed by FFmpeg (Native/ffdecode.c) when the build links it (MULBERRY_FFMPEG); otherwise nothing opens.
|
|
final class FFSource: AudioSource {
|
|
#if MULBERRY_FFMPEG
|
|
private let d: OpaquePointer
|
|
let frames: AVAudioFramePosition
|
|
|
|
private init(_ d: OpaquePointer) {
|
|
self.d = d
|
|
frames = AVAudioFramePosition(ff_frames(d))
|
|
}
|
|
|
|
deinit { ff_close(d) }
|
|
|
|
static func open(_ url: URL) -> FFSource? {
|
|
guard let d = ff_open(url.path) else { return nil }
|
|
return FFSource(d)
|
|
}
|
|
|
|
func read(into buffer: AVAudioPCMBuffer) -> Bool {
|
|
guard let ch = buffer.floatChannelData else { return false }
|
|
let n = ff_read(d, ch[0], ch[1], Int32(buffer.frameCapacity))
|
|
buffer.frameLength = AVAudioFrameCount(max(0, n))
|
|
return n > 0
|
|
}
|
|
|
|
func seek(to frame: AVAudioFramePosition) { ff_seek(d, Int64(frame)) }
|
|
#else
|
|
static func open(_ url: URL) -> FFSource? { nil }
|
|
var frames: AVAudioFramePosition { 0 }
|
|
func read(into buffer: AVAudioPCMBuffer) -> Bool { false }
|
|
func seek(to frame: AVAudioFramePosition) {}
|
|
#endif
|
|
}
|