Sign in to follow this  
Naruto-kun

XAudio2 controlling the pulse rate of a loop

Recommended Posts

Hi guys

 

Im having a tough time figuring out the MS documentation here. I have a tone that I want to loop, where depending on the conditions I input, it will either be a solid tone, or a set of beep pulses. However I need to change the rate at which the beeps pulse without changing the pitch/frequency of the tone. Any ideas?

 

Thanks.

Share this post


Link to post
Share on other sites

This is audio synthesis, MSDN cannot really guess what audio you want to play :)

 

Anyway, modulate (multiply) your beep sound (which is solid frequency) with a square wave that is at much lower frequency and clamped from 0 to 1. The square wave will provide the beep period.

Share this post


Link to post
Share on other sites

The API supports speeding up playback of a whole voice, but in your case - since the pulse frequency and beep tone frequency are independent - you need to mix the sound yourself to get the desired effect. XAudio does not (IIRC) support modulating two streams out of the box.

 

This tutorial shows you how to do streaming from a file; in your case, instead of submitting data loaded from file, you'd submit the data you mixed in memory.

 

The formula would be something like this (somewhat metacode):

 

 

Tau = pi * 2 // full phase of a sine

BeepTone = 880 * Tau / sampleRate // 880 Hz beep

PulseFrequency = 1 / sampleRate; // low frequency oscillator frequency for pulse; you can modify this. Currently 1 pulse per second.

signed int16 soundBuffer[bufferSize]; // this is the buffer you'd submit to the source voice; assuming 16-bit mono
 

for i = 0 to bufferSize -1

{

    // synthesis: multiply a solid 880Hz sine with square pulse of 1Hz

    float sample = sin(BeepTone * i) * squarewave(PulseFrequency * i); // square wave range should be 0 to 1 
 

    // scale to signed int16 range

    soundBuffer[i] = sample * 32767;

}
 

// soundBuffer now contains the mixed wave to submit to source voice

Edited by Nik02

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this