Skip to content

Commit 28a82f7

Browse files
committed
Sound: Add owner property
1 parent 5635768 commit 28a82f7

File tree

5 files changed

+40
-3
lines changed

5 files changed

+40
-3
lines changed

include/scratchcpp/sound.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
namespace libscratchcpp
1010
{
1111

12+
class Thread;
1213
class SoundPrivate;
1314

1415
/*! \brief The Sound class represents a Scratch sound. */
@@ -34,13 +35,15 @@ class LIBSCRATCHCPP_EXPORT Sound : public Asset
3435
virtual void setVolume(double volume);
3536
virtual void setEffect(Effect effect, double value);
3637

37-
virtual void start();
38+
virtual void start(Thread *owner = nullptr);
3839
virtual void stop();
3940

4041
virtual bool isPlaying() const;
4142

4243
std::shared_ptr<Sound> clone() const;
4344

45+
Thread *owner() const;
46+
4447
protected:
4548
void processData(unsigned int size, void *data) override;
4649
virtual bool isClone() const override;

src/scratch/sound.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,11 @@ void Sound::setEffect(Effect effect, double value)
6666
}
6767

6868
/*! Starts the playback of the sound. */
69-
void Sound::start()
69+
void Sound::start(Thread *owner)
7070
{
7171
// Stop sounds in clones (#538)
7272
stopCloneSounds();
73+
impl->owner = owner;
7374
impl->player->start();
7475
}
7576

@@ -78,6 +79,7 @@ void Sound::stop()
7879
{
7980
// Stop sounds in clones (#538)
8081
stopCloneSounds();
82+
impl->owner = nullptr;
8183
impl->player->stop();
8284
}
8385

@@ -107,6 +109,11 @@ std::shared_ptr<Sound> Sound::clone() const
107109
return sound;
108110
}
109111

112+
Thread *Sound::owner() const
113+
{
114+
return impl->owner;
115+
}
116+
110117
void Sound::processData(unsigned int size, void *data)
111118
{
112119
if (impl->player->isLoaded())

src/scratch/sound_p.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ namespace libscratchcpp
1111
{
1212

1313
class Sound;
14+
class Thread;
1415

1516
struct SoundPrivate
1617
{
@@ -22,6 +23,7 @@ struct SoundPrivate
2223
static IAudioOutput *audioOutput;
2324
std::shared_ptr<IAudioPlayer> player = nullptr;
2425
const Sound *cloneRoot = nullptr;
26+
Thread *owner = nullptr;
2527
};
2628

2729
} // namespace libscratchcpp

test/assets/sound_test.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include <scratchcpp/sound.h>
22
#include <scratchcpp/sprite.h>
3+
#include <scratchcpp/thread.h>
34
#include <scratch/sound_p.h>
45
#include <audiooutputmock.h>
56
#include <audioplayermock.h>
@@ -128,6 +129,16 @@ TEST_F(SoundTest, Start)
128129
sound.start();
129130
}
130131

132+
TEST_F(SoundTest, StartWithOwner)
133+
{
134+
Sound sound("sound1", "a", "wav");
135+
Thread thread(nullptr, nullptr, nullptr);
136+
137+
EXPECT_CALL(*m_player, start());
138+
sound.start(&thread);
139+
ASSERT_EQ(sound.owner(), &thread);
140+
}
141+
131142
TEST_F(SoundTest, Stop)
132143
{
133144
Sound sound("sound1", "a", "wav");
@@ -136,6 +147,20 @@ TEST_F(SoundTest, Stop)
136147
sound.stop();
137148
}
138149

150+
TEST_F(SoundTest, StartAndStopWithOwner)
151+
{
152+
Sound sound("sound1", "a", "wav");
153+
Thread thread(nullptr, nullptr, nullptr);
154+
155+
EXPECT_CALL(*m_player, start());
156+
sound.start(&thread);
157+
ASSERT_EQ(sound.owner(), &thread);
158+
159+
EXPECT_CALL(*m_player, stop());
160+
sound.stop();
161+
ASSERT_EQ(sound.owner(), nullptr);
162+
}
163+
139164
TEST_F(SoundTest, IsPlaying)
140165
{
141166
Sound sound("sound1", "a", "wav");

test/mocks/soundmock.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class SoundMock : public Sound
1616
MOCK_METHOD(void, setVolume, (double), (override));
1717
MOCK_METHOD(void, setEffect, (Sound::Effect, double), (override));
1818

19-
MOCK_METHOD(void, start, (), (override));
19+
MOCK_METHOD(void, start, (Thread *), (override));
2020
MOCK_METHOD(void, stop, (), (override));
2121
MOCK_METHOD(bool, isPlaying, (), (const, override));
2222
};

0 commit comments

Comments
 (0)