arduino-audio-tools
MetaData.h
1 #pragma once
2 #include <stdlib.h>
3 #include "AudioConfig.h"
4 #include "AudioTools/CoreAudio/AudioTypes.h"
5 #include "AudioTools/CoreAudio/AudioStreams.h"
6 #include "AudioTools/CoreAudio/AudioHttp/HttpRequest.h"
7 #include "MetaDataICY.h"
8 #include "MetaDataID3.h"
9 
16 namespace audio_tools {
17 
26 class MetaDataOutput : public AudioOutput {
27  public:
28 
29  MetaDataOutput() = default;
30  MetaDataOutput(MetaDataOutput const&) = delete;
31  MetaDataOutput& operator=(MetaDataOutput const&) = delete;
32 
33  ~MetaDataOutput(){
34  end();
35  if (meta!=nullptr) delete meta;
36  }
37 
39  virtual void setCallback(void (*fn)(MetaDataType info, const char* str, int len)) {
40  TRACED();
41  callback = fn;
42  }
43 
44 #ifdef USE_URL_ARDUINO
45 
47  virtual void begin(HttpRequest &http) {
48  TRACED();
49  ICYUrlSetup icySetup;
50  int metaInt = icySetup.setup(http);
51  icySetup.executeCallback(callback);
52  begin(metaInt);
53  }
54 #endif
55 
57  virtual void begin(int iceMetaint=0) {
58  LOGD("%s: %d", LOG_METHOD, iceMetaint);
59  if (callback!=nullptr){
60  if (meta == nullptr) {
61 #if defined(USE_URL_ARDUINO)
62  meta = (iceMetaint > 0) ? new MetaDataICY() : (AbstractMetaData *) new MetaDataID3();
63 #else
64  meta = new MetaDataID3();
65 #endif
66  }
67  meta->setCallback(callback);
68  meta->setIcyMetaInt(iceMetaint);
69  meta->begin();
70  } else {
71  LOGI("callback not defined -> not Metadata processing")
72  }
73  }
74 
75  virtual void end() {
76  if (callback!=nullptr && meta != nullptr) {
77  TRACED();
78  meta->end();
79  }
80  }
81 
83  virtual size_t write(const uint8_t *data, size_t len){
84  LOGD("%s: %d", LOG_METHOD, (int)len);
85 
86  if (callback!=nullptr){
87  if (meta!=nullptr){
88  //CHECK_MEMORY();
89  if (meta->write(data, len)!=len){
90  LOGE("Did not write all data");
91  }
92  //CHECK_MEMORY();
93  } else {
94  LOGW("meta is null");
95  }
96  }
97  return len;
98  }
99 
100  virtual size_t write(uint8_t c) {
101  LOGE("Not Supported");
102  return 0;
103  }
104 
105  protected:
106  AbstractMetaData *meta=nullptr;
107  void (*callback)(MetaDataType info, const char* str, int len)=nullptr;
108 
109 };
110 
111 // legacy name
112 #if USE_OBSOLETE
113 using MetaDataPrint = MetaDataOutput;
114 #endif
115 
116 }
Common Metadata methods.
Definition: AbstractMetaData.h:34
Abstract Audio Ouptut class.
Definition: AudioOutput.h:22
Simple API to process get, put, post, del http requests I tried to use Arduino HttpClient,...
Definition: HttpRequest.h:26
Resolve icy-metaint from HttpRequest and execute metadata callbacks.
Definition: MetaDataICY.h:259
int setup(HttpRequest &http)
Fills the metaint from the Http Request and executes metadata callbacks on http reply parameters.
Definition: MetaDataICY.h:262
void executeCallback(void(*callback)(MetaDataType info, const char *str, int len))
Executes the metadata callbacks with data provided from the http request result parameter.
Definition: MetaDataICY.h:277
Icecast/Shoutcast Metadata Handling. Metadata class which splits the data into audio and metadata....
Definition: MetaDataICY.h:26
Simple ID3 Meta Data Parser which supports ID3 V1 and V2 and implements the Stream interface....
Definition: MetaDataID3.h:550
ID3 and Icecast/Shoutcast metadata output support. Just write the audio data to an object of this cla...
Definition: MetaData.h:26
virtual size_t write(const uint8_t *data, size_t len)
Provide tha audio data to the API to parse for Meta Data.
Definition: MetaData.h:83
virtual void begin(int iceMetaint=0)
Starts the processing - if iceMetaint is defined we use icecast.
Definition: MetaData.h:57
virtual void begin(HttpRequest &http)
Starts the processing - iceMetaint is determined from the HttpRequest.
Definition: MetaData.h:47
virtual void setCallback(void(*fn)(MetaDataType info, const char *str, int len))
Defines the callback.
Definition: MetaData.h:39
MetaDataType
Type of meta info.
Definition: AbstractMetaData.h:11
Generic Implementation of sound input and output for desktop environments using portaudio.
Definition: AudioConfig.h:823