]> git.cworth.org Git - scherzo/blob - score.h
Restructure code to manually add staves, braces, and notes.
[scherzo] / score.h
1 /* scherzo - Music notation training
2  *
3  *      score - Utilities for drawing (simple) musical scores
4  *
5  * Copyright © 2010 Carl Worth
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation, either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program.  If not, see http://www.gnu.org/licenses/ .
19  */
20
21 #ifndef SCORE_H
22 #define SCORE_H
23
24 #include <talloc.h>
25 #include <cairo.h>
26
27 typedef struct score score_t;
28 typedef struct score_staff score_staff_t;
29 typedef struct score_note score_note_t;
30
31 typedef enum score_pitch
32 {
33     SCORE_PITCH_Aff,
34     SCORE_PITCH_Af,
35     SCORE_PITCH_A,
36     SCORE_PITCH_As,
37     SCORE_PITCH_Ass,
38
39     SCORE_PITCH_Bff,
40     SCORE_PITCH_Bf,
41     SCORE_PITCH_B,
42     SCORE_PITCH_Bs,
43     SCORE_PITCH_Bss,
44
45     SCORE_PITCH_Cff,
46     SCORE_PITCH_Cf,
47     SCORE_PITCH_C,
48     SCORE_PITCH_Cs,
49     SCORE_PITCH_Css,
50
51     SCORE_PITCH_Dff,
52     SCORE_PITCH_Df,
53     SCORE_PITCH_D,
54     SCORE_PITCH_Ds,
55     SCORE_PITCH_Dss,
56
57     SCORE_PITCH_Eff,
58     SCORE_PITCH_Ef,
59     SCORE_PITCH_E,
60     SCORE_PITCH_Es,
61     SCORE_PITCH_Ess,
62
63     SCORE_PITCH_Fff,
64     SCORE_PITCH_Ff,
65     SCORE_PITCH_F,
66     SCORE_PITCH_Fs,
67     SCORE_PITCH_Fss,
68
69     SCORE_PITCH_Gff,
70     SCORE_PITCH_Gf,
71     SCORE_PITCH_G,
72     SCORE_PITCH_Gs,
73     SCORE_PITCH_Gss
74 } score_pitch_t;
75
76 typedef enum score_duration
77 {
78     SCORE_DURATION_WHOLE = 1,
79     SCORE_DURATION_1 = 1,
80     SCORE_DURATION_HALF = 2,
81     SCORE_DURATION_2 = 2,
82     SCORE_DURATION_QUARTER = 4,
83     SCORE_DURATION_4 = 4,
84     SCORE_DURATION_EIGHTH = 8,
85     SCORE_DURATION_8 = 8,
86     SCORE_DURATION_SIXTEENTH = 16,
87     SCORE_DURATION_16 = 16,
88     SCORE_DURATION_THIRTYSECOND = 32,
89     SCORE_DURATION_32 = 32,
90     SCORE_DURATION_SIXTYFOURTH = 64,
91     SCORE_DURATION_64 = 64,
92     SCORE_DURATION_ONEHUNDREDTWENTYEIGHTH = 128,
93     SCORE_DURATION_128 = 128
94 } score_duration_t;
95
96 #define SCORE_BUILD_NOTE(pitch, octave, duration) SCORE_PITCH_##pitch, (octave), SCORE_DURATION_##duration
97
98 typedef enum score_clef
99 {
100     SCORE_CLEF_G,
101     SCORE_CLEF_F
102 } score_clef_t;
103
104 /* Allocate a new, empty score object, (with optional ctx as talloc
105  * owner). If ctx is NULL, the caller should call talloc_free on the
106  * score_t* when done with it. Otherwise, the object will be freed
107  * when ctx is freed. */
108 score_t *
109 score_create (void *ctx);
110
111 /* Set an (approximate) staff height. The actual staff height may
112  * differ due to rounding to achieve evenly spaced, sharply rendered
113  * lines. the actual staff height is returned. */
114 int
115 score_set_staff_height (score_t *score, int height);
116
117 /* Set the total width available for drawing the score. */
118 void
119 score_set_width (score_t *score, int width);
120
121 /* Add a brace to the score, connecting the given number of staves.
122  *
123  * The staves to be connected are those that will next be added to the
124  * score. */
125 void
126 score_add_brace (score_t *score, int staves);
127
128 /* Add a new staff to the score */
129 score_staff_t *
130 score_add_staff (score_t *score, score_clef_t clef);
131
132 /* Add a note to a staff of the given pitch, octave, and duration.
133  *
134  * Octave numbers are ISO octave numbers [0:8], (so Octave 4 is from
135  * middle C to the B above middle C).
136  *
137  * Duration values can be symbolic (SCORE_DURATION_WHOLE, _QUARTER,
138  * _EIGHTH, etc.) or numerical as simply the denominator (WHOLE=1,
139  * QUARTER=4, EIGHTH=8, etc.)
140  */
141 score_note_t *
142 score_staff_add_note (score_staff_t *staff,
143                       score_pitch_t pitch,
144                       int octave,
145                       score_duration_t);
146
147 /* Draw the given score_t onto the given cairo_t.
148  *
149  * The caller can call cairo_translate before calling score_draw to
150  * position the result as desired, (and can call cairo_clip to clip it
151  * if desired). */
152 void
153 score_draw (score_t *score, cairo_t *cr);
154
155 #endif