Skip to content
This repository was archived by the owner on Feb 19, 2021. It is now read-only.

Commit a29c9ac

Browse files
authored
Add RPC support & cleanup (#37)
* clean up code * add rich presence * remove unused using * tweak presence output
1 parent 6297bcb commit a29c9ac

20 files changed

+825
-934
lines changed

Audio/AudioPlayer.cs

+12-19
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,11 @@ public class AudioDevice
1616

1717
public AudioDevice(DirectSoundDeviceInfo deviceInfo, string name)
1818
{
19-
this.DeviceInfo = deviceInfo;
20-
this.Name = name;
19+
DeviceInfo = deviceInfo;
20+
Name = name;
2121
}
2222

23-
public override string ToString()
24-
{
25-
return this.Name;
26-
}
23+
public override string ToString() => Name;
2724
}
2825

2926
public class AudioPlayer : IDisposable
@@ -33,7 +30,7 @@ public class AudioPlayer : IDisposable
3330
SampleChannel volumeChannel;
3431
public Guid CurrentDeviceGuid { get; private set; } = Guid.NewGuid();
3532

36-
Queue<short[]> samplesToPlay = new Queue<short[]>();
33+
readonly Queue<short[]> samplesToPlay = new Queue<short[]>();
3734

3835
public AudioPlayer()
3936
{
@@ -43,8 +40,10 @@ public AudioPlayer()
4340
BufferDuration = TimeSpan.FromSeconds(10)
4441
};
4542

46-
volumeChannel = new SampleChannel(provider);
47-
volumeChannel.Volume = Settings.Get<float>(Setting.Volume);
43+
volumeChannel = new SampleChannel(provider)
44+
{
45+
Volume = Settings.Get<float>(Setting.Volume)
46+
};
4847

4948
bool success = Guid.TryParse(Settings.Get<string>(Setting.OutputDeviceGuid), out Guid deviceGuid);
5049

@@ -57,7 +56,7 @@ public AudioPlayer()
5756
public void Initialize(Guid deviceGuid)
5857
{
5958
directOut = new DirectSoundOut(deviceGuid);
60-
CurrentDeviceGuid = deviceGuid;
59+
this.CurrentDeviceGuid = deviceGuid;
6160
directOut.Init(volumeChannel);
6261

6362
Settings.Set(Setting.OutputDeviceGuid, deviceGuid.ToString());
@@ -118,19 +117,13 @@ public float AddVolume(float vol)
118117
return volumeChannel.Volume;
119118
}
120119

121-
public void SetVolume(float vol)
122-
{
123-
volumeChannel.Volume = BoundVolume(vol);
124-
}
120+
public void SetVolume(float vol) => volumeChannel.Volume = BoundVolume(vol);
125121

126122
/// <summary>
127123
/// Get an array of the available audio output devices.
128124
/// <para>Because of a limitation of WaveOut, device's names will be cut if they are too long.</para>
129125
/// </summary>
130-
public AudioDevice[] GetAudioOutputDevices()
131-
{
132-
return DirectSoundOut.Devices.Select(d => new AudioDevice(d, d.Description)).ToArray();
133-
}
126+
public AudioDevice[] GetAudioOutputDevices() => DirectSoundOut.Devices.Select(d => new AudioDevice(d, d.Description)).ToArray();
134127

135128
/// <summary>
136129
/// Set the audio output device (if available); Returns current audio device (desired if valid).
@@ -139,7 +132,7 @@ public AudioDevice[] GetAudioOutputDevices()
139132
/// <returns></returns>
140133
public void SetAudioOutputDevice(Guid deviceGuid)
141134
{
142-
if (deviceGuid == CurrentDeviceGuid)
135+
if (deviceGuid == this.CurrentDeviceGuid)
143136
return;
144137

145138
PlaybackState prevState = directOut?.PlaybackState ?? PlaybackState.Playing;

Audio/FFT.cs

+4-19
Original file line numberDiff line numberDiff line change
@@ -12,29 +12,14 @@ public Complex(float r, float i)
1212
Imaginary = i;
1313
}
1414

15-
public double Magnitude
16-
{
17-
get
18-
{
19-
return Math.Sqrt(Real * Real + Imaginary * Imaginary);
20-
}
21-
}
15+
public double Magnitude => Math.Sqrt(Real * Real + Imaginary * Imaginary);
2216

23-
public static Complex operator *(Complex a, Complex b)
24-
{
25-
return new Complex(a.Real * b.Real - a.Imaginary * b.Imaginary,
17+
public static Complex operator *(Complex a, Complex b) => new Complex(a.Real * b.Real - a.Imaginary * b.Imaginary,
2618
a.Real * b.Imaginary + a.Imaginary * b.Real);
27-
}
2819

29-
public static Complex operator +(Complex a, Complex b)
30-
{
31-
return new Complex(a.Real + b.Real, a.Imaginary + b.Imaginary);
32-
}
20+
public static Complex operator +(Complex a, Complex b) => new Complex(a.Real + b.Real, a.Imaginary + b.Imaginary);
3321

34-
public static Complex operator -(Complex a, Complex b)
35-
{
36-
return new Complex(a.Real - b.Real, a.Imaginary - b.Imaginary);
37-
}
22+
public static Complex operator -(Complex a, Complex b) => new Complex(a.Real - b.Real, a.Imaginary - b.Imaginary);
3823
}
3924

4025
class FFT

Audio/Ogg.cs

