mirror of
https://github.com/clangen/musikcube.git
synced 2025-01-02 11:58:27 +00:00
Fixed clipping when dealing with float -> short conversion, and added
very simple triangle dithering.
This commit is contained in:
parent
c7cae6cb2d
commit
1721f8adf7
@ -55,6 +55,7 @@ SndioOut::SndioOut() {
|
|||||||
this->bufferSamples = 0;
|
this->bufferSamples = 0;
|
||||||
this->latency = 0.0;
|
this->latency = 0.0;
|
||||||
this->pars = { 0 };
|
this->pars = { 0 };
|
||||||
|
this->ditherState = 0.0;
|
||||||
}
|
}
|
||||||
|
|
||||||
SndioOut::~SndioOut() {
|
SndioOut::~SndioOut() {
|
||||||
@ -214,7 +215,18 @@ int SndioOut::Play(IBuffer *buffer, IBufferProvider *provider) {
|
|||||||
float* src = buffer->BufferPointer();
|
float* src = buffer->BufferPointer();
|
||||||
short* dst = this->buffer;
|
short* dst = this->buffer;
|
||||||
for (long i = 0; i < samples; i++) {
|
for (long i = 0; i < samples; i++) {
|
||||||
*dst = (short)((*src) * SHRT_MAX);
|
float sample = *src;
|
||||||
|
if (sample > 1.0f) sample = 1.0f;
|
||||||
|
if (sample < -1.0f) sample = -1.0f;
|
||||||
|
sample *= SHRT_MAX;
|
||||||
|
|
||||||
|
/* triangle (high pass) dither, based on Audacity's
|
||||||
|
implementation */
|
||||||
|
float r = (rand() / (float) RAND_MAX - 0.5f);
|
||||||
|
sample = sample + r - this->ditherState;
|
||||||
|
this->ditherState = r;
|
||||||
|
|
||||||
|
*dst = sample;
|
||||||
++dst; ++src;
|
++dst; ++src;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -227,10 +239,16 @@ int SndioOut::Play(IBuffer *buffer, IBufferProvider *provider) {
|
|||||||
while (totalWritten < dataLength && this->state == StatePlaying) {
|
while (totalWritten < dataLength && this->state == StatePlaying) {
|
||||||
size_t remaining = dataLength - totalWritten;
|
size_t remaining = dataLength - totalWritten;
|
||||||
size_t written = 0;
|
size_t written = 0;
|
||||||
|
|
||||||
{
|
{
|
||||||
LOCK()
|
LOCK()
|
||||||
written = sio_write(this->handle, data, remaining);
|
written = sio_write(this->handle, data, remaining);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (written == 0) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
totalWritten += written;
|
totalWritten += written;
|
||||||
data += written;
|
data += written;
|
||||||
}
|
}
|
||||||
|
@ -77,5 +77,6 @@ class SndioOut : public IOutput {
|
|||||||
short* buffer;
|
short* buffer;
|
||||||
long bufferSamples;
|
long bufferSamples;
|
||||||
double latency;
|
double latency;
|
||||||
|
float ditherState;
|
||||||
std::recursive_mutex mutex;
|
std::recursive_mutex mutex;
|
||||||
};
|
};
|
||||||
|
Loading…
Reference in New Issue
Block a user