<commit_before>#ifndef F_CPU
#define F_CPU F_OSC
#endif
#include <avr/io.h>
#include <avr/interrupt.h>
#include <ctype.h>
#include <math.h>
#include <stdint.h>
#include <util/delay.h>

#define N_OSC 1
#define N_SAMP 256
#define HIGHEST_OCTAVE 10
#define VOLUME_TRANSITION 1 /* If non-zero, volume changes by at most one per wave period */
#define VOLUME_WAIT_NEW_PHASE 1 /* If non-zero, volume changes only at zero phase */

typedef uint16_t PhaseType;

int8_t wav[N_SAMP];
PhaseType increment[N_OSC];
union {
    PhaseType i;
    uint8_t b[sizeof(PhaseType)];
} phase[N_OSC];
PhaseType increments[12];
uint8_t enabled_tones[128/8];
uint8_t tone[N_OSC];
uint8_t age[N_OSC];
uint8_t vel[N_OSC], vel_bak[N_OSC];
uint8_t num_tones = 0;

static uint8_t next_char(void);
static void handle_midi(void);
int main(void);

void tx(uint8_t c) {
    while( !(UCSR0A & (1<<UDRE0)));
    UDR0 = c;
}

void tx_hex(uint8_t c) {
    static char const lut[16] = "0123456789ABCDEF";
    tx(lut[c>>4]);
    tx(lut[c&15]);
}

void tx_s(char const *s) {
    while(*s) {
        tx(*s);
        s++;
    }
}

__attribute__((always_inline))
static inline void do_osc(uint8_t n, volatile uint8_t *reg) {
        phase[n].i += increment[n];
        // skip the next if if carry is not set, i.e. we're not near a zeroed phase yet
        __asm__ volatile goto ("brcc %I0 \n" : /* No outputs */ : /* No inputs */ : /* No clobbers */ : assign);
        if(VOLUME_TRANSITION && vel_bak[n] < vel[n]) {
                vel_bak[n]++;
        } else if(VOLUME_TRANSITION && vel_bak[n] > vel[n]) {
                vel_bak[n]--;
        } else if(VOLUME_WAIT_NEW_PHASE) {
                vel_bak[n] = vel[n];
        }
assign:
        *reg = 0x80 ^ ((vel_bak[n] * wav[phase[n].b[sizeof(PhaseType)-1]]) >> 8);\
}
ISR(TIMER2_OVF_vect) {
    PORTD |= (1<<2);
    if(N_OSC > 0) do_osc(0, &OCR2A);
    if(N_OSC > 1) do_osc(1, &OCR2B);
    if(N_OSC > 2) do_osc(2, &OCR1BL);
    if(N_OSC > 3) do_osc(3, &OCR1AL);
    if(N_OSC > 4) do_osc(4, &OCR0B);
    if(N_OSC > 5) do_osc(5, &OCR0B);
    PORTD &= ~(1<<2);
}
static void set_tone(uint8_t oscillator, uint8_t key, uint8_t velocity) {
    if(tone[oscillator] != 255) {
        enabled_tones[tone[oscillator] >> 3] &= ~(1 << (tone[oscillator] & 0x07));
    }
    increment[oscillator] = increments[key%12] >> (HIGHEST_OCTAVE+1-key/12);<commit_msg>Better volume logic, more funny comments<commit_after>#ifndef F_CPU
#define F_CPU F_OSC
#endif
#include <avr/io.h>
#include <avr/interrupt.h>
#include <ctype.h>
#include <math.h>
#include <stdint.h>
#include <util/delay.h>

#define N_OSC 1
#define N_SAMP 256
#define HIGHEST_OCTAVE 10
#define VOLUME_TRANSITION 1 /* If non-zero, volume changes by at most one per wave period */
#define VOLUME_WAIT_NEW_PHASE 1 /* If non-zero, volume changes only at zero phase */

typedef uint16_t PhaseType;

int8_t wav[N_SAMP];
PhaseType increment[N_OSC];
union {
    PhaseType i;
    uint8_t b[sizeof(PhaseType)];
} phase[N_OSC];
PhaseType increments[12];
uint8_t enabled_tones[128/8];
uint8_t tone[N_OSC];
uint8_t age[N_OSC];
uint8_t vel[N_OSC];
#if VOLUME_TRANSITION || VOLUME_WAIT_NEW_PHASE
uint8_t vel_bak[N_OSC];
#endif
uint8_t num_tones = 0;

static uint8_t next_char(void);
static void handle_midi(void);
int main(void);

void tx(uint8_t c) {
    while( !(UCSR0A & (1<<UDRE0)));
    UDR0 = c;
}

void tx_hex(uint8_t c) {
    static char const lut[16] = "0123456789ABCDEF";
    tx(lut[c>>4]);
    tx(lut[c&15]);
}

void tx_s(char const *s) {
    while(*s) {
        tx(*s);
        s++;
    }
}

__attribute__((always_inline))
static inline void do_osc(uint8_t n, volatile uint8_t *reg) {
        phase[n].i += increment[n];
        // skip the next if if carry is not set, i.e. we're not near a zeroed phase yet
        // using this instead of if(SREG&1) { ... } saves us /two/ whole cycles per channel and interrupt call!
         __asm__ volatile goto ("brcc %I0 \n" : /* No outputs */ : /* No inputs */ : /* No clobbers */ : assign);
        if(VOLUME_TRANSITION && vel_bak[n] < vel[n]) {
                vel_bak[n]++;
        } else if(VOLUME_TRANSITION && vel_bak[n] > vel[n]) {
                vel_bak[n]--;
        } else if(VOLUME_WAIT_NEW_PHASE) {
                vel_bak[n] = vel[n];
        }
assign:
        if(VOLUME_TRANSITION || VOLUME_WAIT_NEW_PHASE) {
                *reg = 0x80 ^ ((vel_bak[n] * wav[phase[n].b[sizeof(PhaseType)-1]]) >> 8);
        } else {
                *reg = 0x80 ^ ((vel[n] * wav[phase[n].b[sizeof(PhaseType)-1]]) >> 8);
        }
}
ISR(TIMER2_OVF_vect) {
    PORTD |= (1<<2);
    if(N_OSC > 0) do_osc(0, &OCR2A);
    if(N_OSC > 1) do_osc(1, &OCR2B);
    if(N_OSC > 2) do_osc(2, &OCR1BL);
    if(N_OSC > 3) do_osc(3, &OCR1AL);
    if(N_OSC > 4) do_osc(4, &OCR0B);
    if(N_OSC > 5) do_osc(5, &OCR0B);
    PORTD &= ~(1<<2);
}
static void set_tone(uint8_t oscillator, uint8_t key, uint8_t velocity) {
    if(tone[oscillator] != 255) {
        enabled_tones[tone[oscillator] >> 3] &= ~(1 << (tone[oscillator] & 0x07));
    }
    increment[oscillator] = increments[key%12] >> (HIGHEST_OCTAVE+1-key/12);
{'fraction_non_alphanumeric': 0.15962256732848437, 'fraction_numerical': 0.037939846667977194, 'mean_word_length': 3.2364696086594504, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 47, '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': '6006768', 'n_tokens_mistral': 2266, 'n_tokens_neox': 2157, 'n_words': 470}