-
Notifications
You must be signed in to change notification settings - Fork 374
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Ihor Makhnyk
committed
Nov 20, 2023
1 parent
ddee5bf
commit bbb1895
Showing
4 changed files
with
63 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
// | ||
// SpeechStore.swift | ||
// | ||
// | ||
// Created by Ihor Makhnyk on 20.11.2023. | ||
// | ||
|
||
import OpenAI | ||
import SwiftUI | ||
import AVFAudio | ||
|
||
public final class SpeechStore: ObservableObject { | ||
public var openAIClient: OpenAIProtocol | ||
|
||
@Published var audioObjects: [AudioObject] = [] | ||
|
||
public init( | ||
openAIClient: OpenAIProtocol | ||
) { | ||
self.openAIClient = openAIClient | ||
} | ||
|
||
struct AudioObject: Identifiable { | ||
let id = UUID() | ||
let prompt: String | ||
let audioPlayer: AVAudioPlayer? | ||
let originResponse: AudioSpeechResult | ||
let format: String | ||
} | ||
|
||
@MainActor | ||
func createSpeech(_ query: AudioSpeechQuery) async { | ||
guard let input = query.input, !input.isEmpty else { return } | ||
do { | ||
let response = try await openAIClient.audioCreateSpeech(query: query) | ||
guard let data = response.audioData else { return } | ||
let player = try? AVAudioPlayer(data: data) | ||
let audioObject = AudioObject(prompt: input, | ||
audioPlayer: player, | ||
originResponse: response, | ||
format: query.responseFormat.rawValue) | ||
audioObjects.append(audioObject) | ||
} catch { | ||
print(error.localizedDescription) | ||
} | ||
} | ||
|
||
func getFileInDocumentsDirectory(_ data: Data, fileName: String, _ dir: @escaping (URL) -> Void) { | ||
if let fileURL = try? FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: true) { | ||
let saveURL = fileURL.appendingPathComponent(fileName) | ||
do { | ||
try data.write(to: saveURL) | ||
dir(saveURL) | ||
} catch { | ||
print(error.localizedDescription) | ||
} | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters