Skip to content
This repository was archived by the owner on Oct 18, 2023. It is now read-only.

Commit fe4a051

Browse files
committed
Add randomize play for AudioSource
1 parent e2e34bf commit fe4a051

3 files changed

Lines changed: 72 additions & 9 deletions

File tree

module/includes/components/AudioSource.hpp

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,20 @@ namespace sw
3030
float m_startLoopPoint;
3131
float m_endPoint;
3232
float m_endLoopPoint;
33+
bool m_randomized;
34+
int m_maxOccurence;
35+
std::vector<std::string> m_audios;
36+
std::string m_last;
37+
int m_lastOccurence;
38+
39+
void defineBuffer(std::string name);
40+
std::string randomHandler();
3341

3442
public:
3543
explicit AudioSource(sw::GameObject& entity);
3644
~AudioSource() override;
3745

38-
AudioSource& setAudio(std::string audio);
46+
AudioSource& addAudio(std::string audio);
3947
AudioSource& play();
4048
AudioSource& pause();
4149
AudioSource& stop();
@@ -46,6 +54,8 @@ namespace sw
4654
AudioSource& setStartLoopPoint(float second);
4755
AudioSource& setEndPoint(float second);
4856
AudioSource& setEndLoopPoint(float second);
57+
AudioSource& setRandomized(bool random);
58+
AudioSource& setMaxOccurence(int occurence);
4959

5060
friend AudioSourceManager;
5161
};

module/includes/resources/OpenResources.hpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,21 +66,21 @@ namespace sw
6666
//std::priority_queue<std::unique_ptr<void>> m_mt; materials -> dev 3
6767
// List des resources
6868
public:
69-
static class TexturesMap : private std::map<std::string, std::shared_ptr<Texture>>
69+
static class TexturesMap : public std::map<std::string, std::shared_ptr<Texture>>
7070
{
7171
public:
7272
using std::map<std::string, std::shared_ptr<Texture>>::operator[];
7373
friend OpenResources;
7474
} m_ntext;
7575

76-
static class FontsMap : private std::map<std::string, std::shared_ptr<Font>>
76+
static class FontsMap : public std::map<std::string, std::shared_ptr<Font>>
7777
{
7878
public:
7979
using std::map<std::string, std::shared_ptr<Font>>::operator[];
8080
friend OpenResources;
8181
} m_nfont;
8282

83-
static class AudioMap : private std::map<std::string, std::shared_ptr<Audio>>
83+
static class AudioMap : public std::map<std::string, std::shared_ptr<Audio>>
8484
{
8585
public:
8686
using std::map<std::string, std::shared_ptr<Audio>>::operator[];

module/sources/components/AudioSource.cpp

Lines changed: 58 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,12 @@ m_currentSample(0.0f),
1818
m_startPoint(0.0f),
1919
m_startLoopPoint(-1),
2020
m_endLoopPoint(-1),
21-
m_endPoint(-1)
21+
m_endPoint(-1),
22+
m_randomized(false),
23+
m_maxOccurence(-1),
24+
m_audios(),
25+
m_last(),
26+
m_lastOccurence(0)
2227
{
2328
alGenSources(1, &m_source);
2429
}
@@ -29,16 +34,50 @@ sw::AudioSource::~AudioSource() noexcept
2934
alDeleteSources(1, &m_source);
3035
}
3136

32-
sw::AudioSource &sw::AudioSource::setAudio(std::string audio)
37+
void sw::AudioSource::defineBuffer(std::string name)
3338
{
34-
auto audioFile = sw::OpenResources::m_naudio[audio];
35-
alSourcei(m_source, AL_BUFFER, audioFile->getBuffer());
36-
m_endPoint = audioFile->getDuration();
39+
auto buffer = sw::OpenResources::m_naudio[name];
40+
alSourcei(m_source, AL_BUFFER, buffer->getBuffer());
41+
m_endPoint = buffer->getDuration();
42+
}
43+
44+
std::string sw::AudioSource::randomHandler()
45+
{
46+
int index;
47+
std::string audioName;
48+
49+
if (m_audios.size() < 2)
50+
return m_audios[0];
51+
do {
52+
index = std::rand() % (m_audios.size() - 1);
53+
audioName = m_audios[index];
54+
} while (m_last == audioName && m_lastOccurence == m_maxOccurence);
55+
if (m_last == audioName)
56+
m_lastOccurence++;
57+
else {
58+
m_lastOccurence = 1;
59+
m_last = audioName;
60+
}
61+
return audioName;
62+
}
63+
64+
sw::AudioSource &sw::AudioSource::addAudio(std::string audio)
65+
{
66+
m_audios.emplace_back(audio);
67+
if (m_audios.empty())
68+
defineBuffer(m_audios[0]);
3769
return (*this);
3870
}
3971

4072
sw::AudioSource &sw::AudioSource::play()
4173
{
74+
int value;
75+
76+
alGetSourcei(m_source, AL_SOURCE_STATE, &value);
77+
if (value == AL_PLAYING)
78+
return *this;
79+
if (m_randomized)
80+
defineBuffer(randomHandler());
4281
alSourcePlay(m_source);
4382
return (*this);
4483
}
@@ -97,4 +136,18 @@ sw::AudioSource &sw::AudioSource::setEndLoopPoint(float second)
97136
{
98137
m_endLoopPoint = second;
99138
return (*this);
139+
}
140+
141+
sw::AudioSource &sw::AudioSource::setRandomized(bool random)
142+
{
143+
m_randomized = random;
144+
if (!m_randomized)
145+
defineBuffer(m_audios[0]);
146+
return (*this);
147+
}
148+
149+
sw::AudioSource &sw::AudioSource::setMaxOccurence(int occurence)
150+
{
151+
m_maxOccurence = occurence;
152+
return (*this);
100153
}

0 commit comments

Comments
 (0)