+9-9
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ namespace ListenMoeClient
66
{
77
class Ogg
88
{
9-
byte[] magic = { 0x4F, 0x67, 0x67, 0x53 }; //OggS
9+
readonly byte[] magic = { 0x4F, 0x67, 0x67, 0x53 }; //OggS
1010

1111
//Pre gen'd CRC lookup table cause why not: http://barrgroup.com/Embedded-Systems/How-To/CRC-Calculation-C-Code
12-
uint[] crcLookup = {
12+
readonly uint[] crcLookup = {
1313
0x00000000, 0x04C11DB7, 0x09823B6E, 0x0D4326D9,
1414
0x130476DC, 0x17C56B6B, 0x1A864DB2, 0x1E475005,
1515
0x2608EDB8, 0x22C9F00F, 0x2F8AD6D6, 0x2B4BCB61,
@@ -118,25 +118,25 @@ private List<byte[]> ReadPage(Stream stream)
118118

119119
ReadUntilMagic(stream); //Magic word
120120
pageBytes.AddRange(magic);
121-
var vn = stream.ReadByte(); //Ogg version number (0)
121+
int vn = stream.ReadByte(); //Ogg version number (0)
122122
pageBytes.Add((byte)vn);
123123

124-
var packetFlag = stream.ReadByte(); //Packet flag, we ignore ¯\_(ツ)_/¯
124+
int packetFlag = stream.ReadByte(); //Packet flag, we ignore ¯\_(ツ)_/¯
125125
pageBytes.Add((byte)packetFlag);
126126

127-
var granulePosition = ReadBytes(stream, 8); //Granule position
127+
byte[] granulePosition = ReadBytes(stream, 8); //Granule position
128128
pageBytes.AddRange(granulePosition);
129129

130-
var streamSN = ReadBytes(stream, 4);
130+
byte[] streamSN = ReadBytes(stream, 4);
131131
pageBytes.AddRange(streamSN);
132132

133-
var pageSN = ReadBytes(stream, 4);
133+
byte[] pageSN = ReadBytes(stream, 4);
134134
pageBytes.AddRange(pageSN);
135135

136-
var crc = ReadBytes(stream, 4);
136+
byte[] crc = ReadBytes(stream, 4);
137137
pageBytes.AddRange(new byte[] { 0, 0, 0, 0});
138138

139-
var segmentCount = stream.ReadByte(); //Number of segments in this page, 0-255
139+
int segmentCount = stream.ReadByte(); //Number of segments in this page, 0-255
140140
pageBytes.Add((byte)segmentCount);
141141

142142
int[] segmentLengths = new int[segmentCount];

Audio/WebStreamPlayer.cs

+13-31
Original file line numberDiff line numberDiff line change
@@ -20,29 +20,17 @@ class WebStreamPlayer
2020

2121
AudioVisualiser visualiser;
2222

23-
public WebStreamPlayer(string url)
24-
{
25-
this.url = url;
26-
}
23+
public WebStreamPlayer(string url) => this.url = url;
2724

28-
public async Task Dispose()
29-
{
30-
await Stop();
31-
}
25+
public async Task Dispose() => await Stop();
3226

33-
public void SetVisualiser(AudioVisualiser visualiser)
34-
{
35-
this.visualiser = visualiser;
36-
}
27+
public void SetVisualiser(AudioVisualiser visualiser) => this.visualiser = visualiser;
3728

38-
public void SetStreamUrl(string url)
39-
{
40-
this.url = url;
41-
}
29+
public void SetStreamUrl(string url) => this.url = url;
4230

4331
public void Play()
4432
{
45-
BasePlayer.Play();
33+
this.BasePlayer.Play();
4634
playing = true;
4735

4836
provideThread = new Thread(() =>
@@ -52,9 +40,9 @@ public void Play()
5240
WebClient wc = new WebClient();
5341
wc.Headers[HttpRequestHeader.UserAgent] = Globals.USER_AGENT;
5442

55-
using (var stream = wc.OpenRead(url))
43+
using (System.IO.Stream stream = wc.OpenRead(url))
5644
{
57-
var readFullyStream = new ReadFullyStream(stream);
45+
ReadFullyStream readFullyStream = new ReadFullyStream(stream);
5846

5947
int packetCounter = 0;
6048
while (playing)
@@ -68,13 +56,13 @@ public void Play()
6856

6957
for (int i = 0; i < packets.Length; i++)
7058
{
71-
var streamBytes = packets[i];
59+
byte[] streamBytes = packets[i];
7260
try
7361
{
7462
int frameSize = OpusPacketInfo.GetNumSamplesPerFrame(streamBytes, 0, Globals.SAMPLE_RATE); //Get frame size from opus packet
7563
short[] rawBuffer = new short[frameSize * 2]; //2 channels
76-
var buffer = decoder.Decode(streamBytes, 0, streamBytes.Length, rawBuffer, 0, frameSize, false);
77-
BasePlayer.QueueBuffer(rawBuffer);
64+
int buffer = decoder.Decode(streamBytes, 0, streamBytes.Length, rawBuffer, 0, frameSize, false);
65+
this.BasePlayer.QueueBuffer(rawBuffer);
7866

7967
if (visualiser != null)
8068
visualiser.AddSamples(rawBuffer);
@@ -95,18 +83,15 @@ public void Play()
9583
provideThread.Start();
9684
}
9785

98-
public float AddVolume(float vol)
99-
{
100-
return BasePlayer.AddVolume(vol);
101-
}
86+
public float AddVolume(float vol) => this.BasePlayer.AddVolume(vol);
10287

10388
public async Task Stop()
10489
{
10590
if (playing)
10691
{
10792
playing = false;
10893

109-
BasePlayer.Stop();
94+
this.BasePlayer.Stop();
11095

11196
if (provideThread != null)
11297
{
@@ -119,9 +104,6 @@ public async Task Stop()
119104
}
120105
}
121106

122-
public bool IsPlaying()
123-
{
124-
return playing;
125-
}
107+
public bool IsPlaying() => playing;
126108
}
127109
}

0 commit comments

Comments
 (0)