Skip to content

Commit

Permalink
feat(amplimix): AfterMix callback.
Browse files Browse the repository at this point in the history
  • Loading branch information
na2axl committed Nov 13, 2024
1 parent f0992cd commit 4069a3b
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 13 deletions.
20 changes: 8 additions & 12 deletions include/SparkyStudios/Audio/Amplitude/Mixer/Amplimix.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,6 @@ namespace SparkyStudios::Audio::Amplitude
class Event;
class Sound;

/**
* @brief Called just before the mixer process audio data.
*
* @param[in] mixer The Amplimix instance.
* @param[in] buffer The audio buffer to be mixed.
* @param[in] frames The number of audio frames to be mixed.
*
* @ingroup mixer
*/
AM_CALLBACK(void, BeforeMixCallback)(Amplimix* mixer, AmVoidPtr buffer, AmUInt32 frames);

/**
* @brief Called just after the mixer process audio data.
*
Expand All @@ -52,7 +41,7 @@ namespace SparkyStudios::Audio::Amplitude
*
* @ingroup mixer
*/
AM_CALLBACK(void, AfterMixCallback)(Amplimix* mixer, AmVoidPtr buffer, AmUInt32 frames);
AM_CALLBACK(void, AfterMixCallback)(const Amplimix* mixer, const AudioBuffer* buffer, AmUInt32 frames);

/**
* @brief A single layer in the mixer.
Expand Down Expand Up @@ -350,6 +339,13 @@ namespace SparkyStudios::Audio::Amplitude
*/
[[nodiscard]] virtual bool IsInitialized() const = 0;

/**
* @brief Sets the callback function to be called after the audio data is mixed.
*
* @param[in] callback The callback function.
*/
virtual void SetAfterMixCallback(AfterMixCallback callback) = 0;

/**
* @brief Processes the audio data by mixing multiple audio sources for the specified number of frames.
*
Expand Down
2 changes: 1 addition & 1 deletion sample_project/pipelines/default.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"id":1,"name":"default","nodes":[{"id":1,"name":"Input","consume":[]},{"id":2,"name":"Attenuation","consume":[1]},{"id":3,"name":"Occlusion","consume":[2]},{"id":4,"name":"StereoPanning","consume":[17]},{"id":5,"name":"AmbisonicPanning","consume":[3]},{"id":6,"name":"NearFieldEffect","consume":[3]},{"id":7,"name":"AmbisonicMixer","consume":[18]},{"id":8,"name":"StereoMixer","consume":[4,6,10,12,14]},{"id":10,"name":"AmbisonicBinauralDecoder","consume":[7]},{"id":11,"name":"Reflections","consume":[1]},{"id":12,"name":"AmbisonicBinauralDecoder","consume":[11]},{"id":13,"name":"Clip","consume":[8]},{"id":14,"name":"Reverb","consume":[1]},{"id":15,"name":"Obstruction","consume":[3]},{"id":16,"name":"EnvironmentEffect","consume":[3]},{"id":17,"name":"StereoMixer","consume":[15,16]},{"id":18,"name":"AmbisonicRotator","consume":[5]},{"id":9,"name":"Output","consume":[8,13]}]}
{"id":1,"name":"default","nodes":[{"id":1,"name":"Input","consume":[]},{"id":2,"name":"Attenuation","consume":[1]},{"id":3,"name":"Occlusion","consume":[2]},{"id":4,"name":"StereoPanning","consume":[17]},{"id":5,"name":"AmbisonicPanning","consume":[3]},{"id":6,"name":"NearFieldEffect","consume":[3]},{"id":7,"name":"AmbisonicMixer","consume":[18]},{"id":8,"name":"StereoMixer","consume":[4,6,10,12,14]},{"id":10,"name":"AmbisonicBinauralDecoder","consume":[7]},{"id":11,"name":"Reflections","consume":[1]},{"id":12,"name":"AmbisonicBinauralDecoder","consume":[11]},{"id":13,"name":"Clip","consume":[8]},{"id":14,"name":"Reverb","consume":[1]},{"id":15,"name":"Obstruction","consume":[3]},{"id":16,"name":"EnvironmentEffect","consume":[3]},{"id":17,"name":"StereoMixer","consume":[15,16]},{"id":18,"name":"AmbisonicRotator","consume":[5]},{"id":9,"name":"Output","consume":[13]}]}
9 changes: 9 additions & 0 deletions src/Mixer/Amplimix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,11 @@ namespace SparkyStudios::Audio::Amplitude
_device.mDeviceOutputFormat = deviceOutputFormat;
}

void AmplimixImpl::SetAfterMixCallback(AfterMixCallback callback)
{
_afterMixCallback = callback;
}

AmUInt64 AmplimixImpl::Mix(AudioBuffer** outBuffer, AmUInt64 frameCount)
{
if (outBuffer != nullptr)
Expand Down Expand Up @@ -457,6 +462,10 @@ namespace SparkyStudios::Audio::Amplitude

if (hasMixedAtLeastOneLayer)
{
// Run the after-mix callback if available
if (_afterMixCallback != nullptr)
_afterMixCallback(this, &_scratchBuffer, frameCount);

if (outBuffer != nullptr)
*outBuffer = &_scratchBuffer;

Expand Down
4 changes: 4 additions & 0 deletions src/Mixer/Amplimix.h
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,8 @@ namespace SparkyStudios::Audio::Amplitude
return _initialized;
}

void SetAfterMixCallback(AfterMixCallback callback) override;

AmUInt64 Mix(AudioBuffer** outBuffer, AmUInt64 frameCount) override;

AmUInt32 Play(
Expand Down Expand Up @@ -247,6 +249,8 @@ namespace SparkyStudios::Audio::Amplitude
DeviceDescription _device;

AudioBuffer _scratchBuffer;

AfterMixCallback _afterMixCallback = nullptr;
};
} // namespace SparkyStudios::Audio::Amplitude

Expand Down

0 comments on commit 4069a3b

Please sign in to comment.