arduino-audio-tools
CodecMP3LAME.h
1 #pragma once
2 
3 #include "AudioTools/AudioCodecs/AudioCodecsBase.h"
4 #include "MP3EncoderLAME.h"
5 
6 namespace audio_tools {
7 
13 struct AudioInfoLAME : public liblame::AudioInfo {
14  AudioInfoLAME () {
15  sample_rate = 44100;
16  channels = 2;
17  bits_per_sample = 16;
18  };
19  AudioInfoLAME (const AudioInfoLAME &) = default;
20 
21  int quality = 7; // 0..9. 0=best (very slow). 9=worst.
22 };
23 
32 class MP3EncoderLAME : public AudioEncoder {
33 
34 public:
36  TRACED();
37  }
38 
39  MP3EncoderLAME(Print &out_stream){
40  TRACED();
41  p_print = &out_stream;
42  }
43 
44  ~MP3EncoderLAME(){
45  TRACED();
46  end();
47  }
48 
50  void setOutput(Print &out_stream){
51  TRACED();
52  p_print = &out_stream;
53  if (enc!=nullptr){
54  enc->setOutput(out_stream);
55  }
56  }
57 
59  void setAudioInfo(AudioInfo from) {
60  TRACED();
62  lame_info.channels = from.channels;
63  lame_info.sample_rate = from.sample_rate;
64  lame_info.bits_per_sample = from.bits_per_sample;
65  }
66 
69  TRACED();
70  lame_info = from;
71  }
72 
73  bool begin(AudioInfoLAME from) {
74  setAudioInfo(from);
75  return begin();
76  }
77 
78  // starts the processing
79  bool begin() {
80  createEnc();
81  if (enc==nullptr) return false;
82  enc->begin();
83  return true;
84  }
85 
86 
87  AudioInfoLAME &audioInfoExt(){
88  return lame_info;
89  }
90 
91  AudioInfoLAME defaultConfig(){
92  AudioInfoLAME def;
93  return def;
94  }
95 
96  // convert PCM data to convert into MP3
97  size_t write(const uint8_t *data, size_t len){
98  if (enc==nullptr) return 0;
99  LOGD("write %d bytes", (int) len);
100  return enc->write((uint8_t*)data, len);
101  }
102 
103  // release resources
104  void end(){
105  TRACED();
106  if (enc!=nullptr){
107  enc->end();
108  delete enc;
109  enc = nullptr;
110  }
111  }
112 
113  liblame::MP3EncoderLAME *driver() {
114  return enc;
115  }
116 
117  const char *mime() {
118  return "audio/mp3";
119  }
120 
121  virtual operator bool() {
122  return enc!=nullptr && (bool)(*enc);
123  }
124 
125 protected:
126  liblame::MP3EncoderLAME *enc=nullptr;
127  AudioInfoLAME lame_info;
128  Print *p_print=nullptr;
129 
130  // Create enc only at begin so that we can use psram
131  void createEnc(){
132  TRACED();
133  if (enc==nullptr){
134  enc = new liblame::MP3EncoderLAME();
135  if (p_print!=nullptr){
136  setOutput(*p_print);
137  } else {
138  LOGE("Output undefined");
139  }
140  LOGI("LibLAME channels: %d", lame_info.channels);
141  LOGI("LibLAME sample_rate: %d", lame_info.sample_rate);
142  LOGI("LibLAME bits_per_sample: %d", lame_info.bits_per_sample);
143  LOGI("LibLAME quality: %d", lame_info.quality);
144  enc->setAudioInfo(lame_info);
145  }
146  }
147 
148 };
149 
150 }
Encoding of PCM data.
Definition: AudioCodecsBase.h:84
void setAudioInfo(AudioInfo from) override
Defines the sample rate, number of channels and bits per sample.
Definition: AudioCodecsBase.h:93
Encodes PCM data to the MP3 format and writes the result to a stream This is basically just a wrapper...
Definition: CodecMP3LAME.h:32
const char * mime()
Provides the mime type of the encoded result.
Definition: CodecMP3LAME.h:117
void setAudioInfo(AudioInfoLAME from)
Defines the Audio Info.
Definition: CodecMP3LAME.h:68
void setAudioInfo(AudioInfo from)
Defines the Audio Info.
Definition: CodecMP3LAME.h:59
void setOutput(Print &out_stream)
Defines the output stream.
Definition: CodecMP3LAME.h:50
Definition: NoArduino.h:58
Generic Implementation of sound input and output for desktop environments using portaudio.
Definition: AudioConfig.h:823
Basic Audio information which drives e.g. I2S.
Definition: AudioTypes.h:52
sample_rate_t sample_rate
Sample Rate: e.g 44100.
Definition: AudioTypes.h:55
uint16_t channels
Number of channels: 2=stereo, 1=mono.
Definition: AudioTypes.h:57
uint8_t bits_per_sample
Number of bits per sample (int16_t = 16 bits)
Definition: AudioTypes.h:59
LAME parameters.
Definition: CodecMP3LAME.h:13