Skip to content

Commit 90a7d77

Browse files
committed
DRAFT CodecDSF
1 parent 36ae7f1 commit 90a7d77

File tree

2 files changed

+23
-20
lines changed

2 files changed

+23
-20
lines changed

src/AudioTools/AudioCodecs/CodecDSF.h

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ struct DSFMetadata : public AudioInfo {
5757
float duration_sec = 0; ///< Approximate audio duration in seconds
5858
uint32_t dsd_buffer_size =
5959
DSD_BUFFER_SIZE; ///< Internal buffer size for DSD processing
60-
float filter_q = 1.41f;
60+
float filter_q = 0.5f; //1.41f;
6161
float filter_cutoff = 0.4f; ///< Cutoff frequency as fraction of Nyquist
6262
int output_buffer_size = 1024;
6363
};
@@ -293,11 +293,11 @@ class DSFDecoder : public AudioDecoder {
293293
// parse the data
294294
parseFMT(data + fmtPos, len - fmtPos);
295295
parseData(data + dataPos, len - dataPos);
296+
headerParsed = true;
296297

297298
// update audio info and initialize filters
298299
setAudioInfo(meta);
299300

300-
headerParsed = true;
301301
return dataPos + sizeof(DSFDataHeader);
302302
}
303303

@@ -397,26 +397,29 @@ class DSFDecoder : public AudioDecoder {
397397

398398
// Add integrated value to channel accumulator
399399
channelAccum[ch] += channelIntegrator[ch];
400+
samplesProcessed += 8;
400401
}
401402
}
402403
}
403404

404-
for (int ch = 0; ch < meta.channels; ch++) {
405-
// store max_value to scale to max 1.0
406-
if (channelAccum[ch] > max_value) {
407-
max_value = channelAccum[ch];
408-
}
405+
float samplesPerChannel = samplesProcessed / meta.channels;
406+
407+
if (samplesPerChannel > 0) {
408+
for (int ch = 0; ch < meta.channels; ch++) {
409+
// Normalize by sample count and apply scaling factor
410+
channelAccum[ch] = channelAccum[ch] / samplesPerChannel * 0.8f;
409411

410-
// Apply low-pass filter to remove high-frequency noise
411-
channelAccum[ch] = channelFilters[ch].process(channelAccum[ch]);
412-
// Serial.print(channelAccum[ch]/max_value);
413-
// Serial.print(" ");
412+
// Apply low-pass filter to remove high-frequency noise
413+
channelAccum[ch] = channelFilters[ch].process(channelAccum[ch]);
414+
//Serial.print(channelAccum[ch]);
415+
//Serial.print(" ");
414416

415-
// Convert to PCM sample and store in buffer
416-
writePCMSample(clip(channelAccum[ch] / max_value));
417+
// Convert to PCM sample and store in buffer
418+
writePCMSample(clip(channelAccum[ch]));
419+
}
417420
}
418421

419-
// Serial.println();
422+
//Serial.println();
420423

421424
// Output the PCM samples for all channels
422425
if (pcmBuffer.isFull()) {

src/AudioTools/CoreAudio/AudioFilter/Filter.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,7 @@ class LowPassFilter : public BiQuadDF2<T> {
313313
begin(frequency, sampleRate, q);
314314
}
315315
void begin(float frequency, float sampleRate, float q = 0.7071f) {
316-
T w0 = frequency * (2.0f * 3.141592654f / sampleRate);
316+
T w0 = frequency * (2.0f * PI / sampleRate);
317317
T sinW0 = sin(w0);
318318
T alpha = sinW0 / ((float)q * 2.0);
319319
T cosW0 = cos(w0);
@@ -347,7 +347,7 @@ class HighPassFilter : public BiQuadDF2<T> {
347347
begin(frequency, sampleRate, q);
348348
}
349349
void begin(float frequency, float sampleRate, float q = 0.7071) {
350-
T w0 = frequency * (2.0f * 3.141592654f / sampleRate);
350+
T w0 = frequency * (2.0f * PI / sampleRate);
351351
T sinW0 = sin(w0);
352352
T alpha = sinW0 / ((float)q * 2.0);
353353
T cosW0 = cos(w0);
@@ -381,7 +381,7 @@ class BandPassFilter : public BiQuadDF2<T> {
381381
begin(frequency, sampleRate, q);
382382
}
383383
void begin(float frequency, float sampleRate, float q = 1.0) {
384-
T w0 = frequency * (2.0f * 3.141592654f / sampleRate);
384+
T w0 = frequency * (2.0f * PI / sampleRate);
385385
T sinW0 = sin(w0);
386386
T alpha = sinW0 / ((T)q * 2.0);
387387
T cosW0 = cos(w0);
@@ -416,7 +416,7 @@ class NotchFilter : public BiQuadDF2<T> {
416416
}
417417

418418
void begin(float frequency, float sampleRate, float q = 1.0) {
419-
T w0 = frequency * (2.0f * 3.141592654f / sampleRate);
419+
T w0 = frequency * (2.0f * PI / sampleRate);
420420
T sinW0 = sin(w0);
421421
T alpha = sinW0 / ((float)q * 2.0);
422422
T cosW0 = cos(w0);
@@ -454,7 +454,7 @@ class LowShelfFilter : public BiQuadDF2<T> {
454454
void begin(float frequency, float sampleRate, float gain,
455455
float slope = 1.0f) {
456456
T a = pow(10.0, gain / 40.0f);
457-
T w0 = frequency * (2.0f * 3.141592654f / sampleRate);
457+
T w0 = frequency * (2.0f * PI / sampleRate);
458458
T sinW0 = sin(w0);
459459
// float alpha = (sinW0 * sqrt((a+1/a)*(1/slope-1)+2) ) / 2.0;
460460
T cosW0 = cos(w0);
@@ -496,7 +496,7 @@ class HighShelfFilter : public BiQuadDF2<T> {
496496
void begin(float frequency, float sampleRate, float gain,
497497
float slope = 1.0f) {
498498
T a = pow(10.0, gain / 40.0f);
499-
T w0 = frequency * (2.0f * 3.141592654f / sampleRate);
499+
T w0 = frequency * (2.0f * PI / sampleRate);
500500
T sinW0 = sin(w0);
501501
// float alpha = (sinW0 * sqrt((a+1/a)*(1/slope-1)+2) ) / 2.0;
502502
T cosW0 = cos(w0);

0 commit comments

Comments
 (0)