1414sw::AudioSource::AudioSource (sw::GameObject &gameObject) :
1515sw::Component(gameObject),
1616m_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-
3233sw::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
4774sw::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()
6393sw::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+
83159YAML::Node sw::AudioSource::save () const
84160{
85161 YAML::Node node;
0 commit comments