Skip to content

Commit

Permalink
[AudD] Fix crash when reading from recognition bytes. Fix incorrect c…
Browse files Browse the repository at this point in the history
…onnection check.

Improve statefulness in Recognition page. Make hint text appear/disappear appropriately.
  • Loading branch information
Soapwood committed Jul 29, 2024
1 parent fb9c5b9 commit 05ec6fe
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 58 deletions.
2 changes: 1 addition & 1 deletion VXMusic/Audio/Conversion/AudioDataConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public AudioDataConverter(IServiceProvider serviceProvider)
{
_logger.LogTrace("Converting WAV to MP3.");

byte[] audioData = File.ReadAllBytes("output.wav"); // TODO Really need to fucking use just bytes.
byte[] audioData = File.ReadAllBytes(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "VirtualXtensions", "VXMusic", "Cache", "output.wav"));

if (audioData == null)
{
Expand Down
97 changes: 50 additions & 47 deletions VXMusicDesktop/MVVM/View/RecognitionView.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,13 @@

<StackPanel Orientation="Horizontal">
<StackPanel Orientation="Vertical"
Margin="0,0,0,0">
Margin="10,0,0,0">

<Button x:Name="RecognitionIntegrationEnableShazamApiButton"
Command="{Binding ShazamButtonClick}"
Width="300" Height="100"
Cursor="Hand"
Margin="20,0,10,0"
Margin="0,0,10,0"
IsEnabled="true">

<Button.Resources>
Expand Down Expand Up @@ -152,56 +152,57 @@
</Button.Background>
</Button>

<!-- BYOAPI -->
<StackPanel Orientation="Horizontal" Margin="0,10,0,0">
<!-- <CheckBox x:Name="ShazamByoApiCheckBox" -->
<!-- Content="Enable BYOAPI" -->
<!-- Cursor="Hand" -->
<!-- IsChecked="{Binding IsShazamByoApiEnabled}" -->
<!-- IsEnabled="{Binding IsShazamApiEnabled}" -->
<!-- VerticalAlignment="Center" HorizontalAlignment="Left" -->
<!-- Margin="20,0,0,0" -->
<!-- Height="30" -->
<!-- Style="{StaticResource CustomCheckBoxStyle}" -->
<!-- Checked="ShazamByoApiCheckButtonChecked" Unchecked="ShazamByoApiCheckButtonUnchecked" /> -->
<StackPanel Orientation="Horizontal">
<Grid>
<PasswordBox x:Name="ShazamByoApiPasswordBox"
<!-- Shazam BYOAPI -->
<StackPanel Orientation="Horizontal" Margin="4,10,0,0">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<TextBlock Grid.Column="0" x:Name="ShazamByoApiKeyHintText"
Text="API Key:"
Foreground="{StaticResource TextBasic}"
Visibility="{Binding IsShazamApiEnabled, Converter={StaticResource BoolToVis}}"
HorizontalAlignment="Left"
VerticalAlignment="Center"
FontSize="16"
Width="Auto"
Height="30"
Margin="0,0,0,0" />
<PasswordBox Grid.Column="1" x:Name="ShazamByoApiPasswordBox"
Visibility="{Binding IsShazamApiEnabled, Converter={StaticResource BoolToVis}}"
Style="{StaticResource ModernApiBox}"

IsEnabled="{Binding IsShazamApiEnabled}"
Margin="20,0,0,0"
Margin="5,0,0,0"
PasswordChanged="ShazamByoApiPasswordBoxChanged"
GotFocus="ShazamByoApiPasswordBoxGotFocus"
LostFocus="ShazamByoApiPasswordBoxLostFocus" />
<Canvas>
<Canvas Grid.Column="1">
<TextBlock x:Name="ShazamByoApiPasswordBoxHintText"
Text="•••••••••••••••••••••••••••••••••••"
Foreground="{StaticResource TextBasic}"
Visibility="{Binding ShouldShazamByoApiPlaceholderBeShown, Converter={StaticResource BoolToVis}}"
IsHitTestVisible="False"
HorizontalAlignment="Center"
HorizontalAlignment="Left"
VerticalAlignment="Center"
FontSize="20"
Width="260"
Width="230"
Height="30"
Panel.ZIndex="6"
Margin="27,0,0,0" />
Margin="10,0,0,0" />
</Canvas>
</Grid>
</StackPanel>
</StackPanel>
</StackPanel>

<StackPanel Orientation="Vertical"
Margin="0,0,0,0">
Margin="10,0,0,0">

<Button x:Name="RecognitionIntegrationEnableAudDApiButton"
Command="{Binding AudDButtonClick}"
Width="300" Height="100"
Cursor="Hand"
Margin="20,0,10,0"
Margin="00,0,00,0"
IsEnabled="true">

<Button.Resources>
Expand Down Expand Up @@ -287,45 +288,47 @@
</Button.Background>
</Button>

<!-- BYOAPI -->
<StackPanel Orientation="Horizontal" Margin="0,10,0,0">
<!-- <CheckBox x:Name="AudDByoApiCheckBox" -->
<!-- Content="Enable BYOAPI" -->
<!-- Cursor="Hand" -->
<!-- IsChecked="{Binding IsAudDByoApiEnabled, Mode=TwoWay}" -->
<!-- IsEnabled="{Binding IsAudDApiEnabled, Mode=TwoWay}" -->
<!-- Margin="20,0,0,0" -->
<!-- Height="30" -->
<!-- VerticalAlignment="Center" HorizontalAlignment="Left" -->
<!-- Style="{StaticResource CustomCheckBoxStyle}" -->
<!-- Checked="AudDByoApiCheckButtonChecked" Unchecked="AudDByoApiCheckButtonUnchecked" /> -->
<StackPanel Orientation="Horizontal">
<Grid>
<PasswordBox x:Name="AudDByoApiPasswordBox"
<!-- AudD BYOAPI -->
<StackPanel Orientation="Horizontal" Margin="4,10,0,0">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<TextBlock Grid.Column="0" x:Name="AudDByoApiKeyHintText"
Text="API Key:"
Foreground="{StaticResource TextBasic}"
Visibility="{Binding IsAudDApiEnabled, Converter={StaticResource BoolToVis}}"
HorizontalAlignment="Left"
VerticalAlignment="Center"
FontSize="16"
Width="Auto"
Height="30"
Margin="0,0,0,0" />
<PasswordBox Grid.Column="1" x:Name="AudDByoApiPasswordBox"
Visibility="{Binding IsAudDApiEnabled, Converter={StaticResource BoolToVis}}"
IsEnabled="{Binding IsAudDApiEnabled}"
Margin="20,0,0,0"
Margin="5,0,0,0"
Style="{StaticResource ModernApiBox}"
VerticalAlignment="Center" HorizontalAlignment="Center"
GotFocus="AudDByoApiPasswordBoxGotFocus"
LostFocus="AudDByoApiPasswordBoxLostFocus"
PasswordChanged="AudDByoApiPasswordBoxChanged" />
<Canvas>
<TextBlock x:Name="AudDByoApiPasswordBoxHintText"
<Canvas Grid.Column="1">
<TextBlock Grid.Column="1" x:Name="AudDByoApiPasswordBoxHintText"
Text="••••••••••••••••••••••••••••••••••"
Foreground="{StaticResource TextBasic}"
Visibility="{Binding ShouldAudDByoApiPlaceholderBeShown, Converter={StaticResource BoolToVis}}"
IsHitTestVisible="False"
HorizontalAlignment="Center"
VerticalAlignment="Center"
FontSize="20"
Width="260"
Width="230"
Height="30"
Panel.ZIndex="6"
Margin="27,0,0,0" />
Margin="10,0,0,0" />
</Canvas>
</Grid>
</StackPanel>
</StackPanel>
</StackPanel>
</StackPanel>
Expand Down
2 changes: 1 addition & 1 deletion VXMusicDesktop/MVVM/View/RecognitionView.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ private void AudDByoApiPasswordBoxChanged(object sender, RoutedEventArgs e)
recognitionViewModel.SharedViewModel.IsAudDApiConnected = false;
recognitionViewModel.AudDByoApiToken = ((PasswordBox)e.OriginalSource).Password;

recognitionViewModel.PerformSaveAndTestShazamByoApi();
recognitionViewModel.PerformSaveAndTestAudDByoApi();
}
}

Expand Down
43 changes: 36 additions & 7 deletions VXMusicDesktop/MVVM/ViewModel/RecognitionViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public class RecognitionViewModel : INotifyPropertyChanged
public ICommand AudDByoApiCheckButtonUnchecked => audDByoApiRadioButtonUnchecked ??= new RelayCommand(PerformEnableAudDByoApiUnchecked);
public ICommand ListenButtonClick => listenButtonClick ??= new RelayCommand(PerformListenButtonClick);
public ICommand RecognitionViewLoaded => recognitionViewLoaded ??= new RelayCommand(OnRecognitionViewLoaded);
public ICommand PasswordHintTextVisibilityToggle => recognitionViewLoaded ??= new RelayCommand(OnRecognitionOptionChanged);

// // //
public event PropertyChangedEventHandler? PropertyChanged;
Expand All @@ -51,7 +52,9 @@ public class RecognitionViewModel : INotifyPropertyChanged
//private bool _isShazamApiConnected;
private bool _isAudDApiEnabled;
private bool _isAudDByoApiEnabled;
// private bool _isAudDApiConnected;

private bool _shouldShazamByoApiPlaceholderBeShown;
private bool _shouldAudDByoApiPlaceholderBeShown;

private bool _isRecognitionReady = true;

Expand Down Expand Up @@ -81,6 +84,9 @@ public void Initialise()
_audDByoApiToken = App.VXMusicSession.RecognitionSettings.AudDSettings.ByoApiKey;
ProcessRecognitionApiState();

_shouldShazamByoApiPlaceholderBeShown = _isShazamApiEnabled && !string.IsNullOrEmpty(_shazamByoApiToken);
_shouldAudDByoApiPlaceholderBeShown = _isAudDApiEnabled && !string.IsNullOrEmpty(_audDByoApiToken);

HasBeenInitialised = true;
// TODO Do we actually want to do this here? It may be better to trigger this in the HomeView
CheckIfCurrentApiIsConnected();
Expand All @@ -91,6 +97,14 @@ private void OnRecognitionViewLoaded(object commandParameter)
ProcessRecognitionApiState();
//CheckIfCurrentApiIsConnected();
}

private void OnRecognitionOptionChanged(object commandParameter)
{
ShouldShazamByoApiPlaceholderBeShown = !ShouldShazamByoApiPlaceholderBeShown;
ShouldAudDByoApiPlaceholderBeShown = !ShouldAudDByoApiPlaceholderBeShown;

CheckIfCurrentApiIsConnected();
}

protected virtual void OnPropertyChanged(string propertyName)
{
Expand Down Expand Up @@ -129,6 +143,7 @@ public bool IsShazamApiEnabled
set
{
_isShazamApiEnabled = value;
ShouldAudDByoApiPlaceholderBeShown = false;
OnPropertyChanged(nameof(IsShazamApiEnabled));
}
}
Expand All @@ -150,15 +165,32 @@ public bool IsShazamByoApiKeySet

public bool ShouldShazamByoApiPlaceholderBeShown
{
get { return IsShazamApiEnabled && IsShazamByoApiKeySet; }
get { return _isShazamApiEnabled && !string.IsNullOrEmpty(_shazamByoApiToken); }
set
{
_shouldShazamByoApiPlaceholderBeShown = value;
OnPropertyChanged(nameof(ShouldShazamByoApiPlaceholderBeShown));
}
}

public bool ShouldAudDByoApiPlaceholderBeShown
{
get { return _isAudDApiEnabled && !string.IsNullOrEmpty(_audDByoApiToken); }
set
{
_shouldAudDByoApiPlaceholderBeShown = value;
OnPropertyChanged(nameof(ShouldAudDByoApiPlaceholderBeShown));
}
}


public bool IsAudDApiEnabled
{
get { return _isAudDApiEnabled; }
set
{
_isAudDApiEnabled = value;
ShouldShazamByoApiPlaceholderBeShown = false;
OnPropertyChanged(nameof(IsAudDApiEnabled));
}
}
Expand All @@ -168,11 +200,6 @@ public bool IsAudDApiKeySet
get { return !string.IsNullOrEmpty(AudDByoApiToken); }
}

public bool ShouldAudDByoApiPlaceholderBeShown
{
get { return IsAudDApiEnabled && IsAudDApiKeySet; }
}

public bool IsAudDByoApiEnabled
{
get { return _isAudDByoApiEnabled; }
Expand Down Expand Up @@ -318,6 +345,7 @@ private void PerformShazamButtonClick(object commandParameter)
//IsAudDByoApiEnabled = false;

ProcessRecognitionApiState();
CheckIfCurrentApiIsConnected();
}

private void PerformAudDButtonClick(object commandParameter)
Expand All @@ -330,6 +358,7 @@ private void PerformAudDButtonClick(object commandParameter)
//IsShazamByoApiEnabled = false;

ProcessRecognitionApiState();
CheckIfCurrentApiIsConnected();
}

private void PerformEnableShazamByoApiChecked(object commandParameter)
Expand Down
4 changes: 2 additions & 2 deletions VXMusicDesktop/Theme/TextBoxTheme.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -200,15 +200,15 @@
<ControlTemplate TargetType="{x:Type PasswordBox}">
<Border CornerRadius="5"
Background="{StaticResource SecondaryColor}"
Width="300" Height="30">
Width="235" Height="25">

<Grid>
<Rectangle StrokeThickness="1"/>
<PasswordBox Margin="1"
BorderThickness="0"
Background="Transparent"
VerticalAlignment="Center"
Padding="5"
VerticalContentAlignment="Center"
Foreground="{StaticResource TextBasic}"
x:Name="PasswordBox"/>

Expand Down

0 comments on commit 05ec6fe

Please sign in to comment.