Skip to content

Commit e3d227c

Browse files
committed
Add cloning methods to AudioPlayer
1 parent 9ec931a commit e3d227c

File tree

8 files changed

+104
-1
lines changed

8 files changed

+104
-1
lines changed

src/audio/iaudioplayer.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,13 @@ class IAudioPlayer
1111
virtual ~IAudioPlayer() { }
1212

1313
virtual bool load(unsigned int size, const void *data, unsigned long sampleRate) = 0;
14+
virtual bool loadCopy(IAudioPlayer *player) = 0;
15+
16+
virtual float volume() const = 0;
1417
virtual void setVolume(float volume) = 0;
1518

19+
virtual bool isLoaded() const = 0;
20+
1621
virtual void start() = 0;
1722
virtual void stop() = 0;
1823

src/audio/internal/audioplayer.cpp

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// SPDX-License-Identifier: Apache-2.0
22

33
#include <iostream>
4+
#include <cassert>
45

56
#include "audioplayer.h"
67
#include "audioengine.h"
@@ -26,7 +27,7 @@ AudioPlayer::~AudioPlayer()
2627

2728
bool AudioPlayer::load(unsigned int size, const void *data, unsigned long sampleRate)
2829
{
29-
if (!AudioEngine::initialized())
30+
if (!AudioEngine::initialized() || m_loaded)
3031
return false;
3132

3233
ma_engine *engine = AudioEngine::engine();
@@ -55,6 +56,33 @@ bool AudioPlayer::load(unsigned int size, const void *data, unsigned long sample
5556
return true;
5657
}
5758

59+
bool AudioPlayer::loadCopy(IAudioPlayer *player)
60+
{
61+
assert(player && dynamic_cast<AudioPlayer *>(player));
62+
63+
if (!AudioEngine::initialized() || !player)
64+
return false;
65+
66+
ma_engine *engine = AudioEngine::engine();
67+
68+
AudioPlayer *playerPtr = static_cast<AudioPlayer *>(player);
69+
ma_result initResult = ma_sound_init_from_data_source(engine, playerPtr->m_decoder, MA_SOUND_FLAG_DECODE, NULL, m_sound);
70+
71+
if (initResult != MA_SUCCESS) {
72+
std::cerr << "Failed to init sound copy." << std::endl;
73+
return false;
74+
}
75+
76+
m_loaded = true;
77+
ma_sound_set_volume(m_sound, m_volume);
78+
return true;
79+
}
80+
81+
float AudioPlayer::volume() const
82+
{
83+
return m_volume;
84+
}
85+
5886
void AudioPlayer::setVolume(float volume)
5987
{
6088
m_volume = volume;
@@ -65,6 +93,11 @@ void AudioPlayer::setVolume(float volume)
6593
ma_sound_set_volume(m_sound, volume);
6694
}
6795

96+
bool AudioPlayer::isLoaded() const
97+
{
98+
return m_loaded;
99+
}
100+
68101
void AudioPlayer::start()
69102
{
70103
if (!m_loaded)

src/audio/internal/audioplayer.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,13 @@ class AudioPlayer : public IAudioPlayer
1717
~AudioPlayer();
1818

1919
bool load(unsigned int size, const void *data, unsigned long sampleRate) override;
20+
bool loadCopy(IAudioPlayer *player) override;
21+
22+
float volume() const override;
2023
void setVolume(float volume) override;
2124

25+
bool isLoaded() const override;
26+
2227
void start() override;
2328
void stop() override;
2429

src/audio/internal/audioplayerstub.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,24 @@ bool AudioPlayerStub::load(unsigned int size, const void *data, unsigned long sa
1313
return true;
1414
}
1515

16+
bool AudioPlayerStub::loadCopy(IAudioPlayer *player)
17+
{
18+
return true;
19+
}
20+
21+
float AudioPlayerStub::volume() const
22+
{
23+
return m_volume;
24+
}
25+
1626
void AudioPlayerStub::setVolume(float volume)
1727
{
28+
m_volume = volume;
29+
}
30+
31+
bool AudioPlayerStub::isLoaded() const
32+
{
33+
return true;
1834
}
1935

2036
void AudioPlayerStub::start()

src/audio/internal/audioplayerstub.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,20 @@ class AudioPlayerStub : public IAudioPlayer
1313
AudioPlayerStub();
1414

1515
bool load(unsigned int size, const void *data, unsigned long sampleRate) override;
16+
bool loadCopy(IAudioPlayer *player) override;
17+
18+
float volume() const override;
1619
void setVolume(float volume) override;
1720

21+
bool isLoaded() const override;
22+
1823
void start() override;
1924
void stop() override;
2025

2126
bool isPlaying() const override;
27+
28+
private:
29+
float m_volume = 1;
2230
};
2331

2432
} // namespace libscratchcpp

test/audio/CMakeLists.txt

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,20 @@ gtest_discover_tests(audioinput_test)
3333
if (LIBSCRATCHCPP_AUDIO_SUPPORT)
3434
target_compile_definitions(audioinput_test PRIVATE LIBSCRATCHCPP_AUDIO_SUPPORT)
3535
endif()
36+
37+
if(LIBSCRATCHCPP_AUDIO_SUPPORT)
38+
# audioplayer_test
39+
add_executable(
40+
audioplayer_test
41+
audioplayer_test.cpp
42+
)
43+
44+
target_link_libraries(
45+
audioplayer_test
46+
GTest::gtest_main
47+
scratchcpp
48+
)
49+
50+
gtest_discover_tests(audioplayer_test)
51+
target_compile_definitions(audioplayer_test PRIVATE LIBSCRATCHCPP_AUDIO_SUPPORT)
52+
endif()

test/audio/audioplayer_test.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#include <audio/internal/audioplayer.h>
2+
3+
#include "../common.h"
4+
5+
using namespace libscratchcpp;
6+
7+
TEST(AudioPlayerTest, Volume)
8+
{
9+
AudioPlayer player;
10+
ASSERT_EQ(player.volume(), 1);
11+
12+
player.setVolume(0.86f);
13+
ASSERT_EQ(player.volume(), 0.86f);
14+
}

test/mocks/audioplayermock.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,13 @@ class AudioPlayerMock : public IAudioPlayer
99
{
1010
public:
1111
MOCK_METHOD(bool, load, (unsigned int, const void *, unsigned long), (override));
12+
MOCK_METHOD(bool, loadCopy, (IAudioPlayer *), (override));
13+
14+
MOCK_METHOD(float, volume, (), (const, override));
1215
MOCK_METHOD(void, setVolume, (float), (override));
1316

17+
MOCK_METHOD(bool, isLoaded, (), (const, override));
18+
1419
MOCK_METHOD(void, start, (), (override));
1520
MOCK_METHOD(void, stop, (), (override));
1621

0 commit comments

Comments
 (0)