1 /******************************************************************************* 2 * Copyright 2015-2016 Juan Francisco Crespo Galán 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 ******************************************************************************/ 16 17 #include "fx/SoundList.h" 18 #include "Exception.h" 19 20 #include <cstring> 21 #include <cstdlib> 22 #include <chrono> 23 24 AUD_NAMESPACE_BEGIN 25 SoundList(bool random)26SoundList::SoundList(bool random) : 27 m_random(random) 28 { 29 std::srand(time(NULL)); 30 } 31 SoundList(std::vector<std::shared_ptr<ISound>> & list,bool random)32SoundList::SoundList(std::vector<std::shared_ptr<ISound>>& list, bool random) : 33 m_list(list), m_random(random) 34 { 35 std::srand(time(NULL)); 36 } 37 createReader()38std::shared_ptr<IReader> SoundList::createReader() 39 { 40 if(m_list.size() > 0) 41 { 42 m_mutex.lock(); 43 44 if(!m_random){ 45 m_index++; 46 if(m_index >= m_list.size()) 47 m_index = 0; 48 } 49 else 50 { 51 int temp; 52 do{ 53 temp = std::rand() % m_list.size(); 54 } while(temp == m_index && m_list.size()>1); 55 m_index = temp; 56 } 57 auto reader = m_list[m_index]->createReader(); 58 m_mutex.unlock(); 59 return reader; 60 } 61 else 62 AUD_THROW(FileException, "The sound list is empty"); 63 } 64 addSound(std::shared_ptr<ISound> sound)65void SoundList::addSound(std::shared_ptr<ISound> sound) 66 { 67 m_list.push_back(sound); 68 } 69 setRandomMode(bool random)70void SoundList::setRandomMode(bool random) 71 { 72 m_random = random; 73 } 74 getRandomMode()75bool SoundList::getRandomMode() 76 { 77 return m_random; 78 } 79 getSize()80int SoundList::getSize() 81 { 82 return m_list.size(); 83 } 84 AUD_NAMESPACE_END