From 23467923b4917aa7db24f262b3129ac5f58803c5 Mon Sep 17 00:00:00 2001 From: Edouard DUPIN Date: Fri, 13 Feb 2015 21:06:55 +0100 Subject: [PATCH] [DEV] set it basic work again with RIVER --- ewolsa/LoadedFile.cpp | 27 +++++++++-------- ewolsa/LoadedFile.h | 3 +- ewolsa/decOgg.cpp | 26 +++++++---------- ewolsa/decOgg.h | 2 +- ewolsa/decWav.cpp | 67 ++++++++++++++++++++----------------------- ewolsa/decWav.h | 2 +- ewolsa/effects.cpp | 14 ++++----- ewolsa/ewolsa.cpp | 9 +++++- ewolsa/music.cpp | 27 +++++++++-------- 9 files changed, 88 insertions(+), 89 deletions(-) diff --git a/ewolsa/LoadedFile.cpp b/ewolsa/LoadedFile.cpp index 3d28dc1..dec6f0b 100644 --- a/ewolsa/LoadedFile.cpp +++ b/ewolsa/LoadedFile.cpp @@ -13,30 +13,31 @@ #include #include -static void* threadCallback2(void *_ptr) { - ewolsa::LoadedFile* decodeFile = (ewolsa::LoadedFile*)_ptr; +static void threadCallback2(void* _userData) { + etk::log::setThreadName("ewolSA decoder"); + ewolsa::LoadedFile* decodeFile = reinterpret_cast(_userData); decodeFile->decode(); - return NULL; } void ewolsa::LoadedFile::decode() { - m_data = ewolsa::ogg::loadAudioFile(m_file, m_nbChanRequested, m_nbSamples); + m_data = ewolsa::ogg::loadAudioFile(m_file, m_nbChanRequested); + m_nbSamples = m_data.size(); } ewolsa::LoadedFile::LoadedFile(const std::string& _fileName, int8_t _nbChanRequested) : + m_thread(nullptr), m_file(_fileName), m_nbSamples(0), m_nbChanRequested(_nbChanRequested), - m_requestedTime(1), - m_data(NULL){ - m_thread = NULL; + m_requestedTime(1){ std::string tmpName = etk::tolower(m_file); // select the corect Loader : if (etk::end_with(tmpName, ".wav") == true) { - m_data = ewolsa::wav::loadAudioFile(m_file, m_nbChanRequested, m_nbSamples); + m_data = ewolsa::wav::loadAudioFile(m_file, m_nbChanRequested); + m_nbSamples = m_data.size(); } else if (etk::end_with(tmpName, ".ogg") == true) { EWOLSA_DEBUG("create thread"); - pthread_create(&m_thread2, NULL, &threadCallback2, this); + m_thread = new std::thread(&threadCallback2, this); EWOLSA_DEBUG("done 1"); std::this_thread::sleep_for(std::chrono::milliseconds(1000)); EWOLSA_DEBUG("done 2"); @@ -45,7 +46,7 @@ ewolsa::LoadedFile::LoadedFile(const std::string& _fileName, int8_t _nbChanReque return; } /* - if (m_data == NULL) { + if (m_data == nullptr) { // write an error ... EWOLSA_ERROR("Can not open file : " << _fileName); } @@ -54,10 +55,8 @@ ewolsa::LoadedFile::LoadedFile(const std::string& _fileName, int8_t _nbChanReque ewolsa::LoadedFile::~LoadedFile() { - // TODO : wait end of thread... - if (m_data != NULL) { - delete[] m_data; - m_data = NULL; + if (m_thread != nullptr) { + delete m_thread; } } diff --git a/ewolsa/LoadedFile.h b/ewolsa/LoadedFile.h index 12685eb..0fde14f 100644 --- a/ewolsa/LoadedFile.h +++ b/ewolsa/LoadedFile.h @@ -18,7 +18,6 @@ namespace ewolsa { class LoadedFile { private: std::thread* m_thread; - pthread_t m_thread2; public: LoadedFile(const std::string& _fileName, int8_t _nbChanRequested=1); ~LoadedFile(); @@ -26,7 +25,7 @@ namespace ewolsa { int32_t m_nbSamples; int32_t m_nbChanRequested; int32_t m_requestedTime; - int16_t* m_data; + std::vector m_data; public: const std::string& getName() { return m_file; diff --git a/ewolsa/decOgg.cpp b/ewolsa/decOgg.cpp index 99fb491..b4445f4 100644 --- a/ewolsa/decOgg.cpp +++ b/ewolsa/decOgg.cpp @@ -48,8 +48,7 @@ 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; +std::vector ewolsa::ogg::loadAudioFile(const std::string& _filename, int8_t _nbChan) { OggVorbis_File vf; int32_t eof=0; int32_t current_section; @@ -64,23 +63,23 @@ int16_t* ewolsa::ogg::loadAudioFile(const std::string& _filename, int8_t _nbChan //EWOLSA_DEBUG("open file (OGG) \"" << fileAccess << "\""); if (false == fileAccess->exist()) { //EWOLSA_ERROR("File Does not exist : \"" << fileAccess << "\""); - return NULL; + return std::vector(); } int32_t fileSize = fileAccess->fileSize(); if (0 == fileSize) { //EWOLSA_ERROR("This file is empty : \"" << fileAccess << "\""); - return NULL; + return std::vector(); } if (false == fileAccess->fileOpenRead()) { //EWOLSA_ERROR("Can not open the file : \"" << fileAccess << "\""); - return NULL; + return std::vector(); } - if (ov_open_callbacks(&(*fileAccess), &vf, NULL, 0, tmpCallback) < 0) { + if (ov_open_callbacks(&(*fileAccess), &vf, nullptr, 0, tmpCallback) < 0) { //EWOLSA_ERROR("Input does not appear to be an Ogg bitstream."); - return NULL; + return std::vector(); } vorbis_info *vi=ov_info(&vf,-1); - _nbSampleOut = ov_pcm_total(&vf,-1) / vi->channels; + int32_t nbSampleOut = ov_pcm_total(&vf,-1) / vi->channels; /* { char **ptr=ov_comment(&vf,-1)->user_comments; @@ -94,11 +93,8 @@ int16_t* ewolsa::ogg::loadAudioFile(const std::string& _filename, int8_t _nbChan EWOLSA_DEBUG("time: " << ((float)_nbSampleOut/(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"); - return NULL; - } + std::vector outputData; + outputData.resize(nbSampleOut*_nbChan, 0); int32_t pos = 0; char pcmout[4096]; while(!eof){ @@ -110,11 +106,11 @@ int16_t* ewolsa::ogg::loadAudioFile(const std::string& _filename, int8_t _nbChan if(ret==OV_EBADLINK){ //EWOLSA_ERROR("Corrupt bitstream section! Exiting."); // TODO : Remove pointer data ... - return NULL; + return std::vector(); } } else { int16_t* pointerIn = (int16_t*)pcmout; - int16_t* pointerOut = outputData+pos; + int16_t* pointerOut = &outputData[0]+pos; if (_nbChan == vi->channels) { memcpy(pointerOut, pointerIn, ret); pos += ret/2; diff --git a/ewolsa/decOgg.h b/ewolsa/decOgg.h index fd3e81c..64b296b 100644 --- a/ewolsa/decOgg.h +++ b/ewolsa/decOgg.h @@ -13,7 +13,7 @@ namespace ewolsa { namespace ogg { - int16_t* loadAudioFile(const std::string& _filename, int8_t _nbChan, int32_t& _nbSampleOut); + std::vector loadAudioFile(const std::string& _filename, int8_t _nbChan); } }; diff --git a/ewolsa/decWav.cpp b/ewolsa/decWav.cpp index e0e51db..00b4cbe 100644 --- a/ewolsa/decWav.cpp +++ b/ewolsa/decWav.cpp @@ -59,8 +59,7 @@ typedef struct { #define COMPR_G721 (64) #define COMPR_MPEG (80) -int16_t* ewolsa::wav::loadAudioFile(const std::string& _filename, int8_t _nbChan, int32_t& _nbSampleOut) { - _nbSampleOut = 0; +std::vector ewolsa::wav::loadAudioFile(const std::string& _filename, int8_t _nbChan) { waveHeader myHeader; memset(&myHeader, 0, sizeof(waveHeader)); etk::FSNode fileAccess(_filename); @@ -69,28 +68,28 @@ int16_t* ewolsa::wav::loadAudioFile(const std::string& _filename, int8_t _nbChan if (false == fileAccess.exist()) { EWOLSA_ERROR("File Does not exist : \"" << fileAccess << "\""); - return NULL; + return std::vector(); } int32_t fileSize = fileAccess.fileSize(); if (0 == fileSize) { EWOLSA_ERROR("This file is empty : \"" << fileAccess << "\""); - return NULL; + return std::vector(); } if (false == fileAccess.fileOpenRead()) { EWOLSA_ERROR("Can not open the file : \"" << fileAccess << "\""); - return NULL; + return std::vector(); } // try to find endienness : if (fileSize < (int64_t)sizeof(waveHeader)) { EWOLSA_ERROR("File : \"" << fileAccess << "\" == > has not enouth data inside might be minumum of " << (int32_t)(sizeof(waveHeader))); - return NULL; + return std::vector(); } // ---------------------------------------------- // read the header : // ---------------------------------------------- if (fileAccess.fileRead(&myHeader.riffTag, 1, 4)!=4) { EWOLSA_ERROR("Can not 4 element in the file : \"" << fileAccess << "\""); - return NULL; + return std::vector(); } bool littleEndien = false; if( myHeader.riffTag[0] == 'R' @@ -102,55 +101,55 @@ int16_t* ewolsa::wav::loadAudioFile(const std::string& _filename, int8_t _nbChan } } else { EWOLSA_ERROR("file: \"" << fileAccess << "\" Does not start with \"RIF\" " ); - return NULL; + return std::vector(); } // get the data size : unsigned char tmpData[32]; if (fileAccess.fileRead(tmpData, 1, 4)!=4) { EWOLSA_ERROR("Can not 4 element in the file : \"" << fileAccess << "\""); - return NULL; + return std::vector(); } myHeader.size = CONVERT_UINT32(littleEndien, tmpData); // get the data size : if (fileAccess.fileRead(&myHeader.waveTag, 1, 4)!=4) { EWOLSA_ERROR("Can not 4 element in the file : \"" << fileAccess << "\""); - return NULL; + return std::vector(); } if( myHeader.waveTag[0] != 'W' || myHeader.waveTag[1] != 'A' || myHeader.waveTag[2] != 'V' || myHeader.waveTag[3] != 'E' ) { EWOLSA_ERROR("file: \"" << fileAccess << "\" This is not a wave file " << myHeader.waveTag[0] << myHeader.waveTag[1] << myHeader.waveTag[2] << myHeader.waveTag[3] ); - return NULL; + return std::vector(); } // get the data size : if (fileAccess.fileRead(&myHeader.fmtTag, 1, 4)!=4) { EWOLSA_ERROR("Can not 4 element in the file : \"" << fileAccess << "\""); - return NULL; + return std::vector(); } if( myHeader.fmtTag[0] != 'f' || myHeader.fmtTag[1] != 'm' || myHeader.fmtTag[2] != 't' || myHeader.fmtTag[3] != ' ' ) { EWOLSA_ERROR("file: \"" << fileAccess << "\" header error ..." << myHeader.fmtTag[0] << myHeader.fmtTag[1] << myHeader.fmtTag[2] << myHeader.fmtTag[3]); - return NULL; + return std::vector(); } // get the data size : if (fileAccess.fileRead(tmpData, 1, 4)!=4) { EWOLSA_ERROR("Can not 4 element in the file : \"" << fileAccess << "\""); - return NULL; + return std::vector(); } myHeader.waveFormatSize = CONVERT_UINT32(littleEndien, tmpData); if (myHeader.waveFormatSize != 16) { EWOLSA_ERROR("file : \"" << fileAccess << "\" == > header error ..."); - return NULL; + return std::vector(); } if (fileAccess.fileRead(tmpData, 1, 16)!=16) { EWOLSA_ERROR("Can not 16 element in the file : \"" << fileAccess << "\""); - return NULL; + return std::vector(); } unsigned char * tmppp = tmpData; myHeader.waveFormat.type = CONVERT_UINT16(littleEndien, tmppp); @@ -174,19 +173,19 @@ int16_t* ewolsa::wav::loadAudioFile(const std::string& _filename, int8_t _nbChan // get the data size : if (fileAccess.fileRead(&myHeader.dataTag, 1, 4)!=4) { EWOLSA_ERROR("Can not 4 element in the file : \"" << fileAccess << "\""); - return NULL; + return std::vector(); } if( myHeader.dataTag[0] != 'd' || myHeader.dataTag[1] != 'a' || myHeader.dataTag[2] != 't' || myHeader.dataTag[3] != 'a' ) { EWOLSA_ERROR("file: \"" << fileAccess << "\" header error ..." << myHeader.dataTag[0] << myHeader.dataTag[1] << myHeader.dataTag[2] << myHeader.dataTag[3]); - return NULL; + return std::vector(); } // get the data size : if (fileAccess.fileRead(tmpData, 1, 4)!=4) { EWOLSA_ERROR("Can not 4 element in the file : \"" << fileAccess << "\""); - return NULL; + return std::vector(); } myHeader.dataSize = CONVERT_UINT32(littleEndien, tmpData); @@ -197,33 +196,30 @@ int16_t* ewolsa::wav::loadAudioFile(const std::string& _filename, int8_t _nbChan //Parse the data and transform it if needed ... if (COMPR_PCM != myHeader.waveFormat.type) { EWOLSA_ERROR("File : \"" << fileAccess << "\" == > support only PCM compression ..."); - return NULL; + return std::vector(); } if (myHeader.waveFormat.channelCount == 0 || myHeader.waveFormat.channelCount>2) { EWOLSA_ERROR("File : \"" << fileAccess << "\" == > support only mono or stereo ..." << myHeader.waveFormat.channelCount); - return NULL; + return std::vector(); } if ( ! ( myHeader.waveFormat.bitsPerSample == 16 || myHeader.waveFormat.bitsPerSample == 24 || myHeader.waveFormat.bitsPerSample == 32 ) ) { EWOLSA_ERROR("File : \"" << fileAccess << "\" == > not supported bit/sample ..." << myHeader.waveFormat.bitsPerSample); - return NULL; + return std::vector(); } if( ! ( 44100 == myHeader.waveFormat.samplesPerSec || 48000 == myHeader.waveFormat.samplesPerSec) ) { EWOLSA_ERROR("File : \"" << fileAccess << "\" == > not supported frequency " << myHeader.waveFormat.samplesPerSec << " != 48000"); - return NULL; + return std::vector(); } EWOLSA_DEBUG(" dataSize : " << myHeader.dataSize); //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 = 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; - } - int16_t * tmpOut = outputData; + std::vector outputData; + outputData.resize(outputSize, 0); + int16_t * tmpOut = &outputData[0]; for( int32_t iii=0; iii(); } left = ((int32_t)((int16_t)CONVERT_INT16(littleEndien, audioSample))) << 16; right = left; } else { if (fileAccess.fileRead(audioSample, 1, 4)!=4) { EWOLSA_ERROR("Read Error at position : " << iii); - return NULL; + return std::vector(); } left = (int32_t)((int16_t)CONVERT_INT16(littleEndien, audioSample)) << 16; right = (int32_t)((int16_t)CONVERT_INT16(littleEndien, audioSample+2)) << 16; @@ -248,14 +244,14 @@ int16_t* ewolsa::wav::loadAudioFile(const std::string& _filename, int8_t _nbChan if (myHeader.waveFormat.channelCount == 1) { if (fileAccess.fileRead(audioSample, 1, 3)!=3) { EWOLSA_ERROR("Read Error at position : " << iii); - return NULL; + return std::vector(); } left = CONVERT_INT24(littleEndien, audioSample); right = left; } else { if (fileAccess.fileRead(audioSample, 1, 6)!=6) { EWOLSA_ERROR("Read Error at position : " << iii); - return NULL; + return std::vector(); } left = CONVERT_INT24(littleEndien, audioSample); right = CONVERT_INT24(littleEndien, audioSample+3); @@ -264,14 +260,14 @@ int16_t* ewolsa::wav::loadAudioFile(const std::string& _filename, int8_t _nbChan if (myHeader.waveFormat.channelCount == 1) { if (fileAccess.fileRead(audioSample, 1, 4)!=4) { EWOLSA_ERROR("Read Error at position : " << iii); - return NULL; + return std::vector(); } left = CONVERT_INT32(littleEndien, audioSample); right = left; } else { if (fileAccess.fileRead(audioSample, 1, 8)!=8) { EWOLSA_ERROR("Read Error at position : " << iii); - return NULL; + return std::vector(); } left = CONVERT_INT32(littleEndien, audioSample); right = CONVERT_INT32(littleEndien, audioSample+4); @@ -286,7 +282,6 @@ int16_t* ewolsa::wav::loadAudioFile(const std::string& _filename, int8_t _nbChan } // close the file: fileAccess.fileClose(); - _nbSampleOut = nbSample; return outputData; } diff --git a/ewolsa/decWav.h b/ewolsa/decWav.h index a75ece1..e6e2a88 100644 --- a/ewolsa/decWav.h +++ b/ewolsa/decWav.h @@ -13,7 +13,7 @@ namespace ewolsa { namespace wav { - int16_t* loadAudioFile(const std::string& _filename, int8_t _nbChan, int32_t& _nbSampleOut); + std::vector loadAudioFile(const std::string& _filename, int8_t _nbChan); } }; diff --git a/ewolsa/effects.cpp b/ewolsa/effects.cpp index 442bde2..763e135 100644 --- a/ewolsa/effects.cpp +++ b/ewolsa/effects.cpp @@ -50,7 +50,7 @@ class RequestPlay { if (true == m_freeSlot) { return; } - if (m_effect->m_data == NULL) { + if (m_effect->m_data.size() == 0) { m_freeSlot = true; return; } @@ -93,7 +93,7 @@ void ewolsa::effects::unInit() { int32_t ewolsa::effects::add(const std::string& _file) { for (size_t iii=0; iiim_file == _file) { @@ -103,7 +103,7 @@ int32_t ewolsa::effects::add(const std::string& _file) { } // effect does not exist ... create a new one ... ewolsa::LoadedFile * tmpEffect = new ewolsa::LoadedFile(_file); - if (NULL == tmpEffect) { + if (nullptr == tmpEffect) { EWOLSA_ERROR("Error to load the effects : \"" << _file << "\""); return -1; } @@ -117,7 +117,7 @@ void ewolsa::effects::rm(int32_t _effectId) { EWOLSA_ERROR("Wrong effect ID : " << _effectId << " != [0.." << ListEffects.size()-1 << "] == > can not remove it ..."); return; } - if (ListEffects[_effectId] == NULL) { + if (ListEffects[_effectId] == nullptr) { EWOLSA_ERROR("effect ID : " << _effectId << " == > has already been removed"); return; } @@ -137,7 +137,7 @@ void ewolsa::effects::play(int32_t _effectId, float _xxx, float _yyy) { EWOLSA_ERROR("Wrong effect ID : " << _effectId << " != [0.." << ListEffects.size()-1 << "] == > can not play it ..."); return; } - if (ListEffects[_effectId] == NULL) { + if (ListEffects[_effectId] == nullptr) { EWOLSA_ERROR("effect ID : " << _effectId << " == > has been removed"); return; } @@ -150,7 +150,7 @@ void ewolsa::effects::play(int32_t _effectId, float _xxx, float _yyy) { } } RequestPlay* newPlay = new RequestPlay(ListEffects[_effectId]); - if (NULL == newPlay) { + if (nullptr == newPlay) { EWOLSA_CRITICAL("Allocation error of a playing element : " << _effectId); return; } @@ -197,7 +197,7 @@ void ewolsa::effects::muteSet(bool _newMute) { void ewolsa::effects::getData(int16_t* _bufferInterlace, int32_t _nbSample, int32_t _nbChannels) { for (size_t iii = 0; iii < ListEffectsPlaying.size(); ++iii) { - if (ListEffectsPlaying[iii]!= NULL) { + if (ListEffectsPlaying[iii]!= nullptr) { ListEffectsPlaying[iii]->play(_bufferInterlace, _nbSample, _nbChannels); } } diff --git a/ewolsa/ewolsa.cpp b/ewolsa/ewolsa.cpp index e391cdd..9764862 100644 --- a/ewolsa/ewolsa.cpp +++ b/ewolsa/ewolsa.cpp @@ -31,6 +31,10 @@ class OutputInterface { audio::format_int16, "speaker", "ewolsa::basicOutput"); + if (m_interface == nullptr) { + EWOLSA_ERROR("can not allocate output interface ... "); + return; + } // set callback mode ... m_interface->setOutputCallback(1024, std::bind(&OutputInterface::onDataNeeded, @@ -43,6 +47,9 @@ class OutputInterface { m_interface->start(); } ~OutputInterface() { + if (m_interface == nullptr) { + return; + } m_interface->stop(); m_interface.reset(); m_manager.reset(); @@ -58,7 +65,7 @@ class OutputInterface { // call music ewolsa::music::getData(static_cast(_data), _nbChunk, _map.size()); // call Effects - ewolsa::effects::getData(static_cast(_data), _nbChunk, _map.size()); + //ewolsa::effects::getData(static_cast(_data), _nbChunk, _map.size()); } }; std::shared_ptr g_ioInterface; diff --git a/ewolsa/music.cpp b/ewolsa/music.cpp index dcd4756..b2bdef8 100644 --- a/ewolsa/music.cpp +++ b/ewolsa/music.cpp @@ -34,11 +34,11 @@ void ewolsa::music::init() { musicNextRead = -1; musicPositionReading = 0; for (size_t iii=0; iiigetName() == _file) { @@ -79,9 +79,9 @@ void ewolsa::music::preLoad(const std::string& _file) { } } ewolsa::LoadedFile* tmp = new ewolsa::LoadedFile(_file, 2); - if (tmp != NULL) { + if (tmp != nullptr) { /* - if (tmp->m_data == NULL) { + if (tmp->m_data == nullptr) { EWOLSA_ERROR("Music has no data ... : " << _file); delete(tmp); return; @@ -101,7 +101,7 @@ bool ewolsa::music::play(const std::string& _file) { int32_t idMusic = -1; // check if music already existed ... for (size_t iii=0; iiigetName() == _file) { @@ -182,23 +182,26 @@ void ewolsa::music::getData(int16_t * _bufferInterlace, int32_t _nbSample, int32 return; } if ( musicCurrentRead >= musicListRead.size() - || musicListRead[musicCurrentRead] == NULL) { + || musicListRead[musicCurrentRead] == nullptr) { musicCurrentRead = -1; musicPositionReading = 0; EWOLSA_ERROR("request read an unexisting audio track ... : " << musicCurrentRead << "/" << musicListRead.size()); return; } - if (musicListRead[musicCurrentRead]->m_data == NULL) { + if (musicListRead[musicCurrentRead]->m_data.size() == 0) { return; } int32_t processTimeMax = std::min(_nbSample*_nbChannels, musicListRead[musicCurrentRead]->m_nbSamples - musicPositionReading); processTimeMax = std::max(0, processTimeMax); int16_t * pointer = _bufferInterlace; int16_t * newData = &musicListRead[musicCurrentRead]->m_data[musicPositionReading]; - //EWOLSA_DEBUG("AUDIO : Play slot... nb sample : " << processTimeMax << " playTime=" <