Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Verified mel spectrograms in winml sample against pytorch #400

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,36 @@
xmlns:model="using:AudioPreprocessing.Model"
xmlns:muxc="using:Microsoft.UI.Xaml.Controls"
mc:Ignorable="d">

<StackPanel Background="White" Orientation="Vertical">
<Button Name="ShowFilePickerButton" Content="Open file" HorizontalAlignment="Center" Click = "OnOpenClick"
<StackPanel Background="White" Orientation="Horizontal" HorizontalAlignment="Center">
<StackPanel Background="White" Orientation="Vertical" Margin="10,10,10,10">
<Button Name="ShowFilePickerButton" Content="Open file" HorizontalAlignment="Center" Click = "OnOpenClick"
VerticalAlignment="Center" />
<CheckBox x:Name = "ColorMelSpectrogramCheckBox" Content = "Check to render Mel Spectrogram with color" HorizontalAlignment="Center"/>
<TextBlock Name="WavFilePath" Text="{x:Bind ViewModel.AudioPath}"
HorizontalAlignment="Center" VerticalAlignment="Center" />

<Button Name="SaveFilebutton" Command="{x:Bind ViewModel.SaveFileCommand}" Content="Save file"
HorizontalAlignment="Center" VerticalAlignment="Center" />
<TextBlock Name="ImagePath" Text="{x:Bind ViewModel.ImagePath}"
<CheckBox x:Name = "ColorMelSpectrogramCheckBox" Content = "Check to render Mel Spectrogram with color" HorizontalAlignment="Center"/>

<VariableSizedWrapGrid Orientation="Horizontal" MaximumRowsOrColumns ="6" HorizontalAlignment="Center">
<NumberBox Name="BatchSize" Header="Batch Size" Value="1"
VariableSizedWrapGrid.ColumnSpan="2"/>
<NumberBox Name="WindowSize" Header="Window Size" Value="400"
VariableSizedWrapGrid.ColumnSpan="2"/>
<NumberBox Name="DFTSize" Header="DFT Size" Value="400"
VariableSizedWrapGrid.ColumnSpan="2"/>
<NumberBox Name="HopSize" Header="Hop Size" Value="200"
VariableSizedWrapGrid.ColumnSpan="2"/>
<NumberBox Name="NMelBins" Header="Number of Mel Bins" Value="128"
VariableSizedWrapGrid.ColumnSpan="3"/>
<NumberBox Name="SampleRate" Header="Sample Rate" Value="8000"
VariableSizedWrapGrid.ColumnSpan="2"/>
<NumberBox Name="Amplitude" Header="Amplitude" Value="5000"
VariableSizedWrapGrid.ColumnSpan="2"/>
</VariableSizedWrapGrid>

<TextBlock Name="ImagePath" Text="{x:Bind ViewModel.ImagePath}"
HorizontalAlignment="Center" VerticalAlignment="Center" />

<StackPanel HorizontalAlignment="Center" Background="White" Orientation="Horizontal">
</StackPanel>
<StackPanel HorizontalAlignment="Center" Background="White" Orientation="Vertical">
<TextBlock Name="WavFilePath" Text="{x:Bind ViewModel.AudioPath}"
HorizontalAlignment="Center" VerticalAlignment="Center" />
<Image Name="spectrogram" Stretch="Fill" Height="500" Width="500" />
</StackPanel>
</StackPanel>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@
using System;
using System.Runtime.InteropServices;
using System.Threading.Tasks;
using Windows.Graphics.Imaging;
using Windows.Storage;
using Windows.Storage.Pickers;
using System.Text.RegularExpressions;


// To learn more about WinUI, the WinUI project structure,
// and more about our project templates, see: http://aka.ms/winui-project-info.
Expand Down Expand Up @@ -38,27 +39,25 @@ public MainWindow()
private async void OnOpenClick(object sender, RoutedEventArgs e)
{
string wavPath = await GetFilePath();
PreprocessModel melSpectrogram = new PreprocessModel();
var softwareBitmap = melSpectrogram.GenerateMelSpectrogram(wavPath, ColorMelSpectrogramCheckBox.IsChecked ?? false);

ViewModel.AudioPath = wavPath;
ViewModel.MelSpectrogramImage = softwareBitmap;

//Image control only accepts BGRA8 encoding and Premultiplied/no alpha channel. This checks and converts
//the SoftwareBitmap we want to bind.
if (softwareBitmap.BitmapPixelFormat != BitmapPixelFormat.Bgra8 ||
softwareBitmap.BitmapAlphaMode != BitmapAlphaMode.Premultiplied)
{
softwareBitmap = SoftwareBitmap.Convert(softwareBitmap, BitmapPixelFormat.Bgra8, BitmapAlphaMode.Premultiplied);
}
var melSpecSettings = new ModelSetting(
ColorMelSpectrogramCheckBox.IsChecked ?? false,
(int)BatchSize.Value,
(int)WindowSize.Value,
(int)DFTSize.Value,
(int)HopSize.Value,
(int)NMelBins.Value,
(int)SampleRate.Value,
(int)Amplitude.Value
);

ViewModel.GenerateMelSpectrograms(wavPath, melSpecSettings);
WavFilePath.Text = ViewModel.AudioPath;

await ((SoftwareBitmapSource)spectrogram.Source).SetBitmapAsync(softwareBitmap);
await ((SoftwareBitmapSource)spectrogram.Source).SetBitmapAsync(ViewModel.MelSpectrogramImage);
}

private async Task<string> GetFilePath()
{
{
FileOpenPicker openPicker = new FileOpenPicker();
openPicker.ViewMode = PickerViewMode.Thumbnail;
openPicker.FileTypeFilter.Add(".wav");
Expand All @@ -75,5 +74,11 @@ private async Task<string> GetFilePath()
StorageFile file = await openPicker.PickSingleFileAsync();
return file.Path;
}

private static readonly Regex _regex = new Regex("[^0-9.-]+"); //regex that matches disallowed text
private static bool IsTextAllowed(string text)
{
return !_regex.IsMatch(text);
}
}
}
Loading