panning and filters © allan c. milne abertay university v14.2.21

26
Panning and Filters © Allan C. Milne Abertay University v14.2.21

Upload: opal-booker

Post on 13-Jan-2016

219 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Panning and Filters © Allan C. Milne Abertay University v14.2.21

PanningandFilters

© Allan C. MilneAbertay University

v14.2.21

Page 2: Panning and Filters © Allan C. Milne Abertay University v14.2.21

Agenda.

Panning.

The output matrix.

Representing pan.

Applying filters.

Voice actions summary.

Page 3: Panning and Filters © Allan C. Milne Abertay University v14.2.21

Panning.• Panning is used to make a sound

appear to originate from a direction to the left or right of the listener.

• We apply different volumes to each of the input channels for each destination in the send list of a source voice.– e.g. if a destination has 2 input channels;

• attenuating the left channel – will cause the sound to appear to move to the right.

Page 4: Panning and Filters © Allan C. Milne Abertay University v14.2.21

SetOutputMatrix (…)• Sets the volume level of each output

channel of the voice.• this function must be applied to each

destination voice in the source voice’s send list.

• The output channels of the source voice must be mapped to the input channels of the destination voice;– this is defined via an output matrix.

Page 5: Panning and Filters © Allan C. Milne Abertay University v14.2.21

The Output Matrix.• A 1-D array of floats:

