head_music 22.0.0 → 24.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (620) hide show
  1. checksums.yaml +4 -4
  2. data/.claude/skills/release/SKILL.md +21 -1
  3. data/.github/workflows/ci.yml +37 -0
  4. data/.gitignore +2 -1
  5. data/.rspec_parallel +2 -0
  6. data/.ruby-version +1 -1
  7. data/.rubycritic.yml +7 -0
  8. data/CHANGELOG.md +302 -0
  9. data/CLAUDE.md +11 -8
  10. data/Gemfile +2 -1
  11. data/Gemfile.lock +27 -24
  12. data/MUSIC_THEORY.md +1 -1
  13. data/README.md +12 -2
  14. data/Rakefile +137 -7
  15. data/bin/validate +5 -0
  16. data/head_music.gemspec +1 -1
  17. data/lib/head_music/analysis/diatonic_interval/naming.rb +19 -17
  18. data/lib/head_music/analysis/key_finder/histograms.rb +55 -0
  19. data/lib/head_music/analysis/key_finder/key_path.rb +39 -0
  20. data/lib/head_music/analysis/key_finder.rb +105 -0
  21. data/lib/head_music/analysis/key_profile.rb +82 -0
  22. data/lib/head_music/analysis/key_region.rb +5 -0
  23. data/lib/head_music/analysis/line_of_fifths.rb +52 -0
  24. data/lib/head_music/analysis/note_input/flow_reader.rb +86 -0
  25. data/lib/head_music/analysis/note_input.rb +121 -0
  26. data/lib/head_music/analysis/pitch_speller/compactness.rb +53 -0
  27. data/lib/head_music/analysis/pitch_speller/key_center.rb +73 -0
  28. data/lib/head_music/analysis/pitch_speller/key_centers.rb +65 -0
  29. data/lib/head_music/analysis/pitch_speller/respelling.rb +75 -0
  30. data/lib/head_music/analysis/pitch_speller/sounding.rb +21 -0
  31. data/lib/head_music/analysis/pitch_speller/successors.rb +58 -0
  32. data/lib/head_music/analysis/pitch_speller.rb +107 -0
  33. data/lib/head_music/analysis/unspelled_note.rb +16 -0
  34. data/lib/head_music/content/bar.rb +69 -10
  35. data/lib/head_music/content/cantus_firmus/example.rb +2 -2
  36. data/lib/head_music/content/chain_link.rb +67 -0
  37. data/lib/head_music/content/comment.rb +1 -16
  38. data/lib/head_music/content/credits.rb +0 -4
  39. data/lib/head_music/content/dynamic_event.rb +35 -0
  40. data/lib/head_music/content/dynamic_events.rb +38 -0
  41. data/lib/head_music/content/flow/bars.rb +56 -0
  42. data/lib/head_music/content/flow/deserializer.rb +123 -25
  43. data/lib/head_music/content/flow/hash_deserializer.rb +29 -7
  44. data/lib/head_music/content/flow/hash_serializer.rb +47 -0
  45. data/lib/head_music/content/flow/navigation.rb +80 -0
  46. data/lib/head_music/content/flow/performance_order.rb +128 -0
  47. data/lib/head_music/content/flow/schema_validation.rb +53 -0
  48. data/lib/head_music/content/flow/schema_values.rb +67 -143
  49. data/lib/head_music/content/flow/staff_system_values.rb +2 -6
  50. data/lib/head_music/content/flow/timeline.rb +90 -58
  51. data/lib/head_music/content/flow/timeline_serializer.rb +58 -0
  52. data/lib/head_music/content/flow/timeline_spans.rb +68 -0
  53. data/lib/head_music/content/flow/timeline_tempos.rb +169 -0
  54. data/lib/head_music/content/flow/timeline_values.rb +139 -0
  55. data/lib/head_music/content/flow/v4_upgrade.rb +46 -0
  56. data/lib/head_music/content/flow/voice_event_values.rb +165 -0
  57. data/lib/head_music/content/flow.rb +114 -62
  58. data/lib/head_music/content/grace_note.rb +72 -0
  59. data/lib/head_music/content/jump.rb +53 -0
  60. data/lib/head_music/content/layout/realization.rb +20 -13
  61. data/lib/head_music/content/layout/rendering.rb +82 -0
  62. data/lib/head_music/content/layout.rb +3 -58
  63. data/lib/head_music/content/note.rb +9 -8
  64. data/lib/head_music/content/note_event.rb +154 -0
  65. data/lib/head_music/content/part.rb +39 -0
  66. data/lib/head_music/content/player.rb +1 -2
  67. data/lib/head_music/content/position.rb +47 -15
  68. data/lib/head_music/content/project.rb +57 -21
  69. data/lib/head_music/content/publication.rb +3 -27
  70. data/lib/head_music/content/rest_event.rb +45 -0
  71. data/lib/head_music/content/sound_resolver.rb +9 -3
  72. data/lib/head_music/content/span.rb +81 -0
  73. data/lib/head_music/content/spans.rb +50 -0
  74. data/lib/head_music/content/staff.rb +6 -2
  75. data/lib/head_music/content/syllable.rb +3 -3
  76. data/lib/head_music/content/tuplet_group/bracketing.rb +77 -0
  77. data/lib/head_music/content/tuplet_group.rb +120 -0
  78. data/lib/head_music/content/tuplet_groups.rb +36 -0
  79. data/lib/head_music/content/voice/continuity.rb +15 -15
  80. data/lib/head_music/content/voice/dynamic_resolver.rb +49 -0
  81. data/lib/head_music/content/voice/melodic_line.rb +3 -3
  82. data/lib/head_music/content/voice/span_anchors.rb +37 -0
  83. data/lib/head_music/content/voice/staff_assignments.rb +56 -0
  84. data/lib/head_music/content/voice.rb +119 -92
  85. data/lib/head_music/content/voice_event/run.rb +50 -0
  86. data/lib/head_music/content/voice_event.rb +230 -0
  87. data/lib/head_music/content/voice_events.rb +60 -0
  88. data/lib/head_music/content/work.rb +3 -21
  89. data/lib/head_music/instruments/alternate_tuning.rb +1 -1
  90. data/lib/head_music/instruments/alternate_tunings.yml +7 -4
  91. data/lib/head_music/instruments/instrument.rb +17 -2
  92. data/lib/head_music/instruments/instrument_family.rb +4 -0
  93. data/lib/head_music/instruments/instruments.yml +123 -0
  94. data/lib/head_music/instruments/stringings.yml +14 -0
  95. data/lib/head_music/locales/de.yml +37 -0
  96. data/lib/head_music/locales/en.yml +110 -0
  97. data/lib/head_music/locales/en_GB.yml +10 -0
  98. data/lib/head_music/locales/es.yml +37 -0
  99. data/lib/head_music/locales/fr.yml +37 -0
  100. data/lib/head_music/locales/it.yml +37 -0
  101. data/lib/head_music/locales/ru.yml +37 -0
  102. data/lib/head_music/midi/bar_layout.rb +150 -0
  103. data/lib/head_music/midi/bar_pieces.rb +41 -0
  104. data/lib/head_music/midi/budget.rb +102 -0
  105. data/lib/head_music/midi/change_cursor.rb +30 -0
  106. data/lib/head_music/midi/channel_allocator.rb +47 -0
  107. data/lib/head_music/midi/chunk_reader.rb +72 -0
  108. data/lib/head_music/midi/conductor_track.rb +74 -0
  109. data/lib/head_music/midi/events.rb +112 -0
  110. data/lib/head_music/midi/flow_builder.rb +116 -0
  111. data/lib/head_music/midi/hairpin_ramps.rb +110 -0
  112. data/lib/head_music/midi/instrument_map.rb +78 -0
  113. data/lib/head_music/midi/key_changes.rb +45 -0
  114. data/lib/head_music/midi/key_overlaps.rb +48 -0
  115. data/lib/head_music/midi/level_marks.rb +61 -0
  116. data/lib/head_music/midi/lyric_attacher.rb +60 -0
  117. data/lib/head_music/midi/lyric_events.rb +44 -0
  118. data/lib/head_music/midi/meta_reader.rb +70 -0
  119. data/lib/head_music/midi/meter_changes.rb +67 -0
  120. data/lib/head_music/midi/note_pairing.rb +48 -0
  121. data/lib/head_music/midi/note_timing.rb +109 -0
  122. data/lib/head_music/midi/note_values.rb +99 -0
  123. data/lib/head_music/midi/onsets.rb +15 -0
  124. data/lib/head_music/midi/parser.rb +36 -0
  125. data/lib/head_music/midi/part_key.rb +18 -0
  126. data/lib/head_music/midi/part_reading.rb +72 -0
  127. data/lib/head_music/midi/part_source.rb +161 -0
  128. data/lib/head_music/midi/part_track.rb +115 -0
  129. data/lib/head_music/midi/performance_timeline.rb +80 -0
  130. data/lib/head_music/midi/preflight.rb +138 -0
  131. data/lib/head_music/midi/program_changes.rb +37 -0
  132. data/lib/head_music/midi/quantized_note.rb +5 -0
  133. data/lib/head_music/midi/quantizer.rb +106 -0
  134. data/lib/head_music/midi/ramp_turn.rb +26 -0
  135. data/lib/head_music/midi/reader.rb +110 -0
  136. data/lib/head_music/midi/smf.rb +41 -0
  137. data/lib/head_music/midi/sound_keys.rb +32 -0
  138. data/lib/head_music/midi/spelling.rb +74 -0
  139. data/lib/head_music/midi/tempo_changes.rb +121 -0
  140. data/lib/head_music/midi/tempo_inference.rb +143 -0
  141. data/lib/head_music/midi/tempo_steps.rb +112 -0
  142. data/lib/head_music/midi/tick_resolution.rb +44 -0
  143. data/lib/head_music/midi/timeline_reader.rb +105 -0
  144. data/lib/head_music/midi/track_reader.rb +128 -0
  145. data/lib/head_music/midi/velocity_resolver.rb +116 -0
  146. data/lib/head_music/midi/voice_separator.rb +100 -0
  147. data/lib/head_music/midi/writer.rb +64 -0
  148. data/lib/head_music/midi.rb +54 -0
  149. data/lib/head_music/named.rb +11 -1
  150. data/lib/head_music/notation/abc/bar_line_writer.rb +86 -0
  151. data/lib/head_music/notation/abc/bar_mark_reader.rb +75 -0
  152. data/lib/head_music/notation/abc/bar_writer.rb +144 -0
  153. data/lib/head_music/notation/abc/beam_adjacency.rb +34 -0
  154. data/lib/head_music/notation/abc/body_lexer.rb +20 -233
  155. data/lib/head_music/notation/abc/broken_rhythm.rb +41 -0
  156. data/lib/head_music/notation/abc/decoration.rb +37 -0
  157. data/lib/head_music/notation/abc/decoration_mapper.rb +102 -0
  158. data/lib/head_music/notation/abc/decoration_writer.rb +30 -0
  159. data/lib/head_music/notation/abc/duration_resolver.rb +5 -3
  160. data/lib/head_music/notation/abc/duration_writer.rb +20 -25
  161. data/lib/head_music/notation/abc/grace_reader.rb +38 -0
  162. data/lib/head_music/notation/abc/grace_scanner.rb +98 -0
  163. data/lib/head_music/notation/abc/grace_writer.rb +38 -0
  164. data/lib/head_music/notation/abc/hairpin_marks.rb +52 -0
  165. data/lib/head_music/notation/abc/header.rb +49 -19
  166. data/lib/head_music/notation/abc/invisible_rests.rb +38 -0
  167. data/lib/head_music/notation/abc/key_mapper.rb +6 -1
  168. data/lib/head_music/notation/abc/line_scanner.rb +251 -0
  169. data/lib/head_music/notation/abc/navigation_tagger.rb +86 -0
  170. data/lib/head_music/notation/abc/opening_fields.rb +63 -0
  171. data/lib/head_music/notation/abc/parser.rb +111 -110
  172. data/lib/head_music/notation/abc/pending_decorations.rb +127 -0
  173. data/lib/head_music/notation/abc/pending_graces.rb +30 -0
  174. data/lib/head_music/notation/abc/pending_note.rb +64 -0
  175. data/lib/head_music/notation/abc/pending_slurs.rb +68 -0
  176. data/lib/head_music/notation/abc/pending_tie.rb +39 -0
  177. data/lib/head_music/notation/abc/pitch_builder.rb +6 -1
  178. data/lib/head_music/notation/abc/pitch_writer.rb +17 -5
  179. data/lib/head_music/notation/abc/position_marks.rb +147 -0
  180. data/lib/head_music/notation/abc/preflight.rb +13 -0
  181. data/lib/head_music/notation/abc/render_plan.rb +35 -0
  182. data/lib/head_music/notation/abc/render_preflight.rb +71 -0
  183. data/lib/head_music/notation/abc/repeat_tagger.rb +31 -2
  184. data/lib/head_music/notation/abc/rest_writer.rb +50 -0
  185. data/lib/head_music/notation/abc/slur_writer.rb +106 -0
  186. data/lib/head_music/notation/abc/tempo_field.rb +72 -0
  187. data/lib/head_music/notation/abc/timeline_fields.rb +54 -0
  188. data/lib/head_music/notation/abc/token.rb +2 -2
  189. data/lib/head_music/notation/abc/tuplet_grouping.rb +104 -0
  190. data/lib/head_music/notation/abc/tuplet_marker.rb +30 -0
  191. data/lib/head_music/notation/abc/tuplet_writer.rb +68 -0
  192. data/lib/head_music/notation/abc/voice_overlay.rb +49 -0
  193. data/lib/head_music/notation/abc/voice_registry.rb +8 -2
  194. data/lib/head_music/notation/abc/voice_replay.rb +45 -0
  195. data/lib/head_music/notation/abc/voice_state.rb +250 -100
  196. data/lib/head_music/notation/abc/voice_tokens.rb +29 -0
  197. data/lib/head_music/notation/abc/voice_writer.rb +125 -0
  198. data/lib/head_music/notation/abc/writer.rb +53 -137
  199. data/lib/head_music/notation/abc.rb +1 -10
  200. data/lib/head_music/notation/bar_map.rb +186 -0
  201. data/lib/head_music/notation/bar_marks.rb +19 -0
  202. data/lib/head_music/notation/bar_splitter.rb +140 -0
  203. data/lib/head_music/notation/barline_placement.rb +15 -0
  204. data/lib/head_music/notation/beam_groups.rb +39 -0
  205. data/lib/head_music/notation/beam_reading.rb +20 -0
  206. data/lib/head_music/notation/chord_clashes.rb +20 -0
  207. data/lib/head_music/notation/clef_changes.rb +21 -0
  208. data/lib/head_music/notation/concert_transposition.rb +68 -0
  209. data/lib/head_music/notation/dotted_duration.rb +63 -23
  210. data/lib/head_music/notation/fermata_marks.rb +78 -0
  211. data/lib/head_music/notation/flow_timed_marks.rb +71 -0
  212. data/lib/head_music/notation/hairpin_segments.rb +22 -0
  213. data/lib/head_music/notation/instrument_names.rb +62 -0
  214. data/lib/head_music/notation/kern/bar_clock.rb +195 -0
  215. data/lib/head_music/notation/kern/barline_reader.rb +37 -0
  216. data/lib/head_music/notation/kern/chord_token.rb +65 -0
  217. data/lib/head_music/notation/kern/citation_reader.rb +69 -0
  218. data/lib/head_music/notation/kern/clef_codes.rb +33 -0
  219. data/lib/head_music/notation/kern/data_rows.rb +101 -0
  220. data/lib/head_music/notation/kern/document.rb +129 -0
  221. data/lib/head_music/notation/kern/duration_reader.rb +79 -0
  222. data/lib/head_music/notation/kern/duration_writer.rb +38 -0
  223. data/lib/head_music/notation/kern/dynamic_fields.rb +93 -0
  224. data/lib/head_music/notation/kern/dynamic_placer.rb +131 -0
  225. data/lib/head_music/notation/kern/dynamic_reader.rb +88 -0
  226. data/lib/head_music/notation/kern/flow_builder.rb +140 -0
  227. data/lib/head_music/notation/kern/grace_tokens.rb +42 -0
  228. data/lib/head_music/notation/kern/instrument_codes.rb +62 -0
  229. data/lib/head_music/notation/kern/interpretation_reader.rb +43 -0
  230. data/lib/head_music/notation/kern/interpretation_rows.rb +119 -0
  231. data/lib/head_music/notation/kern/key_reader.rb +73 -0
  232. data/lib/head_music/notation/kern/layer.rb +158 -0
  233. data/lib/head_music/notation/kern/lexer.rb +89 -0
  234. data/lib/head_music/notation/kern/lyric_reader.rb +84 -0
  235. data/lib/head_music/notation/kern/mark_codes.rb +48 -0
  236. data/lib/head_music/notation/kern/mark_offset.rb +16 -0
  237. data/lib/head_music/notation/kern/melismas.rb +35 -0
  238. data/lib/head_music/notation/kern/meter_reader.rb +60 -0
  239. data/lib/head_music/notation/kern/parser.rb +34 -0
  240. data/lib/head_music/notation/kern/part_grouping.rb +70 -0
  241. data/lib/head_music/notation/kern/part_roster.rb +69 -0
  242. data/lib/head_music/notation/kern/pitch_reader.rb +32 -0
  243. data/lib/head_music/notation/kern/pitch_writer.rb +21 -0
  244. data/lib/head_music/notation/kern/preflight.rb +84 -0
  245. data/lib/head_music/notation/kern/record.rb +26 -0
  246. data/lib/head_music/notation/kern/reference_records.rb +64 -0
  247. data/lib/head_music/notation/kern/render_plan.rb +19 -0
  248. data/lib/head_music/notation/kern/row_offsets.rb +30 -0
  249. data/lib/head_music/notation/kern/span_marks.rb +80 -0
  250. data/lib/head_music/notation/kern/spine_changes.rb +78 -0
  251. data/lib/head_music/notation/kern/spine_columns.rb +86 -0
  252. data/lib/head_music/notation/kern/spine_layout.rb +151 -0
  253. data/lib/head_music/notation/kern/spine_tags.rb +30 -0
  254. data/lib/head_music/notation/kern/spine_tokens.rb +183 -0
  255. data/lib/head_music/notation/kern/spine_voices.rb +127 -0
  256. data/lib/head_music/notation/kern/tempo_reader.rb +49 -0
  257. data/lib/head_music/notation/kern/timeline_reader.rb +169 -0
  258. data/lib/head_music/notation/kern/token_reader.rb +198 -0
  259. data/lib/head_music/notation/kern/tuplet_groups.rb +93 -0
  260. data/lib/head_music/notation/kern/voice_cursor.rb +115 -0
  261. data/lib/head_music/notation/kern/writer.rb +67 -0
  262. data/lib/head_music/notation/kern/written_bars.rb +151 -0
  263. data/lib/head_music/notation/kern.rb +27 -0
  264. data/lib/head_music/notation/lily_pond/bar_commands.rb +87 -0
  265. data/lib/head_music/notation/lily_pond/bar_mark_placer.rb +82 -0
  266. data/lib/head_music/notation/lily_pond/bar_mark_reader.rb +134 -0
  267. data/lib/head_music/notation/lily_pond/bar_mark_writer.rb +65 -0
  268. data/lib/head_music/notation/lily_pond/chord_reader.rb +34 -0
  269. data/lib/head_music/notation/lily_pond/context_reader.rb +68 -12
  270. data/lib/head_music/notation/lily_pond/document.rb +57 -1
  271. data/lib/head_music/notation/lily_pond/duration_reader.rb +41 -3
  272. data/lib/head_music/notation/lily_pond/duration_writer.rb +3 -3
  273. data/lib/head_music/notation/lily_pond/dynamics_item_reader.rb +47 -0
  274. data/lib/head_music/notation/lily_pond/event_placer.rb +191 -0
  275. data/lib/head_music/notation/lily_pond/flow_builder.rb +22 -95
  276. data/lib/head_music/notation/lily_pond/grace_reader.rb +161 -0
  277. data/lib/head_music/notation/lily_pond/grace_writer.rb +35 -0
  278. data/lib/head_music/notation/lily_pond/group_reader.rb +51 -0
  279. data/lib/head_music/notation/lily_pond/hairpin_marks.rb +40 -0
  280. data/lib/head_music/notation/lily_pond/key_mapper.rb +2 -11
  281. data/lib/head_music/notation/lily_pond/lexer.rb +92 -12
  282. data/lib/head_music/notation/lily_pond/mark_reader.rb +184 -0
  283. data/lib/head_music/notation/lily_pond/mark_writer.rb +27 -0
  284. data/lib/head_music/notation/lily_pond/music_item_reader.rb +85 -57
  285. data/lib/head_music/notation/lily_pond/music_reader.rb +29 -33
  286. data/lib/head_music/notation/lily_pond/part_dynamics.rb +53 -0
  287. data/lib/head_music/notation/lily_pond/part_layout.rb +89 -0
  288. data/lib/head_music/notation/lily_pond/pending_tuplets.rb +47 -0
  289. data/lib/head_music/notation/lily_pond/pitch_writer.rb +18 -0
  290. data/lib/head_music/notation/lily_pond/preflight.rb +27 -20
  291. data/lib/head_music/notation/lily_pond/render_plan.rb +115 -24
  292. data/lib/head_music/notation/lily_pond/segment_tokens.rb +135 -0
  293. data/lib/head_music/notation/lily_pond/setting_reader.rb +103 -0
  294. data/lib/head_music/notation/lily_pond/spacer_run.rb +43 -0
  295. data/lib/head_music/notation/lily_pond/spacers.rb +58 -0
  296. data/lib/head_music/notation/lily_pond/span_marks.rb +91 -0
  297. data/lib/head_music/notation/lily_pond/span_pairing.rb +35 -0
  298. data/lib/head_music/notation/lily_pond/tempo_reader.rb +46 -0
  299. data/lib/head_music/notation/lily_pond/tempo_writer.rb +33 -0
  300. data/lib/head_music/notation/lily_pond/time_signature_writer.rb +42 -0
  301. data/lib/head_music/notation/lily_pond/timed_marks.rb +158 -0
  302. data/lib/head_music/notation/lily_pond/token.rb +8 -2
  303. data/lib/head_music/notation/lily_pond/token_cursor.rb +55 -2
  304. data/lib/head_music/notation/lily_pond/transposition_walk.rb +141 -0
  305. data/lib/head_music/notation/lily_pond/tuplet_reader.rb +56 -0
  306. data/lib/head_music/notation/lily_pond/voice_context.rb +23 -9
  307. data/lib/head_music/notation/lily_pond/voice_item_reader.rb +87 -0
  308. data/lib/head_music/notation/lily_pond/voice_stream.rb +203 -31
  309. data/lib/head_music/notation/lily_pond/voice_writer.rb +90 -32
  310. data/lib/head_music/notation/lily_pond/writer.rb +32 -17
  311. data/lib/head_music/notation/lily_pond.rb +4 -7
  312. data/lib/head_music/notation/mark_reading.rb +16 -0
  313. data/lib/head_music/notation/mei/accidental_plan.rb +94 -0
  314. data/lib/head_music/notation/mei/accidental_reading.rb +23 -0
  315. data/lib/head_music/notation/mei/artic_attributes.rb +36 -0
  316. data/lib/head_music/notation/mei/artic_reader.rb +41 -0
  317. data/lib/head_music/notation/mei/attached_marks.rb +70 -0
  318. data/lib/head_music/notation/mei/attribute_values.rb +245 -0
  319. data/lib/head_music/notation/mei/bar_marks.rb +52 -0
  320. data/lib/head_music/notation/mei/barline_reader.rb +101 -0
  321. data/lib/head_music/notation/mei/beam_containers.rb +56 -0
  322. data/lib/head_music/notation/mei/beam_reader.rb +79 -0
  323. data/lib/head_music/notation/mei/clef_reading.rb +34 -0
  324. data/lib/head_music/notation/mei/container_spans.rb +15 -0
  325. data/lib/head_music/notation/mei/control_anchors.rb +194 -0
  326. data/lib/head_music/notation/mei/copy_expansion.rb +113 -0
  327. data/lib/head_music/notation/mei/decimals.rb +29 -0
  328. data/lib/head_music/notation/mei/document.rb +138 -0
  329. data/lib/head_music/notation/mei/downbeat_tempo.rb +41 -0
  330. data/lib/head_music/notation/mei/duration_reading.rb +49 -0
  331. data/lib/head_music/notation/mei/durations.rb +31 -0
  332. data/lib/head_music/notation/mei/dynam_reader.rb +51 -0
  333. data/lib/head_music/notation/mei/editorial.rb +122 -0
  334. data/lib/head_music/notation/mei/ending_passes.rb +24 -0
  335. data/lib/head_music/notation/mei/event_records.rb +61 -0
  336. data/lib/head_music/notation/mei/event_writer.rb +102 -0
  337. data/lib/head_music/notation/mei/fermata_controls.rb +39 -0
  338. data/lib/head_music/notation/mei/fermata_reader.rb +43 -0
  339. data/lib/head_music/notation/mei/flow_builder.rb +62 -0
  340. data/lib/head_music/notation/mei/hairpin_reader.rb +38 -0
  341. data/lib/head_music/notation/mei/head_writer.rb +85 -0
  342. data/lib/head_music/notation/mei/id_index.rb +86 -0
  343. data/lib/head_music/notation/mei/identity_reader.rb +199 -0
  344. data/lib/head_music/notation/mei/ids.rb +70 -0
  345. data/lib/head_music/notation/mei/layer_containers.rb +94 -0
  346. data/lib/head_music/notation/mei/layer_reader.rb +211 -0
  347. data/lib/head_music/notation/mei/layer_signs.rb +37 -0
  348. data/lib/head_music/notation/mei/layer_tree.rb +40 -0
  349. data/lib/head_music/notation/mei/lyric_reader.rb +116 -0
  350. data/lib/head_music/notation/mei/lyric_writer.rb +72 -0
  351. data/lib/head_music/notation/mei/mark_reader.rb +52 -0
  352. data/lib/head_music/notation/mei/mark_readers.rb +18 -0
  353. data/lib/head_music/notation/mei/mark_records.rb +122 -0
  354. data/lib/head_music/notation/mei/marking_events.rb +89 -0
  355. data/lib/head_music/notation/mei/markup.rb +37 -0
  356. data/lib/head_music/notation/mei/measure_clefs.rb +38 -0
  357. data/lib/head_music/notation/mei/measure_contents.rb +87 -0
  358. data/lib/head_music/notation/mei/measure_length.rb +30 -0
  359. data/lib/head_music/notation/mei/measure_writer.rb +79 -0
  360. data/lib/head_music/notation/mei/navigation_events.rb +59 -0
  361. data/lib/head_music/notation/mei/navigation_reader.rb +71 -0
  362. data/lib/head_music/notation/mei/onset_index.rb +49 -0
  363. data/lib/head_music/notation/mei/open_spaces.rb +43 -0
  364. data/lib/head_music/notation/mei/opening_values.rb +44 -0
  365. data/lib/head_music/notation/mei/ornament_reader.rb +47 -0
  366. data/lib/head_music/notation/mei/parser.rb +45 -0
  367. data/lib/head_music/notation/mei/part_layers.rb +58 -0
  368. data/lib/head_music/notation/mei/part_reading.rb +69 -0
  369. data/lib/head_music/notation/mei/part_roster.rb +54 -0
  370. data/lib/head_music/notation/mei/part_spelling.rb +55 -0
  371. data/lib/head_music/notation/mei/pitch_reading.rb +33 -0
  372. data/lib/head_music/notation/mei/preflight.rb +63 -0
  373. data/lib/head_music/notation/mei/render_plan.rb +154 -0
  374. data/lib/head_music/notation/mei/score_def_values.rb +84 -0
  375. data/lib/head_music/notation/mei/score_def_writer.rb +138 -0
  376. data/lib/head_music/notation/mei/score_reader.rb +191 -0
  377. data/lib/head_music/notation/mei/section_writer.rb +48 -0
  378. data/lib/head_music/notation/mei/slur_reader.rb +71 -0
  379. data/lib/head_music/notation/mei/staff_groups.rb +132 -0
  380. data/lib/head_music/notation/mei/staff_numbering.rb +62 -0
  381. data/lib/head_music/notation/mei/staff_state.rb +207 -0
  382. data/lib/head_music/notation/mei/tempo_controls.rb +103 -0
  383. data/lib/head_music/notation/mei/tempo_mark_reader.rb +79 -0
  384. data/lib/head_music/notation/mei/tempo_reading.rb +40 -0
  385. data/lib/head_music/notation/mei/tie_controls.rb +145 -0
  386. data/lib/head_music/notation/mei/timeline_changes.rb +119 -0
  387. data/lib/head_music/notation/mei/timeline_reader.rb +30 -0
  388. data/lib/head_music/notation/mei/tstamp.rb +56 -0
  389. data/lib/head_music/notation/mei/tuplet_reader.rb +35 -0
  390. data/lib/head_music/notation/mei/tuplet_spans.rb +173 -0
  391. data/lib/head_music/notation/mei/voice_assignment.rb +109 -0
  392. data/lib/head_music/notation/mei/writer.rb +59 -0
  393. data/lib/head_music/notation/mei/written_items.rb +112 -0
  394. data/lib/head_music/notation/mei/xml_reading.rb +5 -0
  395. data/lib/head_music/notation/mei.rb +69 -0
  396. data/lib/head_music/notation/melisma_fill.rb +22 -0
  397. data/lib/head_music/notation/music_xml/attributes_reader.rb +103 -0
  398. data/lib/head_music/notation/music_xml/attributes_writer.rb +62 -41
  399. data/lib/head_music/notation/music_xml/barline_reader.rb +121 -0
  400. data/lib/head_music/notation/music_xml/barline_writer.rb +102 -0
  401. data/lib/head_music/notation/music_xml/beam_grouper.rb +10 -40
  402. data/lib/head_music/notation/music_xml/beam_reader.rb +46 -0
  403. data/lib/head_music/notation/music_xml/clef_reader.rb +20 -0
  404. data/lib/head_music/notation/music_xml/direction_marks.rb +51 -0
  405. data/lib/head_music/notation/music_xml/direction_plan.rb +135 -0
  406. data/lib/head_music/notation/music_xml/direction_reader.rb +142 -0
  407. data/lib/head_music/notation/music_xml/direction_writer.rb +73 -0
  408. data/lib/head_music/notation/music_xml/divisions.rb +45 -10
  409. data/lib/head_music/notation/music_xml/document.rb +77 -0
  410. data/lib/head_music/notation/music_xml/duration_writer.rb +21 -6
  411. data/lib/head_music/notation/music_xml/fermata_reader.rb +57 -0
  412. data/lib/head_music/notation/music_xml/flow_builder.rb +47 -0
  413. data/lib/head_music/notation/music_xml/grace_writer.rb +85 -0
  414. data/lib/head_music/notation/music_xml/identity_reader.rb +131 -0
  415. data/lib/head_music/notation/music_xml/instrument_sounds.rb +36 -0
  416. data/lib/head_music/notation/music_xml/instrument_sounds.yml +126 -0
  417. data/lib/head_music/notation/music_xml/key_mapper.rb +30 -0
  418. data/lib/head_music/notation/music_xml/lyric_reader.rb +116 -0
  419. data/lib/head_music/notation/music_xml/lyric_writer.rb +50 -29
  420. data/lib/head_music/notation/music_xml/mark_reader.rb +28 -0
  421. data/lib/head_music/notation/music_xml/mark_readers.rb +25 -0
  422. data/lib/head_music/notation/music_xml/mark_records.rb +124 -0
  423. data/lib/head_music/notation/music_xml/marking_elements.rb +65 -0
  424. data/lib/head_music/notation/music_xml/marking_reader.rb +76 -0
  425. data/lib/head_music/notation/music_xml/navigation_reader.rb +91 -0
  426. data/lib/head_music/notation/music_xml/navigation_writer.rb +60 -0
  427. data/lib/head_music/notation/music_xml/note_lines.rb +39 -0
  428. data/lib/head_music/notation/music_xml/note_reader.rb +100 -0
  429. data/lib/head_music/notation/music_xml/note_writer.rb +114 -70
  430. data/lib/head_music/notation/music_xml/parse_preflight.rb +11 -0
  431. data/lib/head_music/notation/music_xml/parser.rb +39 -0
  432. data/lib/head_music/notation/music_xml/part_durations.rb +83 -0
  433. data/lib/head_music/notation/music_xml/part_reader.rb +183 -0
  434. data/lib/head_music/notation/music_xml/part_roster.rb +61 -0
  435. data/lib/head_music/notation/music_xml/part_staves.rb +47 -0
  436. data/lib/head_music/notation/music_xml/part_writer.rb +137 -0
  437. data/lib/head_music/notation/music_xml/pitch_reader.rb +20 -0
  438. data/lib/head_music/notation/music_xml/pitch_writer.rb +13 -0
  439. data/lib/head_music/notation/music_xml/preflight.rb +14 -19
  440. data/lib/head_music/notation/music_xml/render_plan.rb +57 -49
  441. data/lib/head_music/notation/music_xml/score_instruments.rb +20 -0
  442. data/lib/head_music/notation/music_xml/shared_instrument_sounds.yml +22 -0
  443. data/lib/head_music/notation/music_xml/slur_numbers.rb +98 -0
  444. data/lib/head_music/notation/music_xml/slur_reader.rb +69 -0
  445. data/lib/head_music/notation/music_xml/tempo_direction.rb +91 -0
  446. data/lib/head_music/notation/music_xml/tempo_reader.rb +127 -0
  447. data/lib/head_music/notation/music_xml/tempo_writer.rb +97 -0
  448. data/lib/head_music/notation/music_xml/timeline_reader.rb +37 -0
  449. data/lib/head_music/notation/music_xml/tuplet_marks.rb +76 -0
  450. data/lib/head_music/notation/music_xml/tuplet_reader.rb +86 -0
  451. data/lib/head_music/notation/music_xml/voice_tracks.rb +129 -0
  452. data/lib/head_music/notation/music_xml/wedge_numbers.rb +46 -0
  453. data/lib/head_music/notation/music_xml/writer.rb +25 -85
  454. data/lib/head_music/notation/music_xml/written_timing.rb +89 -0
  455. data/lib/head_music/notation/music_xml/xml_reading.rb +4 -0
  456. data/lib/head_music/notation/music_xml.rb +33 -1
  457. data/lib/head_music/notation/notation_styles.yml +8 -0
  458. data/lib/head_music/notation/parse_preflight.rb +85 -0
  459. data/lib/head_music/notation/pitch_spelling.rb +30 -0
  460. data/lib/head_music/notation/preflight_checks.rb +117 -12
  461. data/lib/head_music/notation/render_plan.rb +47 -4
  462. data/lib/head_music/notation/span_ends.rb +11 -0
  463. data/lib/head_music/notation/tempo_mark_reading.rb +130 -0
  464. data/lib/head_music/notation/tempo_marks.rb +95 -0
  465. data/lib/head_music/notation/tie_folding.rb +90 -0
  466. data/lib/head_music/notation/timeline_reading.rb +67 -0
  467. data/lib/head_music/notation/tuplet_brackets.rb +91 -0
  468. data/lib/head_music/notation/voice_event_validation.rb +34 -0
  469. data/lib/head_music/notation/voice_placer.rb +94 -0
  470. data/lib/head_music/notation/voice_track.rb +133 -0
  471. data/lib/head_music/notation/written_links.rb +37 -0
  472. data/lib/head_music/notation/xml_reading.rb +85 -0
  473. data/lib/head_music/notation/xml_text.rb +22 -0
  474. data/lib/head_music/notation.rb +41 -0
  475. data/lib/head_music/rudiment/articulation.rb +10 -0
  476. data/lib/head_music/rudiment/articulations.yml +6 -0
  477. data/lib/head_music/rudiment/clef.rb +14 -2
  478. data/lib/head_music/rudiment/duration_comparison.rb +18 -0
  479. data/lib/head_music/rudiment/dynamic.rb +52 -0
  480. data/lib/head_music/rudiment/dynamics.yml +27 -0
  481. data/lib/head_music/rudiment/key.rb +10 -6
  482. data/lib/head_music/rudiment/keyed_catalog.rb +73 -0
  483. data/lib/head_music/rudiment/meter.rb +31 -4
  484. data/lib/head_music/rudiment/mode.rb +9 -4
  485. data/lib/head_music/rudiment/note.rb +25 -23
  486. data/lib/head_music/rudiment/ornament.rb +9 -0
  487. data/lib/head_music/rudiment/ornaments.yml +13 -0
  488. data/lib/head_music/rudiment/pitch.rb +1 -0
  489. data/lib/head_music/rudiment/quality.rb +11 -4
  490. data/lib/head_music/rudiment/rest.rb +9 -9
  491. data/lib/head_music/rudiment/rhythm.rb +2 -0
  492. data/lib/head_music/rudiment/rhythmic_duration.rb +114 -0
  493. data/lib/head_music/rudiment/rhythmic_element.rb +10 -11
  494. data/lib/head_music/rudiment/rhythmic_unit.rb +6 -4
  495. data/lib/head_music/rudiment/rhythmic_value/name_reader.rb +67 -0
  496. data/lib/head_music/rudiment/rhythmic_value.rb +73 -78
  497. data/lib/head_music/rudiment/soundable.rb +11 -0
  498. data/lib/head_music/rudiment/span_kind.rb +52 -0
  499. data/lib/head_music/rudiment/span_kinds.yml +31 -0
  500. data/lib/head_music/rudiment/tempo.rb +45 -14
  501. data/lib/head_music/rudiment/tuplet_ratio.rb +84 -0
  502. data/lib/head_music/rudiment/unpitched_note.rb +18 -18
  503. data/lib/head_music/rudiment/unpitched_sound.rb +2 -0
  504. data/lib/head_music/style/guide.rb +5 -2
  505. data/lib/head_music/style/guideline/bar_texture.rb +79 -0
  506. data/lib/head_music/style/guideline/dissonance_figures.rb +88 -0
  507. data/lib/head_music/style/guideline/harmonic_context.rb +15 -11
  508. data/lib/head_music/style/guideline/restruck_suspensions.rb +38 -0
  509. data/lib/head_music/style/guideline/voice_context.rb +39 -0
  510. data/lib/head_music/style/guideline/wording.rb +5 -0
  511. data/lib/head_music/style/guideline.rb +3 -3
  512. data/lib/head_music/style/guidelines/allow_fifth_species_rhythmic_values.rb +11 -6
  513. data/lib/head_music/style/guidelines/allow_free_species_rhythmic_values.rb +80 -0
  514. data/lib/head_music/style/guidelines/allow_whole_half_quarter_notes.rb +1 -1
  515. data/lib/head_music/style/guidelines/always_move.rb +17 -4
  516. data/lib/head_music/style/guidelines/bar_count.rb +103 -0
  517. data/lib/head_music/style/guidelines/clausula_vera.rb +147 -0
  518. data/lib/head_music/style/guidelines/consonant_downbeats.rb +9 -6
  519. data/lib/head_music/style/guidelines/contoured/direction_reading.rb +67 -0
  520. data/lib/head_music/style/guidelines/contoured/turn_reading.rb +66 -0
  521. data/lib/head_music/style/guidelines/contoured/wave_reading.rb +87 -0
  522. data/lib/head_music/style/guidelines/contoured.rb +56 -77
  523. data/lib/head_music/style/guidelines/dissonance_figure_detection.rb +13 -94
  524. data/lib/head_music/style/guidelines/embellished_suspension_treatment.rb +3 -5
  525. data/lib/head_music/style/guidelines/first_bar_entry.rb +5 -5
  526. data/lib/head_music/style/guidelines/first_bar_half_notes.rb +2 -2
  527. data/lib/head_music/style/guidelines/first_bar_quarter_notes.rb +2 -2
  528. data/lib/head_music/style/guidelines/first_bar_whole_note.rb +2 -2
  529. data/lib/head_music/style/guidelines/florid_dissonance_treatment.rb +1 -5
  530. data/lib/head_music/style/guidelines/free_dissonance_treatment.rb +157 -0
  531. data/lib/head_music/style/guidelines/independent_textures.rb +49 -0
  532. data/lib/head_music/style/guidelines/limit_octave_leaps.rb +1 -4
  533. data/lib/head_music/style/guidelines/maximum_notes.rb +1 -6
  534. data/lib/head_music/style/guidelines/minimum_melodic_intervals.rb +2 -9
  535. data/lib/head_music/style/guidelines/minimum_notes.rb +1 -8
  536. data/lib/head_music/style/guidelines/minimum_threshold.rb +3 -1
  537. data/lib/head_music/style/guidelines/mix_species_textures.rb +4 -44
  538. data/lib/head_music/style/guidelines/no_parallel_perfect.rb +7 -2
  539. data/lib/head_music/style/guidelines/no_parallel_perfect_across_barline.rb +6 -37
  540. data/lib/head_music/style/guidelines/no_rests.rb +1 -1
  541. data/lib/head_music/style/guidelines/no_rests_after_note.rb +2 -2
  542. data/lib/head_music/style/guidelines/note_count_per_bar.rb +5 -7
  543. data/lib/head_music/style/guidelines/note_fills_final_bar.rb +1 -1
  544. data/lib/head_music/style/guidelines/notes_same_length.rb +16 -16
  545. data/lib/head_music/style/guidelines/prefer_long_before_short.rb +4 -2
  546. data/lib/head_music/style/guidelines/set_against_another_voice.rb +2 -2
  547. data/lib/head_music/style/guidelines/suspension_treatment.rb +3 -21
  548. data/lib/head_music/style/guidelines/weak_beat_dissonance_treatment.rb +0 -4
  549. data/lib/head_music/style/guides/composite_guide.rb +35 -4
  550. data/lib/head_music/style/guides/fifth_species_melody.rb +3 -6
  551. data/lib/head_music/style/guides/first_species_melody.rb +2 -8
  552. data/lib/head_music/style/guides/first_three_species_melody.rb +2 -8
  553. data/lib/head_music/style/guides/fourth_species_melody.rb +1 -5
  554. data/lib/head_music/style/guides/free_species_harmony.rb +29 -0
  555. data/lib/head_music/style/guides/free_species_melody.rb +27 -0
  556. data/lib/head_music/style/guides/second_species_melody.rb +1 -5
  557. data/lib/head_music/style/guides/species_melody.rb +10 -2
  558. data/lib/head_music/style/guides/third_species_melody.rb +1 -5
  559. data/lib/head_music/style/guides/third_species_triple_meter_melody.rb +1 -5
  560. data/lib/head_music/style/mark.rb +15 -15
  561. data/lib/head_music/time/a_tempo.rb +23 -0
  562. data/lib/head_music/time/change_map.rb +46 -0
  563. data/lib/head_music/time/conductor.rb +27 -2
  564. data/lib/head_music/time/event_map.rb +16 -9
  565. data/lib/head_music/time/fermata.rb +20 -0
  566. data/lib/head_music/time/key_signature_event.rb +30 -1
  567. data/lib/head_music/time/meter_map.rb +7 -27
  568. data/lib/head_music/time/meter_scale.rb +64 -0
  569. data/lib/head_music/time/musical_time_converter.rb +25 -78
  570. data/lib/head_music/time/tempo_event.rb +11 -4
  571. data/lib/head_music/time/tempo_map.rb +8 -34
  572. data/lib/head_music/time/tempo_piece.rb +69 -0
  573. data/lib/head_music/time/tempo_pieces.rb +112 -0
  574. data/lib/head_music/time/tempo_ramp.rb +34 -0
  575. data/lib/head_music/time.rb +8 -1
  576. data/lib/head_music/version.rb +1 -1
  577. data/lib/head_music.rb +73 -3
  578. data/references/content-schema.md +52 -28
  579. data/references/fourth-species-counterpoint.md +21 -21
  580. data/references/wemi.md +1 -1
  581. data/user-stories/backlog/eighteenth-century-two-voice-counterpoint-guide.md +46 -0
  582. data/user-stories/backlog/fifth-species-published-examples.md +60 -0
  583. data/user-stories/backlog/free-species-translations.md +35 -0
  584. data/user-stories/backlog/free-species-with-imitation-guide.md +79 -0
  585. data/user-stories/backlog/free-species-with-sequence-guide.md +83 -0
  586. data/user-stories/done/abc-voices-and-meter-changes.md +274 -0
  587. data/user-stories/done/articulations-ornaments-dynamics.md +386 -0
  588. data/user-stories/done/banjo-notation-transpositions.md +369 -0
  589. data/user-stories/done/bar-markings.md +296 -0
  590. data/user-stories/done/expand-playing-techniques.md +10 -1
  591. data/user-stories/done/faster-test-suite.md +289 -0
  592. data/user-stories/done/free-species-guide.md +254 -0
  593. data/user-stories/done/grade-contour-misses-by-degree.md +634 -0
  594. data/user-stories/done/hairpins.md +229 -0
  595. data/user-stories/done/humdrum-kern-import-and-export.md +508 -0
  596. data/user-stories/done/instrument-variant.md +10 -1
  597. data/user-stories/done/key-and-spelling-inference.md +480 -0
  598. data/user-stories/done/lilypond-transposition-round-trip.md +330 -0
  599. data/user-stories/done/lyric-extenders.md +203 -0
  600. data/user-stories/done/markings-review-follow-ups.md +183 -0
  601. data/user-stories/done/mei-export.md +450 -0
  602. data/user-stories/done/mei-import.md +647 -0
  603. data/user-stories/done/midi-export.md +448 -0
  604. data/user-stories/done/midi-import.md +664 -0
  605. data/user-stories/done/musicxml-import.md +613 -0
  606. data/user-stories/done/musicxml-instrument-fallback.md +468 -0
  607. data/user-stories/done/place-dynamics-where-no-note-starts.md +145 -0
  608. data/user-stories/done/require-the-species-rhythm.md +1 -1
  609. data/user-stories/done/rhythmic-duration.md +220 -0
  610. data/user-stories/done/spans-across-notes.md +283 -0
  611. data/user-stories/done/superclass-for-note.md +11 -0
  612. data/user-stories/done/timeline-expressions.md +535 -0
  613. data/user-stories/done/tuplets-and-grace-notes.md +549 -0
  614. data/user-stories/epics/organizing-content.md +1 -1
  615. data/user-stories/index.html +125 -13
  616. metadata +449 -14
  617. data/lib/head_music/content/placement.rb +0 -175
  618. data/lib/head_music/notation/music_xml/xml_text.rb +0 -21
  619. data/lib/head_music/notation/placement_validation.rb +0 -19
  620. data/user-stories/backlog/lower-the-contour-floor.md +0 -69
