From 143d319c9175d38b5cf009bb228361b67023e452 Mon Sep 17 00:00:00 2001 From: Edouard DUPIN Date: Thu, 3 Apr 2014 12:01:23 +0200 Subject: [PATCH] [DEV] might work, but not ... --- ewolsa/LoadedFile.cpp | 30 +++++------------ ewolsa/LoadedFile.h | 6 ++-- ewolsa/decOgg.cpp | 76 ++++++++++++++++++++++++------------------- ewolsa/decOgg.h | 3 +- 4 files changed, 55 insertions(+), 60 deletions(-) diff --git a/ewolsa/LoadedFile.cpp b/ewolsa/LoadedFile.cpp index 78658b4..71821c0 100644 --- a/ewolsa/LoadedFile.cpp +++ b/ewolsa/LoadedFile.cpp @@ -12,48 +12,34 @@ #include #include #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_thread = NULL; + m_data(NULL) { + m_stopRequested = false; 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); - } else if (end_with(tmpName, ".ogg") == true) { - 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"); + m_nbSamplesTotal = m_nbSamples; + } else if (end_with(tmpName, ".ogg") == true) { + pthread_create(&m_thread2, NULL, &ewolsa::ogg::loadFileThreadedMode, this); } 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 a5f1190..4082a72 100644 --- a/ewolsa/LoadedFile.h +++ b/ewolsa/LoadedFile.h @@ -11,19 +11,20 @@ #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; @@ -31,7 +32,6 @@ namespace ewolsa { const std::string& getName(void) { return m_file; }; - void decode(void); }; }; diff --git a/ewolsa/decOgg.cpp b/ewolsa/decOgg.cpp index 48c8224..ecc353e 100644 --- a/ewolsa/decOgg.cpp +++ b/ewolsa/decOgg.cpp @@ -12,10 +12,12 @@ #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); } @@ -46,10 +48,16 @@ static long localTellFunc(void *datasource) { return file->fileTell(); } -int16_t* ewolsa::ogg::loadAudioFile(const std::string& _filename, int8_t _nbChan, int32_t& _nbSampleOut) { - _nbSampleOut = 0; +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 eof=0; int32_t current_section; ov_callbacks tmpCallback = { LocalReadFunc, @@ -57,29 +65,29 @@ int16_t* ewolsa::ogg::loadAudioFile(const std::string& _filename, int8_t _nbChan localCloseFunc, localTellFunc }; - etk::FSNode fileAccess(_filename); + EWOLSA_WARNING("open file (OGG) '" << self->m_file << "'"); + etk::FSNode fileAccess(self->m_file); // Start loading the XML : - //EWOLSA_DEBUG("open file (OGG) \"" << fileAccess << "\""); if (false == fileAccess.exist()) { - //EWOLSA_ERROR("File Does not exist : \"" << fileAccess << "\""); + EWOLSA_ERROR("File Does not exist : '" << fileAccess << "'"); return NULL; } int32_t fileSize = fileAccess.fileSize(); if (0 == fileSize) { - //EWOLSA_ERROR("This file is empty : \"" << fileAccess << "\""); + 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 << "\""); + 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."); + EWOLSA_ERROR("Input does not appear to be an Ogg bitstream."); return NULL; } vorbis_info *vi=ov_info(&vf,-1); - _nbSampleOut = ov_pcm_total(&vf,-1) / vi->channels; - /* + self->m_nbSamplesTotal = ov_pcm_total(&vf,-1) / vi->channels; { char **ptr=ov_comment(&vf,-1)->user_comments; while(*ptr){ @@ -87,60 +95,62 @@ int16_t* ewolsa::ogg::loadAudioFile(const std::string& _filename, int8_t _nbChan ++ptr; } EWOLSA_DEBUG("Bitstream is " << (int32_t)vi->channels << " channel, " << (int32_t)vi->rate << "Hz"); - EWOLSA_DEBUG("Decoded length: " << _nbSampleOut << " samples"); + EWOLSA_DEBUG("Decoded length: " << self->m_nbSamplesTotal << " samples"); EWOLSA_DEBUG("Encoded by: " << ov_comment(&vf,-1)->vendor); - EWOLSA_DEBUG("time: " << ((float)_nbSampleOut/(float)vi->rate)/60.0); + EWOLSA_DEBUG("time: " << ((float)self->m_nbSamplesTotal/(float)vi->rate)/60.0); } - */ - int16_t* outputData = new int16_t[_nbSampleOut*_nbChan*sizeof(int16_t)]; - if (NULL == outputData) { - //EWOLSA_ERROR("Allocation ERROR try to allocate " << (int32_t)(_nbSampleOut*_nbChan*sizeof(int16_t) ) << "bytes"); + 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; } - int32_t pos = 0; + + self->m_nbSamples = 0; char pcmout[4096]; - while(!eof){ + 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=1; + eof = true; } else if (ret < 0) { if(ret==OV_EBADLINK){ - //EWOLSA_ERROR("Corrupt bitstream section! Exiting."); + EWOLSA_ERROR("Corrupt bitstream section! Exiting."); // TODO : Remove pointer data ... return NULL; } } else { int16_t* pointerIn = (int16_t*)pcmout; - int16_t* pointerOut = outputData+pos; - if (_nbChan == vi->channels) { + int16_t* pointerOut = self->m_data+self->m_nbSamples; + if (self->m_nbChanRequested == vi->channels) { memcpy(pointerOut, pointerIn, ret); - pos += ret/2; + self->m_nbSamples += ret/2; } else { - if ( _nbChan == 1 + 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 " << _nbChan << " channels"); + EWOLSA_ERROR("Can not convert " << (int32_t)vi->channels << " channels in " << self->m_nbChanRequested << " channels"); } } // fwrite(pcmout,1,ret,stdout); - //EWOLSA_DEBUG("get data : " << ret << " Bytes"); + EWOLSA_DEBUG("get data : " << (int32_t)ret << " Bytes"); } } /* cleanup */ ov_clear(&vf); - //EWOLSA_DEBUG("Done."); - return outputData; + EWOLSA_DEBUG("Done."); + + return NULL; } - diff --git a/ewolsa/decOgg.h b/ewolsa/decOgg.h index 074b0a6..3624fec 100644 --- a/ewolsa/decOgg.h +++ b/ewolsa/decOgg.h @@ -13,8 +13,7 @@ namespace ewolsa { namespace ogg { - int16_t* loadAudioFile(const std::string& _filename, int8_t _nbChan, int32_t& _nbSampleOut); - + void* loadFileThreadedMode(void *_ptr); } };