Skip to content

Commit ca1fc7e

Browse files
committed
audio: Add pitch API
1 parent 23044db commit ca1fc7e

File tree

7 files changed

+57
-0
lines changed

7 files changed

+57
-0
lines changed

src/audio/iaudioplayer.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ class IAudioPlayer
1616
virtual float volume() const = 0;
1717
virtual void setVolume(float volume) = 0;
1818

19+
virtual float pitch() const = 0;
20+
virtual void setPitch(float pitch) = 0;
21+
1922
virtual bool isLoaded() const = 0;
2023

2124
virtual void start() = 0;

src/audio/internal/audioplayer.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ bool AudioPlayer::load(unsigned int size, const void *data, unsigned long sample
5858

5959
m_loaded = true;
6060
ma_sound_set_volume(m_sound, m_volume);
61+
ma_sound_set_pitch(m_sound, m_pitch);
6162
return true;
6263
}
6364

@@ -81,6 +82,7 @@ bool AudioPlayer::loadCopy(IAudioPlayer *player)
8182
m_loaded = true;
8283
m_copy = true;
8384
ma_sound_set_volume(m_sound, m_volume);
85+
ma_sound_set_pitch(m_sound, m_pitch);
8486
return true;
8587
}
8688

@@ -99,6 +101,28 @@ void AudioPlayer::setVolume(float volume)
99101
ma_sound_set_volume(m_sound, volume);
100102
}
101103

104+
float AudioPlayer::pitch() const
105+
{
106+
return m_pitch;
107+
}
108+
109+
void AudioPlayer::setPitch(float pitch)
110+
{
111+
/*
112+
* 0.5 -> 220 Hz
113+
* 1 -> 440 Hz
114+
* 2 -> 880 Hz
115+
* 4 -> 1760 Hz
116+
* ...
117+
*/
118+
m_pitch = pitch;
119+
120+
if (!m_loaded)
121+
return;
122+
123+
ma_sound_set_pitch(m_sound, pitch);
124+
}
125+
102126
bool AudioPlayer::isLoaded() const
103127
{
104128
return m_loaded;

src/audio/internal/audioplayer.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ class AudioPlayer : public IAudioPlayer
2222
float volume() const override;
2323
void setVolume(float volume) override;
2424

25+
float pitch() const override;
26+
void setPitch(float pitch) override;
27+
2528
bool isLoaded() const override;
2629

2730
void start() override;
@@ -36,6 +39,7 @@ class AudioPlayer : public IAudioPlayer
3639
bool m_copy = false;
3740
bool m_started = false;
3841
float m_volume = 1;
42+
float m_pitch = 1;
3943
};
4044

4145
} // namespace libscratchcpp

src/audio/internal/audioplayerstub.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,16 @@ void AudioPlayerStub::setVolume(float volume)
2828
m_volume = volume;
2929
}
3030

31+
float AudioPlayerStub::pitch() const
32+
{
33+
return m_pitch;
34+
}
35+
36+
void AudioPlayerStub::setPitch(float pitch)
37+
{
38+
m_pitch = pitch;
39+
}
40+
3141
bool AudioPlayerStub::isLoaded() const
3242
{
3343
return true;

src/audio/internal/audioplayerstub.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ class AudioPlayerStub : public IAudioPlayer
1818
float volume() const override;
1919
void setVolume(float volume) override;
2020

21+
float pitch() const override;
22+
void setPitch(float pitch) override;
23+
2124
bool isLoaded() const override;
2225

2326
void start() override;
@@ -27,6 +30,7 @@ class AudioPlayerStub : public IAudioPlayer
2730

2831
private:
2932
float m_volume = 1;
33+
float m_pitch = 1;
3034
};
3135

3236
} // namespace libscratchcpp

test/audio/audioplayer_test.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,12 @@ TEST(AudioPlayerTest, Volume)
1212
player.setVolume(0.86f);
1313
ASSERT_EQ(player.volume(), 0.86f);
1414
}
15+
16+
TEST(AudioPlayerTest, Pitch)
17+
{
18+
AudioPlayer player;
19+
ASSERT_EQ(player.pitch(), 1);
20+
21+
player.setPitch(1.5f);
22+
ASSERT_EQ(player.pitch(), 1.5f);
23+
}

test/mocks/audioplayermock.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ class AudioPlayerMock : public IAudioPlayer
1414
MOCK_METHOD(float, volume, (), (const, override));
1515
MOCK_METHOD(void, setVolume, (float), (override));
1616

17+
MOCK_METHOD(float, pitch, (), (const, override));
18+
MOCK_METHOD(void, setPitch, (float), (override));
19+
1720
MOCK_METHOD(bool, isLoaded, (), (const, override));
1821

1922
MOCK_METHOD(void, start, (), (override));

0 commit comments

Comments
 (0)