Merge pull request #43 from phniix/Issue42-LoadFromBuffer_avert_silently_handled_exception

Fixes ddiakopoulos/libnyquist#42
pull/45/head
Dimitri Diakopoulos 6 years ago committed by GitHub
commit 8bb474473f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -135,6 +135,7 @@ void NyquistIO::Load(AudioData * data, const std::string & extension, const std:
catch (const std::exception & e) catch (const std::exception & e)
{ {
std::cerr << "caught internal loading exception: " << e.what() << std::endl; std::cerr << "caught internal loading exception: " << e.what() << std::endl;
throw;
} }
} }
else throw std::runtime_error("fatal: no decoders available"); else throw std::runtime_error("fatal: no decoders available");
@ -186,26 +187,26 @@ NyquistFileBuffer nqr::ReadFile(const std::string & pathToFile)
{ {
//std::cout << "[Debug] Open: " << pathToFile << std::endl; //std::cout << "[Debug] Open: " << pathToFile << std::endl;
FILE * audioFile = fopen(pathToFile.c_str(), "rb"); FILE * audioFile = fopen(pathToFile.c_str(), "rb");
if (!audioFile) if (!audioFile)
{ {
throw std::runtime_error("file not found"); throw std::runtime_error("file not found");
} }
fseek(audioFile, 0, SEEK_END); fseek(audioFile, 0, SEEK_END);
size_t lengthInBytes = ftell(audioFile); size_t lengthInBytes = ftell(audioFile);
fseek(audioFile, 0, SEEK_SET); fseek(audioFile, 0, SEEK_SET);
// Allocate temporary buffer // Allocate temporary buffer
std::vector<uint8_t> fileBuffer(lengthInBytes); std::vector<uint8_t> fileBuffer(lengthInBytes);
size_t elementsRead = fread(fileBuffer.data(), 1, lengthInBytes, audioFile); size_t elementsRead = fread(fileBuffer.data(), 1, lengthInBytes, audioFile);
if (elementsRead == 0 || fileBuffer.size() < 64) if (elementsRead == 0 || fileBuffer.size() < 64)
{ {
throw std::runtime_error("error reading file or file too small"); throw std::runtime_error("error reading file or file too small");
} }
NyquistFileBuffer data = {std::move(fileBuffer), elementsRead}; NyquistFileBuffer data = {std::move(fileBuffer), elementsRead};
fclose(audioFile); fclose(audioFile);
@ -218,7 +219,7 @@ NyquistFileBuffer nqr::ReadFile(const std::string & pathToFile)
void nqr::ConvertToFloat32(float * dst, const uint8_t * src, const size_t N, PCMFormat f) void nqr::ConvertToFloat32(float * dst, const uint8_t * src, const size_t N, PCMFormat f)
{ {
assert(f != PCM_END); assert(f != PCM_END);
if (f == PCM_U8) if (f == PCM_U8)
{ {
const uint8_t * dataPtr = reinterpret_cast<const uint8_t *>(src); const uint8_t * dataPtr = reinterpret_cast<const uint8_t *>(src);
@ -254,9 +255,9 @@ void nqr::ConvertToFloat32(float * dst, const uint8_t * src, const size_t N, PCM
for (size_t i = 0; i < N; ++i) for (size_t i = 0; i < N; ++i)
dst[i] = int32_to_float32(Read32(dataPtr[i])); dst[i] = int32_to_float32(Read32(dataPtr[i]));
} }
//@todo add int64 format //@todo add int64 format
else if (f == PCM_FLT) else if (f == PCM_FLT)
{ {
std::memcpy(dst, src, N * sizeof(float)); std::memcpy(dst, src, N * sizeof(float));
@ -276,7 +277,7 @@ void nqr::ConvertToFloat32(float * dst, const uint8_t * src, const size_t N, PCM
void nqr::ConvertToFloat32(float * dst, const int32_t * src, const size_t N, PCMFormat f) void nqr::ConvertToFloat32(float * dst, const int32_t * src, const size_t N, PCMFormat f)
{ {
assert(f != PCM_END); assert(f != PCM_END);
if (f == PCM_16) if (f == PCM_16)
{ {
for (size_t i = 0; i < N; ++i) for (size_t i = 0; i < N; ++i)
@ -315,7 +316,7 @@ void nqr::ConvertFromFloat32(uint8_t * dst, const float * src, const size_t N, P
assert(f != PCM_END); assert(f != PCM_END);
Dither dither(t); Dither dither(t);
if (f == PCM_U8) if (f == PCM_U8)
{ {
uint8_t * destPtr = reinterpret_cast<uint8_t *>(dst); uint8_t * destPtr = reinterpret_cast<uint8_t *>(dst);

Loading…
Cancel
Save