Search code examples
c#audiouwpwin-universal-app

How to create an audio fade in effect with UWP AudioGraph?


I have an app that plays audio files. When the user starts audio playback, I want the audio to fade in from effectively 0 volume to 1 over a duration of 300ms or something like that.

I've been playing with the AudioGraph sample from GitHub and I learned that there's a Limiter effect that can control the loudness of an audio. How can I use this to change the loudness of an audio file over a duration of 300ms?

Or is there another way I can implement a fade effect?


Solution

  • How can I use this to change the loudness of an audio file over a duration of 300ms?

    You can use a timer to control the loudness when starting audio playback. This is a test I conducted in the official sample(Scenario5_InboxEffects.xaml.cs), and the effect is very good. The code is as follows.

     //Scenario5_InboxEffects.xaml.cs
    
     private void TogglePlay()
     {
         if (graphButton.Content.Equals("Start Graph"))
         {
             graph.Start();
             graphButton.Content = "Stop Graph";
             audioPipe.Fill = new SolidColorBrush(Colors.Blue);
             //just for a test
             FadeEffect(300, 1, 1500);
         }
         ....
     }
    
    private async void FadeEffect(int delay, uint startLoudness, uint endLoudness)
    {
         
           for (int i = 1; i <= delay; i++)
           {
               var chip = (endLoudness - startLoudness) / (delay * 1.0) * i;
               limiterEffectDefinition.Loudness = (uint)(startLoudness + chip);
               limiterEffectDefinition.Release = 10;
               fileInputNode.EnableEffectsByDefinition(limiterEffectDefinition);
               await Task.Delay(1);
           }
    }
    

    Or is there another way I can implement a fade effect?

    Sorry, there is currently no related API in Windows Runtime AudioGraph.