Beatmup
source.cpp
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 #include "source.h"
20 #include "../exception.h"
21 #include "../debug.h"
22 #include <cmath>
23 #include <algorithm>
24 
25 using namespace Beatmup;
26 using namespace Audio;
27 
28 
30  // nothing to do by default
31 }
32 
33 
34 template <typename sample> inline void fillSin(
35  sample8* buffer,
36  dtime time,
37  dtime length,
38  float volume,
39  float frequency,
40  float phase,
41  dtime sampleRate,
42  unsigned char numChannels,
43  int scale)
44 {
45  dtime stop = time + length;
46  sample* s = (sample*)buffer;
47  float f = 2 * pi * frequency / sampleRate;
48  sample v;
49  for (dtime t = time; t < stop; ++t) {
50  v.x = scale * std::min(std::max(-1.0f, volume * std::sin(t * f + phase)), 1.0f);
51  for (unsigned char c = 0; c < numChannels; c++)
52  (s++)->x = v.x;
53  }
54 }
55 
56 
58  const dtime sampleRate,
59  const AudioSampleFormat sampleFormat,
60  const unsigned char numChannels,
61  const dtime maxBufferLen)
62 {
63  this->sampleFormat = sampleFormat;
64  this->sampleRate = sampleRate;
65  this->numChannels = numChannels;
66 }
67 
68 
69 void HarmonicSource::render(TaskThread& thread, sample8* buffer, const dtime bufferLength) {
70  BEATMUP_DEBUG_I("FILLING BUFFER OF %d SAMPLES at time %d", bufferLength, time);
71  switch (sampleFormat) {
72  case Int8:
73  fillSin<sample8>(buffer, time, bufferLength, amplitude, frequency, phase, sampleRate, numChannels, 127);
74  break;
75 
76  case Int16:
77  fillSin<sample16>(buffer, time, bufferLength, amplitude, frequency, phase, sampleRate, numChannels, 0x7FFF);
78  break;
79 
80  case Int32:
81  fillSin<sample32>(buffer, time, bufferLength, amplitude, frequency, phase, sampleRate, numChannels, 0x7FFFFFFF);
82  break;
83 
84  case Float32:
85  fillSin<sample32f>(buffer, time, bufferLength, amplitude, frequency, phase, sampleRate, numChannels, 1);
86  break;
87 
88  default:
89  Insanity::insanity("Unsupported sample format");
90  }
91  time += bufferLength;
92 }
93 
94 
96  this->time = time;
97 }
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 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
unsigned char numChannels
Definition: source.h:91
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
static void insanity(const char *message)
Definition: exception.h:136
Thread executing tasks.
Definition: parallelism.h:154
#define BEATMUP_DEBUG_I(...)
Definition: debug.h:33
const float pi
Definition: basic_types.h:29
int dtime
discrete time
Definition: basic_types.h:37
AudioSampleFormat
Format of audio samples.
@ Int8
signed integer, 8 bit per sample
@ Int32
signed integer, 32 bit per sample
@ Float32
floating point, 32 bit per sample
@ Int16
signed integer, 16 bit per sample
CustomPoint< numeric > min(const CustomPoint< numeric > &a, const CustomPoint< numeric > &b)
Definition: geometry.h:724
CustomPoint< numeric > max(const CustomPoint< numeric > &a, const CustomPoint< numeric > &b)
Definition: geometry.h:728
void fillSin(sample8 *buffer, dtime time, dtime length, float volume, float frequency, float phase, dtime sampleRate, unsigned char numChannels, int scale)
Definition: source.cpp:34
JNIEnv jlong jint jint jint jint jfloat scale
jlong jlong jint time
JNIEnv jlong jfloat jfloat s
JNIEnv jlong jfloat jfloat jfloat v