Skip to content

Commit

Permalink
Close #36: Support for dotted notes (#51)
Browse files Browse the repository at this point in the history
* Update Note type to allow for dots
* Update NoteCompiler to handle dotted notes
* Update examples
  • Loading branch information
zzril authored Dec 5, 2024
1 parent 55fd1c1 commit ab3aab5
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 22 deletions.
4 changes: 2 additions & 2 deletions examples/example2.aula
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
bpm: 140
bpm: 115

track:
|| F F C C | D D Co | A A G G | Fo F ; ||
|| D F G ;^ D^ | ;^ F Ab^ Go | D F G. F^ | F^ Do.. ||

4 changes: 2 additions & 2 deletions examples/example3.aula
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
bpm: 115
bpm: 114

track:
|| D F G ;^ D^ | ;^ F Ab^ Go | D F G G^ F^ | F^ D^ D Do ||
|| ;o. E^^ F#^^ G^^ E^^ | B^ ;^^ B^ ;^^ A ;^ D^^ E^^ F#^^ D^^ | A^ ;^^ A^ ;^^ G F#^ E^^ F#^^ G^^ E^^ | G A^ F#^. E^^ D D^ | A Go ; ||

5 changes: 0 additions & 5 deletions examples/example4.aula

This file was deleted.

28 changes: 23 additions & 5 deletions src/note.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,37 @@

// --------

void Note_init_at(Note* note, int8_t pitch, int8_t length) {
static float get_absolute_granularity(const Note* note);

static size_t absolute_length_to_samples(float length);

// --------

static float get_absolute_granularity(const Note* note) {
return pow(2, note->length - note->dots);
}

static size_t absolute_length_to_samples(float length) {
return (size_t) (Config_get_sample_frames_per_bar() * length);
}

// --------

void Note_init_at(Note* note, int8_t pitch, int8_t length, uint8_t dots) {

note->pitch = pitch;
note->length = length;
note->dots = dots;
note->is_rest = false;

return;
}

void Note_init_rest_at(Note* note, int8_t length) {
void Note_init_rest_at(Note* note, int8_t length, uint8_t dots) {

note->pitch = INT8_MIN;
note->length = length;
note->dots = dots;
note->is_rest = true;

return;
Expand All @@ -37,14 +55,14 @@ float Note_get_frequency(const Note* note) {
}

float Note_get_absolute_length(const Note* note) {
return pow(2, note->length);
return pow(2, (note->length + 1)) - pow(2, note->length - note->dots);
}

size_t Note_get_length_in_samples(const Note* note) {
return (size_t) (Config_get_sample_frames_per_bar() * Note_get_absolute_length(note));
return absolute_length_to_samples(Note_get_absolute_length(note));
}

size_t Note_get_granularity_in_samples(const Note* note) {
return Note_get_length_in_samples(note);
return absolute_length_to_samples(get_absolute_granularity(note));
}

5 changes: 3 additions & 2 deletions src/note.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,14 @@ typedef struct Note Note;
struct Note {
int8_t pitch;
int8_t length;
uint8_t dots;
bool is_rest;
};

// --------

void Note_init_at(Note* note, int8_t pitch, int8_t length);
void Note_init_rest_at(Note* note, int8_t length);
void Note_init_at(Note* note, int8_t pitch, int8_t length, uint8_t dots);
void Note_init_rest_at(Note* note, int8_t length, uint8_t dots);

bool Note_is_rest(const Note* note);
float Note_get_frequency(const Note* note);
Expand Down
29 changes: 23 additions & 6 deletions src/note_compiler.c
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,9 @@ static int finish_note(NoteCompiler* compiler, Note* note) {
int8_t half = 0;
int8_t pitch = 0;
int8_t length = -2;
bool frequence_parsing_finished = false;
uint8_t dots = 0;
bool finished_frequence_parsing = false;
bool finished_length_exponent_parsing = false;
bool note_finished = false;

char symbol = compiler->symbol;
Expand Down Expand Up @@ -87,7 +89,7 @@ static int finish_note(NoteCompiler* compiler, Note* note) {
}
}

while(!frequence_parsing_finished && !compiler->finished && (c = peek(compiler)) >= 0) {
while(!finished_frequence_parsing && !compiler->finished && (c = peek(compiler)) >= 0) {

switch(c) {

Expand All @@ -102,13 +104,13 @@ static int finish_note(NoteCompiler* compiler, Note* note) {
break;

default:
frequence_parsing_finished = true;
finished_frequence_parsing = true;
break;
}
}
}

while(!note_finished && !compiler->finished && (c = peek(compiler)) >= 0) {
while(!finished_length_exponent_parsing && !compiler->finished && (c = peek(compiler)) >= 0) {

switch(c) {

Expand All @@ -122,14 +124,29 @@ static int finish_note(NoteCompiler* compiler, Note* note) {
length--;
continue;

default:
finished_length_exponent_parsing = true;
break;
}
}

while(!note_finished && !compiler->finished && (c = peek(compiler)) >= 0) {

switch(c) {

case '.':
advance(compiler);
dots++;
continue;

default:
note_finished = true;
break;
}
}

if(rest) {
Note_init_rest_at(note, length);
Note_init_rest_at(note, length, dots);
return 0;
}

Expand All @@ -138,7 +155,7 @@ static int finish_note(NoteCompiler* compiler, Note* note) {
return status;
}

Note_init_at(note, pitch, length);
Note_init_at(note, pitch, length, dots);

return 0;
}
Expand Down

0 comments on commit ab3aab5

Please sign in to comment.