@@ -8,7 +8,7 @@ ALCdevice* AudioHelper::device = nullptr;
88ALCcontext* AudioHelper::context = nullptr ;
99
1010std::map<std::string, XCWavFile> AudioHelper::wavSourceGroup;
11- std::map<ALuint, std::atomic_bool > AudioHelper::playingGroup ;
11+ std::map<ALuint, ALuint > AudioHelper::effectPlayingList ;
1212void AudioHelper::EvonInit ()
1313{
1414 if (!isEvonInit) {
@@ -34,6 +34,17 @@ void AudioHelper::EvonInit()
3434
3535void AudioHelper::UnloadEvon ()
3636{
37+
38+ for (auto work = effectPlayingList.begin (); work != effectPlayingList.end (); work++) {
39+ ALint state;
40+ alGetSourcei (work->second , AL_SOURCE_STATE, &state);
41+ if (state == AL_PLAYING) {
42+ alSourceStop (work->second );
43+ }
44+ alDeleteBuffers (1 , &work->first );
45+ alDeleteSources (1 , &work->second );
46+ }
47+
3748 alcMakeContextCurrent (NULL );
3849 alcDestroyContext (context);
3950 alcCloseDevice (device);
@@ -65,43 +76,56 @@ XCWavFile AudioHelper::loadWavFromFile(const std::string filename)
6576 return wavFile;
6677}
6778
68- void AudioHelper::playerWavFile (XCWavFile file)
79+ void AudioHelper::setVolume (float volume)
80+ {
81+ audioVolume = volume;
82+ }
83+
84+ void AudioHelper::playFromBuffer (ALuint buffer)
6985{
70- auto target = playingGroup .find (file. wavSource );
71- if (target != playingGroup .end ()) {
72- bool isPlaying = target-> second ;
73- if (!isPlaying) {
74- std::thread playThread (threadWork,file);
75- playThread. detach ( );
86+ auto work = effectPlayingList .find (buffer );
87+ if (work!=effectPlayingList .end ()) {
88+ ALint state ;
89+ alGetSourcei (work-> second , AL_SOURCE_STATE, &state);
90+ if (state!=AL_PLAYING) {
91+ alSourcePlay (work-> second );
7692 }
7793 }
7894 else {
79- playingGroup[file.wavSource ] = true ;
80- std::thread playThread (threadWork, file);
81- playThread.detach ();
95+ ALuint source;
96+ alGenSources (1 , &source);
97+ alSourcei (source, AL_BUFFER, buffer);
98+ alSourcef (source, AL_GAIN, audioVolume);
99+ alSourcePlay (source);
100+
101+ effectPlayingList.insert (std::make_pair (buffer, source));
82102 }
83103}
84104
85- void AudioHelper::threadWork (XCWavFile file )
105+ void AudioHelper::stopByBuffer (ALuint buffer )
86106{
87- ALuint source;
88- alGenSources (1 , &source);
89- alSourcei (source, AL_BUFFER, file.wavSource );
90- alSourcef (source, AL_GAIN, audioVolume);
91- alSourcePlay (source);
92-
93- ALint state;
94- do {
95- alGetSourcei (source, AL_SOURCE_STATE, &state);
96- } while (state == AL_PLAYING);
97- alDeleteSources (1 , &source);
98- playingGroup[file.wavSource ] = false ;
107+ auto work = effectPlayingList.find (buffer);
108+ if (work != effectPlayingList.end ()) {
109+ ALint state;
110+ alGetSourcei (work->second , AL_SOURCE_STATE, &state);
111+ if (state == AL_PLAYING) {
112+ alSourceStop (work->second );
113+ }
114+ }
99115}
100116
101-
102- void AudioHelper::setVolume (float volume)
117+ void AudioHelper::releaseByBuffer (ALuint buffer)
103118{
104- audioVolume = volume;
119+ auto work = effectPlayingList.find (buffer);
120+ if (work != effectPlayingList.end ()) {
121+ ALint state;
122+ alGetSourcei (work->second , AL_SOURCE_STATE, &state);
123+ if (state == AL_PLAYING) {
124+ alSourceStop (work->second );
125+ }
126+ alDeleteSources (1 , &work->second );
127+ effectPlayingList.erase (work);
128+ }
105129}
106130
107131bool AudioHelper::loadWavFile (const std::string filename, XCWavFile *wavFile) {
@@ -180,11 +204,11 @@ bool AudioHelper::loadWavFile(const std::string filename, XCWavFile *wavFile) {
180204 wavFile->wavFormat = AL_FORMAT_STEREO16;
181205 }
182206 // create our openAL buffer and check for success
183- alGenBuffers (1 , &(wavFile->wavSource ));
207+ alGenBuffers (1 , &(wavFile->wavBuffer ));
184208 // errorCheck();
185209 // now we put our data into the openAL buffer and
186210 // check for success
187- alBufferData (wavFile->wavSource , wavFile->wavFormat , (void *)data,
211+ alBufferData (wavFile->wavBuffer , wavFile->wavFormat , (void *)data,
188212 wavFile->wavSize , wavFile->wavFrequent );
189213 // errorCheck();
190214 // clean up and return true if successful
0 commit comments