@@ -1,52 +1,143 @@
1
1
  # A namespace for LilyPond-notation rendering helpers
2
2
  module HeadMusic::Notation::LilyPond
3
3
  # The computed musical facts a Writer needs to serialize a flow:
4
- # the token for every placement, on top of the measure signatures the base
4
+ # the tokens for every voice event in each bar it sounds in, on top of the measure signatures the base
5
5
  # plan tracks. Construction eagerly computes everything that can raise
6
6
  # on unmappable keys, durations, or alterations, so a RenderPlan that builds successfully
7
7
  # cannot fail assembly on those grounds.
8
8
  class RenderPlan < HeadMusic::Notation::RenderPlan
9
- def tokens_by_placement
10
- @tokens_by_placement ||= flow.voices.flat_map(&:placements).to_h do |placement|
11
- [placement, token(placement)]
9
+ MIDDLE_C = "C4"
10
+
11
+ def tokens_by_segment
12
+ @tokens_by_segment ||= flow.voices.each_with_object({}) { |voice, tokens| tokens.merge!(segment_tokens(voice).to_h) }
13
+ end
14
+
15
+ def tuplet_edges(voice)
16
+ segment_tokens(voice).tuplet_edges
17
+ end
18
+
19
+ # The length, as a fraction of a whole note, of a final bar that every
20
+ # voice ends short of together, or nil where the final bar is written
21
+ # full: where the music fills it, or where a mark follows the music in it.
22
+ def short_final_bar_fraction
23
+ music_end_fraction unless trailing_marks?
24
+ end
25
+
26
+ # Whether a short final bar is written full, so marks after the music have
27
+ # spacers to go on.
28
+ def filled_final_bar?(bar_number)
29
+ bar_number == bar_numbers.last && trailing_marks?
30
+ end
31
+
32
+ # The spacers that fill a short final bar after the voice's music, carrying
33
+ # its marks there. A nil voice is a staff nobody is written on.
34
+ def trailing_spacers(voice)
35
+ @trailing_spacers ||= {}.compare_by_identity
36
+ @trailing_spacers[voice] ||= begin
37
+ bar_number = bar_numbers.last
38
+ SpacerRun.new(bar_number, music_end_fraction, bar_length(bar_number), timed_marks(voice).trailing(bar_number, music_end_fraction)).to_s
12
39
  end
