Line data Source code
1 : /* Copyright 2017 noseglasses <shinynoseglasses@gmail.com>
2 : *
3 : * This program is free software: you can redistribute it and/or modify
4 : * it under the terms of the GNU Lesser General Public License as published by
5 : * the Free Software Foundation, either version 3 of the License, or
6 : * (at your option) any later version.
7 : *
8 : * This program is distributed in the hope that it will be useful,
9 : * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 : * GNU Lesser General Public License for more details.
12 : *
13 : * You should have received a copy of the GNU Lesser General Public License
14 : * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 : */
16 :
17 : #include "ppg_leader_sequences.h"
18 : #include "detail/ppg_token_detail.h"
19 : #include "detail/ppg_note_detail.h"
20 : #include "detail/ppg_pattern_detail.h"
21 :
22 6 : void ppg_alphabetic_leader_sequences(
23 : PPG_Layer layer,
24 : PPG_Token leader_token,
25 : uint8_t n_sequences,
26 : PPG_Leader_Functions functions,
27 : bool allow_fallback)
28 6 : {
29 6 : PPG_Token__ *leader = (PPG_Token__ *)leader_token;
30 :
31 6 : PPG_Token__ *leafs[n_sequences];
32 :
33 24 : for(uint8_t seq = 0; seq < n_sequences; ++seq) {
34 :
35 : char buffer[PPG_MAX_SEQUENCE_LENGTH];
36 :
37 : PPG_Token__ *tokens[PPG_MAX_SEQUENCE_LENGTH];
38 :
39 : // Create a note sequence
40 : //
41 18 : functions.retreive_string(seq, buffer, PPG_MAX_SEQUENCE_LENGTH);
42 :
43 : // printf("buffer: \'%s\'\n", buffer);
44 :
45 18 : PPG_Count n_tokens = 0;
46 :
47 132 : for(int i = 0; buffer[i]; i++) {
48 :
49 114 : if(i >= PPG_MAX_SEQUENCE_LENGTH) {
50 0 : PPG_ERROR("Leader sequence \'%s\' exceeds maximum tolerated length %d\n",
51 : buffer, PPG_MAX_SEQUENCE_LENGTH);
52 : }
53 :
54 114 : PPG_Input_Id input = functions.input_from_char(buffer[i]);
55 :
56 114 : tokens[i] = ppg_note_create_standard(input);
57 :
58 114 : ++n_tokens;
59 : }
60 :
61 18 : PPG_Action action
62 18 : = functions.retreive_action(seq);
63 :
64 18 : ppg_token_store_action(tokens[n_tokens - 1], action);
65 :
66 18 : leafs[seq] = tokens[n_tokens - 1];
67 :
68 18 : ppg_pattern_from_list(leader, layer, n_tokens, tokens);
69 : }
70 :
71 6 : if(!allow_fallback) { return; }
72 :
73 : // Check all the leafs and walk back along the branches
74 : // to the first furcation and add fallback tags
75 : //
76 24 : for(uint8_t seq = 0; seq < n_sequences; ++seq) {
77 :
78 18 : PPG_Token__ *token = leafs[seq];
79 18 : PPG_Token__ *child = NULL;
80 :
81 150 : while( (token != leader)
82 123 : && (token->n_children <= 1)) {
83 :
84 114 : if(!child) {
85 :
86 18 : child = token;
87 :
88 18 : token = token->parent;
89 :
90 18 : continue;
91 : }
92 :
93 96 : token->misc.action_flags |= PPG_Action_Fallback;
94 :
95 96 : token->action = child->action;
96 :
97 96 : child = token;
98 :
99 96 : token = token->parent;
100 : }
101 : }
102 : }
|