Skip to content

Commit

Permalink
Update command line interface for Swift 3
Browse files Browse the repository at this point in the history
Still awaiting an update to the CommandLine framework.
  • Loading branch information
nathanntg committed Oct 10, 2016
1 parent 6fd171d commit 32fcfa3
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 26 deletions.
3 changes: 3 additions & 0 deletions SyllableDetector.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,7 @@
};
D8E2DB501D00C74500FC6AE2 = {
CreatedOnToolsVersion = 7.3.1;
LastSwiftMigration = 0800;
};
};
};
Expand Down Expand Up @@ -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;
};
Expand All @@ -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;
};
Expand Down
16 changes: 4 additions & 12 deletions SyllableDetectorCLI/OutputStream.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)!)
}
}
2 changes: 1 addition & 1 deletion SyllableDetectorCLI/TrackDetector.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
26 changes: 13 additions & 13 deletions SyllableDetectorCLI/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import AVFoundation

// UTILITY

private var stderr = StandardErrorOutputStream()
private var stderr = FileHandle.standardError

// PARSE COMMAND LINE

Expand Down Expand Up @@ -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()
}

Expand All @@ -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
Expand 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
}

Expand All @@ -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()
}
Expand Down

0 comments on commit 32fcfa3

Please sign in to comment.