UWP AudioGraph AudioFileInputNode change pitch

522 views Asked by At

How to change pitch in node of AudioGraph in UWP app?

I'm trying to port the application from WP7 to uwp (Windows 10, Mobile). in my WP7 app i use code:

SoundEffect soundEffect = //...(load sound effect).
SoundEffectInstance soundInstance = soundEffect.CreateInstance();
soundInstance.Pitch = pitch;

How to do it in UWP with AudioGraph?

2

There are 2 answers

1
andymule On

You should share a little more of your code so far, but you want to use the PlaybackSpeedFactor property, which is included on the AudioFileInputNode:

https://msdn.microsoft.com/en-us/library/windows/apps/windows.media.audio.audiofileinputnode.playbackspeedfactor

Currently, Microsoft's GitHub has the best examples of using AudioGraph: https://github.com/Microsoft/Windows-universal-samples/tree/master/Samples/AudioCreation/cs/AudioCreation

In the FilePlayback scenario from that last link, all you have to do is add this line to double the pitch of your audio file:

 fileInput.PlaybackSpeedFactor = 2;

You can make this change in real-time from anywhere in the program (e.g., have a slider that controls playback speed). This won't be a problem at all for AudioGraph. You can probably also play stuff backwards by settings this value negative!

0
Adam Dernis On

You may need to take the AudioFrame data run it through a Fourier Tranform, Multiply the pitches by some factor and then run it back through the Fourier Transform.

Here's some code for getting the frame

Where you define your graph graph.QuantumStarted += Graph_QuantumStarted;

[ComImport]
[Guid("5B0D3235-4DBA-4D44-865E-8F1D0E4FD04D")]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]

unsafe interface IMemoryBufferByteAccess
{
    void GetBuffer(out byte* buffer, out uint capacity);
}

private static void Graph_QuantumStarted(AudioGraph sender, object args)
{
    AudioFrame frame = frameOutputNode.GetFrame();
    using (AudioBuffer buffer = frame.LockBuffer(AudioBufferAccessMode.Write))
    using (IMemoryBufferReference reference = buffer.CreateReference())
    {
        //Run Fourier and Adjust here
    }
}