Skip to content

Commit

Permalink
有基本界面,比较像是播放器
Browse files Browse the repository at this point in the history
  • Loading branch information
Milkitic committed Sep 9, 2018
1 parent 51d9b0b commit a6e5116
Show file tree
Hide file tree
Showing 17 changed files with 902 additions and 244 deletions.
33 changes: 33 additions & 0 deletions OsuPlayer/Config.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Milkitic.OsuPlayer
{
public class Config
{
public int Offset { get; set; } = 85;
public VolumeControl Volume { get; set; } = new VolumeControl();
public int DesiredLatency { get; set; } = 80;
}

public class VolumeControl
{
private float _main = 0.7f;
private float _bgm = 1;
private float _hs = 1;

public float Main { get => _main; set => SetValue(ref _main, value); }
public float Music { get => _bgm; set => SetValue(ref _bgm, value); }
public float Hitsound { get => _hs; set => SetValue(ref _hs, value); }

private static void SetValue(ref float source, float value)
{
if (value < 0) source = 0;
else if (value > 1) source = 1;
else source = value;
}
}
}
64 changes: 64 additions & 0 deletions OsuPlayer/Core.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
using Milkitic.OsuPlayer.Models;
using Milkitic.OsuPlayer.Utils;
using NAudio.Wave;
using Newtonsoft.Json;
using System;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using Milkitic.OsuPlayer.Winforms;

namespace Milkitic.OsuPlayer
{
static class Core
{
public static Config Config { get; set; }

[STAThread]
static void Main()
{
var file = Domain.ConfigFile;
if (!File.Exists(file))
{
CreateConfig(file);
}
else
{
try
{
Config = JsonConvert.DeserializeObject<Config>(File.ReadAllText(file));
}
catch (JsonException e)
{
var result = MessageBox.Show(@"载入配置文件时失败,用默认配置覆盖继续打开吗?\r\n" + e.Message,
AppDomain.CurrentDomain.FriendlyName, MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (result == DialogResult.Yes)
{
CreateConfig(file);
}
else
return;
}
}

Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new RenderForm());

SaveConfig(file);
}

private static void SaveConfig(string file)
{
File.WriteAllText(file, JsonConvert.SerializeObject(Config));
}

private static void CreateConfig(string file)
{
Config = new Config();
File.WriteAllText(file, JsonConvert.SerializeObject(Config));
}
}
}
2 changes: 2 additions & 0 deletions OsuPlayer/Domain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ namespace Milkitic.OsuPlayer
internal static class Domain
{
public static string CurrentPath => AppDomain.CurrentDomain.BaseDirectory;
public static string ConfigFile => Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "config.json");

public static string DefaultPath => Path.Combine(CurrentPath, "default");
}
}
26 changes: 20 additions & 6 deletions OsuPlayer/Milkitic.OsuPlayer.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@
<Reference Include="NAudio.Vorbis, Version=1.0.0.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\NAudio.Vorbis.1.0.0.0\lib\net35\NAudio.Vorbis.dll</HintPath>
</Reference>
<Reference Include="Newtonsoft.Json, Version=11.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<HintPath>..\packages\Newtonsoft.Json.11.0.2\lib\net45\Newtonsoft.Json.dll</HintPath>
</Reference>
<Reference Include="NVorbis, Version=0.8.4.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\NVorbis.0.8.4.0\lib\NVorbis.dll</HintPath>
</Reference>
Expand All @@ -69,23 +72,31 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Config.cs" />
<Compile Include="Domain.cs" />
<Compile Include="Interface\IPlayer.cs" />
<Compile Include="Models\PlayStatus.cs" />
<Compile Include="PlayerMain.cs">
<Compile Include="Winforms\RenderForm.cs">
<SubType>Form</SubType>
</Compile>
<Compile Include="PlayerMain.Designer.cs">
<DependentUpon>PlayerMain.cs</DependentUpon>
<Compile Include="Winforms\RenderForm.Designer.cs">
<DependentUpon>RenderForm.cs</DependentUpon>
</Compile>
<Compile Include="Utils\MyAudioFileReader.cs" />
<Compile Include="Utils\HitsoundPlayer.cs" />
<Compile Include="Models\HitsoundElement.cs" />
<Compile Include="Utils\MusicPlayer.cs" />
<Compile Include="Utils\WavePlayer.cs" />
<Compile Include="Program.cs" />
<Compile Include="Core.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<EmbeddedResource Include="PlayerMain.resx">
<DependentUpon>PlayerMain.cs</DependentUpon>
<Compile Include="Winforms\VolumeForm.cs">
<SubType>Form</SubType>
</Compile>
<Compile Include="Winforms\VolumeForm.Designer.cs">
<DependentUpon>VolumeForm.cs</DependentUpon>
</Compile>
<EmbeddedResource Include="Winforms\RenderForm.resx">
<DependentUpon>RenderForm.cs</DependentUpon>
</EmbeddedResource>
<EmbeddedResource Include="Properties\Resources.resx">
<Generator>ResXFileCodeGenerator</Generator>
Expand All @@ -97,6 +108,9 @@
<DependentUpon>Resources.resx</DependentUpon>
<DesignTime>True</DesignTime>
</Compile>
<EmbeddedResource Include="Winforms\VolumeForm.resx">
<DependentUpon>VolumeForm.cs</DependentUpon>
</EmbeddedResource>
<None Include="packages.config" />
<None Include="Properties\Settings.settings">
<Generator>SettingsSingleFileGenerator</Generator>
Expand Down
106 changes: 0 additions & 106 deletions OsuPlayer/PlayerMain.Designer.cs

