Beatmup
wrapper_audio.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 "wrapper.h"
20 
21 #include "include/Beatmup_Audio_Signal.h"
22 #include "include/Beatmup_Audio_SignalPlot.h"
23 #include "include/Beatmup_Audio_Playback.h"
24 #include "include/Beatmup_Audio_HarmonicSource.h"
25 
27 #include <core/audio/signal.h>
28 #include <core/audio/signal_plot.h>
29 #include <core/color/packing.h>
30 
31 /////////////////////////////////////////////////////////////////////////////////////////////
32 // SIGNAL
33 /////////////////////////////////////////////////////////////////////////////////////////////
34 
35 JNIMETHOD(jlong, newAudioSignal, Java_Beatmup_Audio_Signal, newAudioSignal)
36  (JNIEnv * jenv, jclass, jobject jCtx, jint format, jint samplerate, jint channels, jfloat fragment)
37 {
41 }
42 
43 
44 JNIMETHOD(jlong, newAudioSignalFromWAV, Java_Beatmup_Audio_Signal, newAudioSignalFromWAV)
45  (JNIEnv * jenv, jclass, jobject jCtx, jstring jsFilename)
46 {
47  // copy filename first
48  const char* javaChar = jenv->GetStringUTFChars(jsFilename, 0);
49  std::string filename(javaChar);
50  jenv->ReleaseStringUTFChars(jsFilename, javaChar);
51  // do the stuff then
54  try {
56  }
57  catch (Beatmup::Exception& ex) { $pool.throwToJava(jenv, "java/io/IOError", ex.what()); }
59 }
60 
61 
62 JNIMETHOD(jlong, newAudioSignalSource, Java_Beatmup_Audio_Signal, newAudioSignalSource)
64 {
67  return (jlong) new Beatmup::Audio::Signal::Source(*signal);
68 }
69 
70 
71 JNIMETHOD(jint, getDuration, Java_Beatmup_Audio_Signal, getDuration)
72  (JNIEnv * jenv, jclass, jlong handle)
73 {
76  return (jint)signal->getDuration();
77 }
78 
79 
80 JNIMETHOD(jint, getSampleFormat, Java_Beatmup_Audio_Signal, getSampleFormat)
81  (JNIEnv * jenv, jclass, jlong handle)
82 {
85  return (jint)signal->getSampleFormat();
86 }
87 
88 
89 JNIMETHOD(jint, getChannelCount, Java_Beatmup_Audio_Signal, getChannelCount)
90  (JNIEnv * jenv, jclass, jlong handle)
91 {
94  return signal->getChannelCount();
95 }
96 
97 
98 /////////////////////////////////////////////////////////////////////////////////////////////
99 // SIGNAL PLOT
100 /////////////////////////////////////////////////////////////////////////////////////////////
101 
102 JNIMETHOD(jlong, newSignalPlot, Java_Beatmup_Audio_SignalPlot, newSignalPlot)
103  (JNIEnv * jenv, jclass, jobject jCtx)
104 {
106  return (jlong) new Beatmup::Audio::SignalPlot();
107 }
108 
109 
110 JNIMETHOD(void, prepareMetering, Java_Beatmup_Audio_SignalPlot, prepareMetering)
111  (JNIEnv * jenv, jclass, jlong hSignal)
112 {
116 }
117 
118 
119 JNIMETHOD(void, setSignal, Java_Beatmup_Audio_SignalPlot, setSignal)
121 {
125  plot->setSignal(signal);
126 }
127 
128 
129 
130 JNIMETHOD(void, setBitmap, Java_Beatmup_Audio_SignalPlot, setBitmap)
132 {
136  plot->setBitmap(bitmap);
137 }
138 
139 
140 JNIMETHOD(void, setWindow, Java_Beatmup_Audio_SignalPlot, setWindow)
141  (JNIEnv * jenv, jobject, jlong hPlot, jint t1, jint t2, jint y1, jint y2, jfloat scale)
142 {
145  plot->setWindow(Beatmup::IntRectangle(t1, y1, t2, y2), scale);
146 }
147 
148 
149 JNIMETHOD(void, setPlotArea, Java_Beatmup_Audio_SignalPlot, setPlotArea)
150  (JNIEnv * jenv, jobject, jlong hPlot, jint x1, jint y1, jint x2, jint y2)
151 {
154  plot->setPlotArea(Beatmup::IntRectangle(x1, y1, x2, y2));
155 }
156 
157 
158 JNIMETHOD(void, setPalette, Java_Beatmup_Audio_SignalPlot, setPalette)
159  (JNIEnv * jenv, jobject, jlong hPlot, jint background, jint color1, jint color2)
160 {
163  plot->setPalette(
165  Beatmup::fromPackedInt((int32_t)color1),
167  );
168 }
169 
170 
171 JNIMETHOD(void, setChannels, Java_Beatmup_Audio_SignalPlot, setChannels)
172  (JNIEnv * jenv, jobject, jlong hPlot, jint channels)
173 {
176  plot->setChannels(channels);
177 }
178 
179 /////////////////////////////////////////////////////////////////////////////////////////////
180 // PLAYBACK
181 /////////////////////////////////////////////////////////////////////////////////////////////
182 
183 JNIMETHOD(jlong, newPlayback, Java_Beatmup_Audio_Playback, newPlayback)
184  (JNIEnv * jenv, jclass, jobject jCtx)
185 {
188 }
189 
190 
191 JNIMETHOD(void, initialize, Java_Beatmup_Audio_Playback, initialize)
192  (JNIEnv * jenv, jobject, jlong handle, jint sampleRate, jint sampleFormat, jint numChannels, jint bufferLength, jint numBuffers)
193 {
196  try {
198  sampleRate, (Beatmup::AudioSampleFormat)sampleFormat, numChannels, bufferLength, numBuffers
199  ));
200  }
201  catch (Beatmup::Audio::PlaybackException& pex) { $pool.throwToJava(jenv, "Beatmup/Exceptions/PlaybackException", pex.what()); }
202 }
203 
204 
205 JNIMETHOD(void, start, Java_Beatmup_Audio_Playback, start) (JNIEnv * jenv, jobject, jlong handle) {
208  try {
209  pb->start();
210  }
211  catch (Beatmup::Audio::PlaybackException& pex) { $pool.throwToJava(jenv, "Beatmup/Exceptions/PlaybackException", pex.what()); }
212 }
213 
214 
215 JNIMETHOD(void, stop, Java_Beatmup_Audio_Playback, stop) (JNIEnv * jenv, jobject, jlong handle) {
218  try {
219  pb->stop();
220  }
221  catch (Beatmup::Audio::PlaybackException& pex) { $pool.throwToJava(jenv, "Beatmup/Exceptions/PlaybackException", pex.what()); }
222 }
223 
224 
225 JNIMETHOD(void, setSource, Java_Beatmup_Audio_Playback, setSource)
227 {
231  pb->setSource(source);
232 }
233 
234 /////////////////////////////////////////////////////////////////////////////////////////////
235 // HARMONIC SOURCE
236 /////////////////////////////////////////////////////////////////////////////////////////////
237 
238 JNIMETHOD(jlong, newHarmonicSource, Java_Beatmup_Audio_HarmonicSource, newHarmonicSource)
239  (JNIEnv *, jclass)
240 {
243 }
244 
245 JNIMETHOD(void, setFrequency, Java_Beatmup_Audio_HarmonicSource, setFrequency)
246  (JNIEnv * jenv, jobject, jlong handle, jfloat hz)
247 {
250  source->setFrequency(hz);
251 }
252 
253 JNIMETHOD(void, setPhase, Java_Beatmup_Audio_HarmonicSource, setPhase)
254  (JNIEnv * jenv, jobject, jlong handle, jfloat rad)
255 {
258  source->setPhase(rad);
259 }
260 
261 JNIMETHOD(void, setAmplitude, Java_Beatmup_Audio_HarmonicSource, setAmplitude)
262  (JNIEnv * jenv, jobject, jlong handle, jfloat amp)
263 {
266  source->setAmplitude(amp);
267 }
static void throwToJava(JNIEnv *jenv, const char *exceptionClass, const char *message)
Throws a specific exception.
Definition: objectpool.h:156
static const jlong INVALID_HANDLE
Definition: objectpool.h:61
A very basic class for any image.
Abstract audio playback base class.
OpenSL ES Android playback.
Definition: sles_playback.h:29
A Source producing a sinusoidal signal, mainly for test purposes.
Definition: source.h:86
Communicates an error occurred during the playback.
AbstractTask drawing amplitude graph of a given Signal in a bitmap.
Definition: signal_plot.h:31
static void prepareSignal(Signal &signal, bool runTask=true)
Precomputes the dynamics lookup all over the signal, where needed.
Definition: signal.cpp:202
Audio::Source reading samples from a given Signal.
Definition: signal.h:146
An audio signal.
Definition: signal.h:36
static Signal * loadWAV(Context &ctx, const char *fileName)
Definition: signal.cpp:64
Abstract source of audio signal.
Definition: source.h:30
Basic class: task and memory management, any kind of static data.
Definition: context.h:59
Base class for all exceptions.
Definition: exception.h:37
virtual const char * what() const NOEXCEPT override
Definition: exception.h:56
color4i fromPackedInt(int32_t _)
Definition: packing.h:23
AudioSampleFormat
Format of audio samples.
#define BEATMUP_ENTER
Definition: wrapper.h:35
BeatmupJavaObjectPool $pool
JNIEnv jobject jCtx
JNIEnv jlong jint jint color1
source setAmplitude(amp)
JNIEnv jobject jlong handle
JNIMETHOD(void, start, Java_Beatmup_Audio_Playback, start)(JNIEnv *jenv
pb setSource(source)
JNIEnv jlong jlong hSignal
JNIEnv jclass
source setPhase(rad)
plot setBitmap(bitmap)
JNIEnv jlong jint jint jint jint y2
JNIEnv jlong jfloat rad
JNIEnv jlong jobject jSource
JNIEnv jlong jint jint jint jint jfloat scale
JNIEnv jobject jint jint samplerate
JNIEnv jlong jobject jBitmap
JNIEnv jlong jint jint t2
JNIEnv jobject jint jint jint channels
JNIEnv * jenv
JNIEnv jlong jint background
JNIEnv jlong hPlot
JNIEnv jlong jint x1
JNIEnv jlong jfloat amp
return signal getChannelCount()
JNIEnv jobject jint jint jint jfloat fragment
JNIEnv jlong jint jint jint x2
JNIEnv jlong jint jint jint color2
JNIEnv jobject
plot setSignal(signal)
JNIEnv jlong jint jint jint y1
plot setWindow(Beatmup::IntRectangle(t1, y1, t2, y2), scale)
plot setChannels(channels)
JNIEnv jlong jint t1
plot setPalette(Beatmup::fromPackedInt((int32_t) background), Beatmup::fromPackedInt((int32_t) color1), Beatmup::fromPackedInt((int32_t) color2))
plot setPlotArea(Beatmup::IntRectangle(x1, y1, x2, y2))
JNIEnv jobject jint format
BEATMUP_OBJ(Beatmup::Context, ctx, jCtx)
return() jlong(listener)
Beatmup::Context * ctx
JNIEnv jlong jstring filename
jlong jint start
Beatmup::InternalBitmap * bitmap
const char * javaChar