1
0
mirror of https://github.com/OpenRCT2/OpenRCT2 synced 2026-01-30 02:05:13 +01:00

Fix audio source leak

This commit is contained in:
Ted John
2022-05-14 03:24:03 +01:00
parent 2012488956
commit 2effca59d1
3 changed files with 24 additions and 33 deletions

View File

@@ -143,28 +143,6 @@ void Mixer_Channel_SetGroup(void* channel, MixerGroup group)
static_cast<IAudioChannel*>(channel)->SetGroup(group);
}
template<typename T> static void* PlayMusic(T&& src, int32_t loop)
{
auto* mixer = GetMixer();
if (mixer == nullptr)
return nullptr;
auto audioContext = GetContext()->GetAudioContext();
auto stream = audioContext->CreateStreamFromWAV(std::forward<T&&>(src));
if (stream == nullptr)
return nullptr;
auto* channel = mixer->Play(stream, loop, false);
if (channel == nullptr)
{
delete stream;
return nullptr;
}
channel->SetGroup(MixerGroup::RideMusic);
return channel;
}
IAudioChannel* Mixer_Play_Music(IAudioSource* source, int32_t loop, int32_t streaming)
{
auto* mixer = GetMixer();
@@ -181,11 +159,6 @@ IAudioChannel* Mixer_Play_Music(IAudioSource* source, int32_t loop, int32_t stre
return channel;
}
void* Mixer_Play_Music(std::unique_ptr<IStream> stream, int32_t loop)
{
return PlayMusic(std::move(stream), loop);
}
void Mixer_SetVolume(float volume)
{
GetMixer()->SetVolume(volume);

View File

@@ -70,7 +70,6 @@ uint64_t Mixer_Channel_GetOffset(void* channel);
int32_t Mixer_Channel_SetOffset(void* channel, uint64_t offset);
void Mixer_Channel_SetGroup(void* channel, OpenRCT2::Audio::MixerGroup group);
OpenRCT2::Audio::IAudioChannel* Mixer_Play_Music(OpenRCT2::Audio::IAudioSource* source, int32_t loop, int32_t streaming);
void* Mixer_Play_Music(std::unique_ptr<OpenRCT2::IStream> stream, int32_t loop);
void Mixer_SetVolume(float volume);
int32_t DStoMixerVolume(int32_t volume);

View File

@@ -12,6 +12,7 @@
#include "../Context.h"
#include "../OpenRCT2.h"
#include "../audio/AudioChannel.h"
#include "../audio/AudioContext.h"
#include "../audio/AudioMixer.h"
#include "../audio/audio.h"
#include "../config/Config.h"
@@ -60,8 +61,9 @@ namespace OpenRCT2::RideAudio
uint16_t Frequency{};
void* Channel{};
IAudioSource* Source{};
RideMusicChannel(const ViewportRideMusicInstance& instance, void* channel)
RideMusicChannel(const ViewportRideMusicInstance& instance, void* channel, IAudioSource* source)
{
RideId = instance.RideId;
TrackIndex = instance.TrackIndex;
@@ -72,6 +74,7 @@ namespace OpenRCT2::RideAudio
Frequency = instance.Frequency;
Channel = channel;
Source = source;
Mixer_Channel_SetOffset(channel, Offset);
Mixer_Channel_Volume(channel, DStoMixerVolume(Volume));
@@ -103,6 +106,9 @@ namespace OpenRCT2::RideAudio
Channel = src.Channel;
src.Channel = nullptr;
Source = src.Source;
src.Source = nullptr;
return *this;
}
@@ -112,6 +118,11 @@ namespace OpenRCT2::RideAudio
{
Mixer_Stop_Channel(Channel);
Channel = nullptr;
if (Source != nullptr)
{
Source->Release();
Source = nullptr;
}
}
}
@@ -195,7 +206,7 @@ namespace OpenRCT2::RideAudio
{
// Move circus music to the sound mixer group
channel->SetGroup(Audio::MixerGroup::Sound);
_musicChannels.emplace_back(instance, channel);
_musicChannels.emplace_back(instance, channel, nullptr);
}
}
}
@@ -209,10 +220,18 @@ namespace OpenRCT2::RideAudio
if (track != nullptr)
{
auto stream = track->Asset.GetStream();
auto channel = Mixer_Play_Music(std::move(stream), MIXER_LOOP_NONE);
if (channel != nullptr)
if (stream != nullptr)
{
_musicChannels.emplace_back(instance, channel);
auto audioContext = GetContext()->GetAudioContext();
auto source = audioContext->CreateStreamFromWAV(std::move(stream));
if (source != nullptr)
{
auto channel = Mixer_Play_Music(source, MIXER_LOOP_NONE, true);
if (channel != nullptr)
{
_musicChannels.emplace_back(instance, channel, source);
}
}
}
}
}