Skip to content

Commit 56e2d52

Browse files
committed
Provides format detection for files when selecting audio
1 parent a68fd15 commit 56e2d52

File tree

4 files changed

+71
-27
lines changed

4 files changed

+71
-27
lines changed

LuaSTGNode.Legacy.Windows/Input/BGMInput.xaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
</Grid>
5050
<GridSplitter Grid.Column="1"/>
5151
<MediaElement x:Name="mediaPlayer" LoadedBehavior="Play" UnloadedBehavior="Stop" Grid.Column="2"/>
52+
<Label x:Name="labelSEInfo" Content="Audio type: unknown" Grid.Column="2" Margin="20,20,0,0" HorizontalAlignment="Left" VerticalAlignment="Top"/>
5253
<!--<m:MediaPlayer x:Name="mediaPlayer" Grid.Column="2"/>-->
5354
</Grid>
5455
</DockPanel>

LuaSTGNode.Legacy.Windows/Input/BGMInput.xaml.cs

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,14 @@
1-
using System;
2-
using System.Collections.Generic;
1+
using LuaSTGEditorSharp.EditorData;
2+
using LuaSTGEditorSharp.EditorData.Document;
3+
using LuaSTGEditorSharp.EditorData.Document.Meta;
4+
using System;
35
using System.Collections.ObjectModel;
6+
using System.IO;
47
using System.Linq;
58
using System.Text;
6-
using System.Threading.Tasks;
79
using System.Windows;
810
using System.Windows.Controls;
9-
using System.Windows.Data;
10-
using System.Windows.Documents;
1111
using System.Windows.Input;
12-
using System.Windows.Media;
13-
using System.Windows.Media.Imaging;
14-
using System.Windows.Shapes;
15-
using LuaSTGEditorSharp.EditorData;
16-
using LuaSTGEditorSharp.EditorData.Document;
17-
using LuaSTGEditorSharp.EditorData.Document.Meta;
18-
19-
using Path = System.IO.Path;
2012

2113
namespace LuaSTGEditorSharp.Windows.Input
2214
{
@@ -74,7 +66,28 @@ private void BoxBGMData_SelectionChanged(object sender, SelectionChangedEventArg
7466
if (!string.IsNullOrEmpty(m?.Result)) Result = m?.Result;
7567
try
7668
{
77-
mediaPlayer.Source = new Uri(m?.ExInfo1, UriKind.RelativeOrAbsolute);
69+
var uri = new Uri(m?.ExInfo1, UriKind.RelativeOrAbsolute);
70+
var fileStream = File.OpenRead(uri.AbsolutePath);
71+
var binaryReader = new BinaryReader(fileStream, Encoding.Default);
72+
byte[] buffer = binaryReader.ReadBytes(4);
73+
binaryReader.Close();
74+
fileStream.Close();
75+
string header = string.Join("", buffer.Select(element => element.ToString("X2")));
76+
string type = "unknown";
77+
switch (header)
78+
{
79+
case "49443303":
80+
type = "mp3";
81+
break;
82+
case "52494646":
83+
type = "wav";
84+
break;
85+
case "4F676753":
86+
type = "ogg";
87+
break;
88+
}
89+
labelSEInfo.Content = $"Audio type: {type}";
90+
mediaPlayer.Source = uri;
7891
//MessageBox.Show(m?.ExInfo1);
7992
//mediaPlayer.Play();
8093
}

LuaSTGNode.Legacy.Windows/Input/SEInput.xaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@
6060
</ListBox>
6161
</Grid>
6262
<MediaElement x:Name="mediaPlayer" LoadedBehavior="Play" UnloadedBehavior="Stop" Grid.Column="1"/>
63+
<Label x:Name="labelSEInfo" Content="Audio type: unknown" Grid.Column="1" Margin="20,20,0,0" HorizontalAlignment="Left" VerticalAlignment="Top"/>
6364
</Grid>
6465
</DockPanel>
6566
</Grid>

LuaSTGNode.Legacy.Windows/Input/SEInput.xaml.cs

Lines changed: 42 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,14 @@
1-
using System;
2-
using System.Collections.Generic;
1+
using LuaSTGEditorSharp.EditorData;
2+
using LuaSTGEditorSharp.EditorData.Document;
3+
using LuaSTGEditorSharp.EditorData.Document.Meta;
4+
using System;
35
using System.Collections.ObjectModel;
6+
using System.IO;
47
using System.Linq;
58
using System.Text;
6-
using System.Threading.Tasks;
79
using System.Windows;
810
using System.Windows.Controls;
9-
using System.Windows.Data;
10-
using System.Windows.Documents;
1111
using System.Windows.Input;
12-
using System.Windows.Media;
13-
using System.Windows.Media.Imaging;
14-
using System.Windows.Shapes;
15-
using LuaSTGEditorSharp.EditorData;
16-
using LuaSTGEditorSharp.EditorData.Document;
17-
using LuaSTGEditorSharp.EditorData.Document.Meta;
1812

1913
namespace LuaSTGEditorSharp.Windows.Input
2014
{
@@ -97,8 +91,43 @@ private void BoxSEData_SelectionChanged(object sender, SelectionChangedEventArgs
9791
if (!string.IsNullOrEmpty(m?.Result)) Result = m?.Result;
9892
try
9993
{
100-
mediaPlayer.Source = new Uri(m?.ExInfo1, UriKind.RelativeOrAbsolute);
101-
//mediaPlayer.Play();
94+
var uri = new Uri(m?.ExInfo1, UriKind.RelativeOrAbsolute);
95+
if (!string.IsNullOrEmpty(m?.ExInfo1))
96+
{
97+
string type = "unknown";
98+
if (allSEInfoSys.Any(x => x.ExInfo1 == m.ExInfo1))
99+
{
100+
type = Path.GetExtension(m.ExInfo1).Substring(1);
101+
}
102+
else
103+
{
104+
var fileStream = File.OpenRead(uri.AbsolutePath);
105+
var binaryReader = new BinaryReader(fileStream, Encoding.Default);
106+
byte[] buffer = binaryReader.ReadBytes(4);
107+
binaryReader.Close();
108+
fileStream.Close();
109+
string header = string.Join("", buffer.Select(element => element.ToString("X2")));
110+
switch (header)
111+
{
112+
case "49443303":
113+
type = "mp3";
114+
break;
115+
case "52494646":
116+
type = "wav";
117+
break;
118+
case "4F676753":
119+
type = "ogg";
120+
break;
121+
}
122+
}
123+
labelSEInfo.Content = $"Audio type: {type}";
124+
mediaPlayer.Source = uri;
125+
//mediaPlayer.Play();
126+
}
127+
else
128+
{
129+
labelSEInfo.Content = "Audio type: unknown";
130+
}
102131
}
103132
catch (Exception exc) { MessageBox.Show(exc.ToString()); }
104133
codeText.Focus();

0 commit comments

Comments
 (0)