Skip to content

Commit

Permalink
Merge pull request #74 from BlueDeer233/master
Browse files Browse the repository at this point in the history
This PR will be merged into branch `spectrogram`. Further optimization and development is needed. Once the necessary enhancements are completed, we will merge the functionality into the main branch. Thank you for your contributions and efforts!
  • Loading branch information
Moying-moe authored Jun 17, 2024
2 parents 24e6d75 + 64f9e74 commit b4f708e
Show file tree
Hide file tree
Showing 8 changed files with 67 additions and 11 deletions.
3 changes: 3 additions & 0 deletions Langs/Langs.en-US.resx
Original file line number Diff line number Diff line change
Expand Up @@ -460,4 +460,7 @@ Click OK to start render.</value>
<data name="SmoothSlideAnimeTooltip" xml:space="preserve">
<value>When enabled, the Slide Track will disappear with the star instead of one judgment area after another.</value>
</data>
<data name="TextSpectrogram" xml:space="preserve">
<value>TextSpectrogram</value>
</data>
</root>
3 changes: 3 additions & 0 deletions Langs/Langs.ja.resx
Original file line number Diff line number Diff line change
Expand Up @@ -490,4 +490,7 @@ Open Browser to download latest update of Majdata?</value>
<value>When enabled, the Slide Track will disappear with the star instead of one judgment area after another.</value>
<comment>Translation required</comment>
</data>
<data name="TextSpectrogram" xml:space="preserve">
<value>スペクトラム</value>
</data>
</root>
6 changes: 6 additions & 0 deletions Langs/Langs.resx
Original file line number Diff line number Diff line change
Expand Up @@ -126,4 +126,10 @@
<data name="FIles" xml:space="preserve">
<value>sdadad</value>
</data>
<data name="TextFollow" xml:space="preserve">
<value />
</data>
<data name="TextSpectrogram" xml:space="preserve">
<value />
</data>
</root>
3 changes: 3 additions & 0 deletions Langs/Langs.zh-CN.resx
Original file line number Diff line number Diff line change
Expand Up @@ -452,4 +452,7 @@
<data name="SmoothSlideAnimeTooltip" xml:space="preserve">
<value>启用后,Slide轨迹会随着星星消失,而不是以判定区为单位消失</value>
</data>
<data name="TextSpectrogram" xml:space="preserve">
<value>显示频谱</value>
</data>
</root>
5 changes: 5 additions & 0 deletions MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@
Grid.ColumnSpan="2" SizeChanged="MusicWave_SizeChanged" />
<Line StrokeThickness="3" Y2="56" Height="74" VerticalAlignment="Bottom" Stroke="Red" Y1="15" Grid.Column="1"
HorizontalAlignment="Left" Width="45" />
<CheckBox x:Name="SpectrogramCheck" Content="{lex:Loc TextSpectrogram}" HorizontalAlignment="Left" Height="21"
Margin="9,0,0,65" VerticalAlignment="Bottom" Width="87"
Foreground="{DynamicResource ButtonForeground}" HorizontalContentAlignment="Right"
Click="SpectrogramCheck_Click" Background="{DynamicResource ButtonsBackground}"
Template="{DynamicResource DarkCheck}" />

<Menu Height="19" VerticalAlignment="Top" Background="{DynamicResource ButtonsBackground}"
Foreground="{DynamicResource ButtonForeground}" Grid.ColumnSpan="2">
Expand Down
6 changes: 6 additions & 0 deletions MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -496,6 +496,12 @@ private void FollowPlayCheck_Click(object sender, RoutedEventArgs e)
{
FumenContent.Focus();
}

private void SpectrogramCheck_Click(object sender, RoutedEventArgs e)
{
enableSpec = !enableSpec;
DrawWave();
}

