Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Save, Read and Delete Audio files,and add Audio Visualizer for display #38

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Example/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ EXTERNAL SOURCES:
SPEC CHECKSUMS:
OSSSpeechKit: ea0fd8151e7e338bc6ddc6bb749455fc3b33cfde

PODFILE CHECKSUM: 619c7767d93bbf8bc7a5c2d0a1d118e435561c49
PODFILE CHECKSUM: 74abb7e61e1f9880a3040420923d3dad8dfbc311

COCOAPODS: 1.11.3
COCOAPODS: 1.12.0
57 changes: 56 additions & 1 deletion OSSSpeechKit/Classes/OSSSpeech.swift
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,8 @@ public enum OSSSpeechRecognitionTaskType: Int {
public protocol OSSSpeechDelegate: AnyObject {
/// When the microphone has finished accepting audio, this delegate will be called with the final best text output.
func didFinishListening(withText text: String)
///When the microphone has finished accepting audio, this delegate will be called with the final best text output or voice file path.
func didFinishListening(withAudioFileURL url:URL,withText text:String)
/// Handle returning authentication status to user - primary use is for non-authorized state.
func authorizationToMicrophone(withAuthentication type: OSSSpeechKitAuthorizationStatus)
/// If the speech recogniser and request fail to set up, this method will be called.
Expand All @@ -165,6 +167,9 @@ public class OSSSpeech: NSObject {

// MARK: - Private Properties

private var audioRecorder:AVAudioRecorder?
private var audioFileURL:URL!

/// An object that produces synthesized speech from text utterances and provides controls for monitoring or controlling ongoing speech.
private var speechSynthesizer: AVSpeechSynthesizer!

Expand Down Expand Up @@ -344,8 +349,9 @@ public class OSSSpeech: NSObject {
#if !os(macOS)
do {
let category: AVAudioSession.Category = isRecording ? .playAndRecord : .playback
try audioSession.setCategory(category, options: .duckOthers)
try audioSession.setCategory(category, options: isRecording ? .defaultToSpeaker : .duckOthers)
try audioSession.setActive(true, options: .notifyOthersOnDeactivation)
isRecording ? try audioSession.setActive(true) : try audioSession.setActive(true, options: .notifyOthersOnDeactivation)
return true
} catch {
if isRecording {
Expand Down Expand Up @@ -422,6 +428,9 @@ public class OSSSpeech: NSObject {
}
let node = engine.inputNode
node.removeTap(onBus: 0)

audioRecorder?.stop()

if node.inputFormat(forBus: 0).channelCount == 0 {
node.reset()
}
Expand Down Expand Up @@ -535,6 +544,40 @@ public class OSSSpeech: NSObject {
delegate?.didFailToCommenceSpeechRecording()
delegate?.didFailToProcessRequest(withError: OSSSpeechKitErrorType.invalidSpeechRequest.error)
}

readyToRecord()
}

func readyToRecord()
{
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
let dateString = dateFormatter.string(from: Date())

self.audioFileURL = getDocumentsDirectory().appendingPathComponent("\(dateString).m4a")

let audioSettings = [AVFormatIDKey: Int(kAudioFormatMPEG4AAC),
AVSampleRateKey: 12000,
AVNumberOfChannelsKey: 1,
AVEncoderAudioQualityKey: AVAudioQuality.high.rawValue
]

do {
audioRecorder = try AVAudioRecorder(url: audioFileURL, settings: audioSettings)
audioRecorder?.delegate = self
audioRecorder?.record()

}
catch
{
print(error.localizedDescription)
}
}

func getDocumentsDirectory() -> URL {
let paths = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)
let documentsDirectory = paths[0]
return documentsDirectory
}
}

Expand All @@ -547,6 +590,7 @@ extension OSSSpeech: SFSpeechRecognitionTaskDelegate, SFSpeechRecognizerDelegate
public func speechRecognitionTask(_ task: SFSpeechRecognitionTask, didFinishSuccessfully successfully: Bool) {
recognitionTask = nil
delegate?.didFinishListening(withText: spokenText)
delegate?.didFinishListening(withAudioFileURL: audioFileURL, withText: spokenText)
setSession(isRecording: false)
}

Expand All @@ -568,6 +612,17 @@ extension OSSSpeech: SFSpeechRecognitionTaskDelegate, SFSpeechRecognizerDelegate

/// Docs available by Google searching for SFSpeechRecognizerDelegate
public func speechRecognizer(_ speechRecognizer: SFSpeechRecognizer, availabilityDidChange available: Bool) {}
}

//MARK: AVAudioRecorderDelegate
extension OSSSpeech:AVAudioRecorderDelegate
{
public func audioRecorderDidFinishRecording(_ recorder: AVAudioRecorder, successfully flag: Bool) {
if flag
{
audioRecorder?.stop()
print("Audio file save")
}
}
}
#endif