13
40
  end
14
41
 
42
+ # What a part's \new Dynamics says in each bar: spacers that reach each of
43
+ # its levels and hairpin marks at their exact positions.
44
+ def dynamics_bars(part)
45
+ @dynamics_bars ||= {}.compare_by_identity
46
+ @dynamics_bars[part] ||= bar_numbers.to_h do |bar_number|
47
+ text = SpacerRun.new(bar_number, 0r, bar_length(bar_number), part_marks(part).trailing(bar_number, 0r)).to_s
48
+ [bar_number, [text, (bar_number == bar_numbers.last) ? final_barline_marks(part) : nil].compact.join(" ")]
49
+ end
50
+ end
51
+
52
+ # Where a written c' sounds, restated wherever the part's instrument
53
+ # changes it, back to c' for an instrument in C.
54
+ def transposition_changes(part)
55
+ return {} unless transposed? && part
56
+
57
+ @transposition_changes ||= {}.compare_by_identity
58
+ @transposition_changes[part] ||= part.instrument_changes.keys
59
+ .select { |bar_number| bar_numbers.cover?(bar_number) && bar_number > bar_numbers.first }
60
+ .filter_map { |bar_number|
61
+ command = transposition_command(part.instrument_at(bar_number))
62
+ [bar_number, command] unless command == transposition_command(part.instrument_at(bar_number - 1))
63
+ }.to_h
64
+ end
65
+
66
+ def transposition_command(instrument)
67
+ sounding = HeadMusic::Content::Layout::Transposition.for(instrument).sounding(MIDDLE_C)
68
+ "\\transposition #{PitchWriter.token(sounding)}"
69
+ end
70
+
71
+ # Whether a part has a \new Dynamics to write.
72
+ def dynamics?(part)
73
+ !part.dynamic_events.empty? || part.spans.any?(&:hairpin?)
74
+ end
75
+
76
+ # A part's own dynamics are written in a \new Dynamics, so only the voice's
77
+ # are placed among its links. A nil voice is a staff nobody is written on.
78
+ def timed_marks(voice)
79
+ @timed_marks ||= {}.compare_by_identity
80
+ @timed_marks[voice] ||= voice ? TimedMarks.voice(voice, lead: lead?(voice)) : TimedMarks.new([])
81
+ end
82
+
83
+ # What ends a hairpin on the final barline, after the owner's last link or
84
+ # spacer: \after there prints "no broken bound" for a level, and a marker
85
+ # with no duration reaches it cleanly. The first voice then writes the
86
+ # tempo marks there, as a ramp's target.
87
+ def final_barline_marks(owner)
88
+ final_barline = flow.final_barline_position
89
+ hairpin_end = "<>\\!" if owner&.spans&.any? { |span| span.hairpin? && span.to == final_barline }
90
+ marks = [hairpin_end, *(final_tempo_marks if owner && lead?(owner))].compact
91
+ marks.join(" ") unless marks.empty?
92
+ end
93
+
94
+ def bar_length(bar_number)
95
+ return short_final_bar_fraction if bar_number == bar_numbers.last && short_final_bar_fraction
96
+
97
+ meter = effective_meter(bar_number)
98
+ Rational(meter.top_number, meter.bottom_number)
99
+ end
100
+
15
101
  private
