| 11 | 9 | | |
| 12 | 10 | | |
| 13 | 11 | | |
| 14 | | − | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 15 | 16 | | |
| 16 | 17 | | |
| 17 | | − | final class Recording |
| 18 | + | public final class VoiceRecording |
| 18 | 19 | | { |
| 19 | | − | enum Failure: Error, CustomStringConvertible |
| 20 | + | |
| 21 | + | 1public static let chunkFrameCount = DataChannelMediaStream.frameDuration |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | public enum Failure: Error, CustomStringConvertible |
| 20 | 26 | | { |
| 21 | | − | case cannotOpen(String, Error) |
| 22 | | − | case empty(String) |
| 23 | | − | case cannotConvert(String, AVAudioFormat) |
| 24 | | − | case decodeFailed(String, Error?) |
| 27 | + | |
| 28 | + | 2case cannotOpen(URL, Error) |
| 29 | + | |
| 30 | + | case empty(URL) |
| 31 | + | |
| 32 | + | case cannotConvert(URL, AVAudioFormat) |
| 33 | + | |
| 34 | + | case decodeFailed(URL, Error?) |
| 25 | 35 | | |
| 26 | | − | var description: String |
| 36 | + | public var description: String |
| 27 | 37 | | { |
| 28 | 38 | | switch self |
| 29 | 39 | | { |
| 30 | | − | case .cannotOpen(let path, let underlying): |
| 31 | | − | return "Cannot read \(path) as audio: \(underlying)" |
| 32 | | − | case .empty(let path): |
| 33 | | − | return "\(path) decodes to no audio" |
| 34 | | − | case .cannotConvert(let path, let format): |
| 35 | | − | return "Cannot convert \(path) (\(format)) to 48 kHz mono" |
| 36 | | − | case .decodeFailed(let path, let underlying): |
| 37 | | − | return "Decoding \(path) failed: \(underlying.map { "\($0)" } ?? "converter gave no reason")" |
| 40 | + | case .cannotOpen(let url, let underlying): |
| 41 | + | return "Cannot read \(url.path) as audio: \(underlying)" |
| 42 | + | case .empty(let url): |
| 43 | + | return "\(url.path) decodes to no audio" |
| 44 | + | case .cannotConvert(let url, let format): |
| 45 | + | return "Cannot convert \(url.path) (\(format)) to 48 kHz mono" |
| 46 | + | case .decodeFailed(let url, let underlying): |
| 47 | + | return "Decoding \(url.path) failed: \(underlying.map { "\($0)" } ?? "converter gave no reason")" |
| 38 | 48 | | } |
| 39 | 49 | | } |
| 40 | 50 | | } |
| 41 | 51 | | |
| 42 | | − | |
| 43 | | − | let duration: TimeInterval |
| 52 | + | |
| 53 | + | public let duration: TimeInterval |
| 54 | + | |
| 55 | + | |
| 56 | + | public let url: URL |
| 44 | 57 | | |
| 45 | | − | private let path: String |
| 46 | 58 | | private let file: AVAudioFile |
| 47 | 59 | | private let converter: AVAudioConverter |
| 48 | 60 | | private let input: AVAudioPCMBuffer |
| 49 | 61 | | private let output: AVAudioPCMBuffer |
| 50 | 62 | | |
| 51 | | − | |
| 52 | | − | |
| 53 | | − | |
| 54 | | − | init(path: String) throws |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | public init(url: URL) throws |
| 55 | 68 | | { |
| 56 | | − | self.path = path |
| 57 | | − | do { file = try AVAudioFile(forReading: URL(fileURLWithPath: path)) } |
| 58 | | − | catch { throw Failure.cannotOpen(path, error) } |
| 69 | + | self.url = url |
| 70 | + | do { file = try AVAudioFile(forReading: url) } |
| 71 | + | catch { throw Failure.cannotOpen(url, error) } |
| 59 | 72 | | let source = file.processingFormat |
| 60 | | − | guard file.length > 0 else { throw Failure.empty(path) } |
| 73 | + | guard file.length > 0 else { throw Failure.empty(url) } |
| 61 | 74 | | duration = Double(file.length) / source.sampleRate |
| 62 | 75 | | |
| 63 | 76 | | let target = AVAudioFormat(commonFormat: .pcmFormatFloat32, |