@@ -16,7 +16,7 @@ namespace audio_tools {
16
16
class DecoderALAC : public AudioDecoder {
17
17
public:
18
18
// / 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 {
20
20
int32_t rc = dec.Init ((void *)data, len);
21
21
if (rc != 0 ) {
22
22
LOGE (" Init failed" );
@@ -32,8 +32,13 @@ class DecoderALAC : public AudioDecoder {
32
32
}
33
33
34
34
// 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);
37
42
}
38
43
39
44
// / we expect the write is called for a complete frame!
@@ -46,7 +51,7 @@ class DecoderALAC : public AudioDecoder {
46
51
config.bitDepth = info.bits_per_sample ;
47
52
config.numChannels = info.channels ;
48
53
config.sampleRate = info.sample_rate ;
49
- writeCodecInfo ((uint8_t *)&config, sizeof (config));
54
+ writeCodecConfig ((uint8_t *)&config, sizeof (config));
50
55
is_init = true ;
51
56
}
52
57
// Make sure we have the output buffer set up
@@ -72,7 +77,10 @@ class DecoderALAC : public AudioDecoder {
72
77
// Process result
73
78
size_t outputSize =
74
79
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
+ }
76
84
return frameLength;
77
85
}
78
86
@@ -97,27 +105,33 @@ class DecoderALAC : public AudioDecoder {
97
105
*/
98
106
class EncoderALAC : public AudioEncoder {
99
107
public:
108
+ EncoderALAC (int bytesPerPacket = 1024 ) {
109
+ setDefaultBytesPerPacket (bytesPerPacket);
110
+ }
100
111
void setOutput (Print& out_stream) override { p_print = &out_stream; };
101
112
102
113
bool begin () override {
103
114
if (p_print == nullptr ) {
104
115
LOGE (" No output stream set" );
105
116
return false ;
106
117
}
118
+ // define input format
107
119
input_format.mSampleRate = info.sample_rate ;
108
120
input_format.mFormatID = kALACFormatLinearPCM ;
109
121
input_format.mFormatFlags = kALACFormatFlagIsSignedInteger ;
110
122
input_format.mBytesPerPacket = default_bytes_per_packet;
111
- input_format.mFramesPerPacket = 0 ;
112
123
input_format.mBytesPerFrame = info.channels * info.bits_per_sample / 8 ;
124
+ input_format.mFramesPerPacket =
125
+ default_bytes_per_packet / input_format.mBytesPerFrame ;
113
126
input_format.mChannelsPerFrame = info.channels ;
114
127
input_format.mBitsPerChannel = info.bits_per_sample ;
115
- int rc = enc.InitializeEncoder (input_format);
116
128
117
129
// define output format
118
130
out_format = input_format;
119
131
out_format.mFormatID = kALACFormatAppleLossless ;
120
132
133
+ int rc = enc.InitializeEncoder (out_format);
134
+
121
135
in_buffer.resize (default_bytes_per_packet);
122
136
out_buffer.resize (default_bytes_per_packet);
123
137
is_started = rc == 0 ;
@@ -131,7 +145,8 @@ class EncoderALAC : public AudioEncoder {
131
145
132
146
// / Encode the audio samples into ALAC format
133
147
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;
135
150
for (int j = 0 ; j < len; j++) {
136
151
in_buffer.write (data[j]);
137
152
if (in_buffer.isFull ()) {
0 commit comments