Skip to content

Commit d0e2de0

Browse files
committed
DRAFT CodecDSF
1 parent adb4530 commit d0e2de0

File tree

1 file changed

+15
-16
lines changed

1 file changed

+15
-16
lines changed

src/AudioTools/AudioCodecs/CodecDSF.h

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -144,11 +144,6 @@ class DSFDecoder : public AudioDecoder {
144144
channelAccum.resize(meta.channels);
145145
channelIntegrator.resize(meta.channels);
146146

147-
// Initialize integrator states
148-
for (int i = 0; i < meta.channels; i++) {
149-
channelIntegrator[i] = 0.0f;
150-
}
151-
152147
setupTargetPCMRate();
153148
setupDecimationStep();
154149
}
@@ -171,6 +166,7 @@ class DSFDecoder : public AudioDecoder {
171166
dataSize = 0;
172167
filePos = 0;
173168
decimationStep = 64;
169+
max_value = 0;
174170

175171
// update decimaten step & filter parameters
176172
isActive = true;
@@ -256,6 +252,7 @@ class DSFDecoder : public AudioDecoder {
256252

257253
// Metadata
258254
DSFMetadata meta; ///< Extracted DSF file metadata
255+
float max_value = 0.0f;
259256

260257
/// The buffer size is defined in the metadata: it must be at least 1 frame
261258
int getOutputBufferSize() {
@@ -373,6 +370,10 @@ class DSFDecoder : public AudioDecoder {
373370
for (int ch = 0; ch < meta.channels; ch++) {
374371
channelAccum[ch] = 0.0f;
375372
}
373+
// Initialize integrator states
374+
for (int i = 0; i < meta.channels; i++) {
375+
channelIntegrator[i] = 0.0f;
376+
}
376377

377378
// Accumulate DSD samples over decimation period
378379
// DSF uses byte interleaving: bytes alternate between channels
@@ -397,28 +398,26 @@ class DSFDecoder : public AudioDecoder {
397398

398399
// Add integrated value to channel accumulator
399400
channelAccum[ch] += channelIntegrator[ch];
400-
samplesProcessed += 8;
401401
}
402402
}
403403
}
404404

405-
// Average and normalize the accumulated values for each channel
406-
float samplesPerChannel = samplesProcessed / meta.channels;
407-
408405
for (int ch = 0; ch < meta.channels; ch++) {
409-
if (samplesPerChannel > 0) {
410-
// Normalize by sample count and apply scaling factor
411-
channelAccum[ch] = clip(channelAccum[ch] / (samplesPerChannel * 8.0f));
406+
// store max_value to scale to max 1.0
407+
if (channelAccum[ch] > max_value){
408+
max_value = channelAccum[ch];
412409
}
413410

414411
// Apply low-pass filter to remove high-frequency noise
415-
if (ch < channelFilters.size()) {
416-
channelAccum[ch] = channelFilters[ch].process(channelAccum[ch]);
417-
}
412+
channelAccum[ch] = channelFilters[ch].process(channelAccum[ch]);
413+
// Serial.print(channelAccum[ch]/max_value);
414+
// Serial.print(" ");
418415

419416
// Convert to PCM sample and store in buffer
420-
writePCMSample(clip(channelAccum[ch]));
417+
writePCMSample(clip(channelAccum[ch]/max_value));
421418
}
419+
420+
// Serial.println();
422421

423422
// Output the PCM samples for all channels
424423
if (pcmBuffer.isFull()) {

0 commit comments

Comments
 (0)