mkmidilibrary 0.2.1

Music scoring and MIDI library for Rust
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
//! Note rendering element

use std::any::Any;

use mkgraphic::prelude::*;
use mkgraphic::support::canvas::Canvas;
use mkgraphic::support::circle::Circle;

use super::config::{NoteConfig, RenderConfig};
use super::{STAFF_SPACE, StaffPosition};
use crate::core::{DurationType, Note};

/// A graphical element representing a musical note
pub struct NoteElement {
    /// The note to render
    note: Note,
    /// Staff position
    position: StaffPosition,
    /// X coordinate
    x: f32,
    /// Y coordinate (staff center)
    staff_y: f32,
    /// Note configuration
    config: NoteConfig,
    /// Whether the note is selected
    selected: bool,
}

impl NoteElement {
    /// Create a new note element
    pub fn new(note: Note, position: StaffPosition) -> Self {
        Self {
            note,
            position,
            x: 0.0,
            staff_y: 0.0,
            config: NoteConfig::default(),
            selected: false,
        }
    }

    /// Set the position
    pub fn set_position(&mut self, x: f32, staff_y: f32) {
        self.x = x;
        self.staff_y = staff_y;
    }

    /// Set selected state
    pub fn set_selected(&mut self, selected: bool) {
        self.selected = selected;
    }

    /// Get the note Y position
    fn note_y(&self) -> f32 {
        self.staff_y + self.position.to_y(STAFF_SPACE)
    }

    /// Draw the note to a canvas
    pub fn draw_to_canvas(&self, canvas: &mut Canvas, config: &RenderConfig) {
        let y = self.note_y();
        let colors = if self.selected {
            &config.colors.selected
        } else {
            &config.colors.notes
        };

        // Draw accidental if present
        if self.position.accidental != 0 {
            self.draw_accidental(canvas, y, &config.colors.accidentals);
        }

        // Draw notehead
        self.draw_notehead(canvas, y, colors);

        // Draw stem if needed
        if self.needs_stem() {
            self.draw_stem(canvas, y, colors);
        }

        // Draw flags or beams for eighth notes and shorter
        if self.needs_flags() {
            self.draw_flags(canvas, y, colors);
        }

        // Draw dots
        let dots = self.note.duration().dots();
        if dots > 0 {
            self.draw_dots(canvas, y, dots, colors);
        }
    }

    /// Check if the note needs a stem
    fn needs_stem(&self) -> bool {
        !matches!(
            self.note.duration().type_(),
            Some(DurationType::Whole) | Some(DurationType::Breve) | None
        )
    }

    /// Check if the note needs flags
    fn needs_flags(&self) -> bool {
        matches!(
            self.note.duration().type_(),
            Some(DurationType::Eighth)
                | Some(DurationType::N16th)
                | Some(DurationType::N32nd)
                | Some(DurationType::N64th)
                | Some(DurationType::N128th)
        )
    }

    /// Get the number of flags needed
    fn flag_count(&self) -> u8 {
        match self.note.duration().type_() {
            Some(DurationType::Eighth) => 1,
            Some(DurationType::N16th) => 2,
            Some(DurationType::N32nd) => 3,
            Some(DurationType::N64th) => 4,
            Some(DurationType::N128th) => 5,
            _ => 0,
        }
    }

    /// Draw the notehead
    fn draw_notehead(&self, canvas: &mut Canvas, y: f32, colors: &(f32, f32, f32, f32)) {
        let color = Color::new(colors.0, colors.1, colors.2, colors.3);
        let is_filled = self.is_filled_notehead();

        let half_width = self.config.head_width / 2.0;
        let half_height = self.config.head_height / 2.0;

        // Draw elliptical notehead
        canvas.begin_path();

        // Approximate ellipse with bezier curves
        let cx = self.x + half_width;
        let cy = y;

        // Draw filled or hollow notehead
        if is_filled {
            canvas.fill_style(color);
            // Simple circle approximation for filled noteheads
            canvas.add_circle(Circle::new(Point::new(cx, cy), half_height * 0.9));
            canvas.fill();
        } else {
            // Hollow notehead (half note, whole note)
            canvas.stroke_style(color);
            canvas.line_width(1.5);
            canvas.add_circle(Circle::new(Point::new(cx, cy), half_height * 0.9));
            canvas.stroke();
        }
    }

    /// Check if the notehead should be filled
    fn is_filled_notehead(&self) -> bool {
        !matches!(
            self.note.duration().type_(),
            Some(DurationType::Whole) | Some(DurationType::Breve) | Some(DurationType::Half)
        )
    }

