Skip to content

Commit 0c7f797

Browse files
committed
Refactor SoundFlowPlayer.cs to use Math.Clamp instead of if statements.
1 parent b90d01f commit 0c7f797

File tree

2 files changed

+11
-32
lines changed

2 files changed

+11
-32
lines changed

README.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,6 @@
33

44
A cross-platform Terminal User Interface (TUI) music player written in C# (.NET 8) with the goal of being minimalistic and light on resources.
55

6-
MusicSharp makes use of the [SoundFlow](https://github.com/LSXPrime/SoundFlow) and [Terminal.Gui](https://github.com/migueldeicaza/gui.cs) libraries.
7-
8-
## Screenshot
9-
10-
<img src="https://user-images.githubusercontent.com/20845425/99861949-06763200-2b66-11eb-9d5a-9bf2ea5151ee.png" alt="Screenshot of MusicSharp">
11-
126
## Features
137

148
- Cross-platform support (Windows, Mac, Linux).
@@ -20,6 +14,12 @@ MusicSharp makes use of the [SoundFlow](https://github.com/LSXPrime/SoundFlow) a
2014

2115
Download the [latest release of MusicSharp](https://github.com/markjamesm/MusicSharp/releases) and follow the installation instructions.
2216

17+
## Screenshot
18+
19+
<img src="https://user-images.githubusercontent.com/20845425/99861949-06763200-2b66-11eb-9d5a-9bf2ea5151ee.png" alt="Screenshot of MusicSharp">
20+
21+
MusicSharp makes use of the [SoundFlow](https://github.com/LSXPrime/SoundFlow) and [Terminal.Gui](https://github.com/migueldeicaza/gui.cs) libraries.
22+
2323
## Want to Contribute?
2424

2525
To see how you can submit PRs, be sure to check out our [contributing page](https://github.com/markjamesm/MusicSharp/blob/main/CONTRIBUTING.md).

src/AudioPlayer/SoundFlowPlayer.cs

Lines changed: 5 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,9 @@ public void Play(Stream stream)
3131
}
3232

3333
_player = new SoundPlayer(new StreamDataProvider(stream));
34-
35-
// Add the player to the master mixer. This connects the player's output to the audio engine's output.
3634
Mixer.Master.AddComponent(_player);
37-
3835
_player.Play();
36+
3937
PlayerStatus = EPlayerStatus.Playing;
4038
}
4139

@@ -70,41 +68,22 @@ public void IncreaseVolume()
7068
{
7169
// Need to verify what SoundFlow's max volume level is
7270
// For now this should be enough based on testing
73-
if (_player.Volume < 2.0f)
74-
{
75-
_player.Volume += .1f;
76-
}
71+
_player.Volume = Math.Clamp(_player.Volume + .1f, 0f, 2f);
7772
}
7873

7974
public void DecreaseVolume()
8075
{
81-
// Ensure that the volume isn't negative
82-
// otherwise the player will crash
83-
if (_player.Volume > .1f)
84-
{
85-
_player.Volume -= .1f;
86-
}
87-
88-
if (_player.Volume <= .1f)
89-
{
90-
_player.Volume = 0f;
91-
}
76+
_player.Volume = Math.Clamp(_player.Volume - .1f, 0f, 2f);
9277
}
9378

9479
public void SeekForward()
9580
{
96-
if (_player.Time < _player.Duration - 5f)
97-
{
98-
_player.Seek(_player.Time + 5f);
99-
}
81+
_player.Seek(Math.Clamp(_player.Time + 5f, 0f, _player.Duration - 1));
10082
}
10183

10284
public void SeekBackwards()
10385
{
104-
if (_player.Time > 5f)
105-
{
106-
_player.Seek(_player.Time - 5f);
107-
}
86+
_player.Seek(Math.Clamp(_player.Time - 5f, 0f, _player.Duration));
10887
}
10988

11089
public float CurrentTime()

0 commit comments

Comments
 (0)