arduino-audio-tools
Video.h
1 #pragma once
2 #include "AudioTools/CoreAudio/AudioOutput.h"
3 #include "AudioTools/CoreAudio/Buffers.h"
4 #include "stdint.h"
5 
12 namespace audio_tools {
13 
21 class VideoOutput {
22  public:
23  virtual void beginFrame(size_t size) = 0;
24  virtual size_t write(const uint8_t *data, size_t len) = 0;
25  virtual uint32_t endFrame() = 0;
26 };
27 
38  public:
40  virtual void writeAudio(Print *out, uint8_t *data, size_t size) {
41  out->write(data, size);
42  }
43 
46  virtual void delayVideoFrame(int32_t microsecondsPerFrame,
47  uint32_t time_used_ms) {
48  uint32_t delay_ms = microsecondsPerFrame / 1000;
49  delay(delay_ms);
50  }
51 };
52 
64  public:
65  VideoAudioBufferedSync(int bufferSize, int correctionMs) {
66  ring_buffer.resize(bufferSize);
67  correction_ms = correctionMs;
68  }
69 
71  void writeAudio(Print *out, uint8_t *data, size_t size) {
72  p_out = out;
73  if (ring_buffer.availableForWrite() < size) {
74  int bytes_to_play = size - ring_buffer.availableForWrite();
75  uint8_t audio[bytes_to_play];
76  ring_buffer.readArray(audio, bytes_to_play);
77  p_out->write(audio, bytes_to_play);
78  }
79  size_t written = ring_buffer.writeArray(data, size);
80  assert(written = size);
81  }
82 
85  void delayVideoFrame(int32_t microsecondsPerFrame, uint32_t time_used_ms) {
86  uint32_t delay_ms = microsecondsPerFrame / 1000;
87  uint64_t timeout = millis() + delay_ms + correction_ms;
88  uint8_t audio[8];
89  // output audio
90  while (millis() < timeout) {
91  ring_buffer.readArray(audio, 8);
92  p_out->write(audio, 8);
93  }
94  }
95 
96  protected:
97  RingBuffer<uint8_t> ring_buffer{0};
98  Print *p_out = nullptr;
99  int correction_ms = 0;
100 };
101 
102 } // namespace audio_tools
virtual int readArray(T data[], int len)
reads multiple values
Definition: Buffers.h:41
virtual int writeArray(const T data[], int len)
Fills the buffer data.
Definition: Buffers.h:65
Definition: NoArduino.h:58
virtual int availableForWrite()
provides the number of entries that are available to write
Definition: Buffers.h:369
Logic to Synchronize video and audio output: we use a buffer to store the audio and instead of delayi...
Definition: Video.h:63
void delayVideoFrame(int32_t microsecondsPerFrame, uint32_t time_used_ms)
Definition: Video.h:85
void writeAudio(Print *out, uint8_t *data, size_t size)
Process the audio data.
Definition: Video.h:71
Logic to Synchronize video and audio output: This is the minimum implementatin which actually does no...
Definition: Video.h:37
virtual void delayVideoFrame(int32_t microsecondsPerFrame, uint32_t time_used_ms)
Definition: Video.h:46
virtual void writeAudio(Print *out, uint8_t *data, size_t size)
Process the audio data.
Definition: Video.h:40
Abstract class for video playback. This class is used to assemble a complete video frame in memory.
Definition: Video.h:21
Generic Implementation of sound input and output for desktop environments using portaudio.
Definition: AudioConfig.h:823
uint32_t millis()
Returns the milliseconds since the start.
Definition: Time.h:12