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

Commit 94a71b5

Browse files
committed
Merge branch 'feature/audio' into develop
# Conflicts: # module/includes/components/AudioSource.hpp # module/includes/managers/AudioSourceManager.hpp # module/sources/components/AudioSource.cpp
2 parents 6ee8c78 + fe4a051 commit 94a71b5

7 files changed

Lines changed: 159 additions & 45 deletions

File tree

module/includes/components/AudioSource.hpp

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -19,44 +19,44 @@
1919

2020
namespace sw
2121
{
22-
2322
class AudioSourceManager;
2423

2524
class SW_GRAPH_MODULE_EXPORT AudioSource : public sw::Component
2625
{
2726
private:
2827
ALuint m_source;
29-
std::string m_audioFile;
30-
float m_volume;
31-
float m_pitch;
32-
bool m_playOnStart;
33-
YAML::Node save() const;
28+
bool m_loop;
29+
float m_currentSample;
30+
float m_startPoint;
31+
float m_startLoopPoint;
32+
float m_endPoint;
33+
float m_endLoopPoint;
34+
bool m_randomized;
35+
int m_maxOccurence;
36+
std::vector<std::string> m_audios;
37+
std::string m_last;
38+
int m_lastOccurence;
39+
40+
void defineBuffer(std::string name);
41+
std::string randomHandler();
3442

35-
void playOnStart();
3643
public:
3744
explicit AudioSource(sw::GameObject& entity);
3845
~AudioSource() override;
3946

40-
AudioSource& setAudio(std::string audio);
47+
AudioSource& addAudio(std::string audio);
4148
AudioSource& play();
4249
AudioSource& pause();
4350
AudioSource& stop();
4451
AudioSource& setVolume(float volume);
4552
AudioSource& setPitch(float pitch);
46-
////////////////////////////////////////////////////////////////////////////
47-
/// \brief Define if the Audio will play on start
48-
///
49-
/// \param value boolean
50-
/// \return reference to your Audio
51-
////////////////////////////////////////////////////////////////////////////
52-
AudioSource& setPlayOnStart(bool value);
53-
54-
////////////////////////////////////////////////////////////////////////////
55-
/// \brief Get if the animation will play on start
56-
///
57-
/// \return bool
58-
////////////////////////////////////////////////////////////////////////////
59-
[[nodiscard]]const bool& getPlayOnStart();
53+
AudioSource& setLoop(bool loop);
54+
AudioSource& setStartPoint(float second);
55+
AudioSource& setStartLoopPoint(float second);
56+
AudioSource& setEndPoint(float second);
57+
AudioSource& setEndLoopPoint(float second);
58+
AudioSource& setRandomized(bool random);
59+
AudioSource& setMaxOccurence(int occurence);
6060

6161
friend AudioSourceManager;
6262
};

module/includes/managers/AudioSourceManager.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ namespace sw
2424
YAML::Node save() const;
2525
void onUpdate() override {};
2626
void onLoad(YAML::Node& node) override;
27+
void onUpdate() override;
2728
}; // class AudioSourceManager
2829

2930
} // namespace sw

module/includes/resources/Audio.hpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,19 +22,21 @@ namespace sw
2222
class SW_GRAPH_MODULE_EXPORT Audio
2323
{
2424
private:
25-
SF_INFO m_fileInfo;
2625
SNDFILE *m_file;
26+
SF_INFO m_fileInfo;
2727
ALsizei m_numberSamples;
2828
ALsizei m_rate;
2929
std::vector<ALshort> m_samples;
3030
ALenum m_format;
3131
ALuint m_buffer;
32+
double m_duration;
3233

3334
public:
3435
explicit Audio(std::string path);
3536
~Audio();
3637

3738
[[nodiscard]] ALuint getBuffer() const;
39+
[[nodiscard]] double getDuration() const;
3840
};
3941
}
4042

module/includes/resources/OpenResources.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,14 +72,14 @@ namespace sw
7272
friend OpenResources;
7373
} m_ntext;
7474

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

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

