diff --git a/ewolsa/LoadedFile.cpp b/ewolsa/LoadedFile.cpp index 139b0b3..78658b4 100644 --- a/ewolsa/LoadedFile.cpp +++ b/ewolsa/LoadedFile.cpp @@ -13,31 +13,47 @@ #include #include +static void* threadCallback2(void *_ptr) { + ewolsa::LoadedFile* decodeFile = (ewolsa::LoadedFile*)_ptr; + decodeFile->decode(); + return NULL; +} + +void ewolsa::LoadedFile::decode(void) { + m_data = ewolsa::ogg::loadAudioFile(m_file, m_nbChanRequested, m_nbSamples); +} ewolsa::LoadedFile::LoadedFile(const std::string& _fileName, int8_t _nbChanRequested) : m_file(_fileName), m_nbSamples(0), - m_nbSamplesTotal(0), m_nbChanRequested(_nbChanRequested), m_requestedTime(1), - m_data(NULL) { - m_stopRequested = false; + m_data(NULL){ + m_thread = NULL; std::string tmpName = std::tolower(m_file); // select the corect Loader : if (end_with(tmpName, ".wav") == true) { m_data = ewolsa::wav::loadAudioFile(m_file, m_nbChanRequested, m_nbSamples); - m_nbSamplesTotal = m_nbSamples; } else if (end_with(tmpName, ".ogg") == true) { - pthread_create(&m_thread2, NULL, &ewolsa::ogg::loadFileThreadedMode, this); + EWOLSA_DEBUG("create thread"); + pthread_create(&m_thread2, NULL, &threadCallback2, this); + EWOLSA_DEBUG("done 1"); + std::this_thread::sleep_for(std::chrono::milliseconds(1000)); + EWOLSA_DEBUG("done 2"); } else { EWOLSA_ERROR("Extention not managed '" << m_file << "' Sopported extention : .wav / .ogg"); return; } + /* + if (m_data == NULL) { + // write an error ... + EWOLSA_ERROR("Can not open file : " << _fileName); + } + */ } ewolsa::LoadedFile::~LoadedFile(void) { - m_stopRequested = true; // TODO : wait end of thread... if (m_data != NULL) { delete[] m_data; diff --git a/ewolsa/LoadedFile.h b/ewolsa/LoadedFile.h index 4082a72..a5f1190 100644 --- a/ewolsa/LoadedFile.h +++ b/ewolsa/LoadedFile.h @@ -11,20 +11,19 @@ #define __EWOLSA_LOADED_FILE_H__ #include +#include #include namespace ewolsa { class LoadedFile { private: + std::thread* m_thread; pthread_t m_thread2; - public: - bool m_stopRequested; public: LoadedFile(const std::string& _fileName, int8_t _nbChanRequested=1); ~LoadedFile(void); std::string m_file; int32_t m_nbSamples; - int32_t m_nbSamplesTotal; int32_t m_nbChanRequested; int32_t m_requestedTime; int16_t* m_data; @@ -32,6 +31,7 @@ namespace ewolsa { const std::string& getName(void) { return m_file; }; + void decode(void); }; }; diff --git a/ewolsa/decOgg.cpp b/ewolsa/decOgg.cpp index 3f4ec53..48c8224 100644 --- a/ewolsa/decOgg.cpp +++ b/ewolsa/decOgg.cpp @@ -12,12 +12,10 @@ #include #include #include -#include static size_t LocalReadFunc(void *ptr, size_t size, size_t nmemb, void *datasource) { etk::FSNode* file = static_cast(datasource); - EWOLSA_DEBUG("read : " << size << " " << nmemb); return file->fileRead(ptr, size, nmemb); } @@ -146,109 +144,3 @@ int16_t* ewolsa::ogg::loadAudioFile(const std::string& _filename, int8_t _nbChan return outputData; } -void* ewolsa::ogg::loadFileThreadedMode(void *_ptr) { - ewolsa::LoadedFile* self = (ewolsa::LoadedFile*)_ptr; - if (self == NULL) { - EWOLSA_ERROR("NULL handle"); - return NULL; - } - self->m_data = NULL; - self->m_nbSamples = 0; - self->m_nbSamplesTotal = 0; - OggVorbis_File vf; - int32_t current_section; - ov_callbacks tmpCallback = { - LocalReadFunc, - localSeekFunc, - localCloseFunc, - localTellFunc - }; - EWOLSA_WARNING("open file (OGG) '" << self->m_file << "'"); - etk::FSNode fileAccess(self->m_file); - // Start loading the XML : - if (false == fileAccess.exist()) { - EWOLSA_ERROR("File Does not exist : '" << fileAccess << "'"); - return NULL; - } - int32_t fileSize = fileAccess.fileSize(); - if (0 == fileSize) { - EWOLSA_ERROR("This file is empty : '" << fileAccess << "'"); - return NULL; - } - EWOLSA_WARNING("ogg file size = " << fileSize); - if (false == fileAccess.fileOpenRead()) { - EWOLSA_ERROR("Can not open the file : '" << fileAccess << "'"); - return NULL; - } - if (ov_open_callbacks(&fileAccess, &vf, NULL, 0, tmpCallback) < 0) { - EWOLSA_ERROR("Input does not appear to be an Ogg bitstream."); - return NULL; - } - vorbis_info *vi=ov_info(&vf,-1); - self->m_nbSamplesTotal = ov_pcm_total(&vf,-1) / vi->channels; - { - char **ptr=ov_comment(&vf,-1)->user_comments; - while(*ptr){ - EWOLSA_DEBUG("comment : " << *ptr); - ++ptr; - } - EWOLSA_DEBUG("Bitstream is " << (int32_t)vi->channels << " channel, " << (int32_t)vi->rate << "Hz"); - EWOLSA_DEBUG("Decoded length: " << self->m_nbSamplesTotal << " samples"); - EWOLSA_DEBUG("Encoded by: " << ov_comment(&vf,-1)->vendor); - EWOLSA_DEBUG("time: " << ((float)self->m_nbSamplesTotal/(float)vi->rate)/60.0); - } - self->m_data = new int16_t[self->m_nbSamplesTotal*self->m_nbChanRequested*sizeof(int16_t)]; - if (NULL == self->m_data) { - EWOLSA_ERROR("Allocation ERROR try to allocate " << (int32_t)(self->m_nbSamplesTotal*self->m_nbChanRequested*sizeof(int16_t) ) << "bytes"); - return NULL; - } - - self->m_nbSamples = 0; - char pcmout[4096]; - bool eof = false; - while ( eof == false - && self->m_stopRequested == false) { - long ret=ov_read(&vf, pcmout, sizeof(pcmout), ¤t_section); - if (ret == 0) { - /* EOF */ - eof = true; - } else if (ret < 0) { - if(ret==OV_EBADLINK){ - EWOLSA_ERROR("Corrupt bitstream section! Exiting."); - // TODO : Remove pointer data ... - return NULL; - } - } else { - int16_t* pointerIn = (int16_t*)pcmout; - int16_t* pointerOut = self->m_data+self->m_nbSamples; - if (self->m_nbChanRequested == vi->channels) { - memcpy(pointerOut, pointerIn, ret); - self->m_nbSamples += ret/2; - } else { - if ( self->m_nbChanRequested == 1 - && vi->channels == 2) { - for (int32_t iii=0; iiim_nbSamples += ret/4; - } else if ( self->m_nbChanRequested == 2 - && vi->channels == 1) { - for (int32_t iii=0; iiim_nbSamples += ret; - } else { - EWOLSA_ERROR("Can not convert " << (int32_t)vi->channels << " channels in " << self->m_nbChanRequested << " channels"); - } - } - // fwrite(pcmout,1,ret,stdout); - EWOLSA_DEBUG("get data : " << (int32_t)ret << " Bytes"); - } - } - /* cleanup */ - ov_clear(&vf); - EWOLSA_DEBUG("Done."); - - return NULL; -} diff --git a/ewolsa/decOgg.h b/ewolsa/decOgg.h index c9effec..074b0a6 100644 --- a/ewolsa/decOgg.h +++ b/ewolsa/decOgg.h @@ -14,7 +14,7 @@ namespace ewolsa { namespace ogg { int16_t* loadAudioFile(const std::string& _filename, int8_t _nbChan, int32_t& _nbSampleOut); - void* loadFileThreadedMode(void *_ptr); + } };