@@ -16,7 +16,7 @@ namespace audio_tools {
1616class DecoderALAC : public AudioDecoder {
1717 public:
1818 // / write Magic Cookie (ALACSpecificConfig)
19- size_t writeCodecInfo (const uint8_t * data, size_t len) override {
19+ size_t writeCodecConfig (const uint8_t * data, size_t len) override {
2020 int32_t rc = dec.Init ((void *)data, len);
2121 if (rc != 0 ) {
2222 LOGE (" Init failed" );
@@ -32,8 +32,13 @@ class DecoderALAC : public AudioDecoder {
3232 }
3333
3434 // define ALACSpecificConfig
35- size_t writeCodecInfo (ALACSpecificConfig& config) {
36- return writeCodecInfo ((uint8_t *)&config, sizeof (config));
35+ size_t writeCodecConfig (ALACSpecificConfig config) {
36+ return writeCodecConfig ((uint8_t *)&config, sizeof (config));
37+ }
38+
39+ bool begin (ALACSpecificConfig config) {
40+ size_t written = writeCodecConfig (config);
41+ return written == sizeof (ALACSpecificConfig);
3742 }
3843
3944 // / we expect the write is called for a complete frame!
@@ -46,7 +51,7 @@ class DecoderALAC : public AudioDecoder {
4651 config.bitDepth = info.bits_per_sample ;
4752 config.numChannels = info.channels ;
4853 config.sampleRate = info.sample_rate ;
49- writeCodecInfo ((uint8_t *)&config, sizeof (config));
54+ writeCodecConfig ((uint8_t *)&config, sizeof (config));
5055 is_init = true ;
5156 }
5257 // Make sure we have the output buffer set up
@@ -72,7 +77,10 @@ class DecoderALAC : public AudioDecoder {
7277 // Process result
7378 size_t outputSize =
7479 outNumSamples * dec.mConfig .numChannels * dec.mConfig .bitDepth / 8 ;
75- p_print->write (result_buffer.data (), outputSize);
80+ size_t written = p_print->write (result_buffer.data (), outputSize);
81+ if (outputSize != written) {
82+ LOGE (" write error: %d -> %d" , outputSize, written);
83+ }
7684 return frameLength;
7785 }
7886
@@ -97,27 +105,33 @@ class DecoderALAC : public AudioDecoder {
97105 */
98106class EncoderALAC : public AudioEncoder {
99107 public:
108+ EncoderALAC (int bytesPerPacket = 1024 ) {
109+ setDefaultBytesPerPacket (bytesPerPacket);
110+ }
100111 void setOutput (Print& out_stream) override { p_print = &out_stream; };
101112
102113 bool begin () override {
103114 if (p_print == nullptr ) {
104115 LOGE (" No output stream set" );
105116 return false ;
106117 }
118+ // define input format
107119 input_format.mSampleRate = info.sample_rate ;
108120 input_format.mFormatID = kALACFormatLinearPCM ;
109121 input_format.mFormatFlags = kALACFormatFlagIsSignedInteger ;
110122 input_format.mBytesPerPacket = default_bytes_per_packet;
111- input_format.mFramesPerPacket = 0 ;
112123 input_format.mBytesPerFrame = info.channels * info.bits_per_sample / 8 ;
124+ input_format.mFramesPerPacket =
125+ default_bytes_per_packet / input_format.mBytesPerFrame ;
113126 input_format.mChannelsPerFrame = info.channels ;
114127 input_format.mBitsPerChannel = info.bits_per_sample ;
115- int rc = enc.InitializeEncoder (input_format);
116128
117129 // define output format
118130 out_format = input_format;
119131 out_format.mFormatID = kALACFormatAppleLossless ;
120132
133+ int rc = enc.InitializeEncoder (out_format);
134+
121135 in_buffer.resize (default_bytes_per_packet);
122136 out_buffer.resize (default_bytes_per_packet);
123137 is_started = rc == 0 ;
@@ -131,7 +145,8 @@ class EncoderALAC : public AudioEncoder {
131145
132146 // / Encode the audio samples into ALAC format
133147 size_t write (const uint8_t * data, size_t len) override {
134- int32_t ioNumBytes;
148+ LOGI (" write: %d" , (int )len);
149+ int32_t ioNumBytes = len;
135150 for (int j = 0 ; j < len; j++) {
136151 in_buffer.write (data[j]);
137152 if (in_buffer.isFull ()) {
0 commit comments