stride
stride copied to clipboard
[Audio] Adds ICustomBufferAudioSource to implement custom audio sources
PR Details
Adds an entry point for custom audio sources.
Description
The basic idea is to use a similar approach as the streaming sources that read files from the disc in blocks but have an endless callback for audio blocks. For this, the user can implement the interface ICustomBufferAudioSource
and pass it to the constructor of StreamedBufferSound
. This StreamedBuferSound
instance will then behave like any Sound
instance in Stride.
Example code for playing a sine wave on mouse click:
class MyCustomAudioSource : CustomAudioSourceBase
{
// Callback from the audio engine
public override bool ComputeAudioData(AudioData bufferToFill, out bool endOfStream)
{
// Create audio data
GenerateSineWave(bufferToFill.Data);
bufferToFill.CountDataBytes += BlockSizeInBytes;
endOfStream = false;
return true; // success
}
public float Frequency = 440f;
float phase = 0;
float left, right;
private void GenerateSineWave(WaveBuffer buffer)
{
var channels = Channels;
var samples = buffer.ShortBufferCount;
var increment = Frequency / SampleRate;
for (int i = 0; i < samples; i += channels)
{
phase += increment;
if (phase > 1.0f)
phase -= 1.0f;
left = right = (float)Math.Sin(phase * Math.PI * 2);
buffer.ShortBuffer[i] = (short)(left * short.MaxValue);
buffer.ShortBuffer[i + 1] = (short)(right * short.MaxValue);
}
}
}
public class AudioPlay : AsyncScript
{
public override async Task Execute()
{
// Create custom audio source
var mySource = new MyCustomAudioSource();
// Create the sound, spacialized sounds must be mono
var sound = new StreamedBufferSound(Audio.AudioEngine, mySource, spatialized: false);
// Create a sound instance
var soundInstance = sound.CreateInstance();
// Set the volume
soundInstance.Volume = 0.0f;
// Play the sound
soundInstance.Play();
while (Game.IsRunning)
{
var pressed = Input.IsMouseButtonDown(MouseButton.Left);
// Turn up volume on mouse down
soundInstance.Volume = pressed ? 0.5f : 0;
// Set frequency accoring to mouse y position
mySource.Frequency = (float)Math.Pow(20000, 1 - Input.MousePosition.Y);
// Do stuff every new frame
await Script.NextFrame();
}
}
}
Motivation and Context
For a project we require to use the Rapture3D engine from Blue Ripple Sound: https://www.blueripplesound.com/product-listings/gaming
Types of changes
- [ ] Docs change / refactoring / dependency upgrade
- [ ] Bug fix (non-breaking change which fixes an issue)
- [x] New feature (non-breaking change which adds functionality)
- [ ] Breaking change (fix or feature that would cause existing functionality to change)
Checklist
- [ ] My change requires a change to the documentation.
- [ ] I have added tests to cover my changes.
- [ ] All new and existing tests passed.
I'm also looking for this kind of functionality after struggling a fair bit with DynamicSoundSource - my use case is fairly similar, I would like an audio emitter to play sounds, and I am able to generate into a buffer on demand.
Is there anything that I can do to get this PR across the line? (assuming it works)
Hello @markdchurchill,
Thanks for jumping in and sharing your situation!
I'm pretty happy with how this approach turned out. Although the project that initially needed this ended up going in a different direction, I still think this has potential. It probably needs a bit of testing or a simple proof of concept to show it does the job.
If you're up for it, try merging this branch into your fork and give it a whirl. If it works out for you, that'd be a solid argument to get this PR over the finish line.
I would love to see this feature make it into Stride, and your use case could help push this forward. Let me know how it goes or if you need help setting things up!
Reupped as https://github.com/stride3d/stride/pull/2162
Closing as for the updated: #2162