Skip to content

Instantly share code, notes, and snippets.

@boochow
Last active February 16, 2020 08:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save boochow/a0fdf1f40a44200685d323506b8165f4 to your computer and use it in GitHub Desktop.
Save boochow/a0fdf1f40a44200685d323506b8165f4 to your computer and use it in GitHub Desktop.
#include "userosc.h"
typedef struct State {
float w0;
float phase;
uint8_t flags;
} State;
enum {
k_flags_none = 0,
k_flag_reset = 1<<0,
};
static State s_state;
void OSC_INIT(uint32_t platform, uint32_t api)
{
s_state.w0 = 0.f;
s_state.phase = 0.f;
s_state.flags = k_flags_none;
}
void OSC_CYCLE(const user_osc_param_t * const params,
int32_t *yn,
const uint32_t frames)
{
const uint8_t flags = s_state.flags;
s_state.flags = k_flags_none;
const float w0 = s_state.w0 = osc_w0f_for_note((params->pitch)>>8, params->pitch & 0xFF);
float phase = (flags & k_flag_reset) ? 0.f : s_state.phase;
q31_t * __restrict y = (q31_t *)yn;
const q31_t * y_e = y + frames;
for (; y != y_e; ) {
float sig = (phase - 0.5f <= 0.f) ? 1.f : -1.f;
*(y++) = f32_to_q31(sig);
phase += w0;
phase -= (uint32_t)phase;
}
s_state.phase = phase;
}
void OSC_NOTEON(const user_osc_param_t * const params)
{
s_state.flags |= k_flag_reset;
}
void OSC_NOTEOFF(const user_osc_param_t * const params)
{
(void)params;
}
void OSC_PARAM(uint16_t index, uint16_t value)
{
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment