@@ -144,11 +144,6 @@ class DSFDecoder : public AudioDecoder {
144
144
channelAccum.resize (meta.channels );
145
145
channelIntegrator.resize (meta.channels );
146
146
147
- // Initialize integrator states
148
- for (int i = 0 ; i < meta.channels ; i++) {
149
- channelIntegrator[i] = 0 .0f ;
150
- }
151
-
152
147
setupTargetPCMRate ();
153
148
setupDecimationStep ();
154
149
}
@@ -171,6 +166,7 @@ class DSFDecoder : public AudioDecoder {
171
166
dataSize = 0 ;
172
167
filePos = 0 ;
173
168
decimationStep = 64 ;
169
+ max_value = 0 ;
174
170
175
171
// update decimaten step & filter parameters
176
172
isActive = true ;
@@ -256,6 +252,7 @@ class DSFDecoder : public AudioDecoder {
256
252
257
253
// Metadata
258
254
DSFMetadata meta; // /< Extracted DSF file metadata
255
+ float max_value = 0 .0f ;
259
256
260
257
// / The buffer size is defined in the metadata: it must be at least 1 frame
261
258
int getOutputBufferSize () {
@@ -373,6 +370,10 @@ class DSFDecoder : public AudioDecoder {
373
370
for (int ch = 0 ; ch < meta.channels ; ch++) {
374
371
channelAccum[ch] = 0 .0f ;
375
372
}
373
+ // Initialize integrator states
374
+ for (int i = 0 ; i < meta.channels ; i++) {
375
+ channelIntegrator[i] = 0 .0f ;
376
+ }
376
377
377
378
// Accumulate DSD samples over decimation period
378
379
// DSF uses byte interleaving: bytes alternate between channels
@@ -397,28 +398,26 @@ class DSFDecoder : public AudioDecoder {
397
398
398
399
// Add integrated value to channel accumulator
399
400
channelAccum[ch] += channelIntegrator[ch];
400
- samplesProcessed += 8 ;
401
401
}
402
402
}
403
403
}
404
404
405
- // Average and normalize the accumulated values for each channel
406
- float samplesPerChannel = samplesProcessed / meta.channels ;
407
-
408
405
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];
412
409
}
413
410
414
411
// 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(" ");
418
415
419
416
// Convert to PCM sample and store in buffer
420
- writePCMSample (clip (channelAccum[ch]));
417
+ writePCMSample (clip (channelAccum[ch]/max_value ));
421
418
}
419
+
420
+ // Serial.println();
422
421
423
422
// Output the PCM samples for all channels
424
423
if (pcmBuffer.isFull ()) {
0 commit comments