Skip to content

Commit

Permalink
Merge pull request #44 from hozuki/v0.7.8
Browse files Browse the repository at this point in the history
v0.7.8
  • Loading branch information
hozuki committed Jan 17, 2018
2 parents b670060 + 43fddd2 commit dfeb420
Show file tree
Hide file tree
Showing 13 changed files with 348 additions and 244 deletions.
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "external/SharpAL"]
path = external/SharpAL
url = https://github.com/OpenCGSS/SharpAL.git
38 changes: 38 additions & 0 deletions Apps/ScoreViewer/AudioManager.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using DereTore.Common;
using NAudio.Wave;
using SharpAL;

namespace DereTore.Apps.ScoreViewer {
public sealed class AudioManager : DisposableBase {

public AudioManager() {
_audioDevice = new AudioDevice();
_audioContext = new AudioContext(_audioDevice);
}

public AudioDevice AudioDevice => _audioDevice;

public AudioContext AudioContext => _audioContext;

public static readonly WaveFormat StandardFormat = new WaveFormat();

public static bool NeedsConversion(WaveFormat test, WaveFormat standard) {
return test.SampleRate != standard.SampleRate ||
test.BitsPerSample != standard.BitsPerSample ||
test.Channels != standard.Channels ||
test.Encoding != standard.Encoding;
}

protected override void Dispose(bool disposing) {
_audioContext?.Dispose();
_audioDevice?.Dispose();

_audioContext = null;
_audioDevice = null;
}

private AudioDevice _audioDevice;
private AudioContext _audioContext;

}
}
6 changes: 6 additions & 0 deletions Apps/ScoreViewer/DereTore.Apps.ScoreViewer.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,11 @@
<LangVersion>6</LangVersion>
</PropertyGroup>
<ItemGroup>
<Compile Include="AudioManager.cs" />
<Compile Include="Controls\DoubleBufferedPictureBox.cs">
<SubType>Component</SubType>
</Compile>
<Compile Include="Extensions\StreamExtensions.cs" />
<Compile Include="PlayerSettings.cs" />
<Compile Include="Controls\RenderHelper.cs" />
<Compile Include="Controls\RenderParams.cs" />
Expand Down Expand Up @@ -160,6 +162,10 @@
<Project>{EECF4BAA-9C9E-4687-A616-0F5C65C5F14B}</Project>
<Name>DereTore.Exchange.Archive.ACB</Name>
</ProjectReference>
<ProjectReference Include="..\..\external\SharpAL\SharpAL\SharpAL.csproj">
<Project>{7e5e07e6-4300-438e-bd37-c9f6e5e14a41}</Project>
<Name>SharpAL</Name>
</ProjectReference>
<ProjectReference Include="..\..\Interop\DereTore.Interop.OS\DereTore.Interop.OS.csproj">
<Project>{3a0d1281-a503-4e5d-9765-d7bf56f89266}</Project>
<Name>DereTore.Interop.OS</Name>
Expand Down
38 changes: 38 additions & 0 deletions Apps/ScoreViewer/Extensions/StreamExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using System.IO;

