From 32fcfa39d8fa837066af52f6e5b8d8bbf2bbb64b Mon Sep 17 00:00:00 2001 From: Nathan Perkins Date: Mon, 10 Oct 2016 14:15:23 -0400 Subject: [PATCH] Update command line interface for Swift 3 Still awaiting an update to the CommandLine framework. --- SyllableDetector.xcodeproj/project.pbxproj | 3 +++ SyllableDetectorCLI/OutputStream.swift | 16 ++++--------- SyllableDetectorCLI/TrackDetector.swift | 2 +- SyllableDetectorCLI/main.swift | 26 +++++++++++----------- 4 files changed, 21 insertions(+), 26 deletions(-) diff --git a/SyllableDetector.xcodeproj/project.pbxproj b/SyllableDetector.xcodeproj/project.pbxproj index 8501b91..76cde2d 100644 --- a/SyllableDetector.xcodeproj/project.pbxproj +++ b/SyllableDetector.xcodeproj/project.pbxproj @@ -326,6 +326,7 @@ }; D8E2DB501D00C74500FC6AE2 = { CreatedOnToolsVersion = 7.3.1; + LastSwiftMigration = 0800; }; }; }; @@ -574,6 +575,7 @@ CLANG_ANALYZER_NONNULL = YES; PRODUCT_NAME = "$(TARGET_NAME)"; SWIFT_OBJC_BRIDGING_HEADER = "Common/Common-Bridging-Header.h"; + SWIFT_VERSION = 3.0; }; name = Debug; }; @@ -584,6 +586,7 @@ PRODUCT_NAME = "$(TARGET_NAME)"; SWIFT_OBJC_BRIDGING_HEADER = "Common/Common-Bridging-Header.h"; SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule"; + SWIFT_VERSION = 3.0; }; name = Release; }; diff --git a/SyllableDetectorCLI/OutputStream.swift b/SyllableDetectorCLI/OutputStream.swift index a20b158..2f7d912 100644 --- a/SyllableDetectorCLI/OutputStream.swift +++ b/SyllableDetectorCLI/OutputStream.swift @@ -8,22 +8,14 @@ import Foundation -extension OutputStreamType { +extension TextOutputStream { mutating func writeLine(string: String) { self.write("\(string)\n") } } -class StandardOutputStream: OutputStreamType { - func write(string: String) { - let stdout = NSFileHandle.fileHandleWithStandardOutput() - stdout.writeData(string.dataUsingEncoding(NSUTF8StringEncoding)!) - } -} - -class StandardErrorOutputStream: OutputStreamType { - func write(string: String) { - let stderr = NSFileHandle.fileHandleWithStandardError() - stderr.writeData(string.dataUsingEncoding(NSUTF8StringEncoding)!) +extension FileHandle { + func writeLine(_ string: String) { + write("\(string)\n".data(using: String.Encoding.utf8)!) } } diff --git a/SyllableDetectorCLI/TrackDetector.swift b/SyllableDetectorCLI/TrackDetector.swift index 73f485a..dbca549 100644 --- a/SyllableDetectorCLI/TrackDetector.swift +++ b/SyllableDetectorCLI/TrackDetector.swift @@ -69,7 +69,7 @@ class TrackDetector // look for detection var hasDetection = false - for (i, d) in detector.lastOutputs.enumerate() { + for (i, d) in detector.lastOutputs.enumerated() { if Double(d) >= detector.config.thresholds[i] { hasDetection = true break diff --git a/SyllableDetectorCLI/main.swift b/SyllableDetectorCLI/main.swift index adf2da9..76002bf 100644 --- a/SyllableDetectorCLI/main.swift +++ b/SyllableDetectorCLI/main.swift @@ -11,7 +11,7 @@ import AVFoundation // UTILITY -private var stderr = StandardErrorOutputStream() +private var stderr = FileHandle.standardError // PARSE COMMAND LINE @@ -60,7 +60,7 @@ do { config = try SyllableDetectorConfig(fromTextFile: networkPath.value!) } catch { - print("Unable to load the network configuration: \(error)", toStream: &stderr) + stderr.writeLine("Unable to load the network configuration: \(error)") fatalError() } @@ -71,39 +71,39 @@ audioPaths.value!.forEach { // 2b. open asset - let assetRead = AVAsset(URL: NSURL(fileURLWithPath: audioPath)) + let assetRead = AVAsset(url: URL(fileURLWithPath: audioPath)) let avReader: AVAssetReader do { avReader = try AVAssetReader(asset: assetRead) } catch { - print("Unable to read \(audioPath): \(error)", toStream: &stderr) + stderr.writeLine("Unable to read \(audioPath): \(error)") return } // get number of audio tracks - let tracksAudio = assetRead.tracksWithMediaType(AVMediaTypeAudio) + let tracksAudio = assetRead.tracks(withMediaType: AVMediaTypeAudio) guard 0 < tracksAudio.count else { - print("No audio tracks found in \(audioPath).", toStream: &stderr) + stderr.writeLine("No audio tracks found in \(audioPath).") return } // make detectors - let potentialTrackDetectors = tracksAudio.enumerate().map { + let potentialTrackDetectors = tracksAudio.enumerated().map { (i, track) in return TrackDetector(track: track, config: config, channel: i) } // validate let trackDetectors = potentialTrackDetectors.filter { - return avReader.canAddOutput($0.reader) + return avReader.canAdd($0.reader) } if trackDetectors.count == 0 { - print("Can not read audio tracks found in \(audioPath).", toStream: &stderr) + stderr.writeLine("Can not read audio tracks found in \(audioPath).") return } if trackDetectors.count < potentialTrackDetectors.count { - print("Can not read from \(potentialTrackDetectors.count - trackDetectors.count) audio track(s) in \(audioPath). Skipping those tracks.", toStream: &stderr) + stderr.writeLine("Can not read from \(potentialTrackDetectors.count - trackDetectors.count) audio track(s) in \(audioPath). Skipping those tracks.") } // add all @@ -114,12 +114,12 @@ audioPaths.value!.forEach { } // add it - avReader.addOutput($0.reader) + avReader.add($0.reader) } // start reading if !avReader.startReading() { - print("Can not start reading \(audioPath): \(avReader.error).", toStream: &stderr) + stderr.writeLine("Can not start reading \(audioPath): \(avReader.error).") return } @@ -129,7 +129,7 @@ audioPaths.value!.forEach { print("\(audioPath)") } - while avReader.status == AVAssetReaderStatus.Reading { + while avReader.status == AVAssetReaderStatus.reading { for trackDetector in trackDetectors { trackDetector.process() }