This repository has been archived by the owner on Oct 24, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSFX Processor.swift
141 lines (130 loc) · 5.09 KB
/
SFX Processor.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
//
// SFX Processor.swift
// Radio Automne
//
// Created by Aydar Nasibullin on 01.10.2020.
// Copyright © 2020-2021 Fetch Development. All rights reserved.
//
import Foundation
import AVFoundation
import Cocoa
class SFX {
private static var player = AVAudioPlayer()
private static let synth = AVSpeechSynthesizer()
public static var voiceIdentifier: String? = nil
private static let preferredVoices = ["ava.premium", "samantha.premium", "daniel", "alex"]
public enum Effects: String {
case powerOn = "PowOn"
case buttonClick = "Button"
case radioSetup = "RadioSetup"
}
public static func playSFX(sfx: Effects){
print(AVSpeechSynthesisVoice.speechVoices())
if let url = Bundle.main.url(forResource: sfx.rawValue, withExtension: "mp3", subdirectory: "SFX") {
do {
player = try AVAudioPlayer(contentsOf: url)
player.volume = 0.5
player.play()
} catch {
print("ERROR: Couldn't load SFX file named " + sfx.rawValue)
}
}
}
public static func playSFX(sfx: String){
NSSound(named: sfx)?.play()
}
public static func shutUp(speaker: Bool = false){
player.stop()
if speaker { synth.stopSpeaking(at: .immediate) }
}
public static func testVoices() {
print(AVSpeechSynthesisVoice.speechVoices())
}
public static func speak(say: String, lang: String) {
let utterance = AVSpeechUtterance(string: say)
switch lang {
case "en":
if voiceIdentifier == nil {
for s in preferredVoices{
let d = "com.apple.speech.synthesis.voice." + s
if AVSpeechSynthesisVoice(identifier: d) != nil {
voiceIdentifier = d
break
}
}
if voiceIdentifier == nil {
ViewController.defaults.set(0, forKey: "narrator")
AutomneCore.displayAlert(title: "No voices found", message: "Automne was unable to find any voices downloaded on this machine. Narrator will be switched off. Refer to manual in order to download a voice")
return
}
}
utterance.voice = AVSpeechSynthesisVoice(identifier: voiceIdentifier!)
case "ru-RU":
let d = "com.apple.speech.synthesis.voice.milena.premium"
if AVSpeechSynthesisVoice(identifier: d) != nil {
utterance.voice = AVSpeechSynthesisVoice(identifier: d)
} else {
utterance.voice = AVSpeechSynthesisVoice(language: lang)
}
default:
utterance.voice = AVSpeechSynthesisVoice(language: lang)
}
utterance.rate = 0.4
utterance.volume = 0.7
utterance.postUtteranceDelay = 0.7
playSFX(sfx: "Blow")
synth.speak(utterance)
}
public static func speakWelcome(){
speak(say: AutomneAxioms.specialWelcomeNarratives.randomElement()!, lang: "en")
}
public static func composeAndSpeak(track: String, artist: String) -> (Bool, String?) {
if synth.isSpeaking { return (false, "already speaking") }
let a = Int.random(in: 1...7)
if a == 1 || a == 2 {
let s = AutomneAxioms.trackNarratives.randomElement()
let d = a == 1 ? track : artist
let f = s!.0.replacingOccurrences(of: "$", with: a == 1 ? "track" : "artist")
let lang: String
if d.isLatin { lang = "en" }
else if d.isCyrillic { lang = "ru-RU" }
else { return (false, "difficult phrase: " + d) }
if s!.1 { speak(say: f, lang: "en") }
speak(say: d, lang: lang)
if !s!.1 { speak(say: f, lang: "en") }
} else if a == 3 || a == 4 {
let s: String
if a == 3 {
s = AutomneAxioms.messages.randomElement()!
} else {
let date = Date()
let calendar = Calendar.current
let hour = calendar.component(.hour, from: date)
let d: String
switch hour {
case 0...7:
d = "night"
case 8...12:
d = "morning"
case 13...19:
d = "day"
case 20...23:
d = "evening"
default:
return (false, "hour err")
}
s = AutomneAxioms.specialTimeNarratives[d]!.randomElement()!
}
if s.isLatin {
speak(say: s, lang: "en")
} else if s.isCyrillic {
speak(say: s, lang: "ru-RU")
} else {
return (false, "difficult phrase: " + s)
}
} else {
return (false, "rnd case")
}
return (true, nil)
}
}