Commit 158cc035 authored by jhammen's avatar jhammen
Browse files

add sample audio effects processor

parent 37417da2
......@@ -19,5 +19,13 @@
<method name="favnum" returns="integer"/>
</methods>
</class>
<class name="SampleEffect" include="sampleeffect">
<ctors>
<ctor/>
</ctors>
<properties>
<property name="gain" type="float"/>
</properties>
</class>
</classes>
</package>
......@@ -7,6 +7,7 @@
#include "bindutil.h"
#include "myclass.h"
#include "sampleeffect.h"
#include <stdexcept>
#include <cstring>
......@@ -16,6 +17,7 @@ using namespace samplext;
// object references to types in this package
void * SampleXtMyClassObject;
void * SampleXtSampleEffectObject;
//
// SampleXt.MyClass class
......@@ -105,6 +107,76 @@ BXTResult SampleXtMyClassToString(int32_t)
}
#undef BIND_CLASS_NAME
//
// SampleXt.SampleEffect class
//
#define BIND_CLASS_NAME "SampleEffect"
#define BIND_METHOD_NAME "constructor"
BXTResult SampleXtSampleEffectCtor(int32_t nargs)
{
BIND_GET_NARGS()
SampleEffect *obj;
BIND_NARGS_CHECK_EXACT(0) // check exact parameter count
// call the implementation
BIND_WRAP_TRY(obj = new SampleEffect())
// return pointer to new object
BIND_PUT_THIS(obj, false)
return 1;
}
#undef BIND_METHOD_NAME
//
// SampleXt.SampleEffect property getters
//
#define BIND_METHOD_NAME "getter"
BXTResult SampleXtSampleEffectGetters(BXTIndex)
{
// get property name
const char* key;
bipxt_get_string(2, &key);
BIND_GET_THIS_PROPERTY(SampleEffect)
// dispatch appropriate getter
if(!strcmp(key, "gain")) {
BIND_DECLARE_FLOAT ret = obj->gain();
BIND_PUSH_FLOAT(ret)
return 1;
}
else {
return bipxt_not_found();
}
}
#undef BIND_METHOD_NAME
//
// SampleXt.SampleEffect property setters
//
#define BIND_METHOD_NAME "setter"
BXTResult SampleXtSampleEffectSetters(BXTIndex)
{
// get property name
const char* key;
bipxt_get_string(2, &key);
BIND_GET_THIS_PROPERTY(SampleEffect)
// dispatch appropriate setter
if(!strcmp(key, "gain")) {
// get parameter 2 "gain" as float
BIND_GET_CHECK_FLOAT(gain, 2)
BIND_WRAP_TRY(obj->gain(gain))
return 0;
}
else {
return bipxt_not_found();
}
}
#undef BIND_METHOD_NAME
BXTResult SampleXtSampleEffectToString(int32_t)
{
bipxt_put_string("(SampleXt.SampleEffect)", 23);
return 1;
}
#undef BIND_CLASS_NAME
void bindSampleXt()
{
......@@ -116,5 +188,12 @@ void bindSampleXt()
bipxt_bind_method("favnum", 6, &SampleXtMyClassfavnum);
bipxt_bind_method("_tostring", 9, &SampleXtMyClassToString);
bipxt_bind_class_end();
// create class SampleXt.SampleEffect
SampleXtSampleEffectObject = bipxt_bind_class("SampleEffect", 12, &SampleXtSampleEffectCtor);
// bipxt_bind_method("_cloned", 7, BindUtil::Clone<SampleEffect>); // clone
bipxt_bind_method("_get", 4, &SampleXtSampleEffectGetters);
bipxt_bind_method("_set", 4, &SampleXtSampleEffectSetters);
bipxt_bind_method("_tostring", 9, &SampleXtSampleEffectToString);
bipxt_bind_class_end();
}
}
}
\ No newline at end of file
......@@ -5,6 +5,7 @@ namespace bapigen
{
// object references to types in this package
extern void * SampleXtMyClassObject;
extern void * SampleXtSampleEffectObject;
// method to bind this package
void bindSampleXt();
}
......
......@@ -28,7 +28,7 @@ class MyClass {
public:
MyClass(const char *name) : mname(name) { srand(time(0)); }
const char *name() { return mname; }
int favnum() { return rand(); }
int favnum() { return rand() % 10; }
};
} // namespace samplext
......
#include "sampleeffect.h"
#include "bipxtapi.h"
#include <algorithm>
#include <math.h>
namespace samplext {
int bipxt_process(void *obj, int32_t samples) {
return static_cast<SampleEffect *>(obj)->process(samples);
}
SampleEffect::SampleEffect() : mgain(0.0) {
bipxt_audio_processor("Sample Effect", 1, 1, this, bipxt_process);
}
bool SampleEffect::process(int32_t nsamples) {
// convert gain property in db to ratio
const float gainratio = powf(10.0, mgain.load() / 20.0);
// get audio input/output buffers
float *const input = bipxt_audio_input_buffer(this, 0);
float *const output = bipxt_audio_output_buffer(this, 0);
// always check input - will be null if not connected
if (input) {
for (int32_t i = 0; i < nsamples; i++) {
output[i] = input[i] * gainratio;
}
} else {
std::fill(output, output + nsamples, 0.0);
}
return false;
}
} // namespace samplext
/*
MIT No Attribution
Permission is hereby granted, free of charge, to any person obtaining a copy of this
software and associated documentation files (the "Software"), to deal in the Software
without restriction, including without limitation the rights to use, copy, modify,
merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#ifndef SAMPLEDELAY_H
#define SAMPLEDELAY_H
#include <atomic>
namespace samplext {
class SampleEffect {
std::atomic<float> mgain;
public:
SampleEffect();
bool process(int32_t nsamples);
float gain() { return mgain.load(); }
void gain(float gain) { mgain.store(gain); }
};
} // namespace samplext
#endif // SAMPLEDELAY_H
......@@ -7,4 +7,13 @@ function testMyClass() {
println("my favorite number is " + obj.favnum())
}
function testFx() {
local input = Audio.Input("in")
local effect = SampleXt.SampleEffect()
effect.gain = 1.0
input => effect => Audio.Output("out", "system:playback_1")
Script.stayAlive()
}
testMyClass()
testFx()
......@@ -39,6 +39,8 @@ typedef void *BXTClass;
typedef void *BXTFunction;
typedef void *BXTInstance;
typedef BXTResult (*BXTProcess)(BXTInstance, BXTIndex);
BXTResult bipxt_ext_init(uint32_t version);
void bipxt_ext_init_pack(const char *name);
void bipxt_ext_shutdown();
......@@ -71,6 +73,11 @@ void bipxt_add_reference(BXTInstance object);
BXTResult bipxt_execute(BXTFunction function, BXTInstance context);
void bipxt_event_handler(BXTMethod handler);
void bipxt_audio_processor(const char *name, BXTIndex inputs, BXTIndex outputs, void *obj,
BXTProcess func);
BXTFloat *bipxt_audio_input_buffer(void *obj, BXTIndex index);
BXTFloat *bipxt_audio_output_buffer(void *obj, BXTIndex index);
BXTResult bipxt_error(const char *mesg);
BXTResult bipxt_not_found();
}
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment