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
data/CLAUDE.md CHANGED
@@ -4,7 +4,7 @@ This file provides guidance to Claude Code (claude.ai/code) when working with co
4
4
 
5
5
  ## Project Overview
6
6
 
7
- HeadMusic is a Ruby gem for Western music theory. It provides a comprehensive toolkit for working with pitches, scales, intervals, chords, and musical analysis. The gem supports internationalization with translations in 7 languages.
7
+ HeadMusic is a Ruby gem for Western music theory. It provides a comprehensive toolkit for working with pitches, scales, intervals, chords, and musical analysis. The gem supports internationalization with translations in 6 languages, plus British English.
8
8
 
9
9
  ## Development Commands
10
10
 
@@ -13,10 +13,12 @@ HeadMusic is a Ruby gem for Western music theory. It provides a comprehensive to
13
13
  `rake -T` lists the custom tasks.
14
14
 
15
15
  ```bash
16
- bundle exec rake validate # tests with coverage, linting, security audit, code quality
17
- bundle exec rake # tests WITH coverage (the default task, same as `rake spec`)
18
- bundle exec rspec # tests WITHOUT coverage, so a single file can run on its own
16
+ bundle exec rake validate # parallel tests without coverage, linting, security audit, code quality
17
+ bundle exec rake # parallel tests WITH coverage (the default task, same as `rake spec`)
18
+ bundle exec rake spec:without_coverage # parallel tests alone, as `rake validate` runs them
19
+ bundle exec rspec # serial tests WITHOUT coverage, so a single file can run on its own
19
20
  bundle exec rake coverage # opens the last coverage report
21
+ bundle exec rake oracles # only the specs checked against outside tools and files, each missing one saying how to set it up
20
22
  ```
21
23
 
22
24
  ### Git Etiquette
@@ -41,7 +43,7 @@ Do not use an assignment inside a condition.
41
43
 
42
44
  ### Testing
43
45
 
44
- Tests are written in RSpec and located in the `/spec` directory, mirroring the `/lib` structure. The project requires 90% code coverage minimum. Coverage is opt-in: `rake spec` sets `COVERAGE=true`, and `spec_helper.rb` starts SimpleCov only then.
46
+ Tests are written in RSpec and located in the `/spec` directory, mirroring the `/lib` structure. The project requires 90% code coverage minimum. Coverage is opt-in: `rake spec` sets `COVERAGE=true`, and `spec_helper.rb` starts SimpleCov only then. `rake spec` and `rake validate` run the specs with `parallel_rspec`, one process per core, and SimpleCov merges the processes before checking the minimum. Both take `SPEC=path` to narrow the run. Each process writes its own status file, and the task folds them back into `spec/examples.txt`, so `rspec --only-failures` works after a parallel run. `rake validate` skips coverage to save time; run `rake spec` occasionally to check it. CI checks it on pushes to `main` and on pull requests.
45
47
 
46
48
  ## Architecture
47
49
 
@@ -53,6 +55,7 @@ The codebase follows a domain-driven design with clear module boundaries:
53
55
  - Abstract concepts: pitch, interval, scale, chord, key
54
56
  - Duration concepts (without visual representation)
55
57
  - Factory methods: `.get()` for most rudiments
58
+ - Future: catalogs of articulations, ornaments, and dynamics
56
59
 
57
60
  2. **HeadMusic::Notation** - Visual music notation and representation
58
61
  - Staff positions, lines, spaces, ledger lines
@@ -60,7 +63,7 @@ The codebase follows a domain-driven design with clear module boundaries:
60
63
  - Clef placement and rendering
61
64
  - Notehead shapes, stems, flags, beams
62
65
  - Accidental placement rules
63
- - Future: ties, slurs, articulations, dynamics
66
+ - Future: ties, slurs
64
67
 
65
68
  3. **HeadMusic::Instruments** - Instrument definitions
66
69
  - Instrument families and classification
@@ -70,7 +73,7 @@ The codebase follows a domain-driven design with clear module boundaries:
70
73
 
71
74
  4. **HeadMusic::Content** - Musical composition representation
72
75
  - Compositions, voices, bars, positions
73
- - Notes in context (pitch + duration + placement)
76
+ - Voice events in context: note events and rest events (sounds + rhythmic value + position)
74
77
  - Uses HeadMusic::Time for temporal positioning
75
78
 
76
79
  5. **HeadMusic::Time** - Temporal infrastructure
@@ -116,7 +119,7 @@ Module loading order matters and is defined in `lib/head_music.rb`.
116
119
 
117
120
  The gem supports multiple languages through the HeadMusic::Named mixin:
118
121
  - Translations in `lib/head_music/locales/`
119
- - Languages: en, de, es, fr, it, ja, nl
122
+ - Languages: en, de, es, fr, it, ru, and en_GB for British spellings
120
123
  - Use `I18n.locale = :de` to change language
121
124
 
122
125
  ### Testing Patterns
data/Gemfile CHANGED
@@ -14,7 +14,8 @@ gem "activesupport", "~> #{activesupport_version}.0" unless activesupport_versio
14
14
  gem "standard", require: false
15
15
 
16
16
  group :test do
17
- # rubocop is not listed directly: standard pins it (~> 1.88.0) and must
17
+ gem "parallel_tests", require: false
18
+ # rubocop is not listed directly: standard pins it (~> 1.91.0) and must
18
19
  # lead. Listing it here let Dependabot bump it past standard, which the
19
20
  # resolver "fixed" by downgrading standard 1.56 -> 1.35.
20
21
  gem "rubocop-rspec", require: false
data/Gemfile.lock CHANGED
@@ -1,15 +1,16 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- head_music (22.0.0)
4
+ head_music (24.0.0)
5
5
  activesupport (>= 7.0, < 10)
6
6
  humanize (>= 2, < 4)
7
7
  i18n (~> 1.8)
8
+ rexml (~> 3.4, >= 3.4.2)
8
9
 
9
10
  GEM
10
11
  remote: https://rubygems.org/
11
12
  specs:
12
- activesupport (8.1.3.1)
13
+ activesupport (8.1.4.1)
13
14
  base64
14
15
  bigdecimal
15
16
  concurrent-ruby (~> 1.0, >= 1.3.1)
@@ -22,7 +23,7 @@ GEM
22
23
  securerandom (>= 0.3)
23
24
  tzinfo (~> 2.0, >= 2.0.5)
24
25
  uri (>= 0.13.1)
25
- addressable (2.9.0)
26
+ addressable (2.9.2)
26
27
  public_suffix (>= 2.0.2, < 8.0)
27
28
  ast (2.4.3)
28
29
  axiom-types (0.1.1)
@@ -30,7 +31,7 @@ GEM
30
31
  ice_nine (~> 0.11.0)
31
32
  thread_safe (~> 0.3, >= 0.3.1)
32
33
  base64 (0.3.0)
33
- bigdecimal (4.1.2)
34
+ bigdecimal (4.1.3)
34
35
  bundler-audit (0.9.3)
35
36
  bundler (>= 1.2.0)
36
37
  thor (~> 1.0)
@@ -87,7 +88,7 @@ GEM
87
88
  i18n (1.15.2)
88
89
  concurrent-ruby (~> 1.0)
89
90
  ice_nine (0.11.2)
90
- json (2.21.2)
91
+ json (3.1.0)
91
92
  kramdown (2.5.2)
92
93
  rexml (>= 3.4.4)
93
94
  language_server-protocol (3.17.0.6)
@@ -97,12 +98,14 @@ GEM
97
98
  logger (~> 1.6)
98
99
  lint_roller (1.1.0)
99
100
  logger (1.7.0)
100
- minitest (6.0.6)
101
+ minitest (6.1.0)
101
102
  drb (~> 2.0)
102
- prism (~> 1.5)
103
+ prism (>= 1.5, < 3)
103
104
  ostruct (0.6.3)
