Target | Branch | Status | Recommended Nuget packages version |
---|---|---|---|
Current Release | master | ||
Pre-release | dev |
The AudioAnalyzer UWP extension DLL contains a component that can provide realtime audio analysis information for visualization and other purposes. The library also contains prebuilt controls implementing VU meters, a spectrum analyzer as well as a control that has a custom draw capability.
Download and install the AudioAnalyzer nuget package,
Install-Package UWPAudioVisualizer
for convenient use add namespace statement to your C# code as:
using AudioVisualizer;
add namespace statement to your XAML code as:
<Page
x:Class="App4.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
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:visualizer="using:AudioVisualizer"
mc:Ignorable="d">
<Grid>
<StackPanel>
<visualizer:DiscreteVUBar />
<visualizer:CustomVisualizer />
</StackPanel>
</Grid>
</Page>
First you need to create the analyzer object, that implements IVisualizationSource interface which is basis to retrieve audio data.
To use visualizations with MediaPlayer or MediaElement you need to use PlaybackSource class. Both MediaPlayer and MediaElement use Media Foundation pipeline to render media. Pipeline will re-create the Media Foundation Tranform object that does the analysis every time you open a new source. That means that the instance of IVisualizationSource will change for every new media that you open in MediaPlayer.
PlaybackSource monitors media pipeline and get's the new visualization when it is created and raises an event. You should use this event to get the new visualization source and set the source for all the visualization controls you have active. This is also a good place to configure the analyzer if you wish - for more on this please see Configuring the Analyzer. If the player is a global object it is also good idea to set the current visualization source in OnLoad or OnNavigatedTo type of an event/overload.
class VisualizationPage
{
MediaPlayer _player;
AudioVisualizer.PlaybackSource _source;
public VisualizationPage()
{
_player = new MediaPlayer();
_source = new PlaybackSource(_player);
_source.SourceChanged += Playback_SourceChanged;
}
private void Playback_SourceChanged(object sender, IVisualizationSource source)
{
((ISpectralAnalyzerSettings)source).ConfigureSpectrum(4096, 0.5f);
vuMeter.Source = source;
spectrumDisplay.Source = source;
playPositionDisplay.Source = source;
}
private void Page_Loaded(object sender, RoutedEventArgs e)
{
vuMeter.Source = _source.VisualizationSource;
spectrumDisplay.Source = _source.VisualizationSource;
playPositionDisplay.Source = _source.VisualizationSource;
}
}
CustomVisualizer is a control which gets draw event callback every frame and it is up for the code in the event handler to present the visual look of the control. You can use then the [VisualizerDrawEventArgs]
ArrayData class is used to process spectrum data. The instance of this class is created by the library and you rarely want to create your own.
- IReadOnlyList<IReadOnlyList>
- IEnumerable<IReadOnlyList>
- Linear: Frequency amplitudes are represented by linear scale 0.0f - 1.0f
- Logarithmic: Frequency amplitudes are represented in db, where value 1.0f = 0dB
The count of frequency bins in spectrum.
- Linear: Frequency scale is linear
- Logarithmic: Frequency scale is logarithmic
Frequency value difference (linear) or ratio (logarithmic) between spectrum elements
Spectrum element 0 starting frequency
Spectrum element FrequencyCount starting frequency
This method allows to apply rise and fall times to the spectrum data over time and thus smooth the spectrum movements over time. For calculation you need to keep the value of last displayed spectrum values and pass them as the first parameter of the method. The time value of rise (and fall) time indicate the time by what the difference in values of spectrum data has decreased by 67%. So if the input for spectrum bin 1kHz changes from 0 to 1.0 at 1sec and the riseTime = 300ms then by 1.3sec the output value of this method at 1khZ will be 0.67 This method will fail if the AmplitudeScale is Logarithmic or the Frequency Counts and other attributes do not match
- previousData (ArrayData) Spectrum values of the previous instance. If null calculations run as if all values of previous data were 0
- riseTime (TimeSpan) Time constant for rising values
- fallTime (TimeSpan) Time constant for falling values
- timeFromPrevious (TimeSpan) Time passed from previousData
Method returns the calculated spectrum values with fall and rise times applied
You can use this code to calculate the bin frequency like this
public float BinFrequency(ArrayData data, uint bin)
{
if (data.FrequencyScale == ScaleType.Linear)
{
return bin * data.FrequencyStep + data.MinFrequency;
}
else
{
return data.MinFrequency * (float) Math.Pow(data.FrequencyStep, bin);
}
}
You access data as you would in an array ArrayData a1; float third_value_channel_0 = a1[0][3];
This is a class an instance of which is passed into the Draw event of the CustomVisualizer class
This property contains the visualization data, volume and spectrum. If this value is null then this means that there is no current data available - this could mean that the stream is stopped or after for example seek the audio analyzer has not yet caught up with buffering.
This is the Win2D CanvasDrawingSession object you can use to draw the control. You need to cast it to CanvasDrawingSession first before using `var drawingSession = (CanvasDrawingSession)args.DrawingSession;
Represent the actual rendering position of the stream, you can use this to get the actual play position
This is the size of the client area of the control. Use this to scale your drawing for the control when resized