diff --git a/ewolsa/LoadedFile.cpp b/ewolsa/LoadedFile.cpp new file mode 100644 index 0000000..9f7e772 --- /dev/null +++ b/ewolsa/LoadedFile.cpp @@ -0,0 +1,36 @@ +/** + * @author Edouard DUPIN + * + * @copyright 2011, Edouard DUPIN, all right reserved + * + * @license BSD 3 clauses (see license file) + */ + + +#include +#include +#include +#include + + +ewolsa::LoadedFile::LoadedFile(const std::string& _file, int8_t _nbChanRequested) : + m_file(_file), + m_nbSamples(0), + m_requestedTime(1), + m_data(NULL){ + m_data = ewolsa::loadAudioFile(_file, _nbChanRequested, m_nbSamples); + if (m_data == NULL) { + // write an error ... + EWOLSA_ERROR("Can not open file : " << _file); + } +} + + +ewolsa::LoadedFile::~LoadedFile(void) { + if (m_data != NULL) { + delete[] m_data; + m_data = NULL; + } +} + + diff --git a/ewolsa/LoadedFile.h b/ewolsa/LoadedFile.h new file mode 100644 index 0000000..a0e30e9 --- /dev/null +++ b/ewolsa/LoadedFile.h @@ -0,0 +1,32 @@ +/** + * @author Edouard DUPIN + * + * @copyright 2011, Edouard DUPIN, all right reserved + * + * @license BSD 3 clauses (see license file) + */ + + +#ifndef __EWOLSA_LOADED_FILE_H__ +#define __EWOLSA_LOADED_FILE_H__ + +#include + +namespace ewolsa { + class LoadedFile { + public : + LoadedFile(const std::string& _file, int8_t _nbChanRequested=1); + ~LoadedFile(void); + std::string m_file; + int32_t m_nbSamples; + int32_t m_requestedTime; + int16_t* m_data; + public: + const std::string& getName(void) { + return m_file; + }; + }; +}; + +#endif + diff --git a/ewolsa/decWav.cpp b/ewolsa/decWav.cpp index b010605..ac925de 100644 --- a/ewolsa/decWav.cpp +++ b/ewolsa/decWav.cpp @@ -218,7 +218,7 @@ int16_t* ewolsa::loadAudioFile(const std::string& _filename, int8_t _nbChan, int //int32_t globalDataSize = myHeader.dataSize; int32_t nbSample = (myHeader.dataSize/((myHeader.waveFormat.bitsPerSample/8)*myHeader.waveFormat.channelCount)); int32_t outputSize = _nbChan*nbSample; - int16_t * outputData = (int16_t*)malloc(outputSize*sizeof(int16_t)); + int16_t * outputData = new int16_t[outputSize*sizeof(int16_t)]; if (NULL == outputData) { EWOLSA_ERROR("Allocation ERROR try to allocate " << (int32_t)(outputSize*sizeof(int16_t) ) << "bytes"); return NULL; @@ -281,7 +281,7 @@ int16_t* ewolsa::loadAudioFile(const std::string& _filename, int8_t _nbChan, int *tmpOut++ = (int16_t)(((left>>1) + (right>>1))>>16); } else { *tmpOut++ = (int16_t)(left>>16); - *tmpOut++ = (int16_t)(left>>16); + *tmpOut++ = (int16_t)(right>>16); } } // close the file: diff --git a/ewolsa/effects.cpp b/ewolsa/effects.cpp index 6c39661..13456ce 100644 --- a/ewolsa/effects.cpp +++ b/ewolsa/effects.cpp @@ -13,7 +13,10 @@ #include #include #include +#include +#include +static std::mutex localMutex; static bool effectsMute = false; static float effectsVolume = 0; static int32_t effectsVolumeApply = 1<<16; @@ -22,36 +25,20 @@ static int32_t effectsVolumeApply = 1<<16; //---------------------------------------------------------------------------------------------------------- // Effects ... //---------------------------------------------------------------------------------------------------------- -//liste d'effet -class EffectsLoaded { - public : - EffectsLoaded(const std::string& _file) { - m_file = _file; - m_requestedTime = 1; - m_data = ewolsa::loadAudioFile(_file, 1, m_nbSamples); - if (m_data == NULL) { - // write an error ... - } - } - std::string m_file; - int32_t m_nbSamples; - int32_t m_requestedTime; - int16_t* m_data; -}; class RequestPlay { private: bool m_freeSlot; - EffectsLoaded* m_effect; // reference to the effects + ewolsa::LoadedFile* m_effect; // reference to the effects int32_t m_playTime; // position in sample playing in the audio effects public : - RequestPlay(EffectsLoaded * _effect) : + RequestPlay(ewolsa::LoadedFile * _effect) : m_freeSlot(false), m_effect(_effect), m_playTime(0) { }; - void reset(EffectsLoaded * _effect) { + void reset(ewolsa::LoadedFile * _effect) { m_effect=_effect; m_playTime=0; m_freeSlot=false; @@ -91,7 +78,7 @@ class RequestPlay { }; #include -std::vector ListEffects; +std::vector ListEffects; std::vector ListEffectsPlaying; void ewolsa::effects::init(void) { @@ -115,7 +102,7 @@ int32_t ewolsa::effects::add(const std::string& _file) { } } // effect does not exist ... create a new one ... - EffectsLoaded * tmpEffect = new EffectsLoaded(_file); + ewolsa::LoadedFile * tmpEffect = new ewolsa::LoadedFile(_file); if (NULL == tmpEffect) { EWOLSA_ERROR("Error to load the effects : \"" << _file << "\""); return -1; @@ -203,6 +190,7 @@ bool ewolsa::effects::muteGet(void) { void ewolsa::effects::muteSet(bool _newMute) { effectsMute = _newMute; EWOLSA_INFO("Set effects Mute at " << _newMute); + uptateEffectVolume(); } diff --git a/ewolsa/music.cpp b/ewolsa/music.cpp index 8153e54..f268e6c 100644 --- a/ewolsa/music.cpp +++ b/ewolsa/music.cpp @@ -11,25 +11,53 @@ #include #include +#include #include +#include +static std::mutex localMutex; static bool musicMute = false; static float musicVolume = 0; static int32_t musicVolumeApply = 1<<16; static int32_t musicFadingTime = 0; - - - +static int32_t musicPositionReading = 0; +static std::vector musicListRead; +static int32_t musicCurrentRead = -1; +static int32_t musicNextRead = -1; void ewolsa::music::init(void) { ewolsa::music::volumeSet(0); ewolsa::music::muteSet(false); + std::unique_lock lck(localMutex); + musicCurrentRead = -1; + musicNextRead = -1; + musicPositionReading = 0; + for (size_t iii=0; iii lck(localMutex); + musicCurrentRead = -1; + musicNextRead = -1; + musicPositionReading = 0; + for (size_t iii=0; iiigetName() == _file) { + return; + } + } + ewolsa::LoadedFile* tmp = new ewolsa::LoadedFile(_file, 2); + if (tmp != NULL) { + if (tmp->m_data == NULL) { + EWOLSA_ERROR("Music has no data ... : " << _file); + delete(tmp); + return; + } + std::unique_lock lck(localMutex); + musicListRead.push_back(tmp); + } else { + EWOLSA_ERROR("can not preload audio Music"); + } } -bool ewolsa::music::listRm(std::string _file) { - return false; -} - - -bool ewolsa::music::listClean(void) { - return false; -} - - -bool ewolsa::music::listPrevious(void) { - return false; -} - - -bool ewolsa::music::listNext(void) { - return false; -} - - -bool ewolsa::music::listFirst(void) { - return false; -} - - -bool ewolsa::music::listLast(void) { - return false; -} - - - -bool ewolsa::music::listPlay(void) { - return false; -} - - -bool ewolsa::music::listStop(void) { - return false; -} - - - - -bool ewolsa::music::play(std::string _file) { - return false; +bool ewolsa::music::play(const std::string& _file) { + preLoad(_file); + // in all case we stop the current playing music ... + stop(); + int32_t idMusic = -1; + // check if music already existed ... + for (size_t iii=0; iiigetName() == _file) { + idMusic = iii; + break; + } + } + if (idMusic == -1) { + return false; + } + std::unique_lock lck(localMutex); + musicNextRead = idMusic; + EWOLSA_INFO("Playing track " << musicCurrentRead << " request next : " << idMusic); + return true; } bool ewolsa::music::stop(void) { - return false; + if (musicCurrentRead == -1) { + EWOLSA_INFO("No current audio is playing"); + return false; + } + std::unique_lock lck(localMutex); + musicNextRead = -1; + return true; } @@ -111,6 +142,7 @@ static void uptateMusicVolume(void) { // convert in an fixpoint value // V2 = V1*10^(db/20) double coef = pow(10, (musicVolume/20) ); + std::unique_lock lck(localMutex); musicVolumeApply = (int32_t)(coef * (double)(1<<16)); } } @@ -136,18 +168,42 @@ void ewolsa::music::muteSet(bool _newMute) { void ewolsa::music::getData(int16_t * _bufferInterlace, int32_t _nbSample, int32_t _nbChannels) { - /*static int32_t maxValue = 0; - static float angle = 0; - maxValue +=10; - if (maxValue > 16000) { - maxValue = 0; + EWOLSA_VERBOSE("Music !!! " << musicCurrentRead << " ... " << musicNextRead << " pos: " << musicPositionReading); + std::unique_lock lck(localMutex); + if (musicCurrentRead != musicNextRead) { + EWOLSA_DEBUG("change track " << musicCurrentRead << " ==> " << musicNextRead); + // TODO : create fading .... + musicCurrentRead = musicNextRead; + musicPositionReading = 0; } - for (int iii = 0; iii= 360) { - angle -= 360.0; - } - }*/ + if (musicCurrentRead < 0) { + // nothing to play ... + return; + } + if ( musicCurrentRead >= musicListRead.size() + || musicListRead[musicCurrentRead] == NULL) { + musicCurrentRead = -1; + musicPositionReading = 0; + EWOLSA_ERROR("request read an unexisting audio track ... : " << musicCurrentRead << "/" << musicListRead.size()); + return; + } + int32_t processTimeMax = etk_min(_nbSample*_nbChannels, musicListRead[musicCurrentRead]->m_nbSamples - musicPositionReading); + processTimeMax = etk_max(0, processTimeMax); + int16_t * pointer = _bufferInterlace; + int16_t * newData = &musicListRead[musicCurrentRead]->m_data[musicPositionReading]; + //EWOLSA_DEBUG("AUDIO : Play slot... nb sample : " << processTimeMax << " playTime=" <m_nbSamples <= musicPositionReading) { + musicPositionReading = 0; + musicCurrentRead = -1; + } + } diff --git a/ewolsa/music.h b/ewolsa/music.h index 9462111..094fd3a 100644 --- a/ewolsa/music.h +++ b/ewolsa/music.h @@ -17,18 +17,9 @@ namespace ewolsa { void init(void); void unInit(void); void fading(int32_t _timeMs); - // list playing system : is cyclic ... - bool listAdd(std::string _file); - bool listRm(std::string _file); - bool listClean(void); - bool listPrevious(void); - bool listNext(void); - bool listFirst(void); - bool listLast(void); - bool listPlay(void); // List playing - bool listStop(void); // List stopping - bool play(std::string _file); // play specific file ... pause the list element; + void preLoad(const std::string& _file); + bool play(const std::string& _file); bool stop(void); // in db diff --git a/lutin_ewolsa.py b/lutin_ewolsa.py index 6ab82e7..26977b5 100644 --- a/lutin_ewolsa.py +++ b/lutin_ewolsa.py @@ -15,7 +15,8 @@ def create(target): 'ewolsa/decWav.cpp', 'ewolsa/effects.cpp', 'ewolsa/ewolsa.cpp', - 'ewolsa/music.cpp' + 'ewolsa/music.cpp', + 'ewolsa/LoadedFile.cpp' ]) # name of the dependency