Skip to content

Commit

Permalink
Fixed a few memory issues and added a throwing a few errors when some…
Browse files Browse the repository at this point in the history
…thing goes wrong
  • Loading branch information
IhorShevchuk committed Apr 10, 2024
1 parent ff15abb commit e33f81d
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 21 deletions.
33 changes: 22 additions & 11 deletions Sources/RHVoiceSwift/RHSpeechSynthesizer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@ import AVFoundation
#endif

public class RHSpeechSynthesizer {

#if canImport(AVFoundation)
enum SynthesizerError: Error {
case noPlayer
}
#endif

public struct Params {
// TODO: have logger protocol(RHVoiceLoggerProtocol) and add variable of it's type here
public var dataPath: String
Expand Down Expand Up @@ -46,7 +53,7 @@ public class RHSpeechSynthesizer {
#if canImport(AVFoundation)
public func speak(utterance: RHSpeechUtterance) async throws {
let path = String.temporaryPath(extesnion: "wav")
await synthesize(utterance: utterance, to: path)
try await synthesize(utterance: utterance, to: path)
let playerItem = AVPlayerItem(url: URL(fileURLWithPath: path))
try await playItemAsync(playerItem)
try FileManager.default.removeItem(atPath: path)
Expand All @@ -62,35 +69,39 @@ public class RHSpeechSynthesizer {
}
#endif

public func synthesize(utterance: RHSpeechUtterance, to path: String) async {
public func synthesize(utterance: RHSpeechUtterance, to path: String) async throws {

fileStream = PlayerLib.FilePlaybackStream(path)
defer {
fileStream = nil
}

let context = Unmanaged.passRetained(self).toOpaque()

var params = utterance.synthParams
var params = try utterance.synthParams

let paramsAddress = withUnsafePointer(to: &params) { pointer in
UnsafePointer<RHVoice_synth_params>(pointer)
}

if utterance.empty {
if utterance.isEmpty {
return
}

let text: String = utterance.ssml ?? ""
guard let text: String = utterance.ssml else {
return
}
let message = RHVoice_new_message(rhVoiceEngine,
text,
UInt32(text.count),
RHVoice_message_ssml,
paramsAddress,
context)
defer {
RHVoice_delete_message(message)
}

_ = RHVoice_speak(message)

RHVoice_delete_message(message)

fileStream = nil
}

var rhVoiceEngine: RHVoice_tts_engine?
Expand Down Expand Up @@ -188,8 +199,9 @@ private extension RHSpeechSynthesizer {
var statusObserver: NSKeyValueObservation?
try await withCheckedThrowingContinuation { [weak self] continuation in

let continuation = continuation as CheckedContinuation<Void, any Error>
guard let player = self?.player else {
continuation.resume() // TODO: throw an error here
continuation.resume(throwing: SynthesizerError.noPlayer)
return
}

Expand All @@ -215,7 +227,6 @@ private extension RHSpeechSynthesizer {
statusObserver?.invalidate()
self.playerContinuation = nil
await stopAndCancel()
print("finish")
}
#endif

Expand Down
29 changes: 19 additions & 10 deletions Sources/RHVoiceSwift/RHSpeechUtterance.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,12 @@ public struct RHSpeechUtterance {
case Max
}

enum UtteranceError: Error {
case noVoiceProvided
}

public let ssml: String?
var empty: Bool {
var isEmpty: Bool {
return self.ssml?.isEmpty == true
|| self.ssml == nil
|| self.ssml == "<speak></speak>"
Expand All @@ -29,15 +33,20 @@ public struct RHSpeechUtterance {
public var quality: Quality = .Standart

var synthParams: RHVoice_synth_params {
var result = RHVoice_synth_params()
result.absolute_pitch = 0.0
result.absolute_rate = 0.0
result.absolute_volume = 0.0
result.relative_rate = rate
result.relative_pitch = pitch
result.relative_volume = volume
result.voice_profile = voice!.identifier.toPointer()
return result
get throws {
var result = RHVoice_synth_params()
result.absolute_pitch = 0.0
result.absolute_rate = 0.0
result.absolute_volume = 0.0
result.relative_rate = rate
result.relative_pitch = pitch
result.relative_volume = volume
guard let voice else {
throw UtteranceError.noVoiceProvided
}
result.voice_profile = voice.identifier.toPointer()
return result
}
}

public init(ssml: String?) {
Expand Down

0 comments on commit e33f81d

Please sign in to comment.