<commit_before>#include <SDL2/SDL.h>
#include "minigbs.h"

struct chan_len_ctr {
	int   load;
	bool  enabled;
	float counter;
	float inc;
};

struct chan_vol_env {
	int   step;
	bool  up;
	float counter;
	float inc;
};

struct chan_freq_sweep {
	int   freq;
	int   rate;
	bool  up;
	int   shift;
	float counter;
	float inc;
};

static struct chan {
	bool enabled;
	bool powered;
	bool on_left;
	bool on_right;
	bool user_mute;

	int volume;
	int volume_init;

	uint16_t freq;
	float    freq_counter;
	float    freq_inc;

	int val;
	int note;

	struct chan_len_ctr len;
	struct chan_vol_env env;
	struct chan_freq_sweep sweep;

	// square
	int duty;

	// noise
	uint16_t lfsr_reg;
	bool     lfsr_wide;
	int      lfsr_div;
} chans[4];

#define FREQ 44100
#define FREQF ((float)FREQ)
#define HZ 59.7f
#define SAMPLES ((FREQ / (int)HZ))
#define DBLSAMPLES (SAMPLES*2)

static float samples[DBLSAMPLES];
static SDL_AudioDeviceID audio;
static const float duty[] = { 0.125, 0.25, 0.5, 0.25 };
static float logbase;
static const char* notes[] = {
	"A-", "A#", "B-", "C-", "C#", "D-", "D#", "E-", "F-", "F#", "G-", "G#"
};

void set_note_freq(struct chan* c, float freq){
	c->freq_inc = freq / FREQF;
	c->note = MAX(0, (int)roundf(logf(freq/440.0f) / logbase) + 48);
}

bool chan_muted(struct chan* c){
	return c->user_mute || !c->enabled || !(c->on_left || c->on_right);
}

void chan_enable(int i, bool enable){
	chans[i].enabled = enable;

	uint8_t val = (mem[0xFF26] & 0x80)
		| (chans[3].enabled << 3)
		| (chans[2].enabled << 2)
		| (chans[1].enabled << 1)
		| (chans[0].enabled << 0);

	mem[0xFF26] = val;
}

void update_env(struct chan* c){
	c->env.counter += c->env.inc;

	while(c->env.counter > 1.0f){
		c->volume += c->env.up ? 1 : -1;<commit_msg>small fix for notes ui not clearing<commit_after>#include <SDL2/SDL.h>
#include "minigbs.h"

struct chan_len_ctr {
	int   load;
	bool  enabled;
	float counter;
	float inc;
};

struct chan_vol_env {
	int   step;
	bool  up;
	float counter;
	float inc;
};

struct chan_freq_sweep {
	int   freq;
	int   rate;
	bool  up;
	int   shift;
	float counter;
	float inc;
};

static struct chan {
	bool enabled;
	bool powered;
	bool on_left;
	bool on_right;
	bool user_mute;

	int volume;
	int volume_init;

	uint16_t freq;
	float    freq_counter;
	float    freq_inc;

	int val;
	int note;

	struct chan_len_ctr len;
	struct chan_vol_env env;
	struct chan_freq_sweep sweep;

	// square
	int duty;

	// noise
	uint16_t lfsr_reg;
	bool     lfsr_wide;
	int      lfsr_div;
} chans[4];

#define FREQ 44100
#define FREQF ((float)FREQ)
#define HZ 59.7f
#define SAMPLES ((FREQ / (int)HZ))
#define DBLSAMPLES (SAMPLES*2)

static float samples[DBLSAMPLES];
static SDL_AudioDeviceID audio;
static const float duty[] = { 0.125, 0.25, 0.5, 0.25 };
static float logbase;
static const char* notes[] = {
	"A-", "A#", "B-", "C-", "C#", "D-", "D#", "E-", "F-", "F#", "G-", "G#"
};

void set_note_freq(struct chan* c, float freq){
	c->freq_inc = freq / FREQF;
	c->note = MAX(0, (int)roundf(logf(freq/440.0f) / logbase) + 48);
}

bool chan_muted(struct chan* c){
	return c->user_mute || !c->enabled || !(c->on_left || c->on_right) || !c->volume;
}

void chan_enable(int i, bool enable){
	chans[i].enabled = enable;

	uint8_t val = (mem[0xFF26] & 0x80)
		| (chans[3].enabled << 3)
		| (chans[2].enabled << 2)
		| (chans[1].enabled << 1)
		| (chans[0].enabled << 0);

	mem[0xFF26] = val;
}

void update_env(struct chan* c){
	c->env.counter += c->env.inc;

	while(c->env.counter > 1.0f){
		c->volume += c->env.up ? 1 : -1;
{'fraction_non_alphanumeric': 0.16666666666666666, 'fraction_numerical': 0.03169213355970572, 'mean_word_length': 3.5790155440414506, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 30, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1898255', 'n_tokens_mistral': 1651, 'n_tokens_neox': 1533, 'n_words': 386}