module/sources/components/AudioSource.cpp

Lines changed: 95 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -14,42 +14,72 @@
1414
sw::AudioSource::AudioSource(sw::GameObject &gameObject) :
1515
sw::Component(gameObject),
1616
m_source(-1),
17-
m_audioFile(),
18-
m_volume(),
19-
m_pitch(),
20-
m_playOnStart(true)
17+
m_loop(false),
18+
m_currentSample(0.0f),
19+
m_startPoint(0.0f),
20+
m_startLoopPoint(-1),
21+
m_endLoopPoint(-1),
22+
m_endPoint(-1),
23+
m_randomized(false),
24+
m_maxOccurence(-1),
25+
m_audios(),
26+
m_last(),
27+
m_lastOccurence(0)
2128
{
2229
alGenSources(1, &m_source);
2330
gameObject.scene().eventManager["Start"].subscribe(this, &AudioSource::playOnStart);
2431
}
2532

26-
void sw::AudioSource::playOnStart()
27-
{
28-
if (m_playOnStart)
29-
play();
30-
}
31-
3233
sw::AudioSource::~AudioSource() noexcept
3334
{
3435
alSourcei(m_source, AL_BUFFER, 0);
3536
alDeleteSources(1, &m_source);
3637
}
3738

38-
sw::AudioSource &sw::AudioSource::setAudio(std::string audio)
39+
void sw::AudioSource::defineBuffer(std::string name)
40+
{
41+
auto buffer = sw::OpenResources::m_naudio[name];
42+
alSourcei(m_source, AL_BUFFER, buffer->getBuffer());
43+
m_endPoint = buffer->getDuration();
44+
}
45+
46+
std::string sw::AudioSource::randomHandler()
47+
{
48+
int index;
49+
std::string audioName;
50+
51+
if (m_audios.size() < 2)
52+
return m_audios[0];
53+
do {
54+
index = std::rand() % (m_audios.size() - 1);
55+
audioName = m_audios[index];
56+
} while (m_last == audioName && m_lastOccurence == m_maxOccurence);
57+
if (m_last == audioName)
58+
m_lastOccurence++;
59+
else {
60+
m_lastOccurence = 1;
61+
m_last = audioName;
62+
}
63+
return audioName;
64+
}
65+
66+
sw::AudioSource &sw::AudioSource::addAudio(std::string audio)
3967
{
40-
alSourcei(m_source, AL_BUFFER, sw::OpenResources::m_naudio[audio]->getBuffer());
41-
m_audioFile = audio;
42-
alGetSourcef(m_source, AL_GAIN, &m_volume);
43-
alGetSourcef(m_source, AL_PITCH, &m_pitch);
68+
m_audios.emplace_back(audio);
69+
if (m_audios.empty())
70+
defineBuffer(m_audios[0]);
4471
return (*this);
4572
}
4673

4774
sw::AudioSource &sw::AudioSource::play()
4875
{
49-
if (m_source == -1) {
50-
sw::Speech::Warning("No sound defined cannot play");
51-
return (*this);
52-
}
76+
int value;
77+
78+
alGetSourcei(m_source, AL_SOURCE_STATE, &value);
79+
if (value == AL_PLAYING)
80+
return *this;
81+
if (m_randomized)
82+
defineBuffer(randomHandler());
5383
alSourcePlay(m_source);
5484
return (*this);
5585
}
@@ -63,6 +93,7 @@ sw::AudioSource &sw::AudioSource::pause()
6393
sw::AudioSource &sw::AudioSource::stop()
6494
{
6595
alSourceStop(m_source);
96+
setStartLoopPoint(m_startPoint);
6697
return (*this);
6798
}
6899

@@ -80,6 +111,51 @@ sw::AudioSource &sw::AudioSource::setPitch(float pitch)
80111
return (*this);
81112
}
82113