104
- parallel (2.1.0)
105
- parser (3.3.12.0)
105
+ parallel (2.3.0)
106
+ parallel_tests (5.9.0)
107
+ parallel
108
+ parser (3.3.13.0)
106
109
  ast (~> 2.4.1)
107
110
  racc
108
111
  path_expander (2.0.1)
@@ -117,26 +120,26 @@ GEM
117
120
  parser (~> 3.3.0)
118
121
  rainbow (>= 2.0, < 4.0)
119
122
  rexml (~> 3.1)
120
- regexp_parser (2.12.0)
123
+ regexp_parser (2.13.1)
121
124
  rexml (3.4.4)
122
125
  rspec (3.13.2)
123
126
  rspec-core (~> 3.13.0)
124
127
  rspec-expectations (~> 3.13.0)
125
128
  rspec-mocks (~> 3.13.0)
126
- rspec-core (3.13.6)
129
+ rspec-core (3.13.7)
127
130
  rspec-support (~> 3.13.0)
128
- rspec-expectations (3.13.5)
131
+ rspec-expectations (3.13.6)
129
132
  diff-lcs (>= 1.2.0, < 2.0)
130
133
  rspec-support (~> 3.13.0)
131
134
  rspec-its (2.0.0)
132
135
  rspec-core (>= 3.13.0)
133
136
  rspec-expectations (>= 3.13.0)
134
- rspec-mocks (3.13.8)
137
+ rspec-mocks (3.13.9)
135
138
  diff-lcs (>= 1.2.0, < 2.0)
136
139
  rspec-support (~> 3.13.0)
137
- rspec-support (3.13.7)
138
- rubocop (1.88.2)
139
- json (~> 2.3)
140
+ rspec-support (3.13.8)
141
+ rubocop (1.91.0)
142
+ json (>= 2.3)
140
143
  language_server-protocol (~> 3.17.0.2)
141
144
  lint_roller (~> 1.1.0)
142
145
  parallel (>= 1.10)
@@ -179,12 +182,12 @@ GEM
179
182
  tty-which (~> 0.5.0)
180
183
  virtus (~> 2.0)
181
184
  securerandom (0.4.1)
182
- sexp_processor (4.17.5)
185
+ sexp_processor (4.17.6)
183
186
  simplecov (1.0.3)
184
- standard (1.56.0)
187
+ standard (1.57.0)
185
188
  language_server-protocol (~> 3.17.0.2)
186
189
  lint_roller (~> 1.0)
187
- rubocop (~> 1.88.0)
190
+ rubocop (~> 1.91.0)
188
191
  standard-custom (~> 1.0.0)
189
192
  standard-performance (~> 1.8)
190
193
  standard-custom (1.0.2)
@@ -198,10 +201,10 @@ GEM
198
201
  tty-which (0.5.0)
199
202
  tzinfo (2.0.6)
200
203
  concurrent-ruby (~> 1.0)
201
- unicode-display_width (3.2.0)
202
- unicode-emoji (~> 4.1)
203
- unicode-emoji (4.2.0)
204
- uri (1.1.1)
204
+ unicode-display_width (3.3.0)
205
+ unicode-emoji (~> 4.3)
206
+ unicode-emoji (4.3.0)
207
+ uri (1.1.2)
205
208
  virtus (2.0.0)
206
209
  axiom-types (~> 0.1)
207
210
  coercible (~> 1.0)
@@ -219,8 +222,8 @@ DEPENDENCIES
219
222
  bundler-audit (~> 0.9, >= 0)
220
223
  head_music!
221
224
  kramdown
225
+ parallel_tests
222
226
  rake (~> 13.0)
223
- rexml (~> 3.4)
224
227
  rspec (~> 3.0)
225
228
  rspec-its (~> 2.0)
226
229
  rubocop-rake
data/MUSIC_THEORY.md CHANGED
@@ -58,7 +58,7 @@ unpitched note
58
58
  - a sounded rhythmic value without a specific pitch
59
59
 
60
60
  tied duration
61
- - combines multiple rhythmic values into one longer note
61
+ - combines multiple rhythmic values into one longer duration
62
62
  - ties allow rhythmic values to be combined across barlines or strong beats
