Skip to content

Commit

Permalink
Release 0.2.0 with auto stop and ready to Swift 5.0.
Browse files Browse the repository at this point in the history
  • Loading branch information
alexruperez committed Aug 15, 2019
1 parent af896f6 commit 46de7dd
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 19 deletions.
2 changes: 1 addition & 1 deletion .swift-version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
4.2
5.0
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
# Release 0.2.0

- [x] Added auto stop. Thanks [@Jiawen-Zhang](https://github.com/Jiawen-Zhang)!
- [x] Reset speech recognizer when changing locale. Thanks [@johnjcamilleri](https://github.com/johnjcamilleri)!
- [x] Ready to Swift 5.0.

# Release 0.1.8

- [x] Ready to Swift 4.2. Thanks [@SpectralDragon](https://github.com/SpectralDragon)!
Expand Down
29 changes: 28 additions & 1 deletion Example/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,34 @@ class ViewController: UIViewController {
self.button.play()
}
button.errorHandler = {
self.label.text = $0?.localizedDescription
guard let error = $0 else {
self.label.text = "Unknown error."
return
}
switch error {
case .authorization(let reason):
switch reason {
case .denied:
self.label.text = "Authorization denied."
case .restricted:
self.label.text = "Authorization restricted."
case .usageDescription(let key):
self.label.text = "Info.plist \"\(key)\" key is missing."
}
case .cancelled(let reason):
switch reason {
case .notFound:
self.label.text = "Cancelled, not found."
case .user:
self.label.text = "Cancelled by user."
}
case .invalid(let locale):
self.label.text = "Locale \"\(locale)\" not supported."
case .unknown(let unknownError):
self.label.text = unknownError?.localizedDescription
default:
self.label.text = error.localizedDescription
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion SpeechRecognizerButton.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |s|
s.name = 'SpeechRecognizerButton'
s.version = '0.1.8'
s.version = '0.2.0'
s.summary = 'UIButton subclass with push to talk recording, speech recognition and Siri-style waveform view'

s.homepage = 'https://github.com/alexruperez/SpeechRecognizerButton'
Expand Down
12 changes: 6 additions & 6 deletions SpeechRecognizerButton.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -490,8 +490,8 @@
COPY_PHASE_STRIP = NO;
CURRENT_PROJECT_VERSION = "$(DYLIB_CURRENT_VERSION)";
DEBUG_INFORMATION_FORMAT = dwarf;
DYLIB_COMPATIBILITY_VERSION = 0.1.6;
DYLIB_CURRENT_VERSION = 0.1.6;
DYLIB_COMPATIBILITY_VERSION = 0.2.0;
DYLIB_CURRENT_VERSION = 0.2.0;
ENABLE_STRICT_OBJC_MSGSEND = YES;
ENABLE_TESTABILITY = YES;
GCC_C_LANGUAGE_STANDARD = gnu11;
Expand All @@ -514,7 +514,7 @@
SDKROOT = iphoneos;
SWIFT_ACTIVE_COMPILATION_CONDITIONS = DEBUG;
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
SWIFT_VERSION = 4.2;
SWIFT_VERSION = 5.0;
TARGETED_DEVICE_FAMILY = "1,2";
VERSIONING_SYSTEM = "apple-generic";
VERSION_INFO_BUILDER = alexruperez;
Expand Down Expand Up @@ -557,8 +557,8 @@
COPY_PHASE_STRIP = NO;
CURRENT_PROJECT_VERSION = "$(DYLIB_CURRENT_VERSION)";
DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym";
DYLIB_COMPATIBILITY_VERSION = 0.1.6;
DYLIB_CURRENT_VERSION = 0.1.6;
DYLIB_COMPATIBILITY_VERSION = 0.2.0;
DYLIB_CURRENT_VERSION = 0.2.0;
ENABLE_NS_ASSERTIONS = NO;
ENABLE_STRICT_OBJC_MSGSEND = YES;
GCC_C_LANGUAGE_STANDARD = gnu11;
Expand All @@ -573,7 +573,7 @@
MTL_ENABLE_DEBUG_INFO = NO;
SDKROOT = iphoneos;
SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule";
SWIFT_VERSION = 4.2;
SWIFT_VERSION = 5.0;
TARGETED_DEVICE_FAMILY = "1,2";
VALIDATE_PRODUCT = YES;
VERSIONING_SYSTEM = "apple-generic";
Expand Down
21 changes: 11 additions & 10 deletions SpeechRecognizerButton/SFButton.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,13 @@ open class SFButton: UIButton {
AVNumberOfChannelsKey: 1,
AVEncoderAudioQualityKey: AVAudioQuality.high.rawValue]
@IBInspectable public var maxDuration: Double = 60
public var locale = Locale.autoupdatingCurrent
public var locale = Locale.autoupdatingCurrent { willSet { speechRecognizer = nil } }
public var taskHint = SFSpeechRecognitionTaskHint.unspecified
public var queue = OperationQueue.main
public var contextualStrings = [String]()
public var interactionIdentifier: String?
@IBInspectable public var autoStop: Bool = true
@IBInspectable public var autoStopTime: UInt = 120
@IBInspectable public var autoStopPower: Float = -10
@IBInspectable public var pushToTalk: Bool = true
@IBInspectable public var speechRecognition: Bool = true
@IBInspectable public var cancelOnDrag: Bool = true
Expand All @@ -66,7 +67,7 @@ open class SFButton: UIButton {
@IBInspectable public var disabledColor: UIColor?
@IBInspectable public var highlightedAlpha: CGFloat = 0.5

private var counter: NSInteger = 0
private var autoStopCounter: UInt = 0
private var audioPlayer: AVAudioPlayer?
private var audioRecorder: AVAudioRecorder?
fileprivate var audioPlayerDisplayLink: CADisplayLink?
Expand Down Expand Up @@ -179,7 +180,7 @@ open class SFButton: UIButton {
}
audioRecorderDisplayLink?.isPaused = false
waveformView(show: true, animationDuration: animationDuration)
counter = 0
autoStopCounter = 0
}

private func endRecord() {
Expand Down Expand Up @@ -216,13 +217,13 @@ open class SFButton: UIButton {
let normalizedValue = pow(10, averagePower / 20)
waveformView?.updateWithLevel(CGFloat(normalizedValue))

if (averagePower < -10) {
counter += 1
if (counter / 60 >= 2) {
if (averagePower < autoStopPower) {
autoStopCounter += 1
if (autoStopCounter >= autoStopTime) {
endRecord()
}
} else {
counter = 0
autoStopCounter = 0
}
}

Expand Down Expand Up @@ -300,7 +301,7 @@ open class SFButton: UIButton {
switch audioSession.recordPermission {
case .granted: handler?(nil)
case .denied: handler?(.authorization(reason: .denied))
case .undetermined:
default:
audioSession.requestRecordPermission({ _ in
self.checkRecordAuthorization(handler)
})
Expand All @@ -321,7 +322,7 @@ open class SFButton: UIButton {
case .authorized: handler?(nil)
case .denied: handler?(.authorization(reason: .denied))
case .restricted: handler?(.authorization(reason: .restricted))
case .notDetermined:
default:
SFSpeechRecognizer.requestAuthorization { _ in
self.checkSpeechRecognizerAuthorization(handler)
}
Expand Down

0 comments on commit 46de7dd

Please sign in to comment.