@@ -13,31 +13,37 @@ namespace audio_tools {
1313 */
1414class ADPCMDecoder : public AudioDecoderExt {
1515 public:
16+ ADPCMDecoder () = default ;
17+
1618 ADPCMDecoder (AVCodecID id, int blockSize = ADAPCM_DEFAULT_BLOCK_SIZE) {
17- if (id == AV_CODEC_ID_ADPCM_IMA_AMV) {
18- info.sample_rate = 22050 ;
19- info.channels = 1 ;
20- info.bits_per_sample = 16 ;
21- }
22- p_decoder = adpcm_ffmpeg::ADPCMDecoderFactory::create (id);
19+ setBlockSize (blockSize);
20+ setId (id);
21+ }
22+
23+ // / Destructor
24+ ~ADPCMDecoder () {
25+ if (p_decoder) delete p_decoder;
26+ }
27+
28+ // (re) defines the codec id: set the block size first
29+ void setId (AVCodecID id) {
30+ codec_id = id;
2331 if (p_decoder != nullptr ) {
24- p_decoder->setCodecID (id);
25- p_decoder->setBlockSize (blockSize);
26- } else {
27- LOGE (" Decoder not implemented" );
32+ setImplementation ();
2833 }
2934 }
3035
31- // defines the block size
36+ // defines the block size (= size of encoded frame)
3237 void setBlockSize (int blockSize) override {
38+ block_size = blockSize;
3339 if (p_decoder == nullptr ) return ;
3440 p_decoder->setBlockSize (blockSize);
3541 }
3642
37- // / Provides the block size (size of encoded frame) (only available after
43+ // / Provides the block size (= size of encoded frame) (only available after
3844 // / calling begin)
3945 int blockSize () {
40- if (p_decoder == nullptr ) return 0 ;
46+ if (p_decoder == nullptr ) return block_size ;
4147 return p_decoder->blockSize ();
4248 }
4349
@@ -50,7 +56,9 @@ class ADPCMDecoder : public AudioDecoderExt {
5056
5157 bool begin () override {
5258 TRACEI ();
53- if (p_decoder == nullptr ) return false ;
59+ if (p_decoder == nullptr ) {
60+ setImplementation ();
61+ }
5462 if (is_started) return true ;
5563 current_byte = 0 ;
5664 LOGI (" sample_rate: %d, channels: %d" , info.sample_rate , info.channels );
@@ -98,7 +106,8 @@ class ADPCMDecoder : public AudioDecoderExt {
98106 Vector<uint8_t > adpcm_block;
99107 Print *p_print = nullptr ;
100108 int current_byte = 0 ;
101- int block_size = 0 ;
109+ int block_size = ADAPCM_DEFAULT_BLOCK_SIZE;
110+ AVCodecID codec_id = AV_CODEC_ID_ADPCM_MS;
102111 bool is_started = false ;
103112
104113 virtual bool decode (uint8_t byte) {
@@ -126,6 +135,29 @@ class ADPCMDecoder : public AudioDecoderExt {
126135 }
127136 return true ;
128137 }
138+
139+ // / change the decoder implementation
140+ void setImplementation () {
141+ // delete the old decoder
142+ if (p_decoder != nullptr ) {
143+ p_decoder->end ();
144+ delete p_decoder;
145+ p_decoder = nullptr ;
146+ }
147+
148+ if (codec_id == AV_CODEC_ID_ADPCM_IMA_AMV) {
149+ info.sample_rate = 22050 ;
150+ info.channels = 1 ;
151+ info.bits_per_sample = 16 ;
152+ }
153+ p_decoder = adpcm_ffmpeg::ADPCMDecoderFactory::create (codec_id);
154+ if (p_decoder != nullptr ) {
155+ p_decoder->setCodecID (codec_id);
156+ p_decoder->setBlockSize (block_size);
157+ } else {
158+ LOGE (" Decoder not implemented" );
159+ }
160+ }
129161};
130162
131163/* *
@@ -137,21 +169,33 @@ class ADPCMDecoder : public AudioDecoderExt {
137169 */
138170class ADPCMEncoder : public AudioEncoderExt {
139171 public:
172+ ADPCMEncoder () = default ;
173+
140174 ADPCMEncoder (AVCodecID id, int blockSize = ADAPCM_DEFAULT_BLOCK_SIZE) {
141- if (id == AV_CODEC_ID_ADPCM_IMA_AMV) {
142- info.sample_rate = 22050 ;
143- info.channels = 1 ;
144- info.bits_per_sample = 16 ;
145- }
146- p_encoder = adpcm_ffmpeg::ADPCMEncoderFactory::create (id);
175+ setId (id);
176+ setBlockSize (blockSize);
177+ }
178+
179+ // / Destructor
180+ ~ADPCMEncoder () {
181+ if (p_encoder != nullptr ) delete p_encoder;
182+ }
183+
184+ // / (re) defines the codec id
185+ void setId (AVCodecID id) {
186+ codec_id = id;
147187 if (p_encoder != nullptr ) {
148- p_encoder->setCodecID (id);
149- p_encoder->setBlockSize (blockSize);
150- } else {
151- LOGE (" Encoder not implemented" );
188+ setImplementation ();
152189 }
153190 }
154191
192+ // / (re) defines the block size
193+ void setBlockSize (int blockSize) {
194+ block_size = blockSize;
195+ if (p_encoder == nullptr ) return ;
196+ p_encoder->setBlockSize (blockSize);
197+ }
198+
155199 // / Provides the block size (size of encoded frame) (only available after
156200 // / calling begin)
157201 int blockSize () override {
@@ -168,7 +212,9 @@ class ADPCMEncoder : public AudioEncoderExt {
168212
169213 bool begin () override {
170214 TRACEI ();
171- if (p_encoder == nullptr ) return false ;
215+ if (p_encoder == nullptr ) {
216+ setImplementation ();
217+ };
172218 if (is_started) return true ;
173219 LOGI (" sample_rate: %d, channels: %d" , info.sample_rate , info.channels );
174220 p_encoder->begin (info.sample_rate , info.channels );
@@ -208,12 +254,15 @@ class ADPCMEncoder : public AudioEncoderExt {
208254 }
209255
210256 protected:
257+ AVCodecID codec_id = AV_CODEC_ID_ADPCM_MS;
211258 adpcm_ffmpeg::ADPCMEncoder *p_encoder = nullptr ;
212259 Vector<int16_t > pcm_block;
213260 Print *p_print = nullptr ;
214261 bool is_started = false ;
215262 int current_sample = 0 ;
216263 int total_samples = 0 ;
264+ int current_id = -1 ;
265+ int block_size = ADAPCM_DEFAULT_BLOCK_SIZE;
217266
218267 virtual bool encode (int16_t sample) {
219268 if (p_encoder == nullptr ) return false ;
@@ -237,6 +286,29 @@ class ADPCMEncoder : public AudioEncoderExt {
237286 }
238287 return true ;
239288 }
289+
290+ // / change the encoder implementation
291+ bool setImplementation () {
292+ // delete the old encoder
293+ if (p_encoder != nullptr ) {
294+ p_encoder->end ();
295+ delete p_encoder;
296+ p_encoder = nullptr ;
297+ }
298+
299+ if (codec_id == AV_CODEC_ID_ADPCM_IMA_AMV) {
300+ info.sample_rate = 22050 ;
301+ info.channels = 1 ;
302+ info.bits_per_sample = 16 ;
303+ }
304+ p_encoder = adpcm_ffmpeg::ADPCMEncoderFactory::create (codec_id);
305+ if (p_encoder != nullptr ) {
306+ p_encoder->setCodecID (codec_id);
307+ p_encoder->setBlockSize (block_size);
308+ } else {
309+ LOGE (" Encoder not implemented" );
310+ }
311+ }
240312};
241313
242314} // namespace audio_tools
0 commit comments