一个 https://github.com/Acedio/animalese.js 的 Swift 移植版本。
import SwiftUI
import AVFoundation
import Animalese_Swift
struct ContentView: View {
@State var text = "Well, I do have a little musical knowledge..."
@State var pitch = 1.0
@State var shortenWords = false
@State var player: AVAudioPlayer!
private let synth = Animalese(
letterLibrary: NSDataAsset(name: "animalese")!.data,
sampleRate: 44100,
libraryLetterSeconds: 0.15,
outputLetterSeconds: 0.075
)
var body: some View {
NavigationView {
Form {
TextField("Type here...", text: $text)
Slider(value: $pitch, in: 0.2...2.0, step: 0.1, minimumValueLabel: Text("😡"), maximumValueLabel: Text("🐹")) {
Text("Pitch")
}
Toggle(isOn: $shortenWords) {
Text("Words shortening")
}
Button(action: play) {
Text("Play")
}
}.navigationBarTitle(Text("Animalese-Swift Demo"))
}
}
func play() {
let data = synth.synthesize(text, shortenWords: shortenWords, pitch: pitch)
player = try! AVAudioPlayer(data: data)
player.play()
}
}
Animalese-Swift 兼容 SwiftPM。 要安装,请将此软件包添加到您的 Package.swift
文件或您的 Xcode 项目中。