-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
start fleshing out concept for audio player
- Loading branch information
Showing
18 changed files
with
279 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
using System; | ||
using System.Collections.ObjectModel; | ||
using System.IO; | ||
using System.Threading.Tasks; | ||
|
||
namespace G2DataGUI.Common.Data.Audio; | ||
|
||
public class AudioFiles | ||
{ | ||
public static AudioFiles Instance { get; private set; } = new(); | ||
public ObservableCollection<AudioNode> DirectoryAudioFiles { get; private set; } = []; | ||
public event EventHandler CollectionRefreshed; | ||
|
||
private AudioFiles() | ||
{ | ||
_ = ReadAudioFileSystemStructureAsync(); | ||
} | ||
|
||
public void Reload() { } | ||
|
||
private async Task ReadAudioFileSystemStructureAsync() => | ||
await Task.Run(ReadAudioFileSystemStructure).ConfigureAwait(false); | ||
|
||
private void ReadAudioFileSystemStructure() | ||
{ | ||
DirectoryAudioFiles.Clear(); | ||
foreach (var node in RecursiveDirectoryCrawl(Version.Instance.RootContentDirectory)) | ||
{ | ||
DirectoryAudioFiles.Add(node); | ||
} | ||
|
||
CollectionRefreshed?.Invoke(this, EventArgs.Empty); | ||
} | ||
|
||
private ObservableCollection<AudioNode> RecursiveDirectoryCrawl(string directory) | ||
{ | ||
ObservableCollection<AudioNode> node = []; | ||
|
||
foreach(string dir in Directory.GetDirectories(directory)) | ||
{ | ||
var child = new AudioNode(new DirectoryInfo(dir).Name, dir, false, RecursiveDirectoryCrawl(dir)); | ||
node.Add(child); | ||
} | ||
|
||
foreach(string file in Directory.GetFiles(directory, "*.ogg")) | ||
{ | ||
AudioNode child = new(new DirectoryInfo(file).Name, file, true); | ||
node.Add(child); | ||
} | ||
|
||
return node; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
using System.Collections.ObjectModel; | ||
|
||
namespace G2DataGUI.Common.Data.Audio; | ||
|
||
public class AudioNode(string title, string path, bool isFile, ObservableCollection<AudioNode> children) | ||
{ | ||
public ObservableCollection<AudioNode> Children { get; } = children; | ||
public string Title { get; } = title; | ||
public string Path { get; } = path; | ||
public bool IsFile { get; } = isFile; | ||
|
||
public AudioNode(string title, string path, bool isFile) : this(title, path, isFile, []) { } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace G2DataGUI.Events; | ||
|
||
public class AudioEventArgs() : EventArgs | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
using System; | ||
using System.Collections.ObjectModel; | ||
using G2DataGUI.Common.Data.Audio; | ||
using G2DataGUI.UI.Common.ViewModels; | ||
using NAudio.Vorbis; | ||
using NAudio.CoreAudioApi; | ||
using NAudio.Wave; | ||
|
||
namespace G2DataGUI.UI.ViewModels; | ||
|
||
public class AudioViewerViewModel : BaseViewModel, IDisposable | ||
{ | ||
public ObservableCollection<AudioNode> GameDirectoryAudioFiles { get; } = AudioFiles.Instance.DirectoryAudioFiles; | ||
private AudioNode _selectedAudioFile = null; | ||
private VorbisWaveReader _waveReader = null; | ||
private WasapiOut _wasapiOut = null; | ||
public int TreeViewWidth { get; } = 200; | ||
|
||
public static AudioViewerViewModel Instance { get; private set; } = new(); | ||
|
||
private AudioViewerViewModel() { } | ||
|
||
private MMDevice GetDefaultAudioDevice() | ||
{ | ||
using var enumerator = new MMDeviceEnumerator(); | ||
return enumerator.GetDefaultAudioEndpoint(DataFlow.Render, Role.Console); | ||
} | ||
|
||
private void AudioFileCollectionRefreshed(object sender, EventArgs args) { } | ||
|
||
private void LoadAudioFile() | ||
{ | ||
StopPlayback(); | ||
|
||
if (SelectedAudioFile != null) | ||
{ | ||
_waveReader = new VorbisWaveReader(SelectedAudioFile.Path); | ||
_wasapiOut = new(GetDefaultAudioDevice(), AudioClientShareMode.Shared, false, 10); | ||
_wasapiOut.Init(_waveReader); | ||
_wasapiOut.Play(); | ||
} | ||
} | ||
|
||
private void StopPlayback() | ||
{ | ||
if (_wasapiOut != null && _wasapiOut.PlaybackState == PlaybackState.Playing) | ||
{ | ||
_wasapiOut.Stop(); | ||
_wasapiOut?.Dispose(); | ||
_wasapiOut = null; | ||
} | ||
|
||
if (_waveReader != null) | ||
{ | ||
_waveReader?.Dispose(); | ||
_waveReader = null; | ||
} | ||
} | ||
|
||
public void Dispose() => StopPlayback(); | ||
|
||
public AudioNode SelectedAudioFile | ||
{ | ||
get => _selectedAudioFile; | ||
set | ||
{ | ||
if (value.IsFile) | ||
{ | ||
_selectedAudioFile = value; | ||
LoadAudioFile(); | ||
OnPropertyChanged(nameof(SelectedAudioFile)); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using G2DataGUI.Events; | ||
using G2DataGUI.UI.Common.ViewModels; | ||
|
||
namespace G2DataGUI.UI.ViewModels; | ||
|
||
public class AudioViewerWindowViewModel : BaseViewModel | ||
{ | ||
public static AudioViewerWindowViewModel Instance { get; private set; } = new(); | ||
|
||
private AudioViewerWindowViewModel() | ||
{ | ||
|
||
} | ||
|
||
public void OnAudioChange(object sender, AudioEventArgs e) { } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<UserControl x:Class="G2DataGUI.UI.Views.AudioViewerView" | ||
xmlns="https://github.com/avaloniaui" | ||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" | ||
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" | ||
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" | ||
xmlns:vm="using:G2DataGUI.UI.ViewModels" | ||
mc:Ignorable="d" | ||
Focusable="True" | ||
x:CompileBindings="True" | ||
x:DataType="vm:AudioViewerViewModel"> | ||
<Grid Name="AudioViewerPage" | ||
Margin="4"> | ||
<Grid.ColumnDefinitions> | ||
<ColumnDefinition Width="Auto" /> | ||
</Grid.ColumnDefinitions> | ||
<TreeView Name="AudioFileTree" | ||
Grid.Column="0" | ||
Items="{Binding GameDirectoryAudioFiles}" | ||
SelectedItem="{Binding SelectedAudioFile}" | ||
Width="{Binding TreeViewWidth}" | ||
VerticalAlignment="Stretch" | ||
HorizontalAlignment="Left"> | ||
<TreeView.ItemTemplate> | ||
<TreeDataTemplate ItemsSource="{Binding Children}"> | ||
<TextBlock Text="{Binding Title}" /> | ||
</TreeDataTemplate> | ||
</TreeView.ItemTemplate> | ||
</TreeView> | ||
</Grid> | ||
</UserControl> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
using Avalonia.Controls; | ||
using G2DataGUI.UI.ViewModels; | ||
|
||
namespace G2DataGUI.UI.Views; | ||
|
||
public partial class AudioViewerView : UserControl | ||
{ | ||
public AudioViewerView() | ||
{ | ||
DataContext = AudioViewerViewModel.Instance; | ||
InitializeComponent(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<Window x:Class="G2DataGUI.UI.Windows.AudioViewerWindow" | ||
xmlns="https://github.com/avaloniaui" | ||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" | ||
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" | ||
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" | ||
xmlns:vm="using:G2DataGUI.UI.ViewModels" | ||
xmlns:views="using:G2DataGUI.UI.Views" | ||
Width="500" | ||
Height="1000" | ||
Title="AudioViewerWindow" | ||
mc:Ignorable="d"> | ||
<views:AudioViewerView /> | ||
</Window> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
using Avalonia; | ||
using Avalonia.Controls; | ||
using G2DataGUI.UI.ViewModels; | ||
|
||
namespace G2DataGUI.UI.Windows; | ||
|
||
public partial class AudioViewerWindow : Window | ||
{ | ||
public AudioViewerWindow() | ||
{ | ||
DataContext = AudioViewerWindowViewModel.Instance; | ||
InitializeComponent(); | ||
#if DEBUG | ||
this.AttachDevTools(); | ||
#endif | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,4 +10,4 @@ | |
Title="DDS Viewer" | ||
mc:Ignorable="d"> | ||
<views:DDSViewerView /> | ||
</Window> | ||
</Window> |