Skip to content

Commit

Permalink
Add SpeechStore
Browse files Browse the repository at this point in the history
  • Loading branch information
Ihor Makhnyk committed Nov 20, 2023
1 parent ddee5bf commit bbb1895
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 45 deletions.
42 changes: 0 additions & 42 deletions Demo/DemoChat/Sources/MiscStore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

import UIKit
import OpenAI
import AVFAudio

public final class MiscStore: ObservableObject {
public var openAIClient: OpenAIProtocol
Expand Down Expand Up @@ -92,45 +91,4 @@ public final class MiscStore: ObservableObject {
moderationConversationError = error
}
}

// MARK: - Speech
struct AudioObject: Identifiable {
let id = UUID()
let prompt: String
let audioPlayer: AVAudioPlayer?
let originResponse: AudioSpeechResult
let format: String
}

@Published var audioObjects: [AudioObject] = []

@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)
}
}
}

}
60 changes: 60 additions & 0 deletions Demo/DemoChat/Sources/SpeechStore.swift
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)
}
}
}

}
2 changes: 1 addition & 1 deletion Demo/DemoChat/Sources/UI/Misc/MiscView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public struct MiscView: View {
NavigationLink("Moderation Chat", destination: ModerationChatView(store: store))
}
Section(header: Text("Audio")) {
NavigationLink("Create Speech", destination: TextToSpeechView(store: store))
NavigationLink("Create Speech", destination: TextToSpeechView(store: SpeechStore(openAIClient: store.openAIClient)))
}
}
.listStyle(.insetGrouped)
Expand Down
4 changes: 2 additions & 2 deletions Demo/DemoChat/Sources/UI/TextToSpeechView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ import UIKit

public struct TextToSpeechView: View {

@ObservedObject var store: MiscStore
@ObservedObject var store: SpeechStore

@State private var prompt: String = ""
@State private var voice: AudioSpeechQuery.AudioSpeechVoice = .alloy
@State private var speed: Double = 1
@State private var responseFormat: AudioSpeechQuery.AudioSpeechResponseFormat = .mp3

public init(store: MiscStore) {
public init(store: SpeechStore) {
self.store = store
}

Expand Down

0 comments on commit bbb1895

Please sign in to comment.