114+
sw::AudioSource &sw::AudioSource::setLoop(bool loop)
115+
{
116+
m_loop = loop;
117+
alSourcei(m_source, AL_LOOPING, m_loop ? AL_TRUE : AL_FALSE);
118+
return (*this);
119+
}
120+
121+
sw::AudioSource &sw::AudioSource::setStartPoint(float second)
122+
{
123+
alSourcef(m_source, AL_SEC_OFFSET, second);
124+
return (*this);
125+
}
126+
127+
sw::AudioSource &sw::AudioSource::setStartLoopPoint(float second)
128+
{
129+
m_startLoopPoint = second;
130+
return (*this);
131+
}
132+
133+
sw::AudioSource &sw::AudioSource::setEndPoint(float second)
134+
{
135+
m_endPoint = second;
136+
return (*this);
137+
}
138+
139+
sw::AudioSource &sw::AudioSource::setEndLoopPoint(float second)
140+
{
141+
m_endLoopPoint = second;
142+
return (*this);
143+
}
144+
145+
sw::AudioSource &sw::AudioSource::setRandomized(bool random)
146+
{
147+
m_randomized = random;
148+
if (!m_randomized)
149+
defineBuffer(m_audios[0]);
150+
return (*this);
151+
}
152+
153+
sw::AudioSource &sw::AudioSource::setMaxOccurence(int occurence)
154+
{
155+
m_maxOccurence = occurence;
156+
return (*this);
157+
}
158+
83159
YAML::Node sw::AudioSource::save() const
84160
{
85161
YAML::Node node;
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/*
2+
** Society: Creative Rift
3+
** SHIPWRECK ENGINE, 2022
4+
** Author: Guillaume S.
5+
** File name: AudioSourceManager.hpp
6+
** Description: [CHANGE]
7+
*/
8+
9+
#include "AudioSourceManager.hpp"
10+
11+
void sw::AudioSourceManager::onUpdate()
12+
{
13+
for (auto& [_, audioSource] : m_components) {
14+
alGetSourcef(audioSource->m_source, AL_SEC_OFFSET, &audioSource->m_currentSample);
15+
if (audioSource->m_endLoopPoint != -1 && audioSource->m_endLoopPoint <= audioSource->m_currentSample) {
16+
audioSource->stop();
17+
alSourcef(audioSource->m_source, AL_SEC_OFFSET, audioSource->m_startLoopPoint);
18+
audioSource->play();
19+
}
20+
if (audioSource->m_endPoint != -1 && audioSource->m_endPoint <= audioSource->m_currentSample)
21+
audioSource->stop();
22+
}
23+
}

module/sources/resources/Audio.cpp

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,21 @@
1313

1414
sw::Audio::Audio(std::string path) :
1515
m_file(nullptr),
16-
m_fileInfo()
16+
m_fileInfo(),
17+
m_numberSamples(0),
18+
m_rate(0),
19+
m_samples(),
20+
m_format(AL_NONE),
21+
m_buffer(0),
22+
m_duration(0)
1723
{
1824
m_file = sf_open(path.c_str(), SFM_READ, &m_fileInfo);
1925
if (!m_file)
2026
throw sw::Error("Cannot open file: " + path, "");
2127
m_numberSamples = (ALsizei)(m_fileInfo.channels * m_fileInfo.frames);
2228
m_rate = (ALsizei)m_fileInfo.samplerate;
2329
m_samples.resize(m_numberSamples);
30+
m_duration = (double)m_fileInfo.frames / m_fileInfo.samplerate;
2431
sf_read_short(m_file, &m_samples[0], m_numberSamples);
2532
sf_close(m_file);
2633

@@ -45,4 +52,9 @@ sw::Audio::~Audio() noexcept
4552
ALuint sw::Audio::getBuffer() const
4653
{
4754
return (m_buffer);
55+
}
56+
57+
double sw::Audio::getDuration() const
58+
{
59+
return (m_duration);
4860
}

0 commit comments

Comments
 (0)