    /// Draw the stem
    fn draw_stem(&self, canvas: &mut Canvas, y: f32, colors: &(f32, f32, f32, f32)) {
        let color = Color::new(colors.0, colors.1, colors.2, colors.3);
        canvas.stroke_style(color);
        canvas.line_width(self.config.stem_width);

        // Stem direction: up if below middle line, down if above
        let stem_up = self.position.position <= 0;

        let stem_x = if stem_up {
            self.x + self.config.head_width - self.config.stem_width / 2.0
        } else {
            self.x + self.config.stem_width / 2.0
        };

        let stem_y1 = y;
        let stem_y2 = if stem_up {
            y - self.config.stem_height
        } else {
            y + self.config.stem_height
        };

        canvas.begin_path();
        canvas.move_to(Point::new(stem_x, stem_y1));
        canvas.line_to(Point::new(stem_x, stem_y2));
        canvas.stroke();
    }

    /// Draw flags for eighth notes and shorter
    fn draw_flags(&self, canvas: &mut Canvas, y: f32, colors: &(f32, f32, f32, f32)) {
        let color = Color::new(colors.0, colors.1, colors.2, colors.3);
        let num_flags = self.flag_count();
        if num_flags == 0 {
            return;
        }

        canvas.fill_style(color);
        canvas.stroke_style(color);
        canvas.line_width(1.5);

        let stem_up = self.position.position <= 0;
        let stem_x = if stem_up {
            self.x + self.config.head_width - self.config.stem_width / 2.0
        } else {
            self.x + self.config.stem_width / 2.0
        };

        let flag_spacing = STAFF_SPACE * 0.8;

        for i in 0..num_flags {
            let flag_y_start = if stem_up {
                y - self.config.stem_height + (i as f32 * flag_spacing)
            } else {
                y + self.config.stem_height - (i as f32 * flag_spacing)
            };

            // Draw simple flag (curved line)
            canvas.begin_path();
            canvas.move_to(Point::new(stem_x, flag_y_start));

            let flag_direction = if stem_up { 1.0 } else { -1.0 };
            let flag_end_x = stem_x + self.config.flag_width;
            let flag_end_y = flag_y_start + flag_direction * STAFF_SPACE;

            canvas.line_to(Point::new(flag_end_x, flag_end_y));
            canvas.stroke();
        }
    }

    /// Draw dots for dotted notes
    fn draw_dots(&self, canvas: &mut Canvas, y: f32, dots: u8, colors: &(f32, f32, f32, f32)) {
        let color = Color::new(colors.0, colors.1, colors.2, colors.3);
        canvas.fill_style(color);

        let dot_x_start = self.x + self.config.head_width + self.config.dot_spacing;

        // Adjust y if on a line (move dot to space above)
        let dot_y = if self.position.position % 2 == 0 {
            y - STAFF_SPACE / 4.0
        } else {
            y
        };

        for i in 0..dots {
            let dot_x = dot_x_start + (i as f32 * self.config.dot_spacing * 2.0);
            canvas.begin_path();
            canvas.add_circle(Circle::new(
                Point::new(dot_x, dot_y),
                self.config.dot_radius,
            ));
            canvas.fill();
        }
    }

    /// Draw accidental
    fn draw_accidental(&self, canvas: &mut Canvas, y: f32, colors: &(f32, f32, f32, f32)) {
        let color = Color::new(colors.0, colors.1, colors.2, colors.3);
        canvas.stroke_style(color);
        canvas.fill_style(color);
        canvas.line_width(1.0);

        let acc_x = self.x - self.config.accidental_spacing;

        match self.position.accidental {
            1 => self.draw_sharp(canvas, acc_x, y),
            -1 => self.draw_flat(canvas, acc_x, y),
            2 => self.draw_double_sharp(canvas, acc_x, y),
            -2 => self.draw_double_flat(canvas, acc_x, y),
            0 => self.draw_natural(canvas, acc_x, y),
            _ => {}
        }
    }

