]> git.cworth.org Git - scherzo/blob - score.h
Fix high octave numbers (8+) to not be interpreted as 0.
[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 "scherzo-key.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_chord
55 {
56     score_staff_t *staff;
57
58     char *name;
59     double width;
60 } score_chord_t;
61
62 typedef enum score_clef
63 {
64     SCORE_CLEF_G,
65     SCORE_CLEF_TREBLE = SCORE_CLEF_G,
66     SCORE_CLEF_F,
67     SCORE_CLEF_BASS = SCORE_CLEF_F
68 } score_clef_t;
69
70 /* Allocate a new, empty score object, (with optional ctx as talloc
71  * owner). If ctx is NULL, the caller should call talloc_free on the
72  * score_t* when done with it. Otherwise, the object will be freed
73  * when ctx is freed. */
74 score_t *
75 score_create (void *ctx);
76
77 /* Set an (approximate) staff height. The actual staff height may
78  * differ due to rounding to achieve evenly spaced, sharply rendered
79  * lines. the actual staff height is returned. */
80 int
81 score_set_staff_height (score_t *score, int height);
82
83 /* Set the total width available for drawing the score. */
84 void
85 score_set_width (score_t *score, int width);
86
87 /* Set the key for this score */
88 void
89 score_set_key (score_t *score, pitch_t key);
90
91 /* Add a brace to the score, connecting the given number of staves.
92  *
93  * The staves to be connected are those that will next be added to the
94  * score. */
95 void
96 score_add_brace (score_t *score, int staves);
97
98 /* Add a new staff to the score */
99 score_staff_t *
100 score_add_staff (score_t *score, score_clef_t clef);
101
102 /* Add a note to a staff of the given pitch and duration.
103  *
104  * Duration values can be symbolic (SCORE_DURATION_WHOLE, _QUARTER,
105  * _EIGHTH, etc.) or numerical as simply the denominator (WHOLE=1,
106  * QUARTER=4, EIGHTH=8, etc.)
107  */
108 void
109 score_staff_add_note (score_staff_t *staff,
110                       pitch_t pitch,
111                       score_duration_t duration);
112
113 /* Add a note to the score, (automatically selecting the nearest
114  * staff) */
115 void
116 score_add_note (score_t *score, pitch_t pitch, score_duration_t duration);
117
118 /* Add a chord symbol of 'name' to a staff.
119  *
120  * For now, the chord symbols are free-form names.
121  *
122  * The chord name must be a talloc'ed string, which the returned
123  * score_chord_t will talloc_steal.
124  */
125 score_chord_t *
126 score_add_chord (score_staff_t *staff,
127                  const char * name);
128
129 /* Remove all chords from the given staff. */
130 void
131 score_staff_remove_chords (score_staff_t *staff);
132
133 /* Remove all chords from the score. */
134 void
135 score_remove_chords (score_t *score);
136
137 /* Remove all notes from the given staff. */
138 void
139 score_staff_remove_notes (score_staff_t *staff);
140
141 /* Remove all notes from the score. */
142 void
143 score_remove_notes (score_t *score);
144
145 /* Draw the given score_t onto the given cairo_t.
146  *
147  * The caller can call cairo_translate before calling score_draw to
148  * position the result as desired, (and can call cairo_clip to clip it
149  * if desired). */
150 void
151 score_draw (score_t *score, cairo_t *cr);
152
153 #endif