Arduino ESpeak-NG
audio_object.h
Go to the documentation of this file.
1 #pragma once
2 #include <stdio.h> // for snprintf
3 #include "Print.h"
4 #include "audio.h"
5 
9 struct audio_info {
10  int sample_rate = 22050; // undefined
11  int channels = 1; // undefined
12  int bits_per_sample=16; // we assume int16_t
13  Print *out=nullptr;
14 };
16 extern Print *audio_out;
17 
19 extern void(*audio_stream_factory_callback)(audio_info *cfg);
20 extern void(*audio_stream_start_callback)();
21 extern void(*audio_stream_stop_callback)();
22 
24 extern void espeak_set_audio_output(Print *p);
27 
34 struct audio_object {
35  enum audio_object_format format;
36  uint32_t rate;
37  uint8_t channels;
38  const char *device;
39  const char *application_name;
40  Print *p_out_stream = nullptr; // e.g. pointer to stream
42  bool active = false;
43 
44  int open(enum audio_object_format format,uint32_t rate, uint8_t channels){
45  ESPK_LOG("audio_object::open()\n");
46  this->format = format;
47  this->rate = rate;
48  this->channels = channels;
49 
50  // update audio information
56 
57  // callback with audio information
58  if (audio_stream_factory_callback!=nullptr){
62  }
63  }
64  if (audio_stream_start_callback!=nullptr){
66  }
67  return p_out_stream!=nullptr ? 0 : -1;
68  }
69 
70  void close(){
71  ESPK_LOG("audio_object::close()\n");
72  if (audio_stream_stop_callback!=nullptr){
74  }
75  active = false;
76  }
77 
78  int write(const void *data, size_t bytes){
79  if (!active && p_out_stream!=nullptr){
80  if (audio_stream_start_callback!=nullptr){
81  ESPK_LOG("audio_object::audio_stream_start_callback\n");
83  }
84  active = true;
85  }
86  if (p_out_stream==nullptr) return -1;
87  ESPK_LOG("audio_object::write(%d)\n", (int) bytes);
88  // write in batches of 512 bytes
89  int open = bytes;
90  int offset = 0;
91  while(open>0){
92  int to_write = open>1024? 1024 : open;
93  int result = p_out_stream->write(((uint8_t*)data)+offset, to_write);
94  offset+=to_write;
95  open-=to_write;
96  }
97  return 0;
98  }
99 
100  int drain(){
101  if (audio_stream_stop_callback!=nullptr){
102  ESPK_LOG("audio_object::audio_stream_stop_callback\n");
104  active = false;
105  } else {
106  // prevent some ending noise on the ESP32
107  uint8_t none[1024]={0};
108  for (int j=0;j<10;j++){
109  p_out_stream->write(none, 1024);
110  }
111  }
112  return 0;
113  }
114 
115  int flush(){
116  return 0;
117  }
118 
119  const char * strerror(int error){
120  static char errormsg[50] = {0};
121  snprintf(errormsg, 50, "Error %d", error);
122  return (const char*)errormsg;
123 
124  }
125 };
void(* audio_stream_start_callback)()
void espeak_set_audio_output(Print *p)
Defines the audio output.
audio_info espeak_audio_info
Print * audio_out
audio_info espeak_get_audio_info()
Provides the audio information.
void(* audio_stream_stop_callback)()
void(* audio_stream_factory_callback)(audio_info *cfg)
Callback to define the AudioStream.
info about sample rate and channels and output stream
Definition: audio_object.h:9
int channels
Definition: audio_object.h:11
int sample_rate
Definition: audio_object.h:10
int bits_per_sample
Definition: audio_object.h:12
Print * out
Definition: audio_object.h:13
pcaudiolib implementation for microcontrollers using the arduino Print assigned by espeak_set_audio_o...
Definition: audio_object.h:34
void close()
Definition: audio_object.h:70
bool active
Definition: audio_object.h:42
int drain()
Definition: audio_object.h:100
enum audio_object_format format
Definition: audio_object.h:35
int write(const void *data, size_t bytes)
Definition: audio_object.h:78
const char * strerror(int error)
Definition: audio_object.h:119
Print * p_out_stream
Definition: audio_object.h:40
const char * application_name
Definition: audio_object.h:39
int open(enum audio_object_format format, uint32_t rate, uint8_t channels)
Definition: audio_object.h:44
int flush()
Definition: audio_object.h:115
uint8_t channels
Definition: audio_object.h:37
const char * device
Definition: audio_object.h:38
uint32_t rate
Definition: audio_object.h:36
audio_info cfg
Definition: audio_object.h:41