    fn draw_sharp(&self, canvas: &mut Canvas, x: f32, y: f32) {
        let h = STAFF_SPACE * 1.5;
        let w = STAFF_SPACE * 0.6;

        // Vertical lines
        canvas.begin_path();
        canvas.move_to(Point::new(x - w / 4.0, y - h / 2.0));
        canvas.line_to(Point::new(x - w / 4.0, y + h / 2.0));
        canvas.stroke();

        canvas.begin_path();
        canvas.move_to(Point::new(x + w / 4.0, y - h / 2.0));
        canvas.line_to(Point::new(x + w / 4.0, y + h / 2.0));
        canvas.stroke();

        // Horizontal lines (slightly slanted)
        canvas.line_width(2.0);
        canvas.begin_path();
        canvas.move_to(Point::new(x - w / 2.0, y - STAFF_SPACE / 4.0 + 1.0));
        canvas.line_to(Point::new(x + w / 2.0, y - STAFF_SPACE / 4.0 - 1.0));
        canvas.stroke();

        canvas.begin_path();
        canvas.move_to(Point::new(x - w / 2.0, y + STAFF_SPACE / 4.0 + 1.0));
        canvas.line_to(Point::new(x + w / 2.0, y + STAFF_SPACE / 4.0 - 1.0));
        canvas.stroke();
    }

    fn draw_flat(&self, canvas: &mut Canvas, x: f32, y: f32) {
        let h = STAFF_SPACE * 1.5;

        // Vertical line
        canvas.begin_path();
        canvas.move_to(Point::new(x, y - h / 2.0));
        canvas.line_to(Point::new(x, y + STAFF_SPACE / 3.0));
        canvas.stroke();

        // Curved part (simplified)
        canvas.begin_path();
        canvas.move_to(Point::new(x, y));
        canvas.line_to(Point::new(x + STAFF_SPACE * 0.4, y - STAFF_SPACE / 4.0));
        canvas.line_to(Point::new(x, y + STAFF_SPACE / 3.0));
        canvas.stroke();
    }

    fn draw_natural(&self, canvas: &mut Canvas, x: f32, y: f32) {
        let h = STAFF_SPACE * 1.2;
        let w = STAFF_SPACE * 0.4;

        // Vertical lines
        canvas.begin_path();
        canvas.move_to(Point::new(x - w / 2.0, y - h / 2.0));
        canvas.line_to(Point::new(x - w / 2.0, y + STAFF_SPACE / 4.0));
        canvas.stroke();

        canvas.begin_path();
        canvas.move_to(Point::new(x + w / 2.0, y - STAFF_SPACE / 4.0));
        canvas.line_to(Point::new(x + w / 2.0, y + h / 2.0));
        canvas.stroke();

        // Horizontal lines
        canvas.line_width(2.0);
        canvas.begin_path();
        canvas.move_to(Point::new(x - w / 2.0, y - STAFF_SPACE / 4.0));
        canvas.line_to(Point::new(x + w / 2.0, y - STAFF_SPACE / 4.0));
        canvas.stroke();

        canvas.begin_path();
        canvas.move_to(Point::new(x - w / 2.0, y + STAFF_SPACE / 4.0));
        canvas.line_to(Point::new(x + w / 2.0, y + STAFF_SPACE / 4.0));
        canvas.stroke();
    }

    fn draw_double_sharp(&self, canvas: &mut Canvas, x: f32, y: f32) {
        let size = STAFF_SPACE * 0.4;

        // X shape
        canvas.line_width(2.0);
        canvas.begin_path();
        canvas.move_to(Point::new(x - size, y - size));
        canvas.line_to(Point::new(x + size, y + size));
        canvas.stroke();

        canvas.begin_path();
        canvas.move_to(Point::new(x + size, y - size));
        canvas.line_to(Point::new(x - size, y + size));
        canvas.stroke();
    }

    fn draw_double_flat(&self, canvas: &mut Canvas, x: f32, y: f32) {
        // Two flats side by side
        self.draw_flat(canvas, x - STAFF_SPACE * 0.3, y);
        self.draw_flat(canvas, x + STAFF_SPACE * 0.3, y);
    }
}

impl Element for NoteElement {
    fn limits(&self, _ctx: &BasicContext) -> ViewLimits {
        ViewLimits::fixed(
            self.config.head_width + self.config.accidental_spacing,
            self.config.stem_height + self.config.head_height,
        )
    }

    fn draw(&self, _ctx: &Context) {
        // Actual drawing happens via draw_to_canvas
    }

    fn as_any(&self) -> &dyn Any {
        self
    }

    fn as_any_mut(&mut self) -> &mut dyn Any {
        self
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::core::{Pitch, Step};

    #[test]
    fn test_note_element_creation() {
        let pitch = Pitch::from_parts(Step::C, Some(4), None);
        let note = Note::quarter(pitch);
        let position = StaffPosition::new(0, 0);
        let element = NoteElement::new(note, position);

        assert!(!element.selected);
    }
}