Skip to content

Instantly share code, notes, and snippets.

@jonbro
Last active August 29, 2015 14:19
Show Gist options
  • Select an option

  • Save jonbro/57344ba471b1f154dc1b to your computer and use it in GitHub Desktop.

Select an option

Save jonbro/57344ba471b1f154dc1b to your computer and use it in GitHub Desktop.
#include "module_sine.h"
typedef enum {
NOTE,
VOL
} lipsynth_sine_params;
int lipsynth_module_sine_destroy(void *self){
return 1;
}
void lipsynth_module_sine_update(void *self, lipsynth_context *ctx, lipsynth_AudioMemory *outputBuffer){
lipsynth_module_sine* module = self;
// fill up the output buffer with a sine wave!
for(int i=0;i<ctx->bufferLength/2;i++){
// must be called before any parameters are read
if(module->_(lipsynth_Module_update_parameters)(self, ctx)){
if(module->_(lipsynth_Module_has_parameter_value)(self, ctx, NOTE)){
int noteVal = module->_(lipsynth_Module_get_parameter_value)(self, ctx, NOTE);
module->freqVal = pow(2.0, (noteVal)/12.0f)*440.0f;
}
}
// this number is to deal with floating point errors if the sine counter gets too big
// what I should actually do is figure out the period of the wave in samples, and use integers to follow that
if(module->sineCounter > 128){
module->sineCounter -= 128.0f;
}
module->sineCounter += 1.0f/44100.0f*module->freqVal;
outputBuffer->data[i*2] = outputBuffer->data[i*2+1] = sinf(module->sineCounter*M_PI_2);
}
}
int lipsynth_module_sine_init(void *self)
{
lipsynth_module_sine *sine = self;
sine->_(lipsynth_Module_register_parameter)(self, sine->_(ctx), "note", NOTE);
sine->_(lipsynth_Module_register_parameter)(self, sine->_(ctx), "volume", VOL);
sine->sineCounter = 0;
sine->freqVal = 440;
return 1;
}
lipsynth_Module lipsynth_module_sine_proto = {
.lipsynth_Module_init = lipsynth_module_sine_init,
.lipsynth_Module_update = lipsynth_module_sine_update
};
ctx = lipsynth_core_init(2048*2, 128);
// create an output module, create a sine module, and test passing audio data out
int outputModule = lipsynth_core_new_module(ctx, OUTPUT);
int sineModule = lipsynth_core_new_module(ctx, SINE);
lipsynth_core_connect_module(ctx, sineModule, outputModule);
// set some notes on the sine module
for(int i=0;i<16;i++){
lipsynth_core_set_parameter(ctx, sineModule, 0, i, "note", i%2*12);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment