Skip to content

Commit 5149c88

Browse files
committed
rename writeCodecInfo to writeCodecConfig
1 parent b9623c5 commit 5149c88

File tree

4 files changed

+81
-10
lines changed

4 files changed

+81
-10
lines changed
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/**
2+
* @file test-codec-alac.ino
3+
* @author Phil Schatzmann
4+
* @brief generate sine wave -> encoder -> decoder -> audiokit (i2s)
5+
* @version 0.1
6+
*
7+
* @copyright Copyright (c) 2025
8+
*
9+
*/
10+
#include "AudioTools.h"
11+
#include "AudioTools/AudioCodecs/CodecALAC.h"
12+
#include "AudioTools/AudioLibs/AudioBoardStream.h"
13+
14+
SET_LOOP_TASK_STACK_SIZE(16*1024); // 16KB
15+
16+
AudioInfo info(8000, 1, 16);
17+
SineWaveGenerator<int16_t> sineWave( 32000); // subclass of SoundGenerator with max amplitude of 32000
18+
GeneratedSoundStream<int16_t> sound( sineWave); // Stream generated from sine wave
19+
AudioBoardStream out(AudioKitEs8388V1);
20+
//I2SStream out;
21+
//CsvOutput<int16_t> out(Serial);
22+
EncoderALAC enc_alac(1024 * 4);
23+
DecoderALAC dec_alac;
24+
EncodedAudioStream decoder(&out, &dec_alac); // encode and write
25+
EncodedAudioStream encoder(&decoder, &enc_alac); // encode and write
26+
StreamCopy copier(encoder, sound);
27+
28+
void setup() {
29+
Serial.begin(115200);
30+
AudioToolsLogger.begin(Serial, AudioToolsLogLevel::Debug);
31+
32+
// start I2S
33+
Serial.println("starting I2S...");
34+
auto cfgi = out.defaultConfig(TX_MODE);
35+
cfgi.copyFrom(info);
36+
out.begin(cfgi);
37+
38+
// Setup sine wave
39+
sineWave.begin(info, N_B4);
40+
41+
// start encoder
42+
encoder.begin(info);
43+
44+
// start decoder
45+
decoder.begin();
46+
47+
// copy config from encoder to decoder
48+
dec_alac.writeCodecInfo(enc_alac.config());
49+
50+
Serial.println("Test started...");
51+
}
52+
53+
54+
void loop() {
55+
copier.copy();
56+
}

src/AudioTools/AudioCodecs/AudioCodecsBase.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ class AudioDecoder : public AudioWriter, public AudioInfoSource {
6666
}
6767

6868
/// Some decoders need e.g. a magic cookie to provide the relevant info for decoding
69-
virtual size_t writeCodecInfo(const uint8_t* data, size_t len){
69+
virtual size_t writeCodecConfig(const uint8_t* data, size_t len){
7070
LOGE("not implemented");
7171
return 0;
7272
}

src/AudioTools/AudioCodecs/CodecALAC.h

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ namespace audio_tools {
1616
class 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
*/
98106
class 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()) {

src/AudioTools/AudioCodecs/ContainerM4A.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -470,7 +470,7 @@ class ContainerM4A : public ContainerDecoder {
470470
// Notify decoder about the codec info if available
471471
if (p_decoder){
472472
if (p_decoder->selectDecoder(mime())){
473-
p_decoder->writeCodecInfo(alac_config, alac_config_size);
473+
p_decoder->writeCodecConfig(alac_config, alac_config_size);
474474
}
475475
}
476476
}

0 commit comments

Comments
 (0)