schillermann
schillermann

Reputation: 11

Windows 8.1 App Audio with Effects (NAudio or SharpDX)

How can i load an Audio File from FileOpenPicker with NAudio or SharpDX and add an Audio FX like Flanger, Phaser, Echo, Gate, Bit Crusher.

FileOpenPicker openPicker = new FileOpenPicker();
openPicker.FileTypeFilter.Add(".mp3");
openPicker.FileTypeFilter.Add(".wav");
openPicker.FileTypeFilter.Add(".m4a");
openPicker.FileTypeFilter.Add(".wma");
openPicker.FileTypeFilter.Add(".aac");
StorageFile file = await openPicker.PickSingleFileAsync();

Upvotes: 0

Views: 703

Answers (1)

schillermann
schillermann

Reputation: 11

This is my Solution for a FX Sound Effect on my Audio File.

Play Sound

Before we can start, you need the following packages, that you get easily with NuGet-Package:

  • SharpDX
  • SharpDX.MediaFoundation
  • SharpDX.XAudio2

C#

FileOpenPicker openPicker = new FileOpenPicker();
openPicker.FileTypeFilter.Add(".mp3");
openPicker.FileTypeFilter.Add(".wav");
openPicker.FileTypeFilter.Add(".m4a");
openPicker.FileTypeFilter.Add(".wma");
openPicker.FileTypeFilter.Add(".aac");
StorageFile audioFile = await openPicker.PickSingleFileAsync();

MediaManager.Startup();
XAudio2 xaudio2 = new XAudio2();
xaudio2.StartEngine();
MasteringVoice masteringVoice = new MasteringVoice(xaudio2);
AudioPlayer audioPlayer = new AudioPlayer(xaudio2, await audioFile.OpenReadAsync());

The AudioPlayer Class come from Alexandre Mutel https://github.com/sharpdx/SharpDX-Samples/tree/master/Desktop/XAudio2/AudioPlayerApp . In the AudioPlayer Class you change the Constructor from:

public AudioPlayer(XAudio2 xaudio2, Stream audioStream)

To:

public AudioPlayer(XAudio2 xaudio2, IRandomAccessStreamWithContentType audioStream)

Now, you can control the Audio File with audioPlayer.Play(); audioPlayer.Stop();

Add Effect

You can add an Effect so:

SourceVoice sourceVoice = audioPlayer.SourceVoice;
Reverb reverb = new SharpDX.XAPO.Fx.Reverb();
EffectDescriptor effectDescriptor = new EffectDescriptor(reverb);
sourceVoice.SetEffectChain(effectDescriptor);
sourceVoice.EnableEffect(0);

Upvotes: 1

Related Questions