arduino-audio-tools
Loading...
Searching...
No Matches
CodecAC3.h
Go to the documentation of this file.
1#pragma once
2
4#include "AC3Decoder.h"
5
6namespace audio_tools {
7
17 public:
18 AC3AudioDecoder() = default;
19
20 const char *mime() override { return "audio/ac3"; }
21
22 bool begin() override {
23 TRACEI();
24 is_active = ac3.begin();
25 if (is_active) {
26 ac3.setPCMCallback(pcmCallback, this);
27 if (output_channels != 0) ac3.setOutputChannels(output_channels);
28 }
29 return is_active;
30 }
31
32 void end() override {
33 TRACEI();
34 ac3.end();
35 is_active = false;
36 }
37
38 size_t write(const uint8_t *data, size_t len) override {
39 if (!is_active) return 0;
40 return ac3.write(data, len);
41 }
42
43 operator bool() override { return is_active; }
44
48 void setOutputChannels(int n) {
50 ac3.setOutputChannels(n);
51 }
52
53 protected:
54 ::AC3Decoder ac3;
55 bool is_active = false;
57
58 static void pcmCallback(const int16_t *pcm, int frameCount, int channels,
59 int sampleRate, void *ref) {
60 auto *self = (AC3AudioDecoder *)ref;
61 self->onPcm(pcm, frameCount, channels, sampleRate);
62 }
63
64 void onPcm(const int16_t *pcm, int frameCount, int channels,
65 int sampleRate) {
66 if (info.channels != channels || info.sample_rate != sampleRate) {
67 info.channels = channels;
68 info.sample_rate = sampleRate;
71 }
72 if (p_print == nullptr) return;
73 writeBlocking(p_print, (uint8_t *)pcm,
74 frameCount * channels * sizeof(int16_t));
75 }
76};
77
78} // namespace audio_tools
#define TRACEI()
Definition AudioLoggerIDF.h:32
AC-3 (Dolby Digital / ATSC A/52) Decoder using https://github.com/pschatzmann/codec-ac3.
Definition CodecAC3.h:16
bool is_active
Definition CodecAC3.h:55
void setOutputChannels(int n)
Definition CodecAC3.h:48
void end() override
Definition CodecAC3.h:32
size_t write(const uint8_t *data, size_t len) override
Definition CodecAC3.h:38
::AC3Decoder ac3
Definition CodecAC3.h:54
int output_channels
Definition CodecAC3.h:56
static void pcmCallback(const int16_t *pcm, int frameCount, int channels, int sampleRate, void *ref)
Definition CodecAC3.h:58
const char * mime() override
Provides the mime type of the data that is expected by this decoder.
Definition CodecAC3.h:20
bool begin() override
Definition CodecAC3.h:22
void onPcm(const int16_t *pcm, int frameCount, int channels, int sampleRate)
Definition CodecAC3.h:64
Decoding of encoded audio into PCM data.
Definition AudioCodecsBase.h:19
AudioInfo info
Definition AudioCodecsBase.h:80
Print * p_print
Definition AudioCodecsBase.h:79
void notifyAudioChange(AudioInfo info)
Definition AudioTypes.h:175
void writeBlocking(Print *out, uint8_t *data, size_t len)
Definition AudioTypes.h:220
Generic Implementation of sound input and output for desktop environments using portaudio.
Definition LMSEchoCancellationStream.h:6
sample_rate_t sample_rate
Sample Rate: e.g 44100.
Definition AudioTypes.h:53
uint16_t channels
Number of channels: 2=stereo, 1=mono.
Definition AudioTypes.h:55
uint8_t bits_per_sample
Number of bits per sample (int16_t = 16 bits)
Definition AudioTypes.h:57