arduino-audio-tools
MaximilianDSP.h
1 #pragma once
2 #include "AudioConfig.h"
3 #include "maximilian.h"
4 #include "libs/maxiClock.h"
5 
6 // Maximilian play function - return an array of 2 channels
7 void play(maxi_float_t *channels);//run dac!
8 void play1(maxi_float_t *channels);//run dac!
9 void play2(maxi_float_t *channels);//run dac!
10 
11 namespace audio_tools {
12 
17 class Maximilian : public VolumeSupport {
18  public:
19 
20  Maximilian(Print &out, int bufferSize=DEFAULT_BUFFER_SIZE, void (*callback)(maxi_float_t *channels)=play){
21  buffer_size = bufferSize;
22  p_sink = &out;
23  this->callback = callback;
24  }
25 
26  ~Maximilian() {
27  }
28 
30  void begin(AudioInfo cfg){
31  this->cfg = cfg;
32  buffer.resize(buffer_size);
33  maxiSettings::setup(cfg.sample_rate, cfg.channels, DEFAULT_BUFFER_SIZE);
34  }
35 
37  bool setVolume(float f) override{
38  if (f>1.0f){
40  return false;
41  }
42  if (f<0.0f){
44  return false;
45  }
47  return true;
48  }
49 
51  void copy() {
52  // fill buffer with data
53  maxi_float_t out[cfg.channels];
54  uint16_t samples = buffer_size / sizeof(uint16_t);
55  int16_t *p_samples = (int16_t *) buffer.data();
56  for (uint16_t j=0;j<samples;j+=cfg.channels){
57  callback(out);
58  // convert all channels to int16
59  for (int ch=0;ch<cfg.channels;ch++){
60  p_samples[j+ch] = volume() * out[ch] * 32767.0f;
61  }
62  }
63  // write buffer to audio sink
64  unsigned int result = p_sink->write(buffer.data(), buffer_size);
65  LOGI("bytes written %u", result)
66  }
67 
68  protected:
69  Vector<uint8_t> buffer;
70  int buffer_size=256;
71  Print *p_sink=nullptr;
72  AudioInfo cfg;
73  void (*callback)(maxi_float_t *channels);
74 };
75 
76 
77 } // namespace
AudioTools integration with Maximilian.
Definition: MaximilianDSP.h:17
void copy()
Copies the audio data from maximilian to the audio sink, Call this method from the Arduino Loop.
Definition: MaximilianDSP.h:51
bool setVolume(float f) override
Defines the volume. The values are between 0.0 and 1.0.
Definition: MaximilianDSP.h:37
void begin(AudioInfo cfg)
Setup Maximilian with audio parameters.
Definition: MaximilianDSP.h:30
Definition: NoArduino.h:58
Supports the setting and getting of the volume.
Definition: AudioTypes.h:207
virtual float volume()
provides the actual volume in the range of 0.0f to 1.0f
Definition: AudioTypes.h:210
virtual bool setVolume(float volume)
define the actual volume in the range of 0.0f to 1.0f
Definition: AudioTypes.h:212
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