]> git.cworth.org Git - scherzo/blob - score.h
Drop the enumerated pitch values.
[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     int octave;
59     score_duration_t duration;
60
61     struct {
62         double r;
63         double g;
64         double b;
65     } color;
66 } score_note_t;
67
68 typedef struct score_chord
69 {
70     score_staff_t *staff;
71
72     char *name;
73     double width;
74 } score_chord_t;
75
76 typedef enum score_clef
77 {
78     SCORE_CLEF_G,
79     SCORE_CLEF_TREBLE = SCORE_CLEF_G,
80     SCORE_CLEF_F,
81     SCORE_CLEF_BASS = SCORE_CLEF_F
82 } score_clef_t;
83
84 /* Allocate a new, empty score object, (with optional ctx as talloc
85  * owner). If ctx is NULL, the caller should call talloc_free on the
86  * score_t* when done with it. Otherwise, the object will be freed
87  * when ctx is freed. */
88 score_t *
89 score_create (void *ctx);
90
91 /* Set an (approximate) staff height. The actual staff height may
92  * differ due to rounding to achieve evenly spaced, sharply rendered
93  * lines. the actual staff height is returned. */
94 int
95 score_set_staff_height (score_t *score, int height);
96
97 /* Set the total width available for drawing the score. */
98 void
99 score_set_width (score_t *score, int width);
100
101 /* Add a brace to the score, connecting the given number of staves.
102  *
103  * The staves to be connected are those that will next be added to the
104  * score. */
105 void
106 score_add_brace (score_t *score, int staves);
107
108 /* Add a new staff to the score */
109 score_staff_t *
110 score_add_staff (score_t *score, score_clef_t clef);
111
112 /* Add a note to a staff of the given pitch, octave, and duration.
113  *
114  * Octave numbers are ISO octave numbers [0:8], (so Octave 4 is from
115  * middle C to the B above middle C).
116  *
117  * Duration values can be symbolic (SCORE_DURATION_WHOLE, _QUARTER,
118  * _EIGHTH, etc.) or numerical as simply the denominator (WHOLE=1,
119  * QUARTER=4, EIGHTH=8, etc.)
120  */
121 score_note_t *
122 score_add_note (score_staff_t *staff,
123                 pitch_t pitch,
124                 int octave,
125                 score_duration_t);
126
127 /* Add a chord symbol of 'name' to a staff.
128  *
129  * For now, the chord symbols are free-form names.
130  *
131  * The chord name must be a talloc'ed string, which the returned
132  * score_chord_t will talloc_steal.
133  */
134 score_chord_t *
135 score_add_chord (score_staff_t *staff,
136                  const char * name);
137
138 /* Remove the given chord from its staff. */
139 void
140 score_remove_chord (score_chord_t *chord);
141
142 /* Remove the given note from its staff. */
143 void
144 score_remove_note (score_note_t *note);
145
146 void
147 score_set_note_color_rgb (score_note_t *note,
148                           double r,
149                           double g,
150                           double b);
151
152 /* Return the first note on the given staff with the given pitch,
153  * octave, and durations. Returns NULL if no match is found. */
154 score_note_t *
155 score_staff_find_note (score_staff_t *staff,
156                        pitch_t pitch,
157                        int octave,
158                        score_duration_t duration);
159
160 /* Draw the given score_t onto the given cairo_t.
161  *
162  * The caller can call cairo_translate before calling score_draw to
163  * position the result as desired, (and can call cairo_clip to clip it
164  * if desired). */
165 void
166 score_draw (score_t *score, cairo_t *cr);
167
168 #endif