audio-ess/audio/ess/LoadedFile.cpp

85 lines
2.2 KiB
C++
Raw Normal View History

2014-03-20 10:24:13 +01:00
/**
* @author Edouard DUPIN
* @copyright 2011, Edouard DUPIN, all right reserved
2014-08-08 23:35:47 +02:00
* @license APACHE v2.0 (see license file)
2014-03-20 10:24:13 +01:00
*/
#include <etk/types.h>
#include <ethread/tools.h>
2015-04-11 13:20:46 +02:00
#include <audio/ess/debug.h>
#include <audio/ess/LoadedFile.h>
#include <audio/ess/decWav.h>
#include <audio/ess/decOgg.h>
#include <unistd.h>
2014-03-20 10:24:13 +01:00
2015-09-28 23:23:56 +02:00
#if defined(__TARGET_OS__Android)
void* audio::ess::LoadedFile::threadCallback(void* _userData) {
audio::ess::LoadedFile* threadHandle = static_cast<audio::ess::LoadedFile*>(_userData);
if (threadHandle != nullptr) {
threadHandle->threadCall();
}
return nullptr;
}
#endif
void audio::ess::LoadedFile::threadCall() {
decode();
2014-04-03 11:00:51 +02:00
}
2015-04-11 13:20:46 +02:00
void audio::ess::LoadedFile::decode() {
2015-09-29 21:12:20 +02:00
EWOLSA_ERROR("Start decode OGG : " << m_file);
2015-04-11 13:20:46 +02:00
m_data = audio::ess::ogg::loadAudioFile(m_file, m_nbChanRequested);
m_nbSamples = m_data.size();
2015-09-29 21:12:20 +02:00
EWOLSA_ERROR("End decode OGG : " << m_file << " size=" << m_nbSamples);
2014-04-03 11:00:51 +02:00
}
2014-03-20 10:24:13 +01:00
2015-04-11 13:20:46 +02:00
audio::ess::LoadedFile::LoadedFile(const std::string& _fileName, int8_t _nbChanRequested) :
2015-09-28 23:23:56 +02:00
#if !defined(__TARGET_OS__Android)
m_thread(nullptr),
#endif
2014-03-31 16:36:09 +02:00
m_file(_fileName),
2014-03-20 10:24:13 +01:00
m_nbSamples(0),
m_nbChanRequested(_nbChanRequested),
2015-09-28 23:23:56 +02:00
m_requestedTime(1) {
2014-08-13 22:30:47 +02:00
std::string tmpName = etk::tolower(m_file);
2014-03-31 16:36:09 +02:00
// select the corect Loader :
2014-08-17 23:34:44 +02:00
if (etk::end_with(tmpName, ".wav") == true) {
2015-04-11 13:20:46 +02:00
m_data = audio::ess::wav::loadAudioFile(m_file, m_nbChanRequested);
2015-06-22 21:44:41 +02:00
m_nbSamples = m_data.size();
2014-08-17 23:34:44 +02:00
} else if (etk::end_with(tmpName, ".ogg") == true) {
2015-06-26 16:32:58 +02:00
#if 1
2015-07-10 23:47:48 +02:00
EWOLSA_INFO("create thread");
2015-09-28 23:23:56 +02:00
#if defined(__TARGET_OS__Android)
pthread_create(&m_thread, nullptr, &audio::ess::LoadedFile::threadCallback, this);
#else
2016-07-19 21:43:58 +02:00
m_thread = ememory::makeShared<std::thread>(&audio::ess::LoadedFile::threadCall, this);
2015-09-28 23:23:56 +02:00
if (m_thread == nullptr) {
2015-09-29 21:12:20 +02:00
EWOLSA_ERROR("Can not create thread ...");
2015-09-28 23:23:56 +02:00
return;
}
#endif
std::this_thread::sleep_for(std::chrono::milliseconds(1));
2015-06-26 16:32:58 +02:00
#else
decode();
#endif
2014-03-31 16:36:09 +02:00
} else {
EWOLSA_ERROR("Extention not managed '" << m_file << "' Sopported extention : .wav / .ogg");
2014-03-31 16:36:09 +02:00
return;
}
2014-03-20 10:24:13 +01:00
}
2015-04-11 13:20:46 +02:00
audio::ess::LoadedFile::~LoadedFile() {
2015-09-28 23:23:56 +02:00
#if defined(__TARGET_OS__Android)
void* ret = nullptr;
int val = pthread_join(m_thread, &ret);
#else
if (m_thread != nullptr) {
m_thread->join();
}
m_thread.reset();
#endif
2014-03-20 10:24:13 +01:00
}