– [# of output channels * # of input channels]

• Each element is a map of a specific source output channel to a destination input channel.– This map is defined as an amplitude multiplier.

• Source output channels are grouped together and the group repeated for each input channel.– see MSDN XAudio2 SetOutputMatrix reference page

for a stereo output channel source voice and 5+1 input destination voice.

Page 6: Panning and Filters © Allan C. Milne Abertay University v14.2.21

Keeping It Simple.• A mono (1 channel) source voice.• A stereo (2 channel) destination voice.

• output matrix size =– output channels * input channels = 1 * 2 = 2

• matrix[0]– contribution of source voice channel to left input of destination.

• matrix[1]– contribution of output channel to right channel of destination.

• Works well for many scenarios: – use a mono .wav file;– use headphones for listening.

Page 7: Panning and Filters © Allan C. Milne Abertay University v14.2.21

Simple Panning Example.

float matrix[2] = { 0.25f, 0.75f };

source->SetOutputMatrix ( NULL, 1, 2, matrix );

• NULL: use if only 1 destination voice for the source voice.

• 1: mono source voice.• 2: stereo destination voice.• matrix: mapping source voice channel to destination voice input channels.

Page 8: Panning and Filters © Allan C. Milne Abertay University v14.2.21

Representing Pan.• Pan is implemented by destination input channel

volume adjustment expressed as an amplitude multiplier for each channel.

• But how can we express the level of pan as a direction relative to the listener?

• We will define the level of pan as a float between -1 and 1:– -1 = far left = -180 degrees.– 0 = straight ahead = 0 degrees (no pan);– 1 = far right = 180 degrees.

• The value can then be converted into appropriate volume levels for each destination input channel.

Page 9: Panning and Filters © Allan C. Milne Abertay University v14.2.21

Pan Encapsulation.• The pan representation and processing is

encapsulated in the XASound class of my framework.

class XASound : public ISound {public: … … … inline virtual float GetPan () const { return mPan; } virtual void SetPan (const float aPan); virtual void AdjustPan (const float anAmount);

private: IXAudio2SourceVoice *mSourceVoice; … … … float mPan;};

Page 10: Panning and Filters © Allan C. Milne Abertay University v14.2.21

void XASound::SetPan (const float aPan) { // guard stereo channels and valid pan value. if (XACore::GetInstance()->GetChannelCount() != 2) return; if (aPan<-1.0f || aPan>1.0f) return;

mPan = aPan; DoPan (mPan, mSourceVoice);} // end SetPan function.

void XASound::AdjustPan (const float anAmount) { // guard stereo channels. if (XACore::GetInstance()->GetChannelCount() != 2) return;

// keep within pan range. . mPan += anAmount; if (mPan < -1.0f) mPan = -1.0f; if (mPan >1.0f) mPan = 1.0f; DoPan (mPan, mSourceVoice);} // end AdjustPan function.

Page 11: Panning and Filters © Allan C. Milne Abertay University v14.2.21

Simplifying Assumptions.• XAudio2 supports any number of input &

output channels via its output matrix.• For simplicity we will assume that any

panned sound will be mono;– if the original .wav is not mono then convert it

to mono using a wave editor.• We will assume 2 output channels (stereo

speakers or headphones).– NB if your PC audio device is configured for

more than 2 channels then the code supplied here will not pan correctly.

Page 12: Panning and Filters © Allan C. Milne Abertay University v14.2.21

Applying The Pan.//--- Centred sound has output volumes at { 0.5, 0.5 }. void DoPan (const float aPan, IXAudio2SourceVoice *aVoice) { XAUDIO2_VOICE_DETAILS details; aVoice->GetVoiceDetails(&details); int matrixSize = details.InputChannels * 2; // 2=stereo. float *matrix = new float[matrixSize]; float halfPan = aPan / 2.0f; for (int i=0; i<matrixSize; i+=2) matrix[i] = 0.5f - halfPan; for (int i=1; i<matrixSize; i+=2) matrix[i] = 0.5f + halfPan; // 1st param = NULL indicates single destination voice. aVoice->SetOutputMatrix(NULL, details.InputChannels, 2, matrix); delete [] matrix;} // end doPan function.

Page 13: Panning and Filters © Allan C. Milne Abertay University v14.2.21

Alternative DoPan() Approach.

• The following approach to transforming the pan value to the amplitude multipliers for the destination channels is based on that used by DirectSound.

• No pan (mPan==0.0f) is set at – { 1.0f, 1.0f }.

• Adjust pan by applying attenuation to one channel.• E.g. to play the sound to the left, attenuate the right

channel;– { 1.0f, 0.75f }

Page 14: Panning and Filters © Allan C. Milne Abertay University v14.2.21

Continuous Panning.• Continuous panning every game frame

can be achieved based on delta time.• Constant panning:

– define a pan increment per unit time;– every game frame, pan increment * delta

time is the amount to adjust the pan by.• Panning for position, every game frame:

– compute (x,y) position of entity;– compute direction (angle) of entity;– compute pan as ratio of angle to 180;– set pan to this value.

Page 15: Panning and Filters © Allan C. Milne Abertay University v14.2.21

Doing It Correctly!

• The code we have seen here makes two BIG assumptions:– A mono source voice; and– A stereo destination voice.

• To do the panning for a source voice with arbitrary channels and an arbitrary speaker configuration then the output matrix must reflect this and be set accordingly.– see MSDN page on how to do panning.

Page 16: Panning and Filters © Allan C. Milne Abertay University v14.2.21

Filters.

• High, low and band pass filters can be applied by a source voice.

• The XAUDIO2_VOICE_USEFILTER flag must be supplied to XAudio2::CreateSourceVoice(…).

• Filters are defined via the SetFilterParameters(…) function.

Page 17: Panning and Filters © Allan C. Milne Abertay University v14.2.21

Enabling Filters.IXAudio2SourceVoice *source; HRESULT hr;hr = engine->CreateSourceVoice (&source, &format, XAUDIO2_VOICE_USEFILTER);if( FAILED( hr ) ) … ;

• The framework XASound constructors have optional 2nd boolean arguments defining if filters are enabled;– default is false.

XASound *mysound;mySound = new XASound (“noise.wav”, true);

Page 18: Panning and Filters © Allan C. Milne Abertay University v14.2.21

Defining A filter.

XAUDIO2_FILTER_PARAMETERS fParams; fParams.Type = LowPassFilter; fParams.Frequency = 1.0f; // in radians.fParams.OneOverQ = 1.0f;

source->SetFilterParameters(&fParams);

Page 19: Panning and Filters © Allan C. Milne Abertay University v14.2.21

Filters with XASound

mySound->SetFilterType (LowPassFilter);mySound->SetFilterCutoffFrequency (123.0f); // in Hz.mySound->SetFilter1OverQ (1.0f);

or

mySound->SetFilter (LowPassFilter, 123.0f, 1.0f);

or

mysound->SetFilterParameters (fParams);

Page 20: Panning and Filters © Allan C. Milne Abertay University v14.2.21

So We Can Now …

… use panning to render an audio sample in the left/right direction;

… appreciate some of the subtleties of representing and applying panning;

… apply a filter to a source voice;.

… use panning and filters with the XASound encapsulation.

Page 21: Panning and Filters © Allan C. Milne Abertay University v14.2.21

Voice Actions Summary.

• All source and submix voices perform the actions described in the following slides, in the order presented.

• These actions are performed on audio data that travels through the voice.

• so for a source voice this is the audio data as it is submitted from the XAudio2 buffer to its send list destinations.

Page 22: Panning and Filters © Allan C. Milne Abertay University v14.2.21

1. Overall Volume Adjustment.

• applied to all audio channels.• See XAudio2Voice::SetVolume.• This is applied

– after filters and XAPO effect chain for source voices;

– before filters and XAPO effect chain for submix and mastering voices.

Page 23: Panning and Filters © Allan C. Milne Abertay University v14.2.21

2. DSP Effects.

• Optional client-specified chain of DSP (digital sound processing) effects.

• There can be 1 or more effects applied.• Effects can be

• built-in, e.g. reverb; or• user-defined via the IXAPO interface.

Page 24: Panning and Filters © Allan C. Milne Abertay University v14.2.21

3. Output Volume Adjustment.

• This is applied to the output channels.• Volume can be adjusted separately for

each output channel.• See

XAudio2Voice::SetChannelVolumes.

Page 25: Panning and Filters © Allan C. Milne Abertay University v14.2.21

4. Matrix Mix.

• A mix defined by an output matrix of per-channel contributions to output destinations.

• the mix can change the number of channels if necessary.

• Applied separately to each of the output destination voices.

• If the destination is the mastering voice then it is applied to the audio device.

Page 26: Panning and Filters © Allan C. Milne Abertay University v14.2.21

A source voice can also …

• apply sample rate conversion (SRC) to the source audio data to make it consistent with that expected by the send list.

• apply a filter to colour the sound;– See SetFilterParameters(…)

• apply a filter to the voice's outputs;– See SetOutputFilterParameters(…)