Skip to content

Commit e8473fc

Browse files
committed
fix #553: Use clone root audio player when cloning sounds
1 parent 1f816e6 commit e8473fc

File tree

3 files changed

+23
-8
lines changed

3 files changed

+23
-8
lines changed

src/scratch/sound.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,13 +65,15 @@ bool Sound::isPlaying()
6565
/*! Returns an independent copy of the sound which is valid for as long as the original sound exists. */
6666
std::shared_ptr<Sound> Sound::clone() const
6767
{
68+
const Sound *root = impl->cloneRoot ? impl->cloneRoot : this;
6869
auto sound = std::make_shared<Sound>(name(), id(), dataFormat());
6970
sound->setRate(rate());
7071
sound->setSampleCount(sampleCount());
72+
sound->impl->cloneRoot = root;
7173
sound->impl->player->setVolume(impl->player->volume());
7274

73-
if (impl->player->isLoaded()) {
74-
sound->impl->player->loadCopy(impl->player.get());
75+
if (root->impl->player->isLoaded()) {
76+
sound->impl->player->loadCopy(root->impl->player.get());
7577
sound->setData(dataSize(), const_cast<void *>(data()));
7678
}
7779

src/scratch/sound_p.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
namespace libscratchcpp
1111
{
1212

13+
class Sound;
14+
1315
struct SoundPrivate
1416
{
1517
SoundPrivate();
@@ -19,6 +21,7 @@ struct SoundPrivate
1921
int sampleCount = 0;
2022
static IAudioOutput *audioOutput;
2123
std::shared_ptr<IAudioPlayer> player = nullptr;
24+
const Sound *cloneRoot = nullptr;
2225
};
2326

2427
} // namespace libscratchcpp

test/assets/sound_test.cpp

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -134,13 +134,23 @@ TEST_F(SoundTest, Clone)
134134
ASSERT_EQ(clone->rate(), sound.rate());
135135
ASSERT_EQ(clone->sampleCount(), sound.sampleCount());
136136

137+
auto cloneClonePlayer = std::make_shared<AudioPlayerMock>();
138+
EXPECT_CALL(m_playerFactory, createAudioPlayer()).WillOnce(Return(cloneClonePlayer));
139+
EXPECT_CALL(*m_player, isLoaded()).WillOnce(Return(true));
140+
EXPECT_CALL(*cloneClonePlayer, loadCopy(m_player.get())).WillOnce(Return(true));
141+
EXPECT_CALL(*clonePlayer, volume()).WillOnce(Return(0.62));
142+
EXPECT_CALL(*cloneClonePlayer, setVolume(0.62));
143+
EXPECT_CALL(*cloneClonePlayer, isLoaded()).WillOnce(Return(true));
144+
auto cloneClone = clone->clone();
145+
ASSERT_TRUE(cloneClone);
146+
137147
auto anotherClonePlayer = std::make_shared<AudioPlayerMock>();
138-
EXPECT_CALL(m_playerFactory, createAudioPlayer()).WillOnce(Return(clonePlayer));
148+
EXPECT_CALL(m_playerFactory, createAudioPlayer()).WillOnce(Return(anotherClonePlayer));
139149
EXPECT_CALL(*m_player, isLoaded()).WillOnce(Return(false));
140-
EXPECT_CALL(*clonePlayer, loadCopy).Times(0);
141-
EXPECT_CALL(*m_player, volume()).WillOnce(Return(0.62));
142-
EXPECT_CALL(*clonePlayer, setVolume(0.62));
143-
EXPECT_CALL(*clonePlayer, isLoaded()).Times(0);
144-
auto anotherClone = sound.clone();
150+
EXPECT_CALL(*anotherClonePlayer, loadCopy).Times(0);
151+
EXPECT_CALL(*clonePlayer, volume()).WillOnce(Return(0.62));
152+
EXPECT_CALL(*anotherClonePlayer, setVolume(0.62));
153+
EXPECT_CALL(*anotherClonePlayer, isLoaded()).Times(0);
154+
auto anotherClone = clone->clone();
145155
ASSERT_TRUE(anotherClone);
146156
}

0 commit comments

Comments
 (0)