Beatmup
Beatmup::Audio::BasicRealtimePlayback Class Referenceabstract

Realtime playback base. More...

#include <realtime_playback.h>

Inheritance diagram for Beatmup::Audio::BasicRealtimePlayback:
Beatmup::Audio::AbstractPlayback Beatmup::AbstractTask Beatmup::Object Beatmup::Audio::Android::AAudioPlayback Beatmup::Audio::Android::SLESPlayback

Public Member Functions

virtual ~BasicRealtimePlayback ()
 
void initialize (Mode mode)
 Initializes the playback setting its main parameters. More...
 
bool process (TaskThread &thread)
 Executes the task on CPU within a given thread. More...
 
virtual void start ()
 Starts playback. More...
 
virtual void stop ()=0
 Stops playback. More...
 
void bufferQueueCallbackFunc ()
 
void pullBuffer (sample8 *buffer, dtime numFrames)
 Called in pulling output mode to send data to output. More...
 
- Public Member Functions inherited from Beatmup::Audio::AbstractPlayback
ThreadIndex getMaxThreads () const
 Gives the upper limint on the number of threads the task may be performed by. More...
 
void beforeProcessing (ThreadIndex threadCount, ProcessingTarget target, GraphicPipeline *gpu)
 Instruction called before the task is executed. More...
 
void setSource (Source *source)
 Specifies a Source to sample. More...
 
SourcegetSource () const
 Returns the signal source to sample. More...
 
- Public Member Functions inherited from Beatmup::AbstractTask
virtual void afterProcessing (ThreadIndex threadCount, GraphicPipeline *gpu, bool aborted)
 Instruction called after the task is executed. More...
 
virtual bool processOnGPU (GraphicPipeline &gpu, TaskThread &thread)
 Executes the task on GPU. More...
 
virtual TaskDeviceRequirement getUsedDevices () const
 Communicates devices (CPU and/or GPU) the task is run on. More...
 
- Public Member Functions inherited from Beatmup::Object
virtual ~Object ()
 

Protected Types

enum class  OutputMode { PULL , PUSH }
 Specifies how the output audio signal is handled by the audio backend. More...
 

Protected Member Functions

 BasicRealtimePlayback (OutputMode)
 
virtual sample8createBuffer (const AbstractPlayback::Mode &mode) const
 Creates an atomic playing buffer. More...
 
virtual void freeBuffer (sample8 *buffer) const
 Frees an atomic playing buffer. More...
 
virtual void pushBuffer (sample8 *buffer, int bufferIndex)
 Pushes some data to the output. More...
 
- Protected Member Functions inherited from Beatmup::Audio::AbstractPlayback
 AbstractPlayback ()
 
void advanceTime ()
 Moves time pointer one buffer forward. More...
 

Protected Attributes

msize bufferSize
 size of each buffer in bytes More...
 
- Protected Attributes inherited from Beatmup::Audio::AbstractPlayback
Mode mode
 
Sourcesource
 
dtime clock
 

Private Member Functions

void prepareBuffers (const AbstractPlayback::Mode &mode)
 Allocates all the atomic buffers. More...
 
void freeBuffers ()
 

Private Attributes

const OutputMode outputMode
 
sample8 ** buffers
 buffers containing the data to play More...
 
int numBuffers
 
int fillIndex
 points to the buffer currently being filled More...
 
int sendIndex
 points to the first buffer ready for playing More...
 
int playIndex
 points to the buffer currently being played; updated only in the callback More...
 
dtime playingBufferOffset
 when pulling output, offset in frames with respect to the currently playing buffer More...
 
dtime skipFrames
 when pulling output, number of frames to skip to give some time to the rendering process More...
 

Additional Inherited Members

- Public Types inherited from Beatmup::AbstractTask
enum class  TaskDeviceRequirement { CPU_ONLY , GPU_OR_CPU , GPU_ONLY }
 Specifies which device (CPU and/or GPU) is used to run the task. More...
 
- Static Public Member Functions inherited from Beatmup::AbstractTask
static ThreadIndex validThreadCount (int number)
 Valid thread count from a given integer value. More...
 

Detailed Description

Realtime playback base.

Manages a circular buffer queue storing a signal sampled from a Source.

Definition at line 33 of file realtime_playback.h.

Member Enumeration Documentation

◆ OutputMode

Specifies how the output audio signal is handled by the audio backend.

Enumerator
PULL 

The output is requested by pullOutput(..) called by the audio backend.

PUSH 

The output is sent to pushOutput(..) called by the rendering thread.

Definition at line 38 of file realtime_playback.h.

38  {
39  PULL, //!< The output is requested by pullOutput(..) called by the audio backend.
40  PUSH, //!< The output is sent to pushOutput(..) called by the rendering thread.
41  };

Constructor & Destructor Documentation

◆ BasicRealtimePlayback()

BasicRealtimePlayback::BasicRealtimePlayback ( OutputMode  mode)
protected

Definition at line 26 of file realtime_playback.cpp.

26  : outputMode(mode), buffers(nullptr)
27 {}
sample8 ** buffers
buffers containing the data to play

◆ ~BasicRealtimePlayback()

BasicRealtimePlayback::~BasicRealtimePlayback ( )
virtual

Definition at line 30 of file realtime_playback.cpp.

Member Function Documentation

◆ prepareBuffers()

void BasicRealtimePlayback::prepareBuffers ( const AbstractPlayback::Mode mode)
private

Allocates all the atomic buffers.

Definition at line 45 of file realtime_playback.cpp.

45  {
46  freeBuffers();
48  buffers = new sample8*[numBuffers];
49  for (int i = 0; i < numBuffers; i++)
51 }
virtual sample8 * createBuffer(const AbstractPlayback::Mode &mode) const
Creates an atomic playing buffer.
unsigned char numBuffers
number of atomic buffers

◆ freeBuffers()

void BasicRealtimePlayback::freeBuffers ( )
private

Definition at line 35 of file realtime_playback.cpp.

35  {
36  if (buffers == nullptr)
37  return;
38  for (int i = 0; i < numBuffers; i++)
39  freeBuffer(buffers[i]);
40  delete[] buffers;
41  buffers = nullptr;
42 }
virtual void freeBuffer(sample8 *buffer) const
Frees an atomic playing buffer.

◆ createBuffer()

sample8 * BasicRealtimePlayback::createBuffer ( const AbstractPlayback::Mode mode) const
protectedvirtual

Creates an atomic playing buffer.

Parameters
modethe playback mode

Definition at line 54 of file realtime_playback.cpp.

54  {
55  sample8* buffer = (sample8*) malloc(bufferSize);
56  std::memset(buffer, 0, bufferSize);
57  return buffer;
58 }
msize bufferSize
size of each buffer in bytes

◆ freeBuffer()

void BasicRealtimePlayback::freeBuffer ( sample8 buffer) const
protectedvirtual

Frees an atomic playing buffer.

Definition at line 61 of file realtime_playback.cpp.

61  {
62  free(buffer);
63 }

◆ pushBuffer()

virtual void Beatmup::Audio::BasicRealtimePlayback::pushBuffer ( sample8 buffer,
int  bufferIndex 
)
inlineprotectedvirtual

Pushes some data to the output.

Reimplemented in Beatmup::Audio::Android::SLESPlayback.

Definition at line 96 of file realtime_playback.h.

96 {};

◆ initialize()

void BasicRealtimePlayback::initialize ( Mode  mode)
virtual

Initializes the playback setting its main parameters.

Reimplemented from Beatmup::Audio::AbstractPlayback.

Reimplemented in Beatmup::Audio::Android::SLESPlayback, and Beatmup::Audio::Android::AAudioPlayback.

Definition at line 66 of file realtime_playback.cpp.

66  {
68 
69  // set default buffer size
71 
72  // prepare buffers
74 }
virtual void initialize(Mode mode)
Initializes the playback setting its main parameters.
void prepareBuffers(const AbstractPlayback::Mode &mode)
Allocates all the atomic buffers.
uint32_t msize
memory size
Definition: basic_types.h:30
const int AUDIO_SAMPLE_SIZE[]
AudioSampleFormat sampleFormat
format of each sample
unsigned char numChannels
number of channels
dtime bufferLength
length of each atomic buffer in samples

◆ process()

bool BasicRealtimePlayback::process ( TaskThread thread)
virtual

Executes the task on CPU within a given thread.

Generally called by multiple threads.

Parameters
threadassociated task execution context
Returns
true if the execution is finished correctly, false otherwise

Implements Beatmup::AbstractTask.

Definition at line 85 of file realtime_playback.cpp.

85  {
86  if (!source)
87  return false;
88 
89  int myFillIndex = 0;
90 
91  while (!thread.isTaskAborted()) {
92  source->render(thread, buffers[myFillIndex % numBuffers], mode.bufferLength);
93  myFillIndex++;
94  if (thread.isManaging()) {
95  fillIndex = myFillIndex;
96  advanceTime();
97  // if pushing output mode, then push
100  sendIndex++;
101  }
102  // wait till some buffers are available
103  while (fillIndex - playIndex >= numBuffers)
104  std::this_thread::yield();
105  }
106  thread.synchronize();
107  }
108 
109  return true;
110 }
void advanceTime()
Moves time pointer one buffer forward.
virtual void pushBuffer(sample8 *buffer, int bufferIndex)
Pushes some data to the output.
@ PUSH
The output is sent to pushOutput(..) called by the rendering thread.
int playIndex
points to the buffer currently being played; updated only in the callback
int sendIndex
points to the first buffer ready for playing
int fillIndex
points to the buffer currently being filled
virtual void render(TaskThread &thread, sample8 *buffer, const dtime bufferLength)=0
Renders audio data to the target output buffer given by the user.
bool isManaging() const
Definition: parallelism.h:172
virtual void synchronize()=0
Blocks until all the other threads running the same task reach the same point.
virtual bool isTaskAborted() const =0
Returns true if the task is asked to stop from outside.