63
63
 
64
64
  articulation
data/README.md CHANGED
@@ -14,7 +14,7 @@ The **head_music** Ruby gem provides a toolkit for working with Western music th
14
14
  - **Style Analysis**: Rules for species counterpoint and voice leading
15
15
  - **Internationalization**: Support for multiple languages (English, French, German, Italian, Russian, Spanish)
16
16
  - **Instrument Modeling**: Extensive database of musical instruments with ranges and properties
17
- - **Notation Formats**: Read ABC and LilyPond into compositions; write compositions as ABC, LilyPond, and MusicXML
17
+ - **Notation Formats**: Read ABC, LilyPond, and Humdrum `**kern` into compositions; write compositions as ABC, LilyPond, MusicXML, and `**kern`
18
18
 
19
19
  ## Installation
20
20
 
@@ -64,7 +64,17 @@ puts composition.voices.first.pitches.map(&:to_s) # => ["G3", "A3", "B3", "C4",
64
64
  puts composition.to_lilypond # => a complete LilyPond document
65
65
  ```
66
66
 
67
- The LilyPond reader covers absolute and `\relative` pitches, durations and dots, rests and whole-bar rests, chords, intra-bar ties, `\key`, `\time`, `\clef`, bar checks, `\header` title and composer, and `\new Staff` / `\new Voice` contexts. Constructs outside that subset (tuplets, lyrics, variables, articulations, and so on) raise an `UnsupportedFeatureError` rather than being skipped.
67
+ The LilyPond reader covers absolute and `\relative` pitches, durations and dots, rests and whole-bar rests, chords, ties (including across bar checks), `\key`, `\time`, `\clef`, bar checks, `\header` title and composer, `\new Staff` / `\new Voice` contexts, and grand staves and staff groups with `\change Staff`. Constructs outside that subset (tuplets, lyrics, variables, articulations, and so on) raise an `UnsupportedFeatureError` rather than being skipped.
68
+
69
+ ```ruby
70
+ # Read a Humdrum **kern file, such as a Bach chorale from KernScores
71
+ chorale = HeadMusic::Notation::Kern.parse(File.read("chor001.krn"))
72
+ chorale.parts.map { |part| part.player.name } # => ["Soprano", "Alto", "Tenor", "Bass"]
73
+ chorale.work.catalog_number # => "BWV 269"
74
+ puts chorale.to_kern # => the same spines back
75
+ ```
76
+
77
+ The kern reader covers pitches, durations and dots, rests, chords, ties across barlines, pickups and short final bars, repeat barlines, `*k`, key and mode designations, `*M`, `*MM`, `*clef`, `*I` names and vocal and piano codes, `*part` / `*staff` grouping, spine splits, joins, and exchanges, `**text` lyrics, and the reference records of a work. Tuplets, `*+`, and transposed spines raise an `UnsupportedFeatureError`; grace notes, ornaments, articulations, beams, and fermatas are dropped.
68
78
 
69
79
  ## Style Analysis
70
80
 
data/Rakefile CHANGED
@@ -1,9 +1,62 @@
1
1
  require "bundler/gem_tasks"
2
2
  require "rspec/core/rake_task"
3
- require "standard/rake"
3
+ require "rubocop/rake_task"
4
4
 
5
- # The block runs when the task does, just before rspec is launched.
6
- RSpec::Core::RakeTask.new(:spec) { ENV["COVERAGE"] = "true" }
5
+ RuboCop::RakeTask.new
6
+
7
+ # One process per core. parallel_tests places each file by its runtime in
8
+ # tmp/parallel_runtime_rspec.log, or by its size when that log covers too few
9
+ # files, and SimpleCov merges the processes' coverage.
10
+ module ParallelSpecs
11
+ extend Rake::FileUtilsExt
12
+
13
+ STATUS_FILE = "spec/examples.txt"
14
+ PROCESS_STATUS_FILES = "spec/examples[0-9]*.txt"
15
+ RUNTIME_LOG = "tmp/parallel_runtime_rspec.log"
16
+
17
+ def self.run(coverage:)
18
+ narrowed = ENV.key?("SPEC")
19
+ runtimes = narrowed ? runtimes_logged : {}
20
+ rm_f Dir[PROCESS_STATUS_FILES]
21
+ # SimpleCov would merge a serial result from the last ten minutes into the total.
22
+ rm_f %w[coverage/.resultset.json coverage/.resultset.json.lock] if coverage
23
+ # --first-is-1 gives every process a numbered status file to fold back in.
24
+ sh({"COVERAGE" => (coverage ? "true" : nil)}, "bundle exec parallel_rspec --first-is-1 #{ENV.fetch("SPEC", "spec")}")
25
+ ensure
26
+ fold_statuses
27
+ # A narrowed run logs only its own files, which would leave the next full run to group by size.
28
+ write_runtimes(runtimes.merge(runtimes_logged)) if narrowed
29
+ end
30
+
31
+ def self.runtimes_logged
32
+ return {} unless File.exist?(RUNTIME_LOG)
33
+
34
+ File.readlines(RUNTIME_LOG, chomp: true).to_h { |line| line.rpartition(":").values_at(0, 2) }
35
+ end
36
+
37
+ def self.write_runtimes(runtimes)
38
+ File.write(RUNTIME_LOG, runtimes.map { |file, time| "#{file}:#{time}\n" }.join)
39
+ end
40
+
41
+ # Keeps `rspec --only-failures` whole after a parallel run.
42
+ def self.fold_statuses
43
+ require "rspec/core"
44
+ files = Dir[PROCESS_STATUS_FILES]
45
+ this_run = files.flat_map { |file| RSpec::Core::ExampleStatusPersister.load_from(file) }
46
+ return if this_run.empty?
47
+
48
+ merged = RSpec::Core::ExampleStatusMerger.merge(this_run, RSpec::Core::ExampleStatusPersister.load_from(STATUS_FILE))
49
+ File.write(STATUS_FILE, RSpec::Core::ExampleStatusDumper.dump(merged))
50
+ rm_f files
51
+ end
52
+ end
53
+
54
+ desc "Run the specs in parallel with coverage (SPEC=path narrows the run)"
55
+ task(:spec) { ParallelSpecs.run(coverage: true) }
56
+
57
+ # Coverage slows a run considerably, so validation leaves it to `rake spec` and CI.
58
+ desc "Run the specs in parallel without coverage (SPEC=path narrows the run)"
59
+ task("spec:without_coverage") { ParallelSpecs.run(coverage: false) }
7
60
 
8
61
  begin
9
62
  require "yard"
@@ -27,15 +80,25 @@ rescue LoadError
27
80
  # bundler-audit not available
28
81
  end
29
82
 
30
- desc "Run RubyCritic code quality analysis"
83
+ desc "Run only the specs checked against outside tools and files, each missing one skipping with how to set it up"
84
+ RSpec::Core::RakeTask.new(:oracles) { ENV["ORACLES"] = "1" }
85
+
86
+ desc "Run RubyCritic code quality analysis, failing below .rubycritic.yml's minimum score or on any file graded D or F"
31
87
  task :rubycritic do
32
- sh "rubycritic lib"
88
+ require "json"
89
+ sh "rubycritic"
90
+ report = JSON.parse(File.read("tmp/rubycritic/report.json"))
91
+ failing = report["analysed_modules"].select { |file| %w[D F].include?(file["rating"]) }
92
+ next if failing.empty?
93
+
94
+ failing.each { |file| warn format("%s %5.2f %s", file["rating"], file["cost"], file["path"]) }
95
+ abort "RubyCritic graded #{failing.length} file(s) below C"
33
96
  end
34
97
 
35
98
  task default: :spec
36
99
 
37
- desc "Run all validation checks (tests, linting, security audit, code quality)"
38
- task validate: [:spec, :standard, "bundle:audit:check", :rubycritic]
100
+ desc "Run all validation checks (tests without coverage, linting, security audit, code quality)"
101
+ task validate: ["spec:without_coverage", :rubocop, "bundle:audit:check", :rubycritic]
39
102
 
40
103
  desc "Open an irb session preloaded with this library"
41
104
  task :console do
@@ -47,6 +110,73 @@ task :coverage do
47
110
  sh "open coverage/index.html" if File.exist?("coverage/index.html")
48
111
  end
49
112
 
113
+ namespace :music_xml do
114
+ desc "Regenerate the MuseScore 4 and timewise fixtures (spec/fixtures/notation/music_xml/); MSCORE overrides the mscore path"
115
+ task :musescore_fixtures do
116
+ $LOAD_PATH.unshift File.expand_path("lib", __dir__)
117
+ require "head_music"
118
+ require "rspec/core"
119
+ require "shellwords"
120
+ require_relative "spec/support/musescore"
121
+
122
+ with_final_newline = ->(path) { File.write(path, File.read(path).chomp + "\n") }
123
+ MuseScoreFixtures.names.each do |name|
124
+ rendered = MuseScoreFixtures.path(name, "head_music")
125
+ File.write(rendered, MuseScoreFixtures.musicxml(name))
126
+ MuseScore.convert_file(rendered, MuseScoreFixtures.path(name, "musescore4"))
127
+ with_final_newline.call(MuseScoreFixtures.path(name, "musescore4"))
128
+ puts "Wrote #{name}"
129
+ end
130
+
131
+ stylesheet = "/Applications/Dorico 6.app/Contents/Resources/musicxml/parttime.xsl"
132
+ next puts("Skipped the timewise fixtures: #{stylesheet} not found") unless File.exist?(stylesheet)
133
+
134
+ MuseScoreFixtures::TIMEWISE.each do |name|
135
+ timewise = MuseScoreFixtures.path(name, "timewise")
136
+ sh "xsltproc --nonet --novalid #{stylesheet.shellescape} #{MuseScoreFixtures.path(name, "head_music").shellescape} > #{timewise.shellescape}"
137
+ with_final_newline.call(timewise)
138
+ puts "Wrote #{name} timewise"
139
+ end
140
+ end
141
+ end
142
+
143
+ namespace :mei do
144
+ desc "Regenerate the MEI golden documents (spec/fixtures/notation/mei/)"
145
+ task :golden_fixtures do
146
+ $LOAD_PATH.unshift File.expand_path("lib", __dir__), File.expand_path("spec", __dir__)
147
+ require "rspec/core"
148
+ require "spec_helper"
149
+
150
+ MEIFixtures.names.each do |name|
151
+ File.write(MEIFixtures.path(name), MEIFixtures.mei(name))
152
+ puts "Wrote #{name}"
153
+ end
154
+ end
155
+
156
+ desc "Regenerate the MuseScore 4 MEI exports (spec/fixtures/notation/mei/musescore/); MSCORE overrides the mscore path"
157
+ task :musescore_fixtures do
158
+ $LOAD_PATH.unshift File.expand_path("lib", __dir__)
159
+ require "head_music"
160
+ require "rspec/core"
161
+ require_relative "spec/support/musescore"
162
+
163
+ target_directory = File.expand_path("spec/fixtures/notation/mei/musescore", __dir__)
164
+ FileUtils.mkdir_p(target_directory)
165
+ sources = Dir[File.join(MuseScoreFixtures::DIRECTORY, "*.head_music.musicxml")].sort
166
+ failures = sources.filter_map do |source|
167
+ name = File.basename(source, ".head_music.musicxml")
168
+ target = File.join(target_directory, "#{name}.musescore4.mei")
169
+ MuseScore.convert_file(source, target, timeout: 120)
170
+ File.write(target, File.read(target).chomp + "\n")
171
+ puts "Wrote #{name}"
172
+ nil
173
+ rescue MuseScore::ConversionError => error
174
+ "#{name}: #{error.message}"
175
+ end
176
+ abort "MuseScore failed on:\n#{failures.join("\n")}" if failures.any?
177
+ end
178
+ end
179
+
50
180
  namespace :style do
51
181
  desc "Regenerate the pinned English guide strings (spec/fixtures/style/english_strings.yml)"
52
182
  task :snapshot_english do
data/bin/validate ADDED
@@ -0,0 +1,5 @@
1
+ #!/usr/bin/env bash
2
+ # Runs the full validation: specs without coverage, Standard, the audit, and RubyCritic.
3
+ set -euo pipefail
4
+ cd "$(dirname "$0")/.."
5
+ exec bundle exec rake validate "$@"
data/head_music.gemspec CHANGED
@@ -35,9 +35,9 @@ Gem::Specification.new do |spec|
35
35
  spec.add_runtime_dependency "activesupport", ">= 7.0", "< 10"
36
36
  spec.add_runtime_dependency "humanize", ">= 2", "< 4"
37
37
  spec.add_runtime_dependency "i18n", "~> 1.8"
38
+ spec.add_runtime_dependency "rexml", "~> 3.4", ">= 3.4.2"
38
39
 
39
40
  spec.add_development_dependency "rake", "~> 13.0"
40
- spec.add_development_dependency "rexml", "~> 3.4"
41
41
  spec.add_development_dependency "rspec", "~> 3.0"
42
42
  spec.add_development_dependency "rspec-its", "~> 2.0"
43
43
  spec.add_development_dependency "bundler-audit", "~> 0.9"
@@ -20,10 +20,8 @@ class HeadMusic::Analysis::DiatonicInterval::Naming
20
20
  end
21
21
 
22
22
  def quality_name
23
- starting_quality = QUALITY_SEMITONES[simple_number_name.to_sym].keys.first
24
- delta = simple_semitones - (QUALITY_SEMITONES[simple_number_name.to_sym][starting_quality] % 12)
25
- delta -= 12 while delta >= 6
26
- HeadMusic::Rudiment::Quality.from(starting_quality, delta)
23
+ starting_quality, starting_semitones = QUALITY_SEMITONES[simple_number_name.to_sym].first
24
+ HeadMusic::Rudiment::Quality.from(starting_quality, semitones - starting_semitones - 12 * compound_octaves)
27
25
  end
28
26
 
29
27
  def simple_number_name
@@ -34,14 +32,15 @@ class HeadMusic::Analysis::DiatonicInterval::Naming
34
32
  NUMBER_NAMES[number - 1] || (number.to_s + NAME_SUFFIXES[number % 10])
35
33
  end
36
34
 
35
+ # Past the named numbers, an interval is so many octaves and a simple
36
+ # interval. An octave altered upward adds an octave and alters the unison.
37
37
  def name
38
- if named_number?
39
- [quality_name, number_name].join(" ")
40
- elsif simple_name == "perfect octave"
41
- "#{octaves.humanize} octaves"
42
- else
43
- "#{octaves.humanize} octaves and #{quality.article} #{simple_name}"
44
- end
38
+ return [quality_name, number_name].join(" ") if named_number?
39
+
40
+ octaves, remainder = octaves_and_remainder
41
+ return "#{octaves.humanize} octaves" unless remainder
42
+
43
+ "#{octaves.humanize} octaves and #{remainder.start_with?(/[aeiou]/) ? "an" : "a"} #{remainder}"
45
44
  end
46
45
 
47
46
  def shorthand
@@ -50,20 +49,23 @@ class HeadMusic::Analysis::DiatonicInterval::Naming
50
49
 
51
50
  private
52
51
 
53
- def simple_semitones
54
- @simple_semitones ||= semitones % 12
52
+ def named_number?
53
+ number <= NUMBER_NAMES.length
55
54
  end
56
55
 
57
- def named_number?
58
- number < NUMBER_NAMES.length
56
+ # The octaves added to the simple interval.
57
+ def compound_octaves
58
+ (number - simple_number) / 7
59
59
  end
60
60
 
61
61
  def quality
62
62
  @quality ||= HeadMusic::Rudiment::Quality.get(quality_name)
63
63
  end
64
64
 
65
- def octaves
66
- @octaves ||= semitones / 12
65
+ def octaves_and_remainder
66
+ return [compound_octaves, simple_name] unless simple_number == 8 && !quality_name.include?("diminished")
67
+
68
+ [compound_octaves + 1, ("#{quality_name} unison" unless quality_name == "perfect")]
67
69
  end
68
70
 
69
71
  def octave_equivalent?
@@ -0,0 +1,55 @@
1
+ class HeadMusic::Analysis::KeyFinder
2
+ # Duration-weighted pitch-class histograms of a NoteInput's notes: of the
3
+ # whole input, and of the time sounding in each bar.
4
+ class Histograms
5
+ def initialize(input)
6
+ @input = input
7
+ end
8
+
9
+ def whole
10
+ @input.notes.each_with_object(empty) do |note, histogram|
11
+ histogram[note.number % 12] += note.duration
12
+ end
13
+ end
14
+
15
+ # Each bar's histogram summed with +window+ bars on either side.
16
+ def windowed(window)
17
+ bars = by_bar
18
+ prefix = bars.each_with_object([empty]) { |histogram, sums| sums << sums.last.zip(histogram).map(&:sum) }
19
+ bars.each_index.map do |index|
20
+ low = [index - window, 0].max
21
+ high = [index + window, bars.length - 1].min
22
+ prefix[high + 1].zip(prefix[low]).map { |upper, lower| upper - lower }
23
+ end
24
+ end
25
+
26
+ # Each note's duration is credited to the bars it sounds in.
27
+ def by_bar
28
+ starts = @input.bar_starts.values
29
+ index_of_bar = @input.bar_starts.keys.each_with_index.to_h
30
+ histograms = Array.new(starts.length) { empty }
31
+ @input.notes.each do |note|
32
+ credit(histograms, starts, note, index_of_bar.fetch(@input.bar_at(note.onset)))
33
+ end
34
+ histograms
35
+ end
36
+
37
+ private
38
+
39
+ def credit(histograms, starts, note, bar_index)
40
+ from = note.onset
41
+ finish = note.onset + note.duration
42
+ while from < finish
43
+ bar_end = starts[bar_index + 1]
44
+ to = (bar_end.nil? || finish < bar_end) ? finish : bar_end
45
+ histograms[bar_index][note.number % 12] += to - from
46
+ from = to
47
+ bar_index += 1
48
+ end
49
+ end
50
+
51
+ def empty
52
+ Array.new(12, 0r)
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,39 @@
1
+ class HeadMusic::Analysis::KeyFinder
2
+ # The key index for each bar that maximizes the summed scores, less the
3
+ # change penalty for every change of key: Viterbi over bars and keys. A tie
4
+ # keeps the key in force, then goes by preference. A bar that
5
+ # scores nothing changes no key, so a change waits for the music.
6
+ class KeyPath
7
+ def initialize(scores, change_penalty:, preference:)
8
+ @scores = scores
9
+ @change_penalty = change_penalty
10
+ @preference = preference
11
+ end
12
+
13
+ def to_a
14
+ totals = @scores.first.dup
15
+ back_pointers = @scores.drop(1).map do |bar_scores|
16
+ next totals.each_index.to_a if bar_scores.all?(&:zero?)
17
+
18
+ pointers = pointers_from(totals)
19
+ totals = pointers.each_with_index.map { |from, index| totals[from] - ((from == index) ? 0 : @change_penalty) + bar_scores[index] }
20
+ pointers
21
+ end
22
+ path = [best_index(totals)]
23
+ back_pointers.reverse_each { |pointers| path.unshift(pointers[path.first]) }
24
+ path
25
+ end
26
+
27
+ private
28
+
29
+ def pointers_from(totals)
30
+ leader = best_index(totals)
31
+ moved = totals[leader] - @change_penalty
32
+ totals.each_index.map { |index| (totals[index] >= moved) ? index : leader }
33
+ end
34
+
35
+ def best_index(totals)
36
+ totals.each_index.min_by { |index| [-totals[index], *@preference[index]] }
37
+ end
38
+ end
39
+ end