-
Notifications
You must be signed in to change notification settings - Fork 508
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
1 parent
2f22e6e
commit 94ef692
Showing
12 changed files
with
614 additions
and
2 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
374 changes: 374 additions & 0 deletions
374
ios-swiftui/SherpaOnnxTts/SherpaOnnxTts.xcodeproj/project.pbxproj
Large diffs are not rendered by default.
Oops, something went wrong.
7 changes: 7 additions & 0 deletions
7
...wiftui/SherpaOnnxTts/SherpaOnnxTts.xcodeproj/project.xcworkspace/contents.xcworkspacedata
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
8 changes: 8 additions & 0 deletions
8
...OnnxTts/SherpaOnnxTts.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist
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,8 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> | ||
<plist version="1.0"> | ||
<dict> | ||
<key>IDEDidComputeMac32BitWarning</key> | ||
<true/> | ||
</dict> | ||
</plist> |
11 changes: 11 additions & 0 deletions
11
ios-swiftui/SherpaOnnxTts/SherpaOnnxTts/Assets.xcassets/AccentColor.colorset/Contents.json
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,11 @@ | ||
{ | ||
"colors" : [ | ||
{ | ||
"idiom" : "universal" | ||
} | ||
], | ||
"info" : { | ||
"author" : "xcode", | ||
"version" : 1 | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
ios-swiftui/SherpaOnnxTts/SherpaOnnxTts/Assets.xcassets/AppIcon.appiconset/Contents.json
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,13 @@ | ||
{ | ||
"images" : [ | ||
{ | ||
"idiom" : "universal", | ||
"platform" : "ios", | ||
"size" : "1024x1024" | ||
} | ||
], | ||
"info" : { | ||
"author" : "xcode", | ||
"version" : 1 | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
ios-swiftui/SherpaOnnxTts/SherpaOnnxTts/Assets.xcassets/Contents.json
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,6 @@ | ||
{ | ||
"info" : { | ||
"author" : "xcode", | ||
"version" : 1 | ||
} | ||
} |
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,95 @@ | ||
// | ||
// ContentView.swift | ||
// SherpaOnnxTts | ||
// | ||
// Created by fangjun on 2023/11/23. | ||
// | ||
// Speech-to-text with Next-gen Kaldi on iOS without Internet connection | ||
|
||
import SwiftUI | ||
import AVFoundation | ||
|
||
struct ContentView: View { | ||
@State private var sid = "0" | ||
@State private var speed = 1.0 | ||
@State private var text = "" | ||
@State private var showAlert = false | ||
@State var filename: URL = NSURL() as URL | ||
@State var audioPlayer: AVAudioPlayer! | ||
|
||
private var tts = createOfflineTts() | ||
|
||
var body: some View { | ||
|
||
VStack(alignment: .leading) { | ||
HStack { | ||
Spacer() | ||
Text("Next-gen Kaldi: TTS").font(.title) | ||
Spacer() | ||
} | ||
HStack{ | ||
Text("Speaker ID") | ||
TextField("Please input a speaker ID", text: $sid).textFieldStyle(.roundedBorder) | ||
.keyboardType(.numberPad) | ||
} | ||
HStack{ | ||
Text("Speed \(String(format: "%.1f", speed))") | ||
.padding(.trailing) | ||
Slider(value: $speed, in: 0.5...2.0, step: 0.1) { | ||
Text("Speech speed") | ||
} | ||
} | ||
|
||
Text("Please input your text below").padding([.trailing, .top, .bottom]) | ||
|
||
TextEditor(text: $text) | ||
.font(.body) | ||
.opacity(self.text.isEmpty ? 0.25 : 1) | ||
.disableAutocorrection(true) | ||
.border(Color.black) | ||
|
||
Spacer() | ||
HStack { | ||
Spacer() | ||
Button(action: { | ||
let speakerId = Int(self.sid) ?? 0 | ||
let t = self.text.trimmingCharacters(in: .whitespacesAndNewlines) | ||
if t.isEmpty { | ||
self.showAlert = true | ||
return | ||
} | ||
|
||
let audio = tts.generate(text: t, sid: speakerId, speed: Float(self.speed)) | ||
if self.filename.absoluteString.isEmpty { | ||
let tempDirectoryURL = NSURL.fileURL(withPath: NSTemporaryDirectory(), isDirectory: true) | ||
self.filename = tempDirectoryURL.appendingPathComponent("test.wav") | ||
} | ||
|
||
let ret = audio.save(filename: filename.path) | ||
|
||
self.audioPlayer = try! AVAudioPlayer(contentsOf: filename) | ||
self.audioPlayer.play() | ||
}) { | ||
Text("Generate") | ||
}.alert(isPresented: $showAlert) { | ||
Alert(title: Text("Empty text"), message: Text("Please input your text before clicking the Generate button")) | ||
} | ||
Spacer() | ||
Button (action: { | ||
self.audioPlayer.play() | ||
}) { | ||
Text("Play") | ||
}.disabled(filename.absoluteString.isEmpty) | ||
Spacer() | ||
} | ||
Spacer() | ||
} | ||
.padding() | ||
} | ||
} | ||
|
||
struct ContentView_Previews: PreviewProvider { | ||
static var previews: some View { | ||
ContentView() | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
...swiftui/SherpaOnnxTts/SherpaOnnxTts/Preview Content/Preview Assets.xcassets/Contents.json
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,6 @@ | ||
{ | ||
"info" : { | ||
"author" : "xcode", | ||
"version" : 1 | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
ios-swiftui/SherpaOnnxTts/SherpaOnnxTts/SherpaOnnxTtsApp.swift
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,17 @@ | ||
// | ||
// SherpaOnnxTtsApp.swift | ||
// SherpaOnnxTts | ||
// | ||
// Created by fangjun on 2023/11/23. | ||
// | ||
|
||
import SwiftUI | ||
|
||
@main | ||
struct SherpaOnnxTtsApp: App { | ||
var body: some Scene { | ||
WindowGroup { | ||
ContentView() | ||
} | ||
} | ||
} |
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,68 @@ | ||
// | ||
// ViewModel.swift | ||
// SherpaOnnxTts | ||
// | ||
// Created by fangjun on 2023/11/23. | ||
// | ||
|
||
import Foundation | ||
|
||
func getResource(_ forResource: String, _ ofType: String) -> String { | ||
let path = Bundle.main.path(forResource: forResource, ofType: ofType) | ||
precondition( | ||
path != nil, | ||
"\(forResource).\(ofType) does not exist!\n" + "Remember to change \n" | ||
+ " Build Phases -> Copy Bundle Resources\n" + "to add it!" | ||
) | ||
return path! | ||
} | ||
|
||
/// Please refer to | ||
/// https://k2-fsa.github.io/sherpa/onnx/tts/pretrained_models/index.html | ||
/// to download pre-trained models | ||
|
||
func getTtsForVCTK() -> SherpaOnnxOfflineTtsWrapper { | ||
// See the following link | ||
// https://k2-fsa.github.io/sherpa/onnx/tts/pretrained_models/vits.html#vctk-english-multi-speaker-109-speakers | ||
|
||
// vits-vctk.onnx | ||
let model = getResource("vits-vctk", "onnx") | ||
|
||
// lexicon.txt | ||
let lexicon = getResource("lexicon", "txt") | ||
|
||
// tokens.txt | ||
let tokens = getResource("tokens", "txt") | ||
|
||
let vits = sherpaOnnxOfflineTtsVitsModelConfig(model: model, lexicon: lexicon, tokens: tokens) | ||
let modelConfig = sherpaOnnxOfflineTtsModelConfig(vits: vits) | ||
var config = sherpaOnnxOfflineTtsConfig(model: modelConfig) | ||
return SherpaOnnxOfflineTtsWrapper(config: &config) | ||
} | ||
|
||
func getTtsForAishell3() -> SherpaOnnxOfflineTtsWrapper { | ||
// See the following link | ||
// https://k2-fsa.github.io/sherpa/onnx/tts/pretrained_models/vits.html#vits-model-aishell3 | ||
|
||
// vits-vctk.onnx | ||
let model = getResource("vits-aishell3", "onnx") | ||
|
||
// lexicon.txt | ||
let lexicon = getResource("lexicon", "txt") | ||
|
||
// tokens.txt | ||
let tokens = getResource("tokens", "txt") | ||
|
||
let vits = sherpaOnnxOfflineTtsVitsModelConfig(model: model, lexicon: lexicon, tokens: tokens) | ||
let modelConfig = sherpaOnnxOfflineTtsModelConfig(vits: vits) | ||
var config = sherpaOnnxOfflineTtsConfig(model: modelConfig) | ||
return SherpaOnnxOfflineTtsWrapper(config: &config) | ||
} | ||
|
||
func createOfflineTts() -> SherpaOnnxOfflineTtsWrapper { | ||
return getTtsForVCTK() | ||
|
||
// return getTtsForAishell3() | ||
|
||
// please add more models on need by following the above two examples | ||
} |
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