Skip to content

Instantly share code, notes, and snippets.

protocol EmotionClassifierDelegate {
func displayPredictionResult(identifier: String, confidence: Double)
}
extension SoundAnalyzerController: EmotionClassifierDelegate {
func displayPredictionResult(identifier: String, confidence: Double) {
DispatchQueue.main.async {
let roundConfidence = Double(round(100*confidence)/100)
self.transcriberText.text = ("Recognition: \(identifier) with Confidence \(roundConfidence)")
private func startAudioEngine() {
do {
let request = try SNClassifySoundRequest(mlModel: soundClassifier.model)
try analyzer.add(request, withObserver: resultsObserver)
} catch {
print("Unable to prepare request: \(error.localizedDescription)")
return
}
import UIKit
import SoundAnalysis
class SoundAnalyzerController: UIViewController {
private let audioEngine = AVAudioEngine()
private var soundClassifier = EmotionModel()
var inputFormat: AVAudioFormat!
var analyzer: SNAudioStreamAnalyzer!
struct ContentView: View {
@State private var currentPage = 0
var body: some View {
//Pager Manager
VStack{
PagerManager(pageCount: 2, currentIndex: $currentPage) {
Text("First page")
Text("Second page")
struct PagerManager<Content: View>: View {
let pageCount: Int
@Binding var currentIndex: Int
let content: Content
//Set the initial values for the variables
init(pageCount: Int, currentIndex: Binding<Int>, @ViewBuilder content: () -> Content) {
self.pageCount = pageCount
self._currentIndex = currentIndex
self.content = content()
private func process(_ samples: [HKQuantitySample], type: HKQuantityTypeIdentifier) {
var lastHeartRate = 0.0
for sample in samples {
if type == .heartRate {
lastHeartRate = sample.quantity.doubleValue(for: heartRateQuantity)
}
self.value = Int(lastHeartRate)
}
func autorizeHealthKit() {
let healthKitTypes: Set = [
HKObjectType.quantityType(forIdentifier: HKQuantityTypeIdentifier.heartRate)!]
healthStore.requestAuthorization(toShare: healthKitTypes, read: healthKitTypes) { _, _ in }
}