Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Audio] Adds ICustomBufferAudioSource to implement custom audio sources #1313

Closed

Conversation

tebjan
Copy link
Member

@tebjan tebjan commented Feb 1, 2022

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)
  • 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.

@tebjan tebjan marked this pull request as draft February 1, 2022 17:38
* Add wave buffer clas from NAudio
* Adds simple pre-configured base class for real time interactive audio.
@markdchurchill
Copy link

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)

@tebjan
Copy link
Member Author

tebjan commented Feb 20, 2024

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!

@markdchurchill
Copy link

Reupped as #2162

@tebjan
Copy link
Member Author

tebjan commented Feb 21, 2024

Closing as for the updated: #2162

@tebjan tebjan closed this Feb 21, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants