Beatmup
abstract_playback.h
Go to the documentation of this file.
1 /*
2  Beatmup image and signal processing library
3  Copyright (C) 2019, lnstadrum
4 
5  This program is free software: you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation, either version 3 of the License, or
8  (at your option) any later version.
9 
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18 
19 #pragma once
20 
21 #include "../../parallelism.h"
22 #include "../../exception.h"
23 #include "../source.h"
24 #include "../sample_arithmetic.h"
25 
26 namespace Beatmup {
27  namespace Audio {
28 
29  /**
30  Abstract audio playback base class.
31  An AbstractTask incarnation that samples a given Source in a buffered fashion.
32  The further processing of the sampled signal (e.g., sending to a device or writing to file) is done in subclasses.
33  */
34  class AbstractPlayback : public AbstractTask {
35  public:
36  /**
37  Playback configuration
38  */
39  struct Mode {
40  dtime sampleRate; //!< samples per second / sample rate in Hz
41  AudioSampleFormat sampleFormat; //!< format of each sample
42  unsigned char numChannels; //!< number of channels
43  dtime bufferLength; //!< length of each atomic buffer in samples
44  unsigned char numBuffers; //!< number of atomic buffers
45 
46  /**
47  Sets playback configuration.
48  \param sampleRate sampling frequency in Hz
49  \param format sample format
50  \param numChannels number of channels (e.g., 1 for mono, 2 for stereo)
51  \param bufferLength length of a single buffer
52  \param numBuffers number of buffers
53  */
56  Mode();
57 
58  /**
59  \return mode latency in samples.
60  */
61  inline dtime getLatency() const { return bufferLength * numBuffers; }
62  };
63 
64  protected:
69 
70  /**
71  Moves time pointer one buffer forward
72  */
73  void advanceTime();
74 
75  public:
76  ThreadIndex getMaxThreads() const;
77  void beforeProcessing(ThreadIndex threadCount, ProcessingTarget target, GraphicPipeline* gpu);
78 
79  /**
80  Initializes the playback setting its main parameters
81  */
82  virtual void initialize(Mode mode);
83 
84  /**
85  Specifies a Source to sample.
86  \param[in] source The source of audio signal to play from
87  */
88  inline void setSource(Source* source) { this->source = source; }
89 
90  /**
91  Returns the signal source to sample.
92  */
93  inline Source* getSource() const { return source; }
94  };
95 
96  /**
97  Communicates an error occurred during the playback
98  */
99  class PlaybackException : public Exception {
100  public:
101  PlaybackException(const char *message, int resultCode);
102  PlaybackException(const char *message, int resultCode, const AbstractPlayback::Mode& mode);
103  };
104  }
105 }
Task: an operation that can be executed by multiple threads in parallel.
Definition: parallelism.h:90
Abstract audio playback base class.
ThreadIndex getMaxThreads() const
Gives the upper limint on the number of threads the task may be performed by.
void advanceTime()
Moves time pointer one buffer forward.
virtual void initialize(Mode mode)
Initializes the playback setting its main parameters.
Source * getSource() const
Returns the signal source to sample.
void beforeProcessing(ThreadIndex threadCount, ProcessingTarget target, GraphicPipeline *gpu)
Instruction called before the task is executed.
void setSource(Source *source)
Specifies a Source to sample.
Communicates an error occurred during the playback.
PlaybackException(const char *message, int resultCode)
Abstract source of audio signal.
Definition: source.h:30
Base class for all exceptions.
Definition: exception.h:37
std::string message
Definition: exception.h:39
Internal low-level GPU control API.
Definition: pipeline.h:33
unsigned char ThreadIndex
number of threads / thread index
Definition: parallelism.h:68
int dtime
discrete time
Definition: basic_types.h:37
ProcessingTarget
Definition: basic_types.h:55
AudioSampleFormat
Format of audio samples.
AudioSampleFormat sampleFormat
format of each sample
dtime sampleRate
samples per second / sample rate in Hz
unsigned char numBuffers
number of atomic buffers
unsigned char numChannels
number of channels
dtime bufferLength
length of each atomic buffer in samples
JNIEnv jobject jint format
JNIEnv jlong jint mode