Skip to content

Commit

Permalink
example : bark.swiftui (#169)
Browse files Browse the repository at this point in the history
  • Loading branch information
PABannier committed May 10, 2024
1 parent a493125 commit cbba964
Show file tree
Hide file tree
Showing 35 changed files with 820 additions and 2 deletions.
12 changes: 12 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -136,3 +136,15 @@ jobs:
run: |
emcmake cmake . -DCMAKE_BUILD_TYPE=${{ matrix.build }}
make
ios:
runs-on: macos-latest

steps:
- name: Clone
uses: actions/checkout@v4
with:
submodules: recursive

- name: Build swiftui example
run: xcodebuild -project examples/bark.swiftui/bark.swiftui.xcodeproj -scheme bark.swiftui -sdk iphonesimulator build
44 changes: 44 additions & 0 deletions Package.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// swift-tools-version:5.5

import PackageDescription

let package = Package(
name: "bark",
platforms: [
.macOS(.v12),
.iOS(.v14),
.watchOS(.v4),
.tvOS(.v14)
],
products: [
.library(name: "bark", targets: ["bark"]),
],
targets: [
.target(
name: "bark",
path: ".",
sources: [
"bark.cpp",
"encodec.cpp",
"ggml-alloc.c",
"ggml-backend.c",
"ggml.c",
],
publicHeadersPath: "include",
cSettings: [
.unsafeFlags(["-Wno-shorten-64-to-32", "-O3", "-DNDEBUG"]),
.define("GGML_USE_ACCELERATE"),
.unsafeFlags(["-fno-objc-arc"]),
// NOTE: NEW_LAPACK will required iOS version 16.4+
// We should consider add this in the future when we drop support for iOS 14
// (ref: ref: https://developer.apple.com/documentation/accelerate/1513264-cblas_sgemm?language=objc)
// .define("ACCELERATE_NEW_LAPACK"),
// .define("ACCELERATE_LAPACK_ILP64")
],
linkerSettings: [
.linkedFramework("Accelerate")
]
)
],
cxxLanguageStandard: .cxx11
)
3 changes: 2 additions & 1 deletion bark.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1189,6 +1189,8 @@ static bool bark_load_model_from_file(

// codec model
{
// not optimal: we close the file and reopen it using Encodec.cpp library with a
// specific offset
const int offset = fin.tellg();
fin.close();

Expand All @@ -1201,7 +1203,6 @@ static bool bark_load_model_from_file(

printf("\n");

fin.close();

return true;
}
Expand Down
2 changes: 1 addition & 1 deletion bark.h
Original file line number Diff line number Diff line change
Expand Up @@ -219,4 +219,4 @@ extern "C" {

#ifdef __cplusplus
}
#endif
#endif
5 changes: 5 additions & 0 deletions examples/bark.swiftui/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
xcuserdata
xcshareddata

!Models
!bark-swiftui-Info.plist
13 changes: 13 additions & 0 deletions examples/bark.swiftui/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
A sample SwiftUI app using [bark.cpp](https://github.com/PABannier/bark.cpp/) to do audio generation locally.

**Usage**:

1. Select a model from the [bark.cpp repository](https://github.com/PABannier/bark.cpp/tree/master/models).[^1]
2. Add the model to `bark.swiftui/` **via Xcode**. If you drag and drop the model in the Xcode project outside of Xcode, the model weights won't be automatically added to the target.
3. Select the "Release" [^2] build configuration under "Run", then deploy and run to your device.

**Note:** Pay attention to the folder path: `whisper.swiftui.demo/Resources/models` is the appropriate directory to place resources whilst `whisper.swiftui.demo/Models` is related to actual code.

[^1]: I recommend the Bark small model for running on an iOS device.

[^2]: The `Release` build can boost performance of audio generation. In this project, it also added `-O3 -DNDEBUG` to `Other C Flags`, but adding flags to app proj is not ideal in real world (applies to all C/C++ files), consider splitting xcodeproj in workspace in your own project.
5 changes: 5 additions & 0 deletions examples/bark.swiftui/bark-swiftui-Info.plist
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http:https://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict/>
</plist>

0 comments on commit cbba964

Please sign in to comment.