stdin buffer tuning

This commit is contained in:
Christian R. Helmrich
2020-06-13 17:00:02 +02:00
parent 36813e3fd9
commit b83a3c489b
3 changed files with 93 additions and 50 deletions

View File

@@ -1,5 +1,5 @@
/* basicWavReader.cpp - source file for class with basic WAVE file reading capability /* basicWavReader.cpp - source file for class with basic WAVE file reading capability
* written by C. R. Helmrich, last modified in 2019 - see License.htm for legal notices * written by C. R. Helmrich, last modified in 2020 - see License.htm for legal notices
* *
* The copyright in this software is being made available under a Modified BSD-Style License * The copyright in this software is being made available under a Modified BSD-Style License
* and comes with ABSOLUTELY NO WARRANTY. This software may be subject to other third- * and comes with ABSOLUTELY NO WARRANTY. This software may be subject to other third-
@@ -112,11 +112,16 @@ unsigned BasicWavReader::readDataFloat16 (const int fileHandle, int32_t* frameBu
const unsigned chanCount, void* tempBuf) const unsigned chanCount, void* tempBuf)
{ {
#if BWR_BUFFERED_READ #if BWR_BUFFERED_READ
const int16_t* fBuf = (const int16_t*) tempBuf; // words unsigned framesRead = 0;
const int bytesRead = _READ (fileHandle, tempBuf, frameCount * chanCount * 2);
unsigned framesRead = __max (0, bytesRead / (chanCount * 2));
for (unsigned i = framesRead * chanCount; i > 0; i--) for (unsigned fract = 0; fract < (1 << BWR_READ_FRACT); fract++)
{
const int16_t* fBuf = (const int16_t*) tempBuf; // words
const unsigned size = (frameCount + ((fract & 1) > 0 ? 1 << (BWR_READ_FRACT - 1) : 0)) >> BWR_READ_FRACT;
const int bytesRead = _READ (fileHandle, tempBuf, size * chanCount * 2);
const unsigned read = __max (0, bytesRead / (chanCount * 2));
for (unsigned i = read * chanCount; i > 0; i--)
{ {
const int16_t i16 = *(fBuf++); const int16_t i16 = *(fBuf++);
const int32_t e = ((i16 & 0x7C00) >> 10) - 18; // exp. const int32_t e = ((i16 & 0x7C00) >> 10) - 18; // exp.
@@ -126,6 +131,8 @@ unsigned BasicWavReader::readDataFloat16 (const int fileHandle, int32_t* frameBu
if ((i16 & 0x8000) != 0) *frameBuf *= -1; // neg. sign if ((i16 & 0x8000) != 0) *frameBuf *= -1; // neg. sign
frameBuf++; frameBuf++;
} }
framesRead += read;
}
if (framesRead < frameCount) // zero out missing samples if (framesRead < frameCount) // zero out missing samples
{ {
memset (frameBuf, 0, (frameCount - framesRead) * chanCount * sizeof (int32_t)); memset (frameBuf, 0, (frameCount - framesRead) * chanCount * sizeof (int32_t));
@@ -155,11 +162,16 @@ unsigned BasicWavReader::readDataFloat32 (const int fileHandle, int32_t* frameBu
const unsigned chanCount, void* tempBuf) const unsigned chanCount, void* tempBuf)
{ {
#if BWR_BUFFERED_READ #if BWR_BUFFERED_READ
const float* fBuf = (const float*) tempBuf; // 4 bytes unsigned framesRead = 0;
const int bytesRead = _READ (fileHandle, tempBuf, frameCount * chanCount * 4);
unsigned framesRead = __max (0, bytesRead / (chanCount * 4));
for (unsigned i = framesRead * chanCount; i > 0; i--) for (unsigned fract = 0; fract < (1 << BWR_READ_FRACT); fract++)
{
const float* fBuf = (const float*) tempBuf; // 4 bytes
const unsigned size = (frameCount + ((fract & 1) > 0 ? 1 << (BWR_READ_FRACT - 1) : 0)) >> BWR_READ_FRACT;
const int bytesRead = _READ (fileHandle, tempBuf, size * chanCount * 4);
const unsigned read = __max (0, bytesRead / (chanCount * 4));
for (unsigned i = read * chanCount; i > 0; i--)
{ {
const float f32 = *fBuf * float (1 << 23); // * 2^23 const float f32 = *fBuf * float (1 << 23); // * 2^23
fBuf++; fBuf++;
@@ -169,6 +181,8 @@ unsigned BasicWavReader::readDataFloat32 (const int fileHandle, int32_t* frameBu
if (*frameBuf > MAX_VALUE_AUDIO24) *frameBuf = MAX_VALUE_AUDIO24; if (*frameBuf > MAX_VALUE_AUDIO24) *frameBuf = MAX_VALUE_AUDIO24;
frameBuf++; frameBuf++;
} }
framesRead += read;
}
if (framesRead < frameCount) // zero out missing samples if (framesRead < frameCount) // zero out missing samples
{ {
memset (frameBuf, 0, (frameCount - framesRead) * chanCount * sizeof (int32_t)); memset (frameBuf, 0, (frameCount - framesRead) * chanCount * sizeof (int32_t));
@@ -196,14 +210,21 @@ unsigned BasicWavReader::readDataLnPcm08 (const int fileHandle, int32_t* frameBu
const unsigned chanCount, void* tempBuf) const unsigned chanCount, void* tempBuf)
{ {
#if BWR_BUFFERED_READ #if BWR_BUFFERED_READ
const uint8_t* iBuf = (uint8_t*) tempBuf; unsigned framesRead = 0;
const int bytesRead = _READ (fileHandle, tempBuf, frameCount * chanCount);
unsigned framesRead = __max (0, bytesRead / chanCount);
for (unsigned i = framesRead * chanCount; i > 0; i--) for (unsigned fract = 0; fract < (1 << BWR_READ_FRACT); fract++)
{
const uint8_t* iBuf = (const uint8_t*) tempBuf; // 1b
const unsigned size = (frameCount + ((fract & 1) > 0 ? 1 << (BWR_READ_FRACT - 1) : 0)) >> BWR_READ_FRACT;
const int bytesRead = _READ (fileHandle, tempBuf, size * chanCount);
const unsigned read = __max (0, bytesRead / chanCount);
for (unsigned i = read * chanCount; i > 0; i--)
{ {
*(frameBuf++) = ((int32_t) *(iBuf++) - 128) << 16; // * 2^16 *(frameBuf++) = ((int32_t) *(iBuf++) - 128) << 16; // * 2^16
} }
framesRead += read;
}
if (framesRead < frameCount) // zero out missing samples if (framesRead < frameCount) // zero out missing samples
{ {
memset (frameBuf, 0, (frameCount - framesRead) * chanCount * sizeof (int32_t)); memset (frameBuf, 0, (frameCount - framesRead) * chanCount * sizeof (int32_t));
@@ -227,14 +248,21 @@ unsigned BasicWavReader::readDataLnPcm16 (const int fileHandle, int32_t* frameBu
const unsigned chanCount, void* tempBuf) const unsigned chanCount, void* tempBuf)
{ {
#if BWR_BUFFERED_READ #if BWR_BUFFERED_READ
const int16_t* iBuf = (const int16_t*) tempBuf; // words unsigned framesRead = 0;
const int bytesRead = _READ (fileHandle, tempBuf, frameCount * chanCount * 2);
unsigned framesRead = __max (0, bytesRead / (chanCount * 2));
for (unsigned i = framesRead * chanCount; i > 0; i--) for (unsigned fract = 0; fract < (1 << BWR_READ_FRACT); fract++)
{
const int16_t* iBuf = (const int16_t*) tempBuf; // words
const unsigned size = (frameCount + ((fract & 1) > 0 ? 1 << (BWR_READ_FRACT - 1) : 0)) >> BWR_READ_FRACT;
const int bytesRead = _READ (fileHandle, tempBuf, size * chanCount * 2);
const unsigned read = __max (0, bytesRead / (chanCount * 2));
for (unsigned i = read * chanCount; i > 0; i--)
{ {
*(frameBuf++) = (int32_t) *(iBuf++) << 8; // * 2^8 *(frameBuf++) = (int32_t) *(iBuf++) << 8; // * 2^8
} }
framesRead += read;
}
if (framesRead < frameCount) // zero out missing samples if (framesRead < frameCount) // zero out missing samples
{ {
memset (frameBuf, 0, (frameCount - framesRead) * chanCount * sizeof (int32_t)); memset (frameBuf, 0, (frameCount - framesRead) * chanCount * sizeof (int32_t));
@@ -258,16 +286,23 @@ unsigned BasicWavReader::readDataLnPcm24 (const int fileHandle, int32_t* frameBu
const unsigned chanCount, void* tempBuf) const unsigned chanCount, void* tempBuf)
{ {
#if BWR_BUFFERED_READ #if BWR_BUFFERED_READ
const uint8_t* iBuf = (uint8_t*) tempBuf; unsigned framesRead = 0;
const int bytesRead = _READ (fileHandle, tempBuf, frameCount * chanCount * 3);
unsigned framesRead = __max (0, bytesRead / (chanCount * 3));
for (unsigned i = framesRead * chanCount; i > 0; i--) for (unsigned fract = 0; fract < (1 << BWR_READ_FRACT); fract++)
{
const uint8_t* iBuf = (const uint8_t*) tempBuf; // 3b
const unsigned size = (frameCount + ((fract & 1) > 0 ? 1 << (BWR_READ_FRACT - 1) : 0)) >> BWR_READ_FRACT;
const int bytesRead = _READ (fileHandle, tempBuf, size * chanCount * 3);
const unsigned read = __max (0, bytesRead / (chanCount * 3));
for (unsigned i = read * chanCount; i > 0; i--)
{ {
const int32_t i24 = (int32_t) iBuf[0] | ((int32_t) iBuf[1] << 8) | ((int32_t) iBuf[2] << 16); const int32_t i24 = (int32_t) iBuf[0] | ((int32_t) iBuf[1] << 8) | ((int32_t) iBuf[2] << 16);
iBuf += 3; iBuf += 3;
*(frameBuf++) = (i24 > MAX_VALUE_AUDIO24 ? i24 + 2 * MIN_VALUE_AUDIO24 : i24); *(frameBuf++) = (i24 > MAX_VALUE_AUDIO24 ? i24 + 2 * MIN_VALUE_AUDIO24 : i24);
} }
framesRead += read;
}
if (framesRead < frameCount) // zero out missing samples if (framesRead < frameCount) // zero out missing samples
{ {
memset (frameBuf, 0, (frameCount - framesRead) * chanCount * sizeof (int32_t)); memset (frameBuf, 0, (frameCount - framesRead) * chanCount * sizeof (int32_t));
@@ -291,16 +326,23 @@ unsigned BasicWavReader::readDataLnPcm32 (const int fileHandle, int32_t* frameBu
const unsigned chanCount, void* tempBuf) const unsigned chanCount, void* tempBuf)
{ {
#if BWR_BUFFERED_READ #if BWR_BUFFERED_READ
const int32_t* iBuf = (const int32_t*) tempBuf; // dword unsigned framesRead = 0;
const int bytesRead = _READ (fileHandle, tempBuf, frameCount * chanCount * 4);
unsigned framesRead = __max (0, bytesRead / (chanCount * 4));
for (unsigned i = framesRead * chanCount; i > 0; i--) for (unsigned fract = 0; fract < (1 << BWR_READ_FRACT); fract++)
{
const int32_t* iBuf = (const int32_t*) tempBuf; // dword
const unsigned size = (frameCount + ((fract & 1) > 0 ? 1 << (BWR_READ_FRACT - 1) : 0)) >> BWR_READ_FRACT;
const int bytesRead = _READ (fileHandle, tempBuf, size * chanCount * 4);
const unsigned read = __max (0, bytesRead / (chanCount * 4));
for (unsigned i = read * chanCount; i > 0; i--)
{ {
const int32_t i24 = ((*iBuf >> 1) + (1 << 6)) >> 7; // * 2^-8 with rounding, overflow-safe const int32_t i24 = ((*iBuf >> 1) + (1 << 6)) >> 7; // * 2^-8 with rounding, overflow-safe
iBuf++; iBuf++;
*(frameBuf++) = __min (MAX_VALUE_AUDIO24, i24); *(frameBuf++) = __min (MAX_VALUE_AUDIO24, i24);
} }
framesRead += read;
}
if (framesRead < frameCount) // zero out missing samples if (framesRead < frameCount) // zero out missing samples
{ {
memset (frameBuf, 0, (frameCount - framesRead) * chanCount * sizeof (int32_t)); memset (frameBuf, 0, (frameCount - framesRead) * chanCount * sizeof (int32_t));
@@ -350,7 +392,7 @@ unsigned BasicWavReader::open (const int wavFileHandle, const uint16_t maxFrameR
{ {
return 4; // WAVE data part invalid or unsupported return 4; // WAVE data part invalid or unsupported
} }
if ((m_byteBuffer = (char*) malloc (m_waveFrameSize * maxFrameRead)) == nullptr) if ((m_byteBuffer = (char*) malloc (m_waveFrameSize * ((maxFrameRead + (1 << (BWR_READ_FRACT - 1))) >> BWR_READ_FRACT))) == nullptr)
{ {
return 5; // read-in byte buffer allocation failed return 5; // read-in byte buffer allocation failed
} }

View File

@@ -1,5 +1,5 @@
/* basicWavReader.h - header file for class with basic WAVE file reading capability /* basicWavReader.h - header file for class with basic WAVE file reading capability
* written by C. R. Helmrich, last modified in 2019 - see License.htm for legal notices * written by C. R. Helmrich, last modified in 2020 - see License.htm for legal notices
* *
* The copyright in this software is being made available under a Modified BSD-Style License * The copyright in this software is being made available under a Modified BSD-Style License
* and comes with ABSOLUTELY NO WARRANTY. This software may be subject to other third- * and comes with ABSOLUTELY NO WARRANTY. This software may be subject to other third-
@@ -15,6 +15,7 @@
// constant data sizes & limits // constant data sizes & limits
#define BWR_BUFFERED_READ 1 // faster reader #define BWR_BUFFERED_READ 1 // faster reader
#define BWR_READ_FRACT 5 // 2^-READ_FRACT
#define CHUNK_FORMAT_MAX 20 #define CHUNK_FORMAT_MAX 20
#define CHUNK_FORMAT_SIZE 16 #define CHUNK_FORMAT_SIZE 16
#define CHUNK_HEADER_SIZE 8 #define CHUNK_HEADER_SIZE 8

View File

@@ -30,7 +30,7 @@
#include <share.h> #include <share.h>
#endif #endif
#define EXHALE_APP_WCHAR (defined (_MSC_VER) || defined (__MINGW32__)) #define EXHALE_APP_WCHAR defined (_MSC_VER) || defined (__INTEL_COMPILER) || defined (__MINGW32__)
#if EXHALE_APP_WCHAR #if EXHALE_APP_WCHAR
#define _SOPENS _wsopen_s #define _SOPENS _wsopen_s
#else #else