Skip to content

Commit f2453b7

Browse files
authored
Custom sound (#55)
* Bump Microsoft.AspNetCore.SignalR.Client from 6.0.8 to 6.0.9 Signed-off-by: Lasse Gaardsholt <lasse.gaardsholt@gmail.com> * added option to change volume Signed-off-by: Lasse Gaardsholt <lasse.gaardsholt@gmail.com> * added option to play custom sound Signed-off-by: Lasse Gaardsholt <lasse.gaardsholt@gmail.com> * Updated readme Signed-off-by: Lasse Gaardsholt <lasse.gaardsholt@gmail.com> Signed-off-by: Lasse Gaardsholt <lasse.gaardsholt@gmail.com>
1 parent 1468d85 commit f2453b7

File tree

6 files changed

+76
-19
lines changed

6 files changed

+76
-19
lines changed

AppSettings.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
using Microsoft.Extensions.Configuration;
2+
using System;
23
using System.Globalization;
34
using System.IO;
45
using System.Resources;
6+
using System.Windows.Forms;
57

68
namespace busylight_client
79
{
@@ -17,7 +19,10 @@ public class Settings
1719
public string Ring_Tune => Configuration.GetValue("AppSettings:Ring_Tune", "OpenOffice");
1820
public string Ring_Color => Configuration.GetValue("AppSettings:Ring_Color", "Red");
1921
public int Ring_Time => Configuration.GetValue("AppSettings:Ring_Time", 5000);
22+
public int Ring_Volume => Configuration.GetValue("AppSettings:Ring_Volume", 100);
2023
public string Idle_Color => Configuration.GetValue("AppSettings:Idle_Color", "Off");
24+
public string Custom_Sound => Configuration.GetValue<string>("AppSettings:Custom_Sound");
25+
2126

2227
public Settings()
2328
{
@@ -26,6 +31,12 @@ public Settings()
2631
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
2732
.Build();
2833

34+
if (!string.IsNullOrEmpty(this.Custom_Sound))
35+
if (!File.Exists(this.Custom_Sound))
36+
{
37+
MessageBox.Show($"The file {this.Custom_Sound} does not exists !", "File not found", MessageBoxButtons.OK, MessageBoxIcon.Error);
38+
Environment.Exit(1);
39+
}
2940
}
3041

3142
}

README.md

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,18 @@
66

77

88

9-
| Name | Default value | Allowed value |
10-
| ----------- | ------------- | :-----------------------------------------------------------------------------------------------------------------------------------: |
11-
| Location | | * |
12-
| SignalR_Uri | | * |
13-
| ApiKey | | * |
14-
| KeyName | ApiKey | * |
15-
| Ring_Tune | OpenOffice | "OpenOffice", "Quiet", "Funky", "FairyTale", "KuandoTrain", "TelephoneNordic", "TelephoneOriginal", "TelephonePickMeUp", "IM1", "IM2" |
16-
| Ring_Color | Red | "Red", "Green", "Blue", "Yellow", "Off" |
17-
| Ring_Time | 5000 | |
18-
| Idle_Color | Off | See "Ring_Color" |
9+
| Name | Default value | Allowed value |
10+
| ------------ | ------------- | :-----------------------------------------------------------------------------------------------------------------------------------: |
11+
| Location | | * |
12+
| SignalR_Uri | | * |
13+
| ApiKey | | * |
14+
| KeyName | ApiKey | * |
15+
| Ring_Tune | OpenOffice | "OpenOffice", "Quiet", "Funky", "FairyTale", "KuandoTrain", "TelephoneNordic", "TelephoneOriginal", "TelephonePickMeUp", "IM1", "IM2" |
16+
| Ring_Color | Red | "Red", "Green", "Blue", "Yellow", "Off" |
17+
| Ring_Time | 5000 | |
18+
| Ring_Volume | 100 | |
19+
| Idle_Color | Off | Same options as for `Ring_Color` |
20+
| Custom_Sound | | File path to the sound you want to play, it has only been tested with an mp3 file |
1921

2022

2123
Example of a appsettings.json:
@@ -29,7 +31,8 @@ Example of a appsettings.json:
2931
"Ring_Color": "Red",
3032
"KeyName": "ApiKey",
3133
"ApiKey": "some-key",
32-
"Idle_Color": "Off"
34+
"Idle_Color": "Off",
35+
"Custom_Sound": "C:\\sounds\\surprise.mp3"
3336
}
3437
}
3538
```

ServerConnect.cs

Lines changed: 46 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
using Microsoft.AspNetCore.SignalR.Client;
2+
using NetCoreAudio;
23
using System;
34
using System.Globalization;
5+
using System.IO;
46
using System.Threading.Tasks;
57
using System.Windows.Forms;
68

@@ -61,12 +63,40 @@ public async Task Connect()
6163
});
6264
connection.On("Ring", async () =>
6365
{
64-
var tune = GetTuneFromString(_settings.Ring_Tune);
65-
var color = GetColorFromString(_settings.Ring_Color);
66+
67+
if (!string.IsNullOrEmpty(_settings.Custom_Sound))
68+
{
69+
var soundIsPlaying = true;
70+
71+
Player player = new Player();
72+
await player.SetVolume((byte)_settings.Ring_Volume);
73+
player.PlaybackFinished += (sender, args) =>
74+
{
75+
// This event is triggered even if the player still has 1 or 2 seconds to play.
76+
busy.Light(_idleColor);
77+
soundIsPlaying = false;
78+
};
79+
await player.Play(_settings.Custom_Sound);
80+
81+
var redOrBlue = true;
82+
while (soundIsPlaying)
83+
{
84+
busy.Light(redOrBlue ? Busylight.BusylightColor.Red : Busylight.BusylightColor.Blue);
85+
redOrBlue = !redOrBlue;
86+
await Task.Delay(75);
87+
}
88+
89+
}
90+
else
91+
{
92+
var tune = GetTuneFromString(_settings.Ring_Tune);
93+
var color = GetColorFromString(_settings.Ring_Color);
94+
95+
busy.Alert(color, tune, GetVolumeFromInt(_settings.Ring_Volume));
96+
await Task.Delay(_settings.Ring_Time);
97+
busy.Light(_idleColor);
98+
}
6699

67-
busy.Alert(color, tune, Busylight.BusylightVolume.High);
68-
await Task.Delay(_settings.Ring_Time);
69-
busy.Light(_idleColor);
70100
});
71101

72102
try
@@ -132,7 +162,17 @@ private static Busylight.BusylightSoundClip GetTuneFromString(string tuneAsStrin
132162
{
133163
return Busylight.BusylightSoundClip.KuandoTrain;
134164
}
135-
165+
}
166+
private static Busylight.BusylightVolume GetVolumeFromInt(int volumeAsInt)
167+
{
168+
try
169+
{
170+
return (Busylight.BusylightVolume)volumeAsInt;
171+
}
172+
catch (Exception)
173+
{
174+
return Busylight.BusylightVolume.High;
175+
}
136176
}
137177
private void AddClickEvent()
138178
{

appsettings.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@
44
"SignalR_Uri": "http://localhost:50625/BusyHub",
55
"Ring_Time": 6800,
66
"Ring_Tune": "OpenOffice",
7+
"Ring_Volume": 100,
78
"Ring_Color": "Red",
89
"Idle_Color": "Blue",
9-
"ApiKey": "some-key"
10+
"ApiKey": "some-key",
11+
"Custom_Sound": "C:\\Users\\lasse\\Desktop\\dexter__surprise_motherfucker1.mp3"
1012
}
1113
}

busylight-client.csproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,12 @@
2020
</ItemGroup>
2121

2222
<ItemGroup>
23-
<PackageReference Include="Microsoft.AspNetCore.SignalR.Client" Version="6.0.8" />
23+
<PackageReference Include="Microsoft.AspNetCore.SignalR.Client" Version="6.0.9" />
2424
<PackageReference Include="Microsoft.Extensions.Configuration" Version="6.0.1" />
2525
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="6.0.0" />
2626
<PackageReference Include="Microsoft.Extensions.Configuration.FileExtensions" Version="6.0.0" />
2727
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="6.0.0" />
28+
<PackageReference Include="NetCoreAudio" Version="1.7.0" />
2829
</ItemGroup>
2930

3031
<ItemGroup>

surprise.mp3

34.6 KB
Binary file not shown.

0 commit comments

Comments
 (0)