private void Op_Button_Click(object sender, RoutedEventArgs e)
{
Expand Down
51 changes: 40 additions & 11 deletions MainWindowCore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Semver;
using Spectrogram;
using Un4seen.Bass;
using Un4seen.Bass.AddOn.Fx;
using WPFLocalizeExtension.Engine;
Expand All @@ -44,6 +45,8 @@ public partial class MainWindow : Window

//float[] wavedBs;
private readonly short[][] waveRaws = new short[3][];
private short[] bgmRAW;
public bool enableSpec = false;
public Timer chartChangeTimer = new(1000); // 谱面变更延迟解析]\
private readonly Timer currentTimeRefreshTimer = new(100);

Expand Down Expand Up @@ -343,7 +346,7 @@ private void ReadWaveFromFile()
var bgmInfo = Bass.BASS_SampleGetInfo(bgmSample);
var freq = bgmInfo.freq;
var sampleCount = (long)(songLength * freq * 2);
var bgmRAW = new short[sampleCount];
bgmRAW = new short[sampleCount];
Bass.BASS_SampleGetData(bgmSample, bgmRAW);

waveRaws[0] = new short[sampleCount / 20 + 1];
Expand Down Expand Up @@ -654,7 +657,8 @@ private void DrawWave()
var currentTime = Bass.BASS_ChannelBytes2Seconds(bgmStream, Bass.BASS_ChannelGetPosition(bgmStream));
graphics.Clear(Color.FromArgb(100, 0, 0, 0));
var resample = (int)deltatime - 1;
if (resample > 1 && resample <= 3) resample = 1;
if (resample > 3) resample = 2;
Expand All @@ -666,19 +670,44 @@ private void DrawWave()
var linewidth = backBitmap.Width / (float)(stopindex - startindex);
var pen = new Pen(Color.Green, linewidth);
var points = new List<PointF>();
for (var i = startindex; i < stopindex; i = i + 1)
if (enableSpec)
{
if (i < 0) i = 0;
if (i >= waveLevels.Length - 1) break;
var x = (i - startindex) * linewidth;
var y = waveLevels[i] / 65535f * height + height / 2;
points.Add(new PointF(x, y));
//Draw Spectrogram TODO: Performance needs to be optimized
float sampleRate = 0f;
Bass.BASS_ChannelGetAttribute(bgmStream, BASSAttribute.BASS_ATTRIB_FREQ, ref sampleRate);
var spectrogram = new SpectrogramGenerator((int)sampleRate, fftSize: 1024, stepSize: 500, maxFreq: 3000);
var _startindex = (int)((currentTime - deltatime) / (songLength / bgmRAW.Length)) - 500; //Bias a stepSize
var _stopindex = (int)((currentTime + deltatime) / (songLength / bgmRAW.Length)) + 500; //Bias a stepSize
double[] subWaveLevels = new double[_stopindex - _startindex];
for (int i = _startindex; i < _stopindex; i++)
{
var index = i - _startindex;
if (i < 0) i = 0;
if (i >= bgmRAW.Length - 1) break;
subWaveLevels[index] = (float)bgmRAW[i] / 2f;
}
spectrogram.Add(subWaveLevels);
graphics.CompositingMode = CompositingMode.SourceOver;
graphics.CompositingQuality = CompositingQuality.HighQuality;
graphics.DrawImage(spectrogram.GetBitmap(), 0, 0, backBitmap.Width, backBitmap.Height);
}
else
{
for (var i = startindex; i < stopindex; i = i + 1)
{
if (i < 0) i = 0;
if (i >= waveLevels.Length - 1) break;
graphics.DrawLines(pen, points.ToArray());
var x = (i - startindex) * linewidth;
var y = waveLevels[i] / 65535f * height + height / 2;
points.Add(new PointF(x, y));
}
graphics.DrawLines(pen, points.ToArray());
}
//Draw Bpm lines
var lastbpm = -1f;
var bpmChangeTimes = new List<double>(); //在什么时间变成什么值
Expand Down
1 change: 1 addition & 0 deletions MajdataEdit.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
<PackageReference Include="DiscordRichPresence" Version="1.2.1.24" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="Semver" Version="2.3.0" />
<PackageReference Include="Spectrogram" Version="1.6.1" />
<PackageReference Include="TagLibSharp" Version="2.3.0" />
<PackageReference Include="WPFLocalizeExtension" Version="3.10.0" />
<PackageReference Include="XAMLMarkupExtensions" Version="2.1.3" />
Expand Down

0 comments on commit b4f708e

Please sign in to comment.