Beatmup
source.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 "sample_arithmetic.h"
23 
24 namespace Beatmup {
25  namespace Audio {
26 
27  /**
28  Abstract source of audio signal. Produces samples in time, possibly in multiple threads running in parallel.
29  */
30  class Source {
31  public:
32  virtual ~Source() {};
33 
34 
35  /**
36  Prepares a source to render audio data. Called by the user before any rendering to communicate the
37  configuration of the upcoming rendering process.
38  \param sampleRate The output sample rate in Hz
39  \param sampleFormat The output sample format
40  \param numChannels Number of channels in the output
41  \param maxBufferLen Maximum expected length of target audio buffers on rendering
42  phase, in samples per one channel
43  */
44  virtual void prepare(
45  const dtime sampleRate,
46  const AudioSampleFormat sampleFormat,
47  const unsigned char numChannels,
48  const dtime maxBufferLen
49  ) = 0;
50 
51 
52  /**
53  * Called by the source user when an abrupt time change occurs (e.g., due to seeking)
54  */
55  virtual void setClock(dtime time);
56 
57 
58  /**
59  * Returns the maximum number of working threads for rendering from this source. The actual number of
60  * threads may be less than the one returned by this function. It is the source duty to deal with the actual
61  * number of workers.
62  */
63  virtual ThreadIndex getMaxThreads() { return 1; }
64 
65 
66  /**
67  * Renders audio data to the target output buffer given by the user.
68  * Called after at least one call to prepare(). The sampling parameters must match the ones communicated
69  * on the preparation phase. The requested buffer length does not exceed the one set before. The time is
70  * given by the clock set before, and with every call it advances by {bufferLength} samples.
71  * \param thread the task thread issuing this rendering call
72  * \param buffer a pointer to the beginning of a channelwise-multiplexed output buffer
73  * \param bufferLength the requested buffer length, in samples per single channel
74  */
75  virtual void render(
76  TaskThread& thread,
77  sample8* buffer,
78  const dtime bufferLength
79  ) = 0;
80  };
81 
82 
83  /**
84  A Source producing a sinusoidal signal, mainly for test purposes
85  */
86  class HarmonicSource : public Source {
87  private:
91  unsigned char numChannels;
92  public:
93  HarmonicSource() : amplitude(0.05), frequency(800), phase(0) {}
94 
95  void setFrequency(float hz) { frequency = hz; }
96  float getFrequency() const { return frequency; }
97 
98  void setPhase(float radians) { phase = radians; }
99  float getPhase() const { return phase; }
100 
101  void setAmplitude(float amp) { this->amplitude = amp; }
102  float getAmplitude() const { return amplitude; }
103 
104  void prepare(
105  const dtime sampleRate,
107  const unsigned char numChannels,
108  const dtime maxBufferLen
109  );
110 
111  void render(
112  TaskThread& thread,
113  sample8* buffer,
114  const dtime bufferLength
115  );
116 
117  void setClock(dtime time);
118  };
119  }
120 }
A Source producing a sinusoidal signal, mainly for test purposes.
Definition: source.h:86
AudioSampleFormat sampleFormat
Definition: source.h:88
void render(TaskThread &thread, sample8 *buffer, const dtime bufferLength)
Renders audio data to the target output buffer given by the user.
Definition: source.cpp:69
void setClock(dtime time)
Called by the source user when an abrupt time change occurs (e.g., due to seeking)
Definition: source.cpp:95
void setFrequency(float hz)
Definition: source.h:95
void prepare(const dtime sampleRate, const AudioSampleFormat sampleFormat, const unsigned char numChannels, const dtime maxBufferLen)
Prepares a source to render audio data.
Definition: source.cpp:57
float getAmplitude() const
Definition: source.h:102
unsigned char numChannels
Definition: source.h:91
void setAmplitude(float amp)
Definition: source.h:101
float getPhase() const
Definition: source.h:99
void setPhase(float radians)
Definition: source.h:98
float getFrequency() const
Definition: source.h:96
Abstract source of audio signal.
Definition: source.h:30
virtual ~Source()
Definition: source.h:32
virtual void setClock(dtime time)
Called by the source user when an abrupt time change occurs (e.g., due to seeking)
Definition: source.cpp:29
virtual ThreadIndex getMaxThreads()
Returns the maximum number of working threads for rendering from this source.
Definition: source.h:63
virtual void render(TaskThread &thread, sample8 *buffer, const dtime bufferLength)=0
Renders audio data to the target output buffer given by the user.
virtual void prepare(const dtime sampleRate, const AudioSampleFormat sampleFormat, const unsigned char numChannels, const dtime maxBufferLen)=0
Prepares a source to render audio data.
Thread executing tasks.
Definition: parallelism.h:154
unsigned char ThreadIndex
number of threads / thread index
Definition: parallelism.h:68
int dtime
discrete time
Definition: basic_types.h:37
AudioSampleFormat
Format of audio samples.
JNIEnv jlong jfloat amp
jlong jlong jint time