16
102
 
17
103
  def precompute_eager_data
18
104
  super
19
- tokens_by_placement
105
+ tokens_by_segment
106
+ flow.parts.each { |part| dynamics_bars(part) if dynamics?(part) }
107
+ [nil, *flow.voices].each { |voice| trailing_spacers(voice) } if trailing_marks?
20
108
  end
21
109
 
22
- # LilyPond has no way to say "three flats read as dorian", so it renders
23
- # what is printed at the clef.
24
- def key_value(event)
25
- KeyMapper.token(event.printed_key_signature)
110
+ # A part's levels and hairpins, which its \new Dynamics holds apart from
111
+ # its voices, so a voice's level does not cut a part's hairpin.
112
+ def part_marks(part)
113
+ @part_marks ||= {}.compare_by_identity
114
+ @part_marks[part] ||= TimedMarks.part(part)
26
115
  end
27
116
 
28
- # A tied chain within a placement joins its links with the tie mark;
29
- # a chain of rests emits consecutive untied rests, and a tied chord
30
- # repeats the whole chord.
31
- def token(placement)
32
- return rest_token(placement) if placement.rest?
33
- return chord_token(placement) if placement.chord?
117
+ def trailing_marks?
118
+ return @trailing_marks if defined?(@trailing_marks)
34
119
 
35
- note_token(placement)
120
+ owners_marks = flow.parts.map { |part| part_marks(part) } + flow.voices.map { |voice| timed_marks(voice) }
121
+ @trailing_marks = !music_end_fraction.nil? && owners_marks.any? { |marks| marks.trailing(bar_numbers.last, music_end_fraction).any? }
36
122
  end
37
123
 
38
- def rest_token(placement)
39
- placement.rhythmic_value.tied_chain.map { |link| "r#{DurationWriter.token(link)}" }.join(" ")
124
+ # LilyPond has no way to say "three flats read as dorian", so it renders
125
+ # what is printed at the clef.
126
+ def key_value(event)
127
+ KeyMapper.token(event.printed_key_signature)
40
128
  end
41
129
 
42
- def note_token(placement)
43
- pitch_token = PitchWriter.token(placement.pitch)
44
- placement.rhythmic_value.tied_chain.map { |link| "#{pitch_token}#{DurationWriter.token(link)}" }.join("~ ")
130
+ def segment_tokens(voice)
131
+ @segment_tokens ||= {}.compare_by_identity
132
+ @segment_tokens[voice] ||= SegmentTokens.new(voice, segments_by_bar(voice).values.flatten, timed_marks(voice), fermata_carriers)
45
133
  end
46
134
 
47
- def chord_token(placement)
48
- body = "<#{placement.pitches.sort.map { |pitch| PitchWriter.token(pitch) }.join(" ")}>"
49
- placement.rhythmic_value.tied_chain.map { |link| "#{body}#{DurationWriter.token(link)}" }.join("~ ")
135
+ def final_tempo_marks
136
+ final_barline = flow.final_barline_position
137
+ HeadMusic::Notation::TempoMarks.new(flow).select { |mark| mark.position == final_barline }.filter_map do |mark|
138
+ token, rank = TimedMarks.tempo_token(mark)
139
+ (rank == TimedMarks::BEFORE_RANK) ? token : token && "<>#{token}"
140
+ end
50
141
  end
51
142
  end
52
143
  end
@@ -0,0 +1,135 @@
1
+ # A namespace for LilyPond-notation rendering helpers
2
+ module HeadMusic::Notation::LilyPond
3
+ # The LilyPond words for each bar segment of one voice's events.
4
+ #
5
+ # A tied chain within a voice event joins its links with the tie mark, and
6
+ # so does a voice event split at a barline, whose piece before the bar check
7
+ # ends in one; a chain of rests emits consecutive untied rests, and a tied
8
+ # chord repeats the whole chord. Each link carries the voice's levels and
9
+ # hairpin marks that fall in it, and only the first the voice event's own
10
+ # marks, after any hairpin ending there and before any starting.
11
+ # A slur closes on the last link and opens on the first, closing first
12
+ # where they are one word, so one slur can end where the next begins. A
13
+ # rest reads back as one rest per link, so it carries both on its first.
14
+ #
15
+ # Each tuplet bracket opens before its first link and closes after its
16
+ # last, outside the link's marks and tie.
17
+ #
18
+ # A voice event sounding where a fermata begins carries \fermata on the
19
+ # link sounding there, so a reader finds it starting no later.
20
+ class SegmentTokens
21
+ def initialize(voice, segments, timed_marks, fermatas = {})
22
+ @voice = voice
23
+ @segments = segments
24
+ @timed_marks = timed_marks
25
+ @fermatas = fermatas
26
+ end
27
+
28
+ def to_h
29
+ segments.to_h { |segment| [segment, token(segment)] }
30
+ end
31
+
32
+ # What opens and closes each tuplet bracket, by the [segment, link index]
33
+ # it opens before or closes after. A bracket crossing a barline opens in
34
+ # one bar and closes in the next.
35
+ def tuplet_edges
36
+ @tuplet_edges ||= begin
37
+ brackets = HeadMusic::Notation::TupletBrackets.for(voice, segments, error_class: RenderError)
38
+ opens = brackets.to_h { |bracket| [link_key(bracket.first), "\\tuplet #{bracket.ratio.to_a.join("/")} { "] }
39
+ closes = brackets.to_h { |bracket| [link_key(bracket.last), " }"] }
40
+ {opens: opens, closes: closes}
41
+ end
42
+ end
43
+
44
+ private
45
+
46
+ attr_reader :voice, :segments, :timed_marks, :fermatas
47
+
48
+ def token(segment)
49
+ voice_event = segment.voice_event
50
+ words = link_words(segment)
51
+ words[closes_index(segment, words)] += span_marks.closes_at(voice_event.position) if closes?(segment)
52
+ words[0] += span_marks.opens_at(voice_event.position) if starts?(segment)
53
+ words = tied(segment, words) unless voice_event.rest?
54
+ bracketed(segment, words).join(" ")
55
+ end
56
+
57
+ def tied(segment, words)
58
+ words.each_with_index.map { |word, index| (index < words.length - 1 || segment.continues) ? "#{word}~" : word }
59
+ end
60
+
61
+ def bracketed(segment, words)
62
+ words.each_with_index.map do |word, index|
63
+ key = [segment, index]
64
+ "#{tuplet_edges[:opens][key]}#{word}#{tuplet_edges[:closes][key]}"
65
+ end
66
+ end
67
+
68
+ def link_key(link)
69
+ [link.segment, link.index]
70
+ end
71
+
72
+ # A link's offsets are exact and sounding, so marks fall where they
73
+ # sound, though the link is written under its ratio.
74
+ def link_words(segment)
75
+ voice_event = segment.voice_event
76
+ bar_number = segment.bar_number
77
+ from = segment.offset || HeadMusic::Notation::BarSplitter.onset_in_bar(voice_event)
78
+ own = starts?(segment) ? MarkWriter.token(voice_event) : ""
79
+ graces = starts?(segment) ? grace_token(voice_event) : ""
80
+ forced = starts?(segment)
81
+ first = starts?(segment)
82
+ segment.rhythmic_duration!(RenderError).values.map do |link|
83
+ to = from + HeadMusic::Notation::DottedDuration.sounding_fraction(link)
84
+ own += "\\fermata" if fermata_in?(voice_event, bar_number, from, to, first)
85
+ word = graces + timed_marks.around(bar_number, from, to, "#{body(voice_event, forced)}#{DurationWriter.token(link)}", own, factor: link.tuplet_ratio&.factor || 1)
86
+ from = to
87
+ own = graces = ""
88
+ forced = first = false
89
+ word
90
+ end
91
+ end
92
+
93
+ # A fermata where the event starts goes on its first link, and one
94
+ # beginning later on the link sounding there.
95
+ def fermata_in?(voice_event, bar_number, from, to, first)
96
+ fermatas.fetch(voice_event, []).any? do |fermata|
97
+ next first if fermata.from == voice_event.position
98
+
99
+ offset = HeadMusic::Notation::BarSplitter.offset_in_bar(fermata.from)
100
+ fermata.from.bar_number == bar_number && offset >= from && offset < to
101
+ end
102
+ end
103
+
104
+ def grace_token(voice_event)
105
+ return "" if voice_event.grace_notes.empty?
106
+
107
+ position = voice_event.position
108
+ GraceWriter.token(voice_event, slur_opens: span_marks.grace_opens_at(position), implied_slur: span_marks.implied_grace_slur?(position))
109
+ end
110
+
111
+ def body(voice_event, forced)
112
+ return "r" if voice_event.rest?
113
+
114
+ voice_event.chord? ? PitchWriter.chord(voice_event.pitches, forced: forced) : PitchWriter.token(voice_event.pitch)
115
+ end
116
+
117
+ def closes?(segment)
118
+ segment.voice_event.rest? ? starts?(segment) : !segment.continues
119
+ end
120
+
121
+ def closes_index(segment, words)
122
+ segment.voice_event.rest? ? 0 : words.length - 1
123
+ end
124
+
125
+ # A rounded onset just before a barline starts in the bar before its
126
+ # position's.
127
+ def starts?(segment)
128
+ segment.elapsed.zero?
129
+ end
130
+
131
+ def span_marks
132
+ @span_marks ||= SpanMarks.new(voice)
133
+ end
134
+ end
135
+ end
@@ -0,0 +1,103 @@
1
+ # A namespace for LilyPond-notation parsing helpers
2
+ module HeadMusic::Notation::LilyPond
3
+ # Reads the \key, \time, \clef, \tempo, and \transposition commands among a context's
4
+ # music. With no stream to change, as in a \new Dynamics, which only
5
+ # repeats the staves' settings, a setting is read for its syntax alone.
6
+ #
7
+ # LilyPond prints a plain \time 4/4 or 2/2 as common or cut time, so it
8
+ # reads as one, until \numericTimeSignature asks for the numerals, and
9
+ # again after \defaultTimeSignature.
10
+ class SettingReader
11
+ TIME_SIGNATURE_STYLES = {"numericTimeSignature" => true, "defaultTimeSignature" => false}.freeze
12
+ COMMANDS = (%w[key time clef tempo transposition] + TIME_SIGNATURE_STYLES.keys).freeze
13
+ SYMBOLS_BY_METER = {"4/4" => :common, "2/2" => :cut}.freeze
14
+
15
+ def initialize(cursor)
16
+ @cursor = cursor
17
+ @tempo_reader = TempoReader.new(cursor)
18
+ end
19
+
20
+ def read(stream = nil)
21
+ command = cursor.advance
22
+ case command.lexeme
23
+ when "key" then read_key(command, stream)
24
+ when "time" then read_time(command, stream)
25
+ when "clef" then read_clef(command, stream)
26
+ when "tempo" then read_tempo(command, stream)
27
+ when "transposition" then read_transposition(command, stream)
28
+ else stream&.numeric_time_signature = TIME_SIGNATURE_STYLES.fetch(command.lexeme)
29
+ end
30
+ end
31
+
32
+ # Its pitch is absolute even inside \relative, and leaves the relative
33
+ # reference alone.
34
+ def read_transposition_step(command = cursor.advance)
35
+ token = cursor.advance
36
+ unless token&.type == :note && token.duration.nil? && token.multiplier.nil?
37
+ raise cursor.error("\\transposition expects a pitch", token || command)
38
+ end
39
+
40
+ HeadMusic::Notation::ConcertTransposition::Step.sounding(PitchReader.absolute.pitch(token))
41
+ end
42
+
43
+ private
44
+
45
+ attr_reader :cursor, :tempo_reader
46
+
47
+ def read_transposition(command, stream)
48
+ step = read_transposition_step(command)
49
+ stream&.change_transposition(step, command.line)
50
+ end
51
+
52
+ def read_key(command, stream)
53
+ pitch_token = cursor.advance
54
+ mode_token = cursor.advance
55
+ located(command) do
56
+ key_signature = KeyReader.key_signature(pitch_token, mode_token)
57
+ stream&.change_key_signature(key_signature, command.line)
58
+ end
59
+ end
60
+
61
+ def read_time(command, stream)
62
+ meter_token = cursor.advance
63
+ located(command) do
64
+ meter = MeterReader.meter(meter_token)
65
+ stream&.change_meter(symbolized(meter, stream), command.line)
66
+ end
67
+ end
68
+
69
+ def symbolized(meter, stream)
70
+ symbol = SYMBOLS_BY_METER[meter.to_s]
71
+ (symbol && !stream&.numeric_time_signature) ? HeadMusic::Rudiment::Meter.for_symbol(symbol) : meter
72
+ end
73
+
74
+ def read_tempo(command, stream)
75
+ tempo = tempo_reader.read(command)
76
+ stream&.change_tempo(tempo, command.line)
77
+ end
78
+
79
+ def read_clef(command, stream)
80
+ token = cursor.advance
81
+ raise cursor.error("\\clef expects a clef name", token || command) unless clef_name?(token)
82
+
83
+ stream&.clef(token.lexeme)
84
+ end
85
+
86
+ def clef_name?(token)
87
+ return false unless token
88
+
89
+ %i[word string].include?(token.type) || (token.type == :note && token.duration.nil?)
90
+ end
91
+
92
+ # A key or meter reader rejects a token without knowing where in the
93
+ # document it came from, so an error that carries no line is given the
94
+ # line of the command that introduced it.
95
+ def located(command)
96
+ yield
97
+ rescue ParseError => parse_error
98
+ raise parse_error if parse_error.line_number
99
+
100
+ raise cursor.error(parse_error.message, command)
101
+ end
102
+ end
103
+ end
@@ -0,0 +1,43 @@
1
+ # A namespace for LilyPond-notation rendering helpers
2
+ module HeadMusic::Notation::LilyPond
3
+ # Spacers from one offset in a bar to another, each mark on the first
4
+ # spacer where it falls, and each command, such as a \tempo, before it. Between marks where notes start, such as at
5
+ # tuplet onsets, a multiplied spacer reaches what no note value does.
6
+ class SpacerRun
7
+ def initialize(bar_number, from, to, marks)
8
+ @bar_number = bar_number
9
+ @from = from
10
+ @to = to
11
+ @marks_by_offset = marks.group_by(&:offset)
12
+ end
13
+
14
+ def to_s
15
+ [from, *marks_by_offset.keys.sort, to].uniq.each_cons(2).flat_map { |start, finish| spacers_between(start, finish) }.join(" ")
16
+ end
17
+
18
+ private
19
+
20
+ attr_reader :bar_number, :from, :to, :marks_by_offset
21
+
22
+ def spacers_between(start, finish)
23
+ at_onsets = [start, finish].all? { |offset| marks_at(offset).all?(&:onset) }
24
+ first, *later = spacers(finish - start, at_onsets) || raise_unreachable(marks_by_offset[finish] || marks_by_offset[start])
25
+ commands, marks = marks_at(start).partition(&:before?)
26
+ ["#{commands.map { |mark| "#{mark.token} " }.join}#{first}#{marks.map(&:token).join}", *later]
27
+ end
28
+
29
+ def marks_at(offset)
30
+ marks_by_offset.fetch(offset, [])
31
+ end
32
+
33
+ def spacers(fraction, multiplied)
34
+ chain = HeadMusic::Notation::DottedDuration.rhythmic_duration_for(fraction)&.values&.map { |link| "s#{DurationWriter.token(link)}" }
35
+ chain || (multiplied ? ["s1*#{fraction.numerator}/#{fraction.denominator}"] : nil)
36
+ end
37
+
38
+ def raise_unreachable(marks)
39
+ subject = marks ? "the #{marks.first.label}" : "the dynamics in bar #{bar_number}"
40
+ raise RenderError, "cannot place #{subject} in LilyPond output: no binary note value reaches it"
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,58 @@
1
+ # A namespace for LilyPond-notation parsing helpers
2
+ module HeadMusic::Notation::LilyPond
3
+ # The spacers each voice has read, which move its position without placing
4
+ # an event. Those that music follows become rests, since a voice holds no
5
+ # gaps. Those left in the bar where the music ends hold only time for a
6
+ # level there; once they reach a later bar, that bar is part of the music.
7
+ class Spacers
8
+ def initialize
9
+ @held = Hash.new { |hash, voice| hash[voice] = [] }.compare_by_identity
10
+ @ends = {}.compare_by_identity
11
+ @remainders = {}
12
+ end
13
+
14
+ # Where the voice's next item goes, past any spacers it has read.
15
+ def position(voice)
16
+ @ends.fetch(voice) { voice.next_position }
17
+ end
18
+
19
+ def hold(voice, event)
20
+ @ends[voice] = after(position(voice), event)
21
+ @held[voice] << event
22
+ end
23
+
24
+ # Spacers round to the nearest subtick, as the notes of a septuplet do,
25
+ # measured from where a run of them reached exactly, so s1*1/28 s1*3/28
26
+ # lands on the onsets of the septuplet sixteenths it stands beside.
27
+ def after(position, event)
28
+ exact = @remainders.fetch(position, 0) + (event.fraction * HeadMusic::Rudiment::Rhythm::SUBTICKS_PER_WHOLE)
29
+ step = exact.round
30
+ position.advance(subticks: step).tap { |reached| @remainders[reached] = exact - step }
31
+ end
32
+
33
+ def rest(voice)
34
+ @ends.delete(voice)
35
+ @held.delete(voice)&.each do |spacer|
36
+ rhythmic_duration = HeadMusic::Notation::DottedDuration.rhythmic_duration_for(spacer.fraction, tuplet: spacer.tuplet)
37
+ unless rhythmic_duration
38
+ raise UnsupportedFeatureError.new("A spacer of #{spacer.fraction} whole notes has no rest to stand for it", line_number: spacer.line)
39
+ end
40
+
41
+ voice.place(voice.next_position, rhythmic_duration)
42
+ end
43
+ end
44
+
45
+ def finish
46
+ @held.keys.each { |voice| rest(voice) if past_music_bar?(voice) }
47
+ end
48
+
49
+ private
50
+
51
+ def past_music_bar?(voice)
52
+ music_stop = voice.next_position
53
+ bar_start = HeadMusic::Content::Position.new(voice.flow, music_stop.bar_number, 1, 0)
54
+ barline = (music_stop == bar_start) ? music_stop : music_stop.start_of_next_bar
55
+ position(voice) > barline
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,91 @@
1
+ # A namespace for LilyPond-notation rendering helpers
2
+ module HeadMusic::Notation::LilyPond
3
+ # The slur and phrasing-slur marks of one voice's spans, by the position of
4
+ # the voice event that carries them.
5
+ #
6
+ # LilyPond holds one slur, and one phrasing slur, open at a time, so a span
7
+ # that begins while another of its kind is open is named with \=, as
8
+ # \=1( … \=1), and only then. A span that begins where another ends is not
9
+ # open with it, since the close is written first, unless that note is
10
+ # written in pieces: then the open goes on the first and the close on the
11
+ # last.
12
+ #
13
+ # A span from grace notes opens on the first of them. A slur from them to
14
+ # their main note that needs no name is left to \acciaccatura or
15
+ # \appoggiatura, which imply it, so it writes no marks at all.
16
+ class SpanMarks
17
+ BRACKETS = {slur: %w[( )], phrase: %w[\\( \\)]}.freeze
18
+
19
+ def initialize(voice)
20
+ @voice = voice
21
+ @flow = voice.flow
22
+ @opens = Hash.new { |hash, key| hash[key] = [] }
23
+ @grace_opens = Hash.new { |hash, key| hash[key] = [] }
24
+ @closes = Hash.new { |hash, key| hash[key] = [] }
25
+ @implied = Set.new
26
+ named(voice.spans.select(&:curve?)).each { |span, id| add(span, id) }
27
+ end
28
+
29
+ def opens_at(position)
30
+ @opens.fetch(key(position), []).join
31
+ end
32
+
33
+ def grace_opens_at(position)
34
+ @grace_opens.fetch(key(position), []).join
35
+ end
36
+
37
+ def closes_at(position)
38
+ @closes.fetch(key(position), []).join
39
+ end
40
+
41
+ # Whether the grace notes before the note event at the position carry a
42
+ # slur to it that their command implies.
43
+ def implied_grace_slur?(position)
44
+ @implied.include?(key(position))
45
+ end
46
+
47
+ private
48
+
49
+ def add(span, id)
50
+ return @implied << key(span.from) if implied?(span, id)
51
+
52
+ opening, closing = BRACKETS.fetch(span.kind)
53
+ name = id && "\\=#{id}"
54
+ (span.from_grace? ? @grace_opens : @opens)[key(span.from)] << "#{name}#{opening}"
55
+ @closes[key(span.to)] << "#{name}#{closing}"
56
+ end
57
+
58
+ # One command implies one slur, so the grace notes must share a slash.
59
+ def implied?(span, id)
60
+ return false unless span.from_grace? && span.kind == :slur && span.to == span.from && id.nil?
61
+
62
+ @voice.voice_event_at(span.from).grace_notes.map(&:slashed?).uniq.length == 1
63
+ end
64
+
65
+ def key(position)
66
+ HeadMusic::Content::Position.new(@flow, position.to_s).to_s
67
+ end
68
+
69
+ def named(spans)
70
+ spans.each_with_object([]) do |span, placed|
71
+ open_ids = placed.filter_map { |other, id| id || :plain if other.kind == span.kind && open_at?(other, span) }
72
+ id = open_ids.include?(:plain) ? (1..).find { |candidate| !open_ids.include?(candidate) } : nil
73
+ placed << [span, id]
74
+ end
75
+ end
76
+
77
+ # Grace notes come before their main note, so a span ending on that note
78
+ # is still open across them.
79
+ def open_at?(other, span)
80
+ position = span.from
81
+ return other.to >= position if span.from_grace?
82
+
83
+ other.to > position || (other.to == position && in_pieces?(other.to))
84
+ end
85
+
86
+ def in_pieces?(position)
87
+ voice_event = @voice.voice_event_at(position)
88
+ !voice_event.rest? && HeadMusic::Notation::BarSplitter.segments_of(voice_event).sum { |segment| segment.rhythmic_duration&.values&.length || 1 } > 1
89
+ end
90
+ end
91
+ end
@@ -0,0 +1,35 @@
1
+ # A namespace for LilyPond-notation parsing helpers
2
+ module HeadMusic::Notation::LilyPond
3
+ # Pairs slur and phrase marks within each voice by kind and name. As in
4
+ # LilyPond, an open while one of the same kind and name is open is
5
+ # ignored, a close with none open is dropped, and so is a span the voice
6
+ # refuses, such as a slur ending on a rest. One opened on a note's first
7
+ # grace note starts from_grace, and may end on that same note.
8
+ class SpanPairing
9
+ def initialize
10
+ @open = Hash.new { |hash, voice| hash[voice] = {} }
11
+ end
12
+
13
+ def apply(voice_event, span_marks)
14
+ open = @open[voice_event.voice]
15
+ span_marks.each do |mark|
16
+ key = [mark.kind, mark.id]
17
+ next open[key] ||= [voice_event.position, mark.from_grace] if mark.opening
18
+
19
+ from, from_grace = open.delete(key)
20
+ add_span(voice_event, mark.kind, from, from_grace) if from
21
+ end
22
+ end
23
+
24
+ private
25
+
26
+ def add_span(voice_event, kind, from, from_grace)
27
+ to = voice_event.position
28
+ return if from > to || (from == to && !from_grace)
29
+
30
+ voice_event.voice.add_span(kind, from: from, to: to, from_grace: from_grace)
31
+ rescue ArgumentError
32
+ nil
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,46 @@
1
+ # A namespace for LilyPond-notation parsing helpers
2
+ module HeadMusic::Notation::LilyPond
3
+ # Reads what follows a \tempo: text, as a string or a markup's words, a
4
+ # metronome mark, or both, as in \tempo "Allegro" 4. = 60. A range, as in
5
+ # 4 = 100-120, reads as its first number.
6
+ class TempoReader
7
+ def initialize(cursor)
8
+ @cursor = cursor
9
+ @duration_reader = DurationReader.new
10
+ end
11
+
12
+ # Nil for a \tempo of blank text alone, which says nothing.
13
+ def read(command)
14
+ text = read_text
15
+ metronome = cursor.accept(:metronome)
16
+ raise cursor.error("\\tempo expects text or a metronome mark", cursor.peek || command) unless text || metronome
17
+
18
+ text = nil if text&.strip&.empty?
19
+ return HeadMusic::Rudiment::Tempo.for_text(text) if text && !metronome
20
+ return unless metronome
21
+
22
+ HeadMusic::Rudiment::Tempo.new(beat_value(metronome), beats_per_minute(metronome), text: text)
23
+ end
24
+
25
+ private
26
+
27
+ attr_reader :cursor, :duration_reader
28
+
29
+ def read_text
30
+ return cursor.markup_text if cursor.markup?
31
+
32
+ cursor.accept(:string)&.lexeme
33
+ end
34
+
35
+ def beat_value(metronome)
36
+ duration_reader.beat_value(metronome)
37
+ end
38
+
39
+ def beats_per_minute(metronome)
40
+ count = metronome.lexeme[Lexer::METRONOME_PATTERN, 2].to_i
41
+ raise cursor.error("A \\tempo must count a positive number of beats per minute", metronome) unless count.positive?
42
+
43
+ count
44
+ end
45
+ end
46
+ end