-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSoundConfiguration.cs
executable file
·189 lines (171 loc) · 6.92 KB
/
SoundConfiguration.cs
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using Gtk;
using KarrotObjectNotation;
using System.Threading.Tasks;
using System.Threading;
using NetCoreAudio;
namespace KarrotSoundProduction
{
/// <summary>
/// Represents the configuration for a sound, including the file path, keybinding, and other properties.
/// </summary>
public class SoundConfiguration
{
private Player player;
/// <summary>
/// The file path to the sound in question.
/// </summary>
/// <value></value>
public string FilePath { get; private set; }
/// <summary>
/// The path to the original sound file. Differs from FilePath only when the file was decoded from its original format.
/// </summary>
/// <value></value>
public string OriginalFilePath { get; private set; }
/// <summary>
/// The integer key code that represents the key used to play the sound.
/// </summary>
/// <value></value>
public Gdk.Key Key { get; private set; }
/// <summary>
/// The integer key code that represents the key used to stop the sound.
/// </summary>
/// <value></value>
public Gdk.Key? StopKey { get; private set; }
/// <summary>
/// The integer key code that represents the key used to instantly stop the sound.
/// Not generally used. If set, does not prevent the global kill binding from killing this sound.
/// </summary>
/// <value></value>
public Gdk.Key KillKey { get; private set; }
/// <summary>
/// The time, in milliseconds, that it takes to fade the sound in.
/// </summary>
/// <value></value>
public int FadeInTime { get; private set; }
/// <summary>
/// The time, in milliseconds, that the sound will fade out either at the end or if the stop key is pressed.
/// </summary>
/// <value></value>
public int FadeOutTime { get; private set; }
/// <summary>
/// The maximum volume, in percent, that the sound will reach when fading in, or the volume the sound will be played at.
/// </summary>
/// <value></value>
public float MaxVolume { get; private set; }
/// <summary>
/// The minimum volume, in percent, that the sound will start at when fading in.
/// </summary>
/// <value></value>
public float MinVolume { get; private set; }
/// <summary>
/// The factor by which the sound's sample rate will be multiplied. (Default: 1.0)
/// </summary>
/// <value></value>
public float PlaybackSpeed { get; private set; }
/// <summary>
/// Gets the KONNode object that represents this sound configuration.
/// </summary>
/// <returns></returns>
public KONNode GetNode()
{
KONNode output = new KONNode("SOUND");
output.AddValue("filePath", OriginalFilePath);
output.AddValue("keyCode", (int)Key);
if (StopKey != null)
output.AddValue("stopKeyCode", (int)StopKey);
output.AddValue("fadeInTime", FadeInTime);
output.AddValue("fadeOutTime", FadeOutTime);
output.AddValue("maxVolume", MaxVolume);
output.AddValue("minVolume", MinVolume);
output.AddValue("PlaybackSpeed", PlaybackSpeed);
return output;
}
/// <summary>
/// When attached to a key trigger event, starts the sound when the key is pressed.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
/// <returns></returns>
public async void PlaySound(object sender, KeyTriggerEventArgs e)
{
Player player = new();
int initialVolume = FadeInTime >= 100 ? 0 : 100;
//await player.SetVolume(initialVolume);
SoundboardConfiguration.CurrentConfig.CurrentlyPlaying.Add(player);
await player.Play(FilePath, this);
//await Task.Delay(1000);
//await player.SetVolume(20);
//await playerTask;
/*if (FadeInTime >= 100)
{
while (player.CurrentVolume < 100)
{
await player.SetVolume(player.CurrentVolume + 1);
await Task.Delay(FadeInTime / 100);
}
}*/
SoundboardConfiguration.CurrentConfig.CurrentlyPlaying.Remove(player);
}
/// <summary>
/// When attached to a key trigger event, stops or fades out the sound when the key is pressed.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
/// <returns></returns>
public async void StopSound(object sender, KeyTriggerEventArgs e)
{
if (FadeOutTime >= 100)
{
while (player.CurrentVolume > 0)
{
await player.SetVolume(player.CurrentVolume - 1);
Thread.Sleep(FadeOutTime / 100);
}
}
await player.Stop();
SoundboardConfiguration.CurrentConfig.CurrentlyPlaying.Remove(player);
}
/// <summary>
/// When attached to a key trigger event, instantly stops the sound when the key is pressed, regardless of the configured fade out time.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
/// <returns></returns>
public async void KillSound(object sender, KeyTriggerEventArgs e)
{
await player.Stop();
SoundboardConfiguration.CurrentConfig.CurrentlyPlaying.Remove(player);
}
public SoundConfiguration(string filePath, Gdk.Key key, Gdk.Key? stopKey = null, string originalFilePath = null, int fadeInTime = 0, int fadeOutTime = 0, float maxVolume = 100, float minVolume = 0, float speed = 1)
{
FilePath = filePath;
if (originalFilePath == null) originalFilePath = filePath;
OriginalFilePath = originalFilePath;
Key = key;
StopKey = stopKey;
FadeInTime = fadeInTime;
FadeOutTime = fadeOutTime;
MaxVolume = maxVolume;
MinVolume = minVolume;
PlaybackSpeed = speed;
player = new();
}
public override string ToString() => ToString(true);
public string ToString(bool withKey = true)
{
if (withKey)
return $"{Key}\t\t{Path.GetFileNameWithoutExtension(FilePath)}";
else
return Path.GetFileNameWithoutExtension(FilePath);
}
}
}