namespace DereTore.Apps.ScoreViewer.Extensions {
public static class StreamExtensions {

public static byte[] ReadToEnd(this Stream stream) {
return ReadToEnd(stream, 102400);
}

public static byte[] ReadToEnd(this Stream stream, int bufferSize) {
byte[] data;
var buffer = new byte[bufferSize];
var length = stream.Length;

using (var memoryStream = new MemoryStream()) {
var read = 1;
long totalRead = 0;

while (read > 0) {
read = stream.Read(buffer, 0, bufferSize);
memoryStream.Write(buffer, 0, read);

totalRead += read;

// totalread >= length: for WaveOffsetStream
if (read < bufferSize || totalRead >= length) {
break;
}
}

data = memoryStream.ToArray();
}

return data;
}

}
}
18 changes: 11 additions & 7 deletions Apps/ScoreViewer/Forms/FViewer.EventHandlers.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.IO;
using System.Windows.Forms;
using DereTore.Apps.ScoreViewer.Controls;
Expand Down Expand Up @@ -165,9 +165,9 @@ public void UpdateSfx(TimeSpan rawMusicTime) {
foreach (var note in _score.Notes) {
if (!(note.HitTiming < prev) && (note.HitTiming < now)) {
if (note.IsFlick) {
_sfxManager.PlayWave(_currentFlickHcaFileName, TimeSpan.FromSeconds(note.HitTiming), PlayerSettings.SfxVolume);
_sfxManager.PlayWave(_currentFlickHcaFileName, PlayerSettings.SfxVolume);
} else if (note.IsTap || note.IsHold || note.IsSlide) {
_sfxManager.PlayWave(_currentTapHcaFileName, TimeSpan.FromSeconds(note.HitTiming), PlayerSettings.SfxVolume);
_sfxManager.PlayWave(_currentTapHcaFileName, PlayerSettings.SfxVolume);
}
}
}
Expand Down Expand Up @@ -220,6 +220,8 @@ private void BtnScoreUnload_Click(object sender, EventArgs e) {
_scorePlayer.Dispose();
_scorePlayer = null;
}
_audioManager?.Dispose();
_audioManager = null;
_musicWaveStream?.Dispose();
_musicWaveStream = null;
if (_audioFileStream != null) {
Expand Down Expand Up @@ -256,10 +258,12 @@ private void BtnScoreLoad_Click(object sender, EventArgs e) {
} else {
throw new ArgumentOutOfRangeException(nameof(audioFileExtension), $"Unsupported audio format: '{audioFileExtension}'.");
}
_scorePlayer = new ScorePlayer();
_audioManager = new AudioManager();
_scorePlayer = new ScorePlayer(_audioManager);
_scorePlayer.PlaybackStopped += MusicPlayer_PlaybackStopped;
_scorePlayer.AddInputStream(_musicWaveStream, PlayerSettings.MusicVolume);
_sfxManager = new SfxManager(_scorePlayer);
_scorePlayer.LoadStream(_musicWaveStream);
//_scorePlayer.AddInputStream(_musicWaveStream, PlayerSettings.MusicVolume);
_sfxManager = new SfxManager(_audioManager);
PreloadNoteSounds();
_sfxBufferTime = 0d;
_scorePlayer.PositionChanged += MusicPlayer_PositionChanged;
Expand All @@ -283,7 +287,7 @@ private void BtnScoreLoad_Click(object sender, EventArgs e) {
timer.Start();
}

private void MusicPlayer_PlaybackStopped(object sender, NAudio.Wave.StoppedEventArgs e) {
private void MusicPlayer_PlaybackStopped(object sender, EventArgs e) {
BtnStop_Click(this, EventArgs.Empty);
}

Expand Down
9 changes: 4 additions & 5 deletions Apps/ScoreViewer/Forms/FViewer.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Diagnostics;
using System.IO;
using System.Windows.Forms;
Expand Down Expand Up @@ -90,10 +90,8 @@ private void PreloadNoteSounds() {
for (var i = 0; i < sfxTypeCount; ++i) {
var sfxDirName = string.Format(SoundEffectAudioDirectoryNameFormat, i.ToString("00"));
foreach (var waveAudioName in new[] { TapHcaName, FlickHcaName }) {
var key = $"{sfxDirName}/{waveAudioName}";
using (var dataStream = File.Open(key, FileMode.Open, FileAccess.Read)) {
_sfxManager.PreloadWave(dataStream, key);
}
var fileName = $"{sfxDirName}/{waveAudioName}";
_sfxManager.PreloadWave(fileName);
}
}
}
Expand Down Expand Up @@ -182,6 +180,7 @@ private void SetControlsEnabled(ViewerState state) {
private readonly Timer timer = new Timer(5);
private uint _lastRedrawTime;

private AudioManager _audioManager;
private ScorePlayer _scorePlayer;
private LiveMusicWaveStream _musicWaveStream;
private SfxManager _sfxManager;
Expand Down
6 changes: 3 additions & 3 deletions Apps/ScoreViewer/PlayerSettings.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using DereTore.Common;

namespace DereTore.Apps.ScoreViewer {
Expand Down Expand Up @@ -27,10 +27,10 @@ public static float SfxVolume {
}

// Compensates for systematic offset of official scores, which turns out to be very close to zero
public static TimeSpan GlobalOffset { get; set; } = TimeSpan.Zero;
public static TimeSpan GlobalOffset { get; set; } = TimeSpan.FromSeconds(0.036);

// Compensates for a future ~3ms (128 samples) HCA encoder delay on WAV music files
public static TimeSpan MusicFileOffset { get; set; } = TimeSpan.Zero;
public static TimeSpan MusicFileOffset { get; set; } = TimeSpan.FromSeconds(0.003);

// Total offset
public static TimeSpan SfxOffset {
Expand Down
Loading

0 comments on commit dfeb420

Please sign in to comment.