Skip to content

Commit

Permalink
swiftui : added a progress bar to the app via the progress cb
Browse files Browse the repository at this point in the history
  • Loading branch information
PABannier committed May 10, 2024
1 parent a5b3b4c commit 11ed15e
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 5 deletions.
4 changes: 4 additions & 0 deletions examples/bark.swiftui/bark.swiftui.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
E05A4F8C2BEE16590000CD31 /* BarkState.swift in Sources */ = {isa = PBXBuildFile; fileRef = E05A4F8B2BEE16590000CD31 /* BarkState.swift */; };
E05A4F952BEE284C0000CD31 /* AudioPlayer.swift in Sources */ = {isa = PBXBuildFile; fileRef = E05A4F942BEE284C0000CD31 /* AudioPlayer.swift */; };
E05A4F9A2BEE3DF60000CD31 /* bark in Frameworks */ = {isa = PBXBuildFile; productRef = E05A4F992BEE3DF60000CD31 /* bark */; };
E061784F2BEE99AE00497E2F /* ProgressData.swift in Sources */ = {isa = PBXBuildFile; fileRef = E061784E2BEE99AE00497E2F /* ProgressData.swift */; };
/* End PBXBuildFile section */

/* Begin PBXFileReference section */
Expand All @@ -28,6 +29,7 @@
E05A4F942BEE284C0000CD31 /* AudioPlayer.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AudioPlayer.swift; sourceTree = "<group>"; };
E05A4F972BEE3CB90000CD31 /* bark_swift_package */ = {isa = PBXFileReference; lastKnownFileType = wrapper; path = bark_swift_package; sourceTree = "<group>"; };
E05A4F982BEE3CCB0000CD31 /* bark-swiftui-Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist; path = "bark-swiftui-Info.plist"; sourceTree = SOURCE_ROOT; };
E061784E2BEE99AE00497E2F /* ProgressData.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ProgressData.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -110,6 +112,7 @@
isa = PBXGroup;
children = (
E05A4F892BEE15BA0000CD31 /* LibBark.swift */,
E061784E2BEE99AE00497E2F /* ProgressData.swift */,
);
path = Bindings;
sourceTree = "<group>";
Expand Down Expand Up @@ -191,6 +194,7 @@
E05A4F782BEE15150000CD31 /* bark_swiftuiApp.swift in Sources */,
E05A4F952BEE284C0000CD31 /* AudioPlayer.swift in Sources */,
E05A4F8A2BEE15BA0000CD31 /* LibBark.swift in Sources */,
E061784F2BEE99AE00497E2F /* ProgressData.swift in Sources */,
E05A4F8C2BEE16590000CD31 /* BarkState.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down
6 changes: 5 additions & 1 deletion examples/bark.swiftui/bark.swiftui/Bindings/LibBark.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,11 @@ actor BarkContext {
}

static func createContext(path: String, seed: Int) throws -> BarkContext {
let context = bark_load_model(path, bark_verbosity_level(0), UInt32(seed))
var context_params = bark_context_default_params()
context_params.verbosity = bark_verbosity_level(0)
context_params.progress_callback = cCallbackBridge

let context = bark_load_model(path, context_params, UInt32(seed))
if let context {
return BarkContext(context: context)
} else {
Expand Down
39 changes: 39 additions & 0 deletions examples/bark.swiftui/bark.swiftui/Bindings/ProgressData.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
//
// ProgressData.swift
// bark.swiftui
//
// Created by Pierre-Antoine BANNIER on 10/05/2024.
//

import Foundation
import Combine
import bark

class ProgressData: ObservableObject {
static let shared = ProgressData()

@Published var progress: Float = 0.0
@Published var stepTitle: String = "Progress..."

private init() {}
}

func cCallbackBridge(bctx: OpaquePointer?, step: bark_encoding_step, progress: Int32, userData: UnsafeMutableRawPointer?) {
DispatchQueue.main.async {
let progressValue = Float(progress) / 100.0
var stepTitle: String

switch step {
case bark_encoding_step(rawValue: 0):
stepTitle = "Semantic tokens (1/3)"
case bark_encoding_step(rawValue: 1):
stepTitle = "Coarse tokens (2/3)"
default:
stepTitle = "Fine tokens (3/3)"
}

// Update the shared observable object
ProgressData.shared.progress = progressValue
ProgressData.shared.stepTitle = stepTitle
}
}
5 changes: 1 addition & 4 deletions examples/bark.swiftui/bark.swiftui/Models/BarkState.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@
import Foundation
import AVFoundation

let kSampleRate: Double = 44100;
let kNumChannels: UInt32 = 1;


@MainActor
class BarkState: NSObject, ObservableObject {
Expand Down Expand Up @@ -38,7 +35,6 @@ class BarkState: NSObject, ObservableObject {
canGenerate = true
} catch {
print(error.localizedDescription)
messageLog += "\(error.localizedDescription)"
}
}

Expand All @@ -49,6 +45,7 @@ class BarkState: NSObject, ObservableObject {
messageLog += "Loaded model \(modelUrl.lastPathComponent)"
} else {
messageLog += "Could not locate model\n"
throw LoadError.couldNotLocateModel
}
}

Expand Down
12 changes: 12 additions & 0 deletions examples/bark.swiftui/bark.swiftui/Views/ContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import SwiftUI

struct ContentView: View {
@StateObject var barkState = BarkState()
@ObservedObject var progressData = ProgressData.shared
@State private var textInput: String = ""

var body: some View {
Expand Down Expand Up @@ -41,6 +42,17 @@ struct ContentView: View {
.disabled(!barkState.canGenerate)
}

HStack {
Text(verbatim: progressData.stepTitle)

Spacer()

ProgressView(value: progressData.progress)
.frame(width: 150)
}
.frame(maxWidth: .infinity, alignment: .leading)
.padding()

ScrollView {
Text(verbatim: barkState.messageLog)
.frame(maxWidth: .infinity, alignment: .leading)
Expand Down

0 comments on commit 11ed15e

Please sign in to comment.