Skip to content

Commit

Permalink
Replaced DateTime.Now with MusicPlayer.Time, removed MultiTrackMMLPla…
Browse files Browse the repository at this point in the history
…yer.Loop property that should never have been there in the first place
  • Loading branch information
Enichan committed Aug 4, 2019
1 parent 6cb1ab1 commit 4f8911a
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 24 deletions.
8 changes: 4 additions & 4 deletions MidiPlayer/Form1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ protected override void OnClosed(EventArgs e) {

private void Play() {
try {
TimeSpan now = new TimeSpan(DateTime.Now.Ticks);
TimeSpan now = new TimeSpan(MusicPlayer.Time.Ticks);
player.Play(now);

while (player != null && !stopPlaying) {
Expand All @@ -145,7 +145,7 @@ private void Play() {
}
}
Thread.Sleep(1);
now = new TimeSpan(DateTime.Now.Ticks);
now = new TimeSpan(MusicPlayer.Time.Ticks);
}

lock (playerLock) {
Expand Down Expand Up @@ -271,7 +271,7 @@ private void scrSeek_MouseDown(object sender, MouseEventArgs e) {
if (player != null) {
double perc = e.X / (double)scrSeek.Width;
int seconds = (int)(perc * player.Duration.TotalSeconds);
player.Seek(new TimeSpan(DateTime.Now.Ticks), TimeSpan.FromSeconds(seconds));
player.Seek(new TimeSpan(MusicPlayer.Time.Ticks), TimeSpan.FromSeconds(seconds));
SetScrollValue(seconds + 1);
SetScrollValue(seconds);
}
Expand All @@ -283,7 +283,7 @@ private void btnPlay_Click(object sender, EventArgs e) {
if (player != null) {
if (player.Playing && !player.Paused)
player.Stop();
player.Play(new TimeSpan(DateTime.Now.Ticks));
player.Play(new TimeSpan(MusicPlayer.Time.Ticks));
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion MidiPlayer/PlayerMML.cs
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ protected override void PlayNote(Note note, int channel, TimeSpan time) {
/// <summary>
/// Thread-safe
/// </summary>
public new bool Loop { get { return loop; } set { loop = value; } }
public bool Loop { get { return loop; } set { loop = value; } }
public override TimeSpan Elapsed { get { return elapsed; } }
public bool Paused {
get {
Expand Down
3 changes: 2 additions & 1 deletion PitchSample/SoundSource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
using System.Linq;
using System.Text;
using Cireon.Audio;
using TextPlayer;

namespace PitchSample {
class SoundSource {
Expand All @@ -41,7 +42,7 @@ public SoundSource(Source source) {
}

public void Fade() {
fadeStart = new TimeSpan(DateTime.Now.Ticks);
fadeStart = new TimeSpan(MusicPlayer.Time.Ticks);
fadeEnd = fadeStart + TimeSpan.FromMilliseconds(100);
}

Expand Down
6 changes: 3 additions & 3 deletions TextPlayer/IMusicPlayer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ interface IMusicPlayer {
/// <param name="stream">A stream containing the song's code.</param>
void Load(StreamReader stream);
/// <summary>
/// Plays the song. Uses DateTime.Now as the starting time.
/// Plays the song. Uses MusicPlayer.Time as the starting time.
/// </summary>
void Play();
/// <summary>
Expand All @@ -57,7 +57,7 @@ interface IMusicPlayer {
/// </summary>
void Stop();
/// <summary>
/// Update this music player. Uses DateTime.Now as the current time.
/// Update this music player. Uses MusicPlayer.Time as the current time.
/// </summary>
void Update();
/// <summary>
Expand All @@ -66,7 +66,7 @@ interface IMusicPlayer {
/// <param name="currentTime">Current player time.</param>
void Update(TimeSpan currentTime);
/// <summary>
/// Seeks to position within the song (relative to TimeSpan.Zero). Uses DateTime.Now as the current time.
/// Seeks to position within the song (relative to TimeSpan.Zero). Uses MusicPlayer.Time as the current time.
/// </summary>
/// <param name="position">Position relative to TimeSpan.Zero to seek to.</param>
void Seek(TimeSpan position);
Expand Down
15 changes: 6 additions & 9 deletions TextPlayer/MML/MultiTrackMMLPlayer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ namespace TextPlayer.MML {
public abstract class MultiTrackMMLPlayer : IMusicPlayer {
private List<MMLPlayerTrack> tracks;
private bool muted;
private bool loop;
private MMLSettings settings;
private TimeSpan duration;
protected TimeSpan startTime;
Expand Down Expand Up @@ -78,10 +77,10 @@ public virtual void SetTempo(int tempo) {
}

/// <summary>
/// Plays the song. Uses DateTime.Now as the starting time.
/// Plays the song. Uses MusicPlayer.Time as the starting time.
/// </summary>
public virtual void Play() {
Play(new TimeSpan(DateTime.Now.Ticks));
Play(MusicPlayer.Time);
}

/// <summary>
Expand All @@ -97,10 +96,10 @@ public virtual void Play(TimeSpan currentTime) {
}

/// <summary>
/// Update this music player. Uses DateTime.Now as the current time.
/// Update this music player. Uses MusicPlayer.Time as the current time.
/// </summary>
public virtual void Update() {
Update(new TimeSpan(DateTime.Now.Ticks));
Update(MusicPlayer.Time);
}

/// <summary>
Expand Down Expand Up @@ -163,11 +162,11 @@ public virtual void Stop() {
}

/// <summary>
/// Seeks to position within the song (relative to TimeSpan.Zero). Uses DateTime.Now as the current time.
/// Seeks to position within the song (relative to TimeSpan.Zero). Uses MusicPlayer.Time as the current time.
/// </summary>
/// <param name="position">Position relative to TimeSpan.Zero to seek to.</param>
public virtual void Seek(TimeSpan position) {
Seek(new TimeSpan(DateTime.Now.Ticks), position);
Seek(MusicPlayer.Time, position);
}

/// <summary>
Expand Down Expand Up @@ -413,8 +412,6 @@ public bool Muted {
/// </summary>
public TimeSpan Duration { get { return duration; } }

public bool Loop { get { return loop; } set { loop = value; } }

private TimeSpan nextTick {
get {
long max = 0;
Expand Down
34 changes: 28 additions & 6 deletions TextPlayer/MusicPlayer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,34 @@
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Diagnostics;

namespace TextPlayer {
/// <summary>
/// Abstract base music playing class.
/// </summary>
public abstract class MusicPlayer : IMusicPlayer {
#region Static
private static Stopwatch time;
private static object timeLock = new object();

static MusicPlayer() {
time = new Stopwatch();
time.Start();
}

/// <summary>
/// Get default time used for timing music players, thread-safe
/// </summary>
public static TimeSpan Time {
get {
lock (timeLock) {
return time.Elapsed;
}
}
}
#endregion

protected bool playing = false;
protected TimeSpan lastTime;
protected TimeSpan startTime;
Expand Down Expand Up @@ -73,10 +95,10 @@ public void Load(StreamReader stream) {
}

/// <summary>
/// Plays the song. Uses DateTime.Now as the starting time.
/// Plays the song. Uses MusicPlayer.Time as the starting time.
/// </summary>
public virtual void Play() {
Play(new TimeSpan(DateTime.Now.Ticks));
Play(MusicPlayer.Time);
}

/// <summary>
Expand All @@ -102,10 +124,10 @@ public virtual void Stop() {
}

/// <summary>
/// Update this music player. Uses DateTime.Now as the current time.
/// Update this music player. Uses MusicPlayer.Time as the current time.
/// </summary>
public virtual void Update() {
Update(new TimeSpan(DateTime.Now.Ticks));
Update(MusicPlayer.Time);
}

/// <summary>
Expand All @@ -119,11 +141,11 @@ public virtual void Update(TimeSpan currentTime) {
}

/// <summary>
/// Seeks to position within the song (relative to TimeSpan.Zero). Uses DateTime.Now as the current time.
/// Seeks to position within the song (relative to TimeSpan.Zero). Uses MusicPlayer.Time as the current time.
/// </summary>
/// <param name="position">Position relative to TimeSpan.Zero to seek to.</param>
public virtual void Seek(TimeSpan position) {
Seek(new TimeSpan(DateTime.Now.Ticks));
Seek(MusicPlayer.Time, position);
}

/// <summary>
Expand Down

0 comments on commit 4f8911a

Please sign in to comment.