]> git.cworth.org Git - scherzo/blob - score.h
d5391012bb428e26f901a8432535ce797f3c8700
[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 #include "pitch.h"
28
29 typedef enum score_duration
30 {
31     SCORE_DURATION_WHOLE = 1,
32     SCORE_DURATION_1 = 1,
33     SCORE_DURATION_HALF = 2,
34     SCORE_DURATION_2 = 2,
35     SCORE_DURATION_QUARTER = 4,
36     SCORE_DURATION_4 = 4,
37     SCORE_DURATION_EIGHTH = 8,
38     SCORE_DURATION_8 = 8,
39     SCORE_DURATION_SIXTEENTH = 16,
40     SCORE_DURATION_16 = 16,
41     SCORE_DURATION_THIRTYSECOND = 32,
42     SCORE_DURATION_32 = 32,
43     SCORE_DURATION_SIXTYFOURTH = 64,
44     SCORE_DURATION_64 = 64,
45     SCORE_DURATION_ONEHUNDREDTWENTYEIGHTH = 128,
46     SCORE_DURATION_128 = 128
47 } score_duration_t;
48
49 #define SCORE_BUILD_NOTE(pitch, octave, duration) SCORE_PITCH_##pitch, (octave), SCORE_DURATION_##duration
50
51 typedef struct score score_t;
52 typedef struct score_staff score_staff_t;
53
54 typedef struct score_note
55 {
56     score_staff_t *staff;
57     pitch_t pitch;
58     score_duration_t duration;
59
60     struct {
61         double r;
62         double g;
63         double b;
64     } color;
65 } score_note_t;
66
67 typedef struct score_chord
68 {
69     score_staff_t *staff;
70
71     char *name;
72     double width;
73 } score_chord_t;
74
75 typedef enum score_clef
76 {
77     SCORE_CLEF_G,
78     SCORE_CLEF_TREBLE = SCORE_CLEF_G,
79     SCORE_CLEF_F,
80     SCORE_CLEF_BASS = SCORE_CLEF_F
81 } score_clef_t;
82
83 /* Allocate a new, empty score object, (with optional ctx as talloc
84  * owner). If ctx is NULL, the caller should call talloc_free on the
85  * score_t* when done with it. Otherwise, the object will be freed
86  * when ctx is freed. */
87 score_t *
88 score_create (void *ctx);
89
90 /* Set an (approximate) staff height. The actual staff height may
91  * differ due to rounding to achieve evenly spaced, sharply rendered
92  * lines. the actual staff height is returned. */
93 int
94 score_set_staff_height (score_t *score, int height);
95
96 /* Set the total width available for drawing the score. */
97 void
98 score_set_width (score_t *score, int width);
99
100 /* Add a brace to the score, connecting the given number of staves.
101  *
102  * The staves to be connected are those that will next be added to the
103  * score. */
104 void
105 score_add_brace (score_t *score, int staves);
106
107 /* Add a new staff to the score */
108 score_staff_t *
109 score_add_staff (score_t *score, score_clef_t clef);
110
111 /* Add a note to a staff of the given pitch and duration.
112  *
113  * Duration values can be symbolic (SCORE_DURATION_WHOLE, _QUARTER,
114  * _EIGHTH, etc.) or numerical as simply the denominator (WHOLE=1,
115  * QUARTER=4, EIGHTH=8, etc.)
116  */
117 score_note_t *
118 score_add_note (score_staff_t *staff,
119                 pitch_t pitch,
120                 score_duration_t);
121
122 /* Add a chord symbol of 'name' to a staff.
123  *
124  * For now, the chord symbols are free-form names.
125  *
126  * The chord name must be a talloc'ed string, which the returned
127  * score_chord_t will talloc_steal.
128  */
129 score_chord_t *
130 score_add_chord (score_staff_t *staff,
131                  const char * name);
132
133 /* Remove the given chord from its staff. */
134 void
135 score_remove_chord (score_chord_t *chord);
136
137 /* Remove the given note from its staff. */
138 void
139 score_remove_note (score_note_t *note);
140
141 void
142 score_set_note_color_rgb (score_note_t *note,
143                           double r,
144                           double g,
145                           double b);
146
147 /* Return the first note on the given staff with the given pitch,
148  * octave, and durations. Returns NULL if no match is found. */
149 score_note_t *
150 score_staff_find_note (score_staff_t *staff,
151                        pitch_t pitch,
152                        score_duration_t duration);
153
154 /* Draw the given score_t onto the given cairo_t.
155  *
156  * The caller can call cairo_translate before calling score_draw to
157  * position the result as desired, (and can call cairo_clip to clip it
158  * if desired). */
159 void
160 score_draw (score_t *score, cairo_t *cr);
161
162 #endif