arduino-audio-tools
Loading...
Searching...
No Matches
CodecDSF.h
Go to the documentation of this file.
19#pragma once
20#pragma GCC optimize("O3")
21
25
26namespace audio_tools {
27
41struct DSFMetadata : public AudioInfo {
42 DSFMetadata() = default;
43 DSFMetadata(int rate) { sample_rate = rate; }
51 float duration_sec = 0;
55 float filter_cutoff = 0.45f;
59 int output_buffer_size = 2 * 1024;
61 bool is_raw = false;
62};
63
72struct __attribute__((packed)) DSDPrefix {
73 char id[4]; // "DSD "
74 uint64_t chunkSize; // 28
75 uint64_t fileSize; // total file size
76 uint64_t metadataOffset; // offset to "ID3 " chunk (0 if none)
77};
78
88struct __attribute__((packed)) DSFFormat {
89 char id[4]; // "fmt "
90 uint64_t chunkSize; // 52
91 uint32_t formatVersion; // 1
92 uint32_t formatID; // 0
93 uint32_t channelType; // e.g., 2 for stereo
94 uint32_t channelNum; // number of channels
95 uint32_t samplingFrequency; // e.g., 2822400
96 uint32_t bitsPerSample; // 1
97 uint64_t sampleCount; // total samples per channel
98 uint32_t blockSizePerChannel; // e.g., 4096
99 uint32_t reserved; // 0
100};
101
111struct __attribute__((packed)) DSFDataHeader {
112 char id[4]; // "data"
113 uint64_t chunkSize; // size of DSD data
114};
115
134class DSFDecoder : public AudioDecoder {
135 public:
136 DSFDecoder() = default;
137 DSFDecoder(DSFMetadata metaData) { setMetaData(metaData); };
138
139 const char* mime() override { return "audio/dsf"; }
140
141 AudioInfo audioInfo() override { return meta; }
142
143 void setAudioInfo(AudioInfo from) override {
144 TRACED();
146 meta.copyFrom(from);
147 if (isHeaderAvailable()) {
148 int buffer_size = getOutputBufferSize();
149 pcmBuffer.resize(buffer_size);
150
151 int perChBufSize = meta.block_size_per_channel * 2;
153 for (int ch = 0; ch < meta.channels; ch++) {
154 channelDsdBuffers[ch].resize(perChBufSize);
155 }
156
157 setupFilters();
159 }
160 }
161
162 bool begin() {
163 TRACED();
164 headerParsed = false;
165 blockPos = 0;
166 decimationStep = 64;
167 isActive = true;
168 return true;
169 }
170
171 void end() override { isActive = false; }
172
173 const DSFMetadata getMetadata() { return meta; }
174
175 void setMetaData(DSFMetadata metaData) {
176 meta = metaData;
178 }
179
181 void setInfoCallback(void (*callback)(const DSFFormat& fmt)) {
182 infoCallback = callback;
183 }
184
186
187 operator bool() { return isActive; }
188
189 size_t write(const uint8_t* data, size_t len) {
190 LOGD("write: %u", (unsigned)len);
191 size_t i = 0;
192
193 i += processHeader(data, len);
194
195 if (headerParsed && i < len) {
196 i += processDSDData(data, len, i);
197 }
198
199 return len;
200 }
201
202 protected:
203 bool headerParsed = false;
204 bool isActive = false;
206 void (*infoCallback)(const DSFFormat& fmt) = nullptr;
207
212
214
216 int frame_size = meta.bits_per_sample / 8 * meta.channels;
217 if (meta.bits_per_sample == 24) frame_size = 4 * meta.channels;
218 int buffer_size = frame_size;
219 if (meta.output_buffer_size > buffer_size)
220 buffer_size = meta.output_buffer_size;
221 return buffer_size;
222 }
223
224 size_t processHeader(const uint8_t* data, size_t len) {
225 if (headerParsed) return 0;
226 LOGI("processHeader: %u", (unsigned)len);
227
228 if (memcmp(data, "DSD ", 4) != 0) {
229 LOGE("Invalid DSF header magic");
230 return 0;
231 }
232
233 int dataPos = findTag("data", data, len);
234 int fmtPos = findTag("fmt ", data, len);
235 if (dataPos < 0 || fmtPos < 0) {
236 LOGE("DSF header not found in data (fmt: %d, data: %d)", fmtPos, dataPos);
237 return 0;
238 }
239
240 parseFMT(data + fmtPos, len - fmtPos);
241 parseData(data + dataPos, len - dataPos);
242 headerParsed = true;
243
244 if (infoCallback) {
245 infoCallback(*reinterpret_cast<const DSFFormat*>(data + fmtPos));
246 }
247
248 if (!meta.is_raw) {
250 }
251
252 return dataPos + sizeof(DSFDataHeader);
253 }
254
255 size_t processDSDData(const uint8_t* data, size_t len, size_t startPos) {
256 LOGD("processDSDData: %u (%u)", (unsigned)len, (unsigned)startPos);
257
258 if (meta.is_raw) {
259 size_t rawLen = len - startPos;
260 writeBlocking(getOutput(),(uint8_t*) data + startPos, rawLen);
261 return rawLen;
262 }
263
264 size_t totalProcessed = 0;
265 size_t pos = startPos;
266
267 while (pos < len) {
268 size_t buffered = bufferDSDData(data, len, pos);
269 if (buffered == 0) break;
270 pos += buffered;
271 totalProcessed += buffered;
273 }
274
275 return totalProcessed;
276 }
277
278 size_t bufferDSDData(const uint8_t* data, size_t len, size_t startPos) {
279 size_t consumed = 0;
281
282 for (size_t i = startPos; i < len; i++) {
284 if (ch >= meta.channels) {
285 blockPos = 0;
286 ch = 0;
287 }
288 if (channelDsdBuffers[ch].availableForWrite() <= 0) break;
289 channelDsdBuffers[ch].write(data[i]);
290 consumed++;
291 blockPos++;
292 if (blockPos >= blockSetSize) blockPos = 0;
293 }
294 return consumed;
295 }
296
298 int bytesPerDecimation = decimationStep / 8;
299 int stages = getFilterStages();
300
301 while (allChannelsHaveData(bytesPerDecimation)) {
302 for (int ch = 0; ch < meta.channels; ch++) {
303 int ones = 0;
304 for (int b = 0; b < bytesPerDecimation; b++) {
305 uint8_t dsdByte;
306 channelDsdBuffers[ch].read(dsdByte);
307 ones += popcount8(dsdByte);
308 }
309
310 float pcm = 2.0f * ones / decimationStep - 1.0f;
311
312 for (int s = 0; s < stages; s++) {
313 pcm = channelFilters[ch * stages + s].process(pcm);
314 }
315
316 writePCMSample(clip(pcm));
317 }
318
319 if (pcmBuffer.isFull()) {
320 size_t frameSize = pcmBuffer.available();
321 writeBlocking(getOutput(), (uint8_t*)pcmBuffer.data(), frameSize);
323 }
324 }
325
326 // Flush remaining PCM samples
327 if (pcmBuffer.available() > 0) {
331 }
332
333 // Compact linear buffers so write space is reclaimed
334 for (int ch = 0; ch < meta.channels; ch++) {
335 channelDsdBuffers[ch].trim();
336 }
337 }
338
339 bool allChannelsHaveData(int bytesNeeded) {
340 for (int ch = 0; ch < meta.channels; ch++) {
341 if (channelDsdBuffers[ch].available() < bytesNeeded) return false;
342 }
343 return true;
344 }
345
347 v = (v & 0x55) + ((v >> 1) & 0x55);
348 v = (v & 0x33) + ((v >> 2) & 0x33);
349 return (v & 0x0F) + ((v >> 4) & 0x0F);
350 }
351
352 float clip(float value) {
353 if (value > 1.0f) return 1.0f;
354 if (value < -1.0f) return -1.0f;
355 return value;
356 }
357
359 int stages = meta.filter_stages;
360 if (stages < 0) stages = 0;
361 if (stages > 3) stages = 3;
362 return stages;
363 }
364
366 TRACEI();
367 int stages = getFilterStages();
368 if (stages == 0 || meta.sample_rate <= 0 || meta.channels <= 0) return;
369
370 float cutoffFreq = meta.sample_rate * meta.filter_cutoff;
371 channelFilters.resize(meta.channels * stages);
372
373 // Butterworth Q values for maximally-flat passband
374 static const float butterworthQ[][3] = {
375 {0.7071f, 0, 0},
376 {0.5412f, 1.3066f, 0},
377 {0.5176f, 0.7071f, 1.9319f},
378 };
379
380 for (int ch = 0; ch < meta.channels; ch++) {
381 for (int s = 0; s < stages; s++) {
382 float q = butterworthQ[stages - 1][s];
383 channelFilters[ch * stages + s].begin(cutoffFreq, meta.sample_rate, q);
384 }
385 }
386 }
387
389 TRACEI();
390 if (meta.sample_rate == 0 || meta.dsd_sample_rate == 0) {
391 LOGE("Invalid sample rates: DSD=%u, PCM=%u",
392 (unsigned)meta.dsd_sample_rate, (unsigned)meta.sample_rate);
393 return;
394 }
395
397 if (decimationStep < 64) {
398 LOGW("Decimation step %u too low, setting to 64",
399 (unsigned)decimationStep);
400 decimationStep = 64;
401 }
402 if (decimationStep > 512) {
403 LOGW("Decimation step %u too high, setting to 512",
404 (unsigned)decimationStep);
405 decimationStep = 512;
406 }
407
408 decimationStep = (decimationStep / 8) * 8;
409 if (decimationStep < 64) decimationStep = 64;
410
411 LOGI("Decimation step set to %u for DSD rate %u and target PCM rate %u",
412 (unsigned)decimationStep, (unsigned)meta.dsd_sample_rate,
413 (unsigned)meta.sample_rate);
414 }
415
416 void writePCMSample(float filteredValue) {
417 switch (meta.bits_per_sample) {
418 case 8: {
419 int8_t buffer8 = NumberConverter::fromFloatT<int8_t>(filteredValue);
420 pcmBuffer.write(buffer8);
421 break;
422 }
423 case 16: {
424 int16_t buffer16 = NumberConverter::fromFloatT<int16_t>(filteredValue);
425 pcmBuffer.writeArray((uint8_t*)&buffer16, sizeof(int16_t));
426 break;
427 }
428 case 24: {
429 // clip to the int24 range (-8388608..8388607) before the cast
430 float scaled24 = filteredValue * 8388607.0f;
431 if (scaled24 > 8388607.0f) scaled24 = 8388607.0f;
432 if (scaled24 < -8388608.0f) scaled24 = -8388608.0f;
433 int24_t buffer24 = static_cast<int24_t>(scaled24);
434 pcmBuffer.writeArray((uint8_t*)&buffer24, sizeof(int24_t));
435 break;
436 }
437 case 32: {
438 // 1.0f * 2147483647.0f is 2^31, which does not fit, so clip
439 int32_t buffer32 =
440 NumberConverter::fromFloatT<int32_t>(filteredValue);
441 pcmBuffer.writeArray((uint8_t*)&buffer32, sizeof(int32_t));
442 break;
443 }
444 default:
445 LOGE("Unsupported bits per sample: %d", meta.bits_per_sample);
446 break;
447 }
448 }
449
450 int findTag(const char* tag, const uint8_t* data, size_t len) {
451 int taglen = strlen(tag);
452 for (size_t j = 0; j + taglen <= len; j++) {
453 if (memcmp(tag, data + j, taglen) == 0) {
454 return j;
455 }
456 }
457 return -1;
458 }
459
460 bool parseFMT(const uint8_t* data, size_t len) {
461 TRACEI();
462 if (len < sizeof(DSFFormat)) {
463 LOGE("FMT section too short to parse DSF format header");
464 return false;
465 }
466 DSFFormat* fmt = (DSFFormat*)data;
467 meta.channels = fmt->channelNum;
468 if (meta.channels == 0) meta.channels = fmt->channelType;
469 meta.dsd_sample_rate = fmt->samplingFrequency;
470 meta.block_size_per_channel = fmt->blockSizePerChannel;
472
473 if (meta.channels == 0 || meta.channels > 8) {
474 LOGE("Invalid channel count: %u (must be 1-8)", (unsigned)meta.channels);
475 return false;
476 }
477
478 LOGI("channels: %u, DSD sample rate: %u, block size: %u",
479 (unsigned)meta.channels, (unsigned)meta.dsd_sample_rate,
480 (unsigned)meta.block_size_per_channel);
481 return true;
482 }
483
484 bool parseData(const uint8_t* data, size_t len) {
485 TRACEI();
486 if (len < sizeof(DSFDataHeader)) {
487 LOGE("Data section too short to parse DSF data header");
488 return false;
489 }
490 DSFDataHeader* header = (DSFDataHeader*)data;
491 meta.dsd_data_bytes = header->chunkSize;
492
493 uint64_t totalBits = meta.dsd_data_bytes * 8;
494 uint64_t totalDSDSamples = totalBits / meta.channels;
495 uint64_t totalPCMFrames =
496 totalDSDSamples / (meta.dsd_sample_rate / meta.sample_rate);
497 meta.pcm_frames = totalPCMFrames;
498 meta.duration_sec = (float)totalPCMFrames / meta.sample_rate;
499 return true;
500 }
501};
502
519class DSFEncoder : public AudioEncoder {
520 public:
521 DSFEncoder() = default;
522 DSFEncoder(DSFMetadata metaData) { setMetaData(metaData); }
523
524 const char* mime() override { return "audio/dsf"; }
525
526 void setOutput(Print& out) override { p_print = &out; }
527
528 void setAudioInfo(AudioInfo from) override {
530 meta.copyFrom(from);
531 }
532
533 void setMetaData(DSFMetadata metaData) {
534 meta = metaData;
536 }
537
538 const DSFMetadata getMetadata() { return meta; }
539
540 bool begin() override {
541 TRACED();
543
546
548 for (int ch = 0; ch < meta.channels; ch++) {
550 }
551
552 chState.resize(meta.channels);
553 for (int ch = 0; ch < meta.channels; ch++) {
554 chState[ch] = ChannelModState();
555 }
556
557 totalDsdBytes = 0;
558 headerWritten = false;
559 isOpen = true;
560 return true;
561 }
562
563 void end() override {
564 if (!isOpen) return;
566 isOpen = false;
567 }
568
569 size_t write(const uint8_t* data, size_t len) override {
570 if (!isOpen || p_print == nullptr) return 0;
571
572 if (!headerWritten) {
574 headerWritten = true;
575 }
576
577 int bytesPerSample = meta.bits_per_sample / 8;
578 if (meta.bits_per_sample == 24) bytesPerSample = 4;
579 int frameSize = bytesPerSample * meta.channels;
580 if (frameSize == 0) return 0;
581
582 size_t pos = 0;
583 while (pos + frameSize <= len) {
584 for (int ch = 0; ch < meta.channels; ch++) {
585 float sample = readPCMSample(data + pos + ch * bytesPerSample);
586 convertSampleToDSD(ch, sample);
587 }
588 pos += frameSize;
589
590 if (allBlocksFull()) {
592 }
593 }
594
595 return pos;
596 }
597
598 operator bool() override { return isOpen; }
599
600 protected:
602 float dsError1 = 0;
603 float dsError2 = 0;
604 float prevSample = 0;
606 int bitPos = 0;
607 };
608
610 Print* p_print = nullptr;
615 bool headerWritten = false;
616 bool isOpen = false;
617
618 float readPCMSample(const uint8_t* ptr) {
619 switch (meta.bits_per_sample) {
620 case 8:
621 return *reinterpret_cast<const int8_t*>(ptr) / 127.0f;
622 case 16: {
623 int16_t v;
624 memcpy(&v, ptr, sizeof(int16_t));
625 return v / 32767.0f;
626 }
627 case 24: {
628 int24_t v;
629 memcpy(&v, ptr, sizeof(int24_t));
630 return static_cast<float>(static_cast<int32_t>(v)) / 8388607.0f;
631 }
632 case 32: {
633 int32_t v;
634 memcpy(&v, ptr, sizeof(int32_t));
635 return v / 2147483647.0f;
636 }
637 default:
638 return 0.0f;
639 }
640 }
641
642 void convertSampleToDSD(int ch, float currentSample) {
643 float prevSample = chState[ch].prevSample;
644
645 for (uint32_t i = 0; i < oversamplingRatio; i++) {
646 // Linear interpolation between previous and current PCM sample
647 float t = (float)i / oversamplingRatio;
648 // no level reduction: the decoder maps the bit density back to the same
649 // level, so a PCM full scale of 1.0 decodes to about 1.0
650 float input = prevSample + t * (currentSample - prevSample);
651
652 // 2nd order error-feedback delta-sigma: NTF = (1 - z^-1)^2
653 float shaped = input + 2.0f * chState[ch].dsError1 - chState[ch].dsError2;
654 int bit = (shaped >= 0.0f) ? 1 : 0;
655 float feedback = bit ? 1.0f : -1.0f;
656 float quantError = shaped - feedback;
657
658 if (quantError > 4.0f) quantError = 4.0f;
659 if (quantError < -4.0f) quantError = -4.0f;
660
661 chState[ch].dsError2 = chState[ch].dsError1;
662 chState[ch].dsError1 = quantError;
663
664 // LSB first for DSF format
665 if (bit) {
666 chState[ch].currentByte |= (1 << chState[ch].bitPos);
667 }
668 chState[ch].bitPos++;
669
670 if (chState[ch].bitPos >= 8) {
671 channelBlocks[ch].write(chState[ch].currentByte);
672 chState[ch].currentByte = 0;
673 chState[ch].bitPos = 0;
674 }
675 }
676
677 chState[ch].prevSample = currentSample;
678 }
679
681 for (int ch = 0; ch < meta.channels; ch++) {
682 if (!channelBlocks[ch].isFull()) return false;
683 }
684 return true;
685 }
686
688 for (int ch = 0; ch < meta.channels; ch++) {
690 channelBlocks[ch].available());
691 totalDsdBytes += channelBlocks[ch].available();
692 channelBlocks[ch].reset();
693 }
694 }
695
697 bool hasData = false;
698 for (int ch = 0; ch < meta.channels; ch++) {
699 if (channelBlocks[ch].available() > 0 || chState[ch].bitPos > 0) {
700 hasData = true;
701 break;
702 }
703 }
704 if (!hasData) return;
705
706 for (int ch = 0; ch < meta.channels; ch++) {
707 if (chState[ch].bitPos > 0) {
708 channelBlocks[ch].write(chState[ch].currentByte);
709 chState[ch].currentByte = 0;
710 chState[ch].bitPos = 0;
711 }
712 }
713
714 // Pad to full block size with DSD silence (alternating bit pattern)
715 for (int ch = 0; ch < meta.channels; ch++) {
716 while (!channelBlocks[ch].isFull()) {
717 channelBlocks[ch].write(0x69);
718 }
719 }
721 }
722
724 uint64_t dsdDataBytes = meta.dsd_data_bytes;
725 bool streaming = (dsdDataBytes == 0);
726 uint64_t headerTotal =
727 sizeof(DSDPrefix) + sizeof(DSFFormat) + sizeof(DSFDataHeader);
728
729 DSDPrefix prefix;
730 memcpy(prefix.id, "DSD ", 4);
731 prefix.chunkSize = sizeof(DSDPrefix);
732 prefix.fileSize = streaming ? 0 : (headerTotal + dsdDataBytes);
733 prefix.metadataOffset = 0;
734
735 DSFFormat fmt;
736 memcpy(fmt.id, "fmt ", 4);
737 fmt.chunkSize = sizeof(DSFFormat);
738 fmt.formatVersion = 1;
739 fmt.formatID = 0;
740 fmt.channelType = meta.channels;
741 fmt.channelNum = meta.channels;
742 fmt.samplingFrequency = meta.dsd_sample_rate;
743 fmt.bitsPerSample = 1;
744 fmt.sampleCount = streaming ? 0 : (dsdDataBytes * 8 / meta.channels);
745 fmt.blockSizePerChannel = meta.block_size_per_channel;
746 fmt.reserved = 0;
747
748 DSFDataHeader dataHdr;
749 memcpy(dataHdr.id, "data", 4);
750 dataHdr.chunkSize = streaming ? 0 : (sizeof(DSFDataHeader) + dsdDataBytes);
751
752 p_print->write((uint8_t*)&prefix, sizeof(prefix));
753 p_print->write((uint8_t*)&fmt, sizeof(fmt));
754 p_print->write((uint8_t*)&dataHdr, sizeof(dataHdr));
755 }
756};
757
758} // namespace audio_tools
#define LOGW(...)
Definition AudioLoggerIDF.h:29
#define TRACEI()
Definition AudioLoggerIDF.h:32
#define TRACED()
Definition AudioLoggerIDF.h:31
#define LOGI(...)
Definition AudioLoggerIDF.h:28
#define LOGD(...)
Definition AudioLoggerIDF.h:27
#define LOGE(...)
Definition AudioLoggerIDF.h:30
Definition Arduino.h:56
virtual size_t write(const uint8_t *data, size_t len)
Definition Arduino.h:120
Decoding of encoded audio into PCM data.
Definition AudioCodecsBase.h:19
Print * getOutput()
Definition AudioCodecsBase.h:68
void setAudioInfo(AudioInfo from) override
for most decoders this is not needed
Definition AudioCodecsBase.h:32
Encoding of PCM data.
Definition AudioCodecsBase.h:101
void setAudioInfo(AudioInfo from) override
Defines the sample rate, number of channels and bits per sample.
Definition AudioCodecsBase.h:110
virtual int frameSize()
Definition AudioCodecsBase.h:126
void writeBlocking(Print *out, uint8_t *data, size_t len)
Definition AudioTypes.h:225
Decodes DSF files containing DSD audio and converts to PCM.
Definition CodecDSF.h:134
uint32_t blockPos
Definition CodecDSF.h:205
bool isHeaderAvailable()
Definition CodecDSF.h:185
void setMetaData(DSFMetadata metaData)
Definition CodecDSF.h:175
bool headerParsed
Definition CodecDSF.h:203
void(* infoCallback)(const DSFFormat &fmt)
Definition CodecDSF.h:206
int getFilterStages()
Definition CodecDSF.h:358
DSFMetadata meta
Definition CodecDSF.h:213
void convertDSDToPCM()
Definition CodecDSF.h:297
int getOutputBufferSize()
Definition CodecDSF.h:215
void setAudioInfo(AudioInfo from) override
for most decoders this is not needed
Definition CodecDSF.h:143
Vector< SingleBuffer< uint8_t > > channelDsdBuffers
Definition CodecDSF.h:209
bool begin()
Definition CodecDSF.h:162
size_t processDSDData(const uint8_t *data, size_t len, size_t startPos)
Definition CodecDSF.h:255
void end() override
Definition CodecDSF.h:171
const DSFMetadata getMetadata()
Definition CodecDSF.h:173
float clip(float value)
Definition CodecDSF.h:352
int popcount8(uint8_t v)
Definition CodecDSF.h:346
DSFDecoder(DSFMetadata metaData)
Definition CodecDSF.h:137
void setupDecimationStep()
Definition CodecDSF.h:388
bool allChannelsHaveData(int bytesNeeded)
Definition CodecDSF.h:339
SingleBuffer< uint8_t > pcmBuffer
Definition CodecDSF.h:208
bool isActive
Definition CodecDSF.h:204
bool parseData(const uint8_t *data, size_t len)
Definition CodecDSF.h:484
const char * mime() override
Provides the mime type of the data that is expected by this decoder.
Definition CodecDSF.h:139
int findTag(const char *tag, const uint8_t *data, size_t len)
Definition CodecDSF.h:450
uint32_t decimationStep
Definition CodecDSF.h:211
Vector< LowPassFilter< float > > channelFilters
Definition CodecDSF.h:210
size_t bufferDSDData(const uint8_t *data, size_t len, size_t startPos)
Definition CodecDSF.h:278
void writePCMSample(float filteredValue)
Definition CodecDSF.h:416
bool parseFMT(const uint8_t *data, size_t len)
Definition CodecDSF.h:460
AudioInfo audioInfo() override
provides the actual input AudioInfo
Definition CodecDSF.h:141
size_t processHeader(const uint8_t *data, size_t len)
Definition CodecDSF.h:224
void setInfoCallback(void(*callback)(const DSFFormat &fmt))
Register a callback that receives the raw DSFFormat header after parsing.
Definition CodecDSF.h:181
void setupFilters()
Definition CodecDSF.h:365
size_t write(const uint8_t *data, size_t len)
Definition CodecDSF.h:189
DSF (DSD Stream File) format encoder.
Definition CodecDSF.h:519
void setOutput(Print &out) override
Default output assignment (encoders may override to store Print reference)
Definition CodecDSF.h:526
Vector< ChannelModState > chState
Definition CodecDSF.h:612
uint32_t oversamplingRatio
Definition CodecDSF.h:613
void setMetaData(DSFMetadata metaData)
Definition CodecDSF.h:533
DSFMetadata meta
Definition CodecDSF.h:609
void flushPartialBlocks()
Definition CodecDSF.h:696
void convertSampleToDSD(int ch, float currentSample)
Definition CodecDSF.h:642
void setAudioInfo(AudioInfo from) override
Defines the sample rate, number of channels and bits per sample.
Definition CodecDSF.h:528
bool headerWritten
Definition CodecDSF.h:615
void end() override
Definition CodecDSF.h:563
const DSFMetadata getMetadata()
Definition CodecDSF.h:538
size_t write(const uint8_t *data, size_t len) override
Definition CodecDSF.h:569
bool isOpen
Definition CodecDSF.h:616
float readPCMSample(const uint8_t *ptr)
Definition CodecDSF.h:618
bool allBlocksFull()
Definition CodecDSF.h:680
uint64_t totalDsdBytes
Definition CodecDSF.h:614
const char * mime() override
Provides the mime type of the encoded result.
Definition CodecDSF.h:524
void writeBlockSet()
Definition CodecDSF.h:687
DSFEncoder(DSFMetadata metaData)
Definition CodecDSF.h:522
bool begin() override
Definition CodecDSF.h:540
Print * p_print
Definition CodecDSF.h:610
Vector< SingleBuffer< uint8_t > > channelBlocks
Definition CodecDSF.h:611
void writeDSFHeader()
Definition CodecDSF.h:723
A simple Buffer implementation which just uses a (dynamically sized) array.
Definition Buffers.h:194
bool write(T sample) override
write add an entry to the buffer
Definition Buffers.h:228
int available() override
provides the number of entries that are available to read
Definition Buffers.h:255
bool isFull() override
checks if the buffer is full
Definition Buffers.h:262
int writeArray(const T data[], int len) override
Fills the buffer data.
Definition Buffers.h:223
T * data()
Provides address of actual data.
Definition Buffers.h:306
bool resize(size_t size)
Resizes the buffer if supported: returns false if not supported.
Definition Buffers.h:327
void reset() override
clears the buffer
Definition Buffers.h:308
Vector implementation which provides the most important methods as defined by std::vector....
Definition Vector.h:21
24bit integer which is used for I2S sound processing. The values are represented as int32_t,...
Definition int24_4bytes_t.h:22
Generic Implementation of sound input and output for desktop environments using portaudio.
Definition LMSEchoCancellationStream.h:6
int24_4bytes_t int24_t
Definition int24_t.h:12
Basic Audio information which drives e.g. I2S.
Definition AudioTypes.h:56
void copyFrom(AudioInfo info)
Same as set.
Definition AudioTypes.h:106
sample_rate_t sample_rate
Sample Rate: e.g 44100.
Definition AudioTypes.h:58
uint16_t channels
Number of channels: 2=stereo, 1=mono.
Definition AudioTypes.h:60
uint8_t bits_per_sample
Number of bits per sample (int16_t = 16 bits)
Definition AudioTypes.h:62
float prevSample
Definition CodecDSF.h:604
uint8_t currentByte
Definition CodecDSF.h:605
float dsError2
Definition CodecDSF.h:603
float dsError1
Definition CodecDSF.h:602
int bitPos
Definition CodecDSF.h:606
Metadata structure for DSF (DSD Stream File) format.
Definition CodecDSF.h:41
uint64_t dsd_data_bytes
Total size of DSD data in bytes (0 for streaming mode)
Definition CodecDSF.h:47
bool is_raw
When true, output de-interleaved DSD bitstream instead of converting to PCM.
Definition CodecDSF.h:61
int output_buffer_size
PCM output buffer size in bytes (must be >= one frame)
Definition CodecDSF.h:59
uint32_t dsd_sample_rate
DSD sample rate in Hz (e.g. 2822400 for DSD64, 5644800 for DSD128)
Definition CodecDSF.h:45
uint32_t block_size_per_channel
DSF block size per channel in bytes (from file header, typically 4096)
Definition CodecDSF.h:53
int filter_stages
Number of cascaded Butterworth biquad filter stages (1-3), 0 to disable filtering.
Definition CodecDSF.h:57
uint64_t pcm_frames
Estimated number of PCM frames after DSD-to-PCM conversion.
Definition CodecDSF.h:49
float filter_cutoff
Anti-aliasing filter cutoff as fraction of sample_rate (~0.45 = 90% of Nyquist at 44....
Definition CodecDSF.h:55
DSFMetadata(int rate)
Definition CodecDSF.h:43
float duration_sec
Approximate audio duration in seconds.
Definition CodecDSF.h:51
enum VBanDataTypeList __attribute__