This file was deleted.

30 changes: 0 additions & 30 deletions OsuPlayer/Program.cs

This file was deleted.

21 changes: 11 additions & 10 deletions OsuPlayer/Utils/HitsoundPlayer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,7 @@ private set
private Task _playingTask, _offsetTask;

private readonly Stopwatch _sw = new Stopwatch();
private OsuFile _osufile;
private int _generalOffset = 85;
public OsuFile Osufile { get; private set; }

public HitsoundPlayer(string filePath)
{
Expand All @@ -58,7 +57,7 @@ public HitsoundPlayer(string filePath)
foreach (var path in allPaths)
WavePlayer.SaveToCache(path); // Cache each file once before play.

FileInfo musicInfo = new FileInfo(Path.Combine(dirInfo.FullName, _osufile.General.AudioFilename));
FileInfo musicInfo = new FileInfo(Path.Combine(dirInfo.FullName, Osufile.General.AudioFilename));
_musicPlayer = new MusicPlayer(musicInfo.FullName);
Duration = (int)Math.Ceiling(Math.Max(_hitsoundList.Max(k => k.Offset), _musicPlayer.Duration));
}
Expand Down Expand Up @@ -146,7 +145,7 @@ private void DynamicOffset()
{
preMs = playerMs;
var d = playerMs - (_sw.ElapsedMilliseconds + _controlOffset);
var r = _generalOffset - d;
var r = Core.Config.Offset - d;
if (Math.Abs(r) > 5)
{
Console.WriteLine($@"music: {_musicPlayer.PlayTime}, hs: {PlayTime}, {d}({r})");
Expand Down Expand Up @@ -209,7 +208,7 @@ private List<HitsoundElement> FillHitsoundList(string filePath, DirectoryInfo di
{
try
{
_osufile = new OsuFile(filePath);
Osufile = new OsuFile(filePath);
}
catch (NotSupportedException)
{
Expand All @@ -223,7 +222,7 @@ private List<HitsoundElement> FillHitsoundList(string filePath, DirectoryInfo di
{
throw new Exception("载入时发生未知错误。", e);
}
List<RawHitObject> hitObjects = _osufile.HitObjects.HitObjectList;
List<RawHitObject> hitObjects = Osufile.HitObjects.HitObjectList;
List<HitsoundElement> hitsoundList = new List<HitsoundElement>();

var mapFiles = dirInfo.GetFiles("*.wav").Select(p => p.Name).ToArray();
Expand All @@ -235,10 +234,10 @@ private List<HitsoundElement> FillHitsoundList(string filePath, DirectoryInfo di
foreach (var item in obj.SliderInfo.Edges)
{
//var currentTiming = file.TimingPoints.GetRedLine(item.Offset);
var currentLine = _osufile.TimingPoints.GetLine(item.Offset);
var currentLine = Osufile.TimingPoints.GetLine(item.Offset);
var element = new HitsoundElement
{
GameMode = _osufile.General.Mode,
GameMode = Osufile.General.Mode,
Offset = item.Offset,
Volume = (obj.SampleVolume != 0 ? obj.SampleVolume : currentLine.Volume) / 100f,
Hitsound = item.EdgeHitsound,
Expand All @@ -256,12 +255,12 @@ private List<HitsoundElement> FillHitsoundList(string filePath, DirectoryInfo di
else
{
//var currentTiming = file.TimingPoints.GetRedLine(obj.Offset);
var currentLine = _osufile.TimingPoints.GetLine(obj.Offset);
var currentLine = Osufile.TimingPoints.GetLine(obj.Offset);
var offset = obj.Offset; //todo: spinner & hold

var element = new HitsoundElement
{
GameMode = _osufile.General.Mode,
GameMode = Osufile.General.Mode,
Offset = offset,
Volume = (obj.SampleVolume != 0 ? obj.SampleVolume : currentLine.Volume) / 100f,
Hitsound = obj.Hitsound,
Expand Down Expand Up @@ -298,5 +297,7 @@ private void SetFullPath(DirectoryInfo dirInfo, string[] mapFiles, HitsoundEleme
}

#endregion Load


}
}
Loading

0 comments on commit a6e5116

Please sign in to comment.