Commit 88b28a54 authored by Jacob Vosmaer's avatar Jacob Vosmaer

Add 'make format' based on clang-format

parent dad1ec67
BasedOnStyle: LLVM
IndentWidth: 8
UseTab: Always
BreakBeforeBraces: Linux
AllowShortIfStatementsOnASingleLine: false
IndentCaseLabels: false
ForEachMacros: ['for_each_voice']
AlwaysBreakAfterReturnType: AllDefinitions
......@@ -104,3 +104,5 @@ pdf: $(PRG).pdf
%.png: %.fig
$(FIG2DEV) -L png $< $@
format:
clang-format -i -style=file *.c *.h
......@@ -10,7 +10,8 @@ static uint8_t _buf_tail;
void
buffer_push(uint8_t x)
{
ATOMIC_BLOCK(ATOMIC_RESTORESTATE) {
ATOMIC_BLOCK(ATOMIC_RESTORESTATE)
{
_buf[_buf_tail] = x;
_buf_tail = (_buf_tail + 1) % BUFFER_SIZE;
}
......@@ -19,7 +20,8 @@ buffer_push(uint8_t x)
uint8_t
buffer_pop(uint8_t *x)
{
ATOMIC_BLOCK(ATOMIC_RESTORESTATE) {
ATOMIC_BLOCK(ATOMIC_RESTORESTATE)
{
if (_buf_head == _buf_tail) {
return 1; // error: buffer is empty
}
......
......@@ -3,12 +3,12 @@
#include "dac.h"
#include "gate.h"
#include "midi.h"
#include "voice.h"
#include "mono.h"
#include "pitch.h"
#include "poly.h"
#include "poly2.h"
#include "mono.h"
#include "vibrato.h"
#include "pitch.h"
#include "voice.h"
enum _programs {
PGM_POLY,
......@@ -19,58 +19,63 @@ enum _programs {
static uint8_t current_program = NUM_PGM;
void (*pgm_init[NUM_PGM])(void) = {
poly_init,
poly2_init,
mono_init,
poly_init,
poly2_init,
mono_init,
};
void (*note_on[NUM_PGM])(uint8_t n) = {
poly_note_on,
poly2_note_on,
mono_note_on,
poly_note_on,
poly2_note_on,
mono_note_on,
};
void (*note_off[NUM_PGM])(uint8_t n) = {
poly_note_off,
poly2_note_off,
mono_note_off,
poly_note_off,
poly2_note_off,
mono_note_off,
};
void (*control_change[NUM_PGM])(uint8_t ctl, uint8_t val) = {
poly_control_change,
poly2_control_change,
mono_control_change,
poly_control_change,
poly2_control_change,
mono_control_change,
};
void (*pitch_bend[NUM_PGM])(uint16_t bend) = {
poly_pitch_bend,
poly2_pitch_bend,
mono_pitch_bend,
poly_pitch_bend,
poly2_pitch_bend,
mono_pitch_bend,
};
void (*update_clock[NUM_PGM])(uint8_t delta) = {
poly_update_clock,
poly2_update_clock,
mono_update_clock,
poly_update_clock,
poly2_update_clock,
mono_update_clock,
};
void
io_init(void)
{
// More outputs to save power; PORTB6/7 is XTAL
DDRB = _BV(PORTB0) | _BV(PORTB1) | _BV(PORTB2) | _BV(PORTB3) | _BV(PORTB4) | _BV(PORTB5);
// More outputs to save power; PORTB6/7 is XTAL
DDRB = _BV(PORTB0) | _BV(PORTB1) | _BV(PORTB2) | _BV(PORTB3) |
_BV(PORTB4) | _BV(PORTB5);
// More outputs to save power; PORTC6 is reset, "PORTC7" does not exist (?)
DDRC = _BV(PORTC0) | _BV(PORTC1) | _BV(PORTC2) | _BV(PORTC3) | _BV(PORTC4) | _BV(PORTC5);
// More outputs to save power; PORTC6 is reset, "PORTC7" does not exist
// (?)
DDRC = _BV(PORTC0) | _BV(PORTC1) | _BV(PORTC2) | _BV(PORTC3) |
_BV(PORTC4) | _BV(PORTC5);
// PORTD0 is UART RX. Set all other PORTD pins output, to save power.
DDRD = _BV(PORTD1) | _BV(PORTD2) | _BV(PORTD3) | _BV(PORTD4) | _BV(PORTD5) | _BV(PORTD6) | _BV(PORTD7);
DDRD = _BV(PORTD1) | _BV(PORTD2) | _BV(PORTD3) | _BV(PORTD4) |
_BV(PORTD5) | _BV(PORTD6) | _BV(PORTD7);
}
void
clock_init(void)
{
// Run Timer0 (8-bit timer) at F_CPU/1024 -- faster incurs roll arounds. Use 16 bit timer?
// Run Timer0 (8-bit timer) at F_CPU/1024 -- faster incurs roll arounds.
// Use 16 bit timer?
TCCR0B |= _BV(CS00) | _BV(CS02);
}
......
#include <avr/io.h>
#include <util/delay.h>
#include "dac.h"
#include "voice.h"
#include "safe_add.h"
#include "voice.h"
#include <avr/io.h>
#include <util/delay.h>
const uint8_t dac_chan[NUM_VOICES] = {2, 3, 4, 5};
// Offset to get a consistent "zero" on each CV output
const uint16_t dac_offset[NUM_VOICES] = {
57,
48,
50,
55,
57,
48,
50,
55,
};
// This number corresponds to a 1V increase at the respective CV output.
const uint16_t dac_scale[NUM_VOICES] = {
8382,
8382,
8383,
8384,
8382,
8382,
8383,
8384,
};
enum { NUM_SEMITONES = 12 };
// This tuning is chosen to be 'good enough' for the 8382-8384 scale range above
const int16_t dac_semitones[NUM_SEMITONES] = {
0,
699,
1397,
2096,
2794,
3493,
4192,
4890,
5589,
6287,
6986,
7684,
0, 699, 1397, 2096, 2794, 3493, 4192, 4890, 5589, 6287, 6986, 7684,
};
uint16_t
dac_pitch_delta(uint8_t semitones)
{
if (semitones >= NUM_SEMITONES) {
semitones = NUM_SEMITONES -1;
semitones = NUM_SEMITONES - 1;
}
return dac_semitones[semitones];
......@@ -134,8 +123,6 @@ dac_note_pitch(uint8_t voice, uint8_t note)
return 0;
}
return safe_add(
dac_offset[voice] + octave * dac_scale[voice],
dac_semitones[(note - MIDI_LOWEST) % NUM_SEMITONES]
);
return safe_add(dac_offset[voice] + octave * dac_scale[voice],
dac_semitones[(note - MIDI_LOWEST) % NUM_SEMITONES]);
}
#include <avr/io.h>
#include "voice.h"
#include "gate.h"
#include "pitch.h"
#include "voice.h"
#include <avr/io.h>
static uint8_t _gate_retrig[NUM_VOICES];
enum {
// RETRIG_DELAY should be as low as possible to reduce MIDI latency, but
// high enough to allow the hardware envelope generators in the CrowBX to
// high enough to allow the hardware envelope generators in the CrowBX
// to
// reset. I know 0x3f to be too low.
RETRIG_DELAY = 0x5f,
};
......@@ -15,7 +16,7 @@ enum {
void
gate_init(void)
{
for_each_voice(v) {
for_each_voice (v) {
gate_off(v);
}
}
......@@ -56,17 +57,17 @@ gate_on(uint8_t v)
void
gate_update(uint8_t delta)
{
for_each_voice(v) {
for_each_voice (v) {
if (_gate_retrig[v] == 0) {
continue;
}
if (delta < _gate_retrig[v]) {
_gate_retrig[v] -= delta;
continue;
}
_gate_retrig[v] = 0;
gate_on_legato(v);
gate_on_legato(v);
}
}
#include <stdio.h>
#include "test.h"
#include "list.h"
#include "test.h"
#include <stdio.h>
#define TEST_ARRAY_SIZE 10
INIT_LIST(l, TEST_ARRAY_SIZE);
......@@ -162,7 +162,7 @@ test_index(void)
}
int
main(int argc, char** argv)
main(int argc, char **argv)
{
test_flush();
test_empty();
......
......@@ -41,7 +41,6 @@ l_append(struct list *l, uint8_t x)
l->array[x] = l->sup;
}
void
l_delete(struct list *l, uint8_t x)
{
......
......@@ -5,17 +5,17 @@
struct list {
uint8_t head;
uint8_t * const array;
uint8_t *const array;
const uint8_t sup;
};
#define INIT_LIST(name, size) \
static uint8_t _ ## name ## _array[size]; \
static struct list _ ## name = {\
.array = _ ## name ## _array,\
.sup = size,\
};\
static struct list *name = &_ ## name;
#define INIT_LIST(name, size) \
static uint8_t _##name##_array[size]; \
static struct list _##name = { \
.array = _##name##_array, \
.sup = size, \
}; \
static struct list *name = &_##name;
void l_push(struct list *l, uint8_t x);
void l_append(struct list *l, uint8_t x);
......
#include <stdio.h>
#include <assert.h>
#include "midi.h"
#include "midi-test.h"
#include "midi.h"
#include <assert.h>
#include <stdio.h>
void
test_init(void)
......@@ -25,7 +25,6 @@ test_note_on(void)
midi_parse(0x64);
assert_status(MIDI_NOTE_ON, 1, 0x6064);
// running status
midi_parse(0x10);
assert_status(MIDI_NOTE_ON, 0, 0);
......@@ -170,7 +169,7 @@ test_pitch_bend(void)
}
int
main(int argc, char** argv)
main(int argc, char **argv)
{
test_init();
test_note_on();
......
......@@ -8,18 +8,26 @@ void status_init(void);
void midi_parse(uint8_t);
uint8_t midi_load(uint8_t *status, uint8_t *data1, uint8_t *data2);
#define assert_status(status, avail, data) { \
uint8_t _status, _avail, _data1, _data2; \
_avail = midi_load(&_status, &_data1, &_data2); \
assert((_avail != MIDI_READ_OK) == !avail); \
if (_avail == MIDI_READ_OK) { \
assert(((_data1 << 8) | _data2) == data); \
assert(_status == status); \
} \
}
#define assert_status(status, avail, data) \
{ \
uint8_t _status, _avail, _data1, _data2; \
_avail = midi_load(&_status, &_data1, &_data2); \
assert((_avail != MIDI_READ_OK) == !avail); \
if (_avail == MIDI_READ_OK) { \
assert(((_data1 << 8) | _data2) == data); \
assert(_status == status); \
} \
}
// Fake uart functions to let midi.c compile
void uart_init(void) { }
uint8_t uart_read(uint8_t *data) { return 0; }
void
uart_init(void)
{
}
uint8_t
uart_read(uint8_t *data)
{
return 0;
}
#endif
......@@ -20,19 +20,18 @@ midi_set_channel(uint8_t channel)
uint8_t _status;
enum {
MIDI_MAX_DATA_LEN = 2,
enum { MIDI_MAX_DATA_LEN = 2,
};
uint8_t data[MIDI_MAX_DATA_LEN];
uint8_t parsed_len;
const uint8_t statuses[MIDI_NUM_STATUSES] = {
0b1001, // note on
0b1000, // note off
0b1100, // program change
0b1011, // control change
0b1110, // pitch bend
0b1001, // note on
0b1000, // note off
0b1100, // program change
0b1011, // control change
0b1110, // pitch bend
};
uint8_t
......@@ -101,7 +100,8 @@ midi_parse(uint8_t midi_byte)
}
if (midi_byte >= 128) {
// We were expecting a 7-bit midi data value. Something is wrong.
// We were expecting a 7-bit midi data value. Something is
// wrong.
status_init();
return;
}
......
......@@ -23,9 +23,8 @@ enum _cc {
void midi_init(void);
void midi_set_channel(uint8_t channel);
enum {
MIDI_READ_OK,
MIDI_READ_NO_DATA,
enum { MIDI_READ_OK,
MIDI_READ_NO_DATA,
};
uint8_t midi_read(uint8_t *status, uint8_t *data1, uint8_t *data2);
......
#include "mono.h"
#include "pitch.h"
#include "gate.h"
#include "voice.h"
#include "list.h"
#include "vibrato.h"
#include "midi.h"
#include "notes.h"
#include "pitch.h"
#include "vibrato.h"
#include "voice.h"
#include <util/delay.h>
......@@ -17,10 +17,10 @@ static uint8_t current_note;
void mono_set_pitch(void);
const float detune_factor[NUM_VOICES] = {
1.2,
-1.2,
0.6,
-0.6,
1.2,
-1.2,
0.6,
-0.6,
};
static uint8_t detune_amount;
......@@ -42,7 +42,7 @@ mono_note_on(uint8_t n)
l_push(notes, n);
mono_set_pitch();
for_each_voice(v) {
for_each_voice (v) {
gate_on(v);
}
}
......@@ -54,7 +54,7 @@ mono_set_pitch(void)
current_note = l_first(notes);
}
for_each_voice(v) {
for_each_voice (v) {
int16_t detune = detune_factor[v] * detune_amount;
detune += vibrato_detune();
pitch_set_note(v, current_note, detune, current_bend);
......@@ -76,14 +76,14 @@ mono_note_off(uint8_t n)
}
if (l_empty(notes)) {
for_each_voice(v) {
for_each_voice (v) {
gate_off(v);
}
return;
}
// Re-trigger previously playing note
for_each_voice(v) {
for_each_voice (v) {
gate_on(v);
}
......@@ -116,7 +116,7 @@ mono_pitch_bend(uint16_t bend)
void
mono_update_clock(uint8_t delta)
{
(void) delta;
(void)delta;
mono_set_pitch();
}
#ifndef _notes_h
#define _notes_h
enum {
NUM_NOTES = 128,
enum { NUM_NOTES = 128,
};
#endif
#include "pitch.h"
#include "voice.h"
#include "dac.h"
#include "safe_add.h"
#include "voice.h"
#include <math.h>
static uint16_t _decay_counter[NUM_VOICES];
......@@ -15,7 +15,7 @@ enum { DECAY_SCALE = 10000 };
void
pitch_init(void)
{
for_each_voice(v) {
for_each_voice (v) {
_decay_counter[v] = 0;
}
_env_decay = 0;
......@@ -34,7 +34,8 @@ pitch_set_note(uint8_t voice, uint8_t note, int16_t detune, uint16_t bend)
data = safe_add(data, detune);
data = safe_add(data, _env_delta[voice]);
float bend_factor = ((int16_t) (bend & BEND_MAX) - BEND_CENTER) / ((float) BEND_MAX);
float bend_factor =
((int16_t)(bend & BEND_MAX) - BEND_CENTER) / ((float)BEND_MAX);
int16_t bend_data = dac_pitch_delta(4) * bend_factor;
data = safe_add(data, bend_data);
......@@ -44,7 +45,7 @@ pitch_set_note(uint8_t voice, uint8_t note, int16_t detune, uint16_t bend)
void
pitch_update_clock(uint8_t delta)
{
for_each_voice(v) {
for_each_voice (v) {
if (_decay_counter[v] <= delta) {
_decay_counter[v] = 0;
_env_delta[v] = 0;
......@@ -52,7 +53,9 @@ pitch_update_clock(uint8_t delta)
}
_decay_counter[v] = _decay_counter[v] - delta;
_env_delta[v] = -log((DECAY_SCALE - _decay_counter[v] + 1) / ((float) DECAY_SCALE)) * (_env_amount / 127.0) * dac_pitch_delta(5);
_env_delta[v] = -log((DECAY_SCALE - _decay_counter[v] + 1) /
((float)DECAY_SCALE)) *
(_env_amount / 127.0) * dac_pitch_delta(5);
}
}
......@@ -83,5 +86,5 @@ pitch_env_trigger(uint8_t voice)
return;
}
_decay_counter[voice] = (_env_decay/127.0) * DECAY_SCALE;
_decay_counter[voice] = (_env_decay / 127.0) * DECAY_SCALE;
}
......@@ -11,9 +11,8 @@ void pitch_env_set_amount(uint8_t amount);
void pitch_env_trigger(uint8_t voice);
void pitch_init(void);
enum {
BEND_CENTER = 8192,
BEND_MAX = 16383,
enum { BEND_CENTER = 8192,
BEND_MAX = 16383,
};
#endif
#include "pitch.h"
#include "poly.h"
#include "gate.h"
#include "voice.h"
#include "poly_voice.h"
#include "vibrato.h"
#include "midi.h"
#include "notes.h"
#include "pitch.h"
#include "poly_voice.h"
#include "vibrato.h"
#include "voice.h"
static uint8_t playing[NUM_VOICES];
static uint8_t current_note[NUM_VOICES];
......@@ -16,7 +16,7 @@ poly_init(void)
{
voice_init();
current_bend = BEND_CENTER;
for_each_voice(v) {
for_each_voice (v) {
playing[v] = NUM_NOTES;
current_note[v] = NUM_NOTES;
}
......@@ -33,7 +33,8 @@ poly_note_on(uint8_t note)
{
uint8_t v = voice_acquire(note);
if (v == NUM_VOICES) {
// This should never happen if the voice assigner is working correctly.
// This should never happen if the voice assigner is working
// correctly.
return;
}
......@@ -45,7 +46,7 @@ poly_note_on(uint8_t note)
void
poly_note_off(uint8_t note)
{
for_each_voice(v) {
for_each_voice (v) {
if (playing[v] == note) {
playing[v] = NUM_NOTES;
voice_release(v);
......@@ -76,9 +77,9 @@ poly_pitch_bend(uint16_t bend)
void
poly_update_clock(uint8_t delta)
{
(void) delta;
(void)delta;
for_each_voice(v) {
for_each_voice (v) {
poly_set_pitch(v, current_note[v]);
}
}
#include "pitch.h"
#include "poly2.h"
#include "gate.h"
#include "voice.h"
#include "vibrato.h"
#include "midi.h"
#include "list.h"
#include "midi.h"
#include "notes.h"
#include "pitch.h"
#include "vibrato.h"
#include "voice.h"
static uint8_t playing[NUM_VOICES];
static uint16_t current_bend;
......@@ -21,7 +21,7 @@ poly2_init(void)
{
l_flush(want_play);
l_flush(avail);
for_each_voice(i) {
for_each_voice (i) {
playing[i] = NUM_NOTES;
current_note[i] = NUM_NOTES;
l_push(avail, i);
......@@ -50,7 +50,7 @@ poly2_note_on(uint8_t note)
static uint8_t
poly2_is_playing(uint8_t note)
{
for_each_voice(v) {
for_each_voice (v) {
if (playing[v] == note) {
return 1;
}
......@@ -62,19 +62,18 @@ poly2_is_playing(uint8_t note)
void
poly2_update_playing(void)
{
for_each_voice(v) {
if ((playing[v] < NUM_NOTES) && !(l_index(want_play, playing[v]) < NUM_VOICES)) {
for_each_voice (v) {
if ((playing[v] < NUM_NOTES) &&
!(l_index(want_play, playing[v]) < NUM_VOICES)) {
gate_off(v);
playing[v] = NUM_NOTES;
l_push(avail, v);
}
}
for (
uint8_t i = 0, j = want_play->head;
(i < NUM_VOICES) && (j != want_play->sup);
i++, (j = l_succ(want_play, j))
) {
for (uint8_t i = 0, j = want_play->head;
(i < NUM_VOICES) && (j != want_play->sup);
i++, (j = l_succ(want_play, j))) {
if (poly2_is_playing(j)) {
continue;
}
......@@ -126,9 +125,9 @@ poly2_pitch_bend(uint16_t bend)
void
poly2_update_clock(uint8_t delta)
{
(void) delta;
(void)delta;
for_each_voice(v) {
for_each_voice (v) {
poly2_set_pitch(v, current_note[v]);
}
}
......@@ -13,8 +13,8 @@
// avail: 0,1,2,3 playing: empty
// -- play voice 0
// avail: 1,2,3 playing: 0
// -- play voice 1. Note that we are drawing the 'playing' structure backwards here.
// avail: 2, 3 playing: 0, 1 <- playing_head
// -- play voice 1. Note that we are drawing the 'playing' structure backwards
// here. avail: 2, 3 playing: 0, 1 <- playing_head
// -- release voice 0
// avail: 2, 3, 0 playing: 1
// -- play voice 2
......@@ -23,8 +23,8 @@
// avail: 0 playing: 1, 2, 3
// -- play voice 0
// avail: empty playing: 1, 2, 3, 0
// -- new voice is requested: we must steal one first. voice 1 is the oldest now; we release it.
// avail: 1 playing: 2, 3, 0
// -- new voice is requested: we must steal one first. voice 1 is the oldest
// now; we release it. avail: 1 playing: 2, 3, 0
// -- now use voice 1 and add it to the back (playing_head) of the playing list
// avail: empty playing: 2, 3, 0, 1
......@@ -87,7 +87,7 @@ void
voice_init(void)
{
l_flush(avail);
for_each_voice(i) {
for_each_voice (i) {
avail_enqueue(i);
}
......
......@@ -8,7 +8,8 @@ void voice_init(void);
// voice_acquire returns a voice number for playing a new note
uint8_t voice_acquire();
// voice_release releases a voice, if playing. If v is not playing this is a no-op.
// voice_release releases a voice, if playing. If v is not playing this is a
// no-op.
void voice_release(uint8_t v);
#endif
#include <avr/io.h>
#include <avr/interrupt.h>
#include "uart.h"
#include "buffer.h"
#include <avr/interrupt.h>
#include <avr/io.h>
ISR(USART_RX_vect)
{
......
......@@ -5,4 +5,3 @@ uint8_t uart_read(uint8_t *data);
void uart_init(void);
#endif
......@@ -7,10 +7,9 @@ static uint8_t rate;
static int16_t vibrato;
static uint8_t depth;
enum {
NUM_CC = 128,
LFO_RANGE = 8, // LFO rate range in octaves
PHASE_MIN_INCR = 20000, // minimum phase increment
enum { NUM_CC = 128,
LFO_RANGE = 8, // LFO rate range in octaves
PHASE_MIN_INCR = 20000, // minimum phase increment
};
static float exp_table[NUM_CC];
......@@ -19,7 +18,8 @@ void
vibrato_precompute(void)
{