◆ start()

void BasicRealtimePlayback::start ( )
virtual

Starts playback.

Reimplemented in Beatmup::Audio::Android::SLESPlayback, and Beatmup::Audio::Android::AAudioPlayback.

Definition at line 77 of file realtime_playback.cpp.

77  {
78  // setting up control variables
82 }
dtime playingBufferOffset
when pulling output, offset in frames with respect to the currently playing buffer
dtime skipFrames
when pulling output, number of frames to skip to give some time to the rendering process

◆ stop()

virtual void Beatmup::Audio::BasicRealtimePlayback::stop ( )
pure virtual

◆ bufferQueueCallbackFunc()

void Beatmup::Audio::BasicRealtimePlayback::bufferQueueCallbackFunc ( )
inline

Definition at line 119 of file realtime_playback.h.

119  {
120  // a buffer is played, step forward
121  playIndex++;
122  }

◆ pullBuffer()

void BasicRealtimePlayback::pullBuffer ( sample8 buffer,
dtime  numFrames 
)

Called in pulling output mode to send data to output.

Parameters
bufferPointer to a buffer to put data to
numFramesNumber of "frames" (samples per single channel) to put to the buffer

Definition at line 113 of file realtime_playback.cpp.

113  {
115 
116  // skipping frames first
117  if (skipFrames > 0) {
118  if (numFrames < skipFrames) {
119  skipFrames -= numFrames;
120  return;
121  }
122  else {
123  buffer += skipFrames * frameSize;
124  numFrames -= skipFrames;
125  skipFrames = 0;
126  }
127  }
128 
129  // copying data from internal buffers until the requested number of frames is reached or underrun occurs
130  const dtime playingBufferSize = mode.bufferLength;
131  while (numFrames > 0 && playIndex - fillIndex < 0) {
132  const dtime
133  chunk = std::min(numFrames, playingBufferSize - playingBufferOffset),
134  numBytes = chunk * frameSize;
135  std::memcpy(buffer, buffers[playIndex % numBuffers] + playingBufferOffset * frameSize, (size_t) numBytes);
136  numFrames -= chunk;
137  playingBufferOffset += chunk;
138  buffer += numBytes;
139  // switching to the next buffer if it is the moment
140  if (playingBufferOffset >= playingBufferSize) {
141  playIndex++;
143  }
144  }
145 
146  // check for underrun
147  if (playIndex - fillIndex >= 0 && numFrames > 0) {
149  BEATMUP_DEBUG_I("UNDERRUN");
150  }
151 }
#define BEATMUP_DEBUG_I(...)
Definition: debug.h:33
int dtime
discrete time
Definition: basic_types.h:37
CustomPoint< numeric > min(const CustomPoint< numeric > &a, const CustomPoint< numeric > &b)
Definition: geometry.h:724

Member Data Documentation

◆ outputMode

const OutputMode Beatmup::Audio::BasicRealtimePlayback::outputMode
private

Definition at line 43 of file realtime_playback.h.

◆ buffers

sample8** Beatmup::Audio::BasicRealtimePlayback::buffers
private

buffers containing the data to play

Definition at line 45 of file realtime_playback.h.

◆ numBuffers

int Beatmup::Audio::BasicRealtimePlayback::numBuffers
private

Definition at line 46 of file realtime_playback.h.

◆ fillIndex

int Beatmup::Audio::BasicRealtimePlayback::fillIndex
private

points to the buffer currently being filled

Definition at line 48 of file realtime_playback.h.

◆ sendIndex

int Beatmup::Audio::BasicRealtimePlayback::sendIndex
private

points to the first buffer ready for playing

Definition at line 49 of file realtime_playback.h.

◆ playIndex

int Beatmup::Audio::BasicRealtimePlayback::playIndex
private

points to the buffer currently being played; updated only in the callback

Definition at line 50 of file realtime_playback.h.

◆ playingBufferOffset

dtime Beatmup::Audio::BasicRealtimePlayback::playingBufferOffset
private

when pulling output, offset in frames with respect to the currently playing buffer

Definition at line 52 of file realtime_playback.h.

◆ skipFrames

dtime Beatmup::Audio::BasicRealtimePlayback::skipFrames
private

when pulling output, number of frames to skip to give some time to the rendering process

Definition at line 53 of file realtime_playback.h.

◆ bufferSize

msize Beatmup::Audio::BasicRealtimePlayback::bufferSize
protected

size of each buffer in bytes

Definition at line 79 of file realtime_playback.h.


The documentation for this class was generated from the following files: