Download stringtomidi.py from qwrt/Monster_textmidis_filtered: direct link, hf CLI and curl.
- Browser
- Download file 4.7 kB
-
https://huggingface.co/datasets/qwrt/Monster_textmidis_filtered/resolve/main/stringtomidi.py
- Command line
-
hf download hf://datasets/qwrt/Monster_textmidis_filtered/stringtomidi.py
-
curl -L -o stringtomidi.py https://huggingface.co/datasets/qwrt/Monster_textmidis_filtered/resolve/main/stringtomidi.py
4.7 kB
| # convert_custom_string_to_midi.py | |
| """ | |
| Konverterar strängen som ser ut så här: | |
| 4G5d21 5E9a0 5J9b13 | |
| till en MIDI‑fil. | |
| • Varje rad = ny stämpel (= +1 s) [Ändrat från 2s till 1s] | |
| • Formatet är: [oktav][notnamn][volym][notlängd][tidstämpel] | |
| • Notlängd som bokstav: a=1, b=2, c=4, d=8, e=16, f=32, g=64 (multiplikator för 100ms) | |
| • Tidstämpel: 0‑59 = antal takter (1 takt ≙ 16.667 ms) | |
| • Volym: 0‑10 (skalas upp till 0‑127) | |
| """ | |
| from __future__ import annotations | |
| import re | |
| from pathlib import Path | |
| from typing import List, Tuple | |
| import mido | |
| # ------------------------------------------------------------ | |
| # Konfiguration | |
| # ------------------------------------------------------------ | |
| STAMP_SPAN_MS = 1_000 # 1 sekund per rad (ändrat från 2_000) | |
| TAKTER_PER_STAMP = 60 # 60 takter per rad | |
| TACKT_MS = STAMP_SPAN_MS / TAKTER_PER_STAMP # ≈16.667 ms | |
| BASE_DUR_MS = 33 # 0.1 sekund som bas för notlängd | |
| VELOCITY_SCALE = 12.8 # 8 * 12.8 ≈ 102 | |
| TEMPO_US_PER_BEAT = 500_000 # 120 BPM | |
| TICKS_PER_BEAT = 480 | |
| NOTE_MAP = { # samma konstiga namngivning som i Java‑koden | |
| 'C': 0, 'I': 1, 'D': 2, 'J': 3, | |
| 'E': 4, 'F': 5, 'K': 6, 'G': 7, | |
| 'L': 8, 'A': 9, 'H': 10, 'B': 11, | |
| } | |
| DURATION_MAP = { # a=1, b=2, c=4, d=8, e=16, f=32, g=64 | |
| 'a': 1, 'b': 2, 'c': 4, 'd': 8, 'e': 16, 'f': 32, 'g': 64, 'h': 128, 'i': 256 | |
| } | |
| # Nytt regex för det nya formatet: oktav + notnamn + volym + notlängd + tidstämpel | |
| TOKEN_RE = re.compile(r'^(\d+)([A-Z])(\d+)([a-i])(\d+)$') | |
| def parse_custom(text: str) -> List[Tuple[float, int, int, int]]: | |
| """ | |
| Läser texten och returnerar en lista med | |
| (start_ms, midi_pitch, velocity, duration_ms) | |
| """ | |
| events = [] | |
| for stamp_idx, line in enumerate(text.replace("*","").strip().splitlines()): | |
| base_time_ms = stamp_idx * STAMP_SPAN_MS | |
| for token in line.strip().split(): | |
| m = TOKEN_RE.match(token) | |
| if not m: | |
| raise ValueError(f'Felaktigt token: {token}') | |
| # Tolka nytt format: oktav + notnamn + volym + notlängd + tidstämpel | |
| octave_str, letter, vel_str, dur_letter, time_str = m.groups() | |
| octave = int(octave_str) | |
| time_since = int(time_str) | |
| velocity_in = int(vel_str) | |
| # Konvertera notlängdsbokstav till millisekunder | |
| duration_ms = DURATION_MAP[dur_letter] * BASE_DUR_MS | |
| pitch = NOTE_MAP[letter] + (octave + 1) * 12 # MIDI har C‑1 = 0 | |
| velocity = min(127, int(round(velocity_in * VELOCITY_SCALE))) | |
| start_ms = base_time_ms + time_since * TACKT_MS | |
| events.append((start_ms, pitch, velocity, duration_ms)) | |
| return events | |
| def build_midi(events: List[Tuple[float, int, int, int]], | |
| out_path: str | Path = "output.mid") -> None: | |
| """ | |
| Bygger en simpel 1‑spårig SMF‑fil av eventlistan. | |
| """ | |
| # Skapa Note On + Note Off‑par | |
| timed = [] | |
| end_ms=None | |
| for start_ms, pitch, vel, duration_ms in events: | |
| end_ms = start_ms + duration_ms # Använd notlängden från formatet | |
| timed.append((start_ms, pitch, vel, True)) # True = note_on | |
| timed.append((end_ms, pitch, 0, False)) # False = note_off | |
| print(f"noter: {len(events)}, längd: {round(end_ms/1000,2)}s, tempo: {round(1000*len(events)/end_ms,1)} noter/s") | |
| # sortera kronologiskt | |
| timed.sort(key=lambda t: t[0]) | |
| mid = mido.MidiFile(ticks_per_beat=TICKS_PER_BEAT) | |
| track = mido.MidiTrack() | |
| mid.tracks.append(track) | |
| track.append(mido.MetaMessage('set_tempo', tempo=TEMPO_US_PER_BEAT, time=0)) | |
| last_tick = 0 | |
| for abs_ms, pitch, velocity, is_on in timed: | |
| sec = abs_ms / 1000.0 | |
| tick = mido.second2tick(sec, TICKS_PER_BEAT, TEMPO_US_PER_BEAT) | |
| delta = int(round(tick - last_tick)) | |
| msg_type = 'note_on' if is_on else 'note_off' | |
| track.append(mido.Message(msg_type, note=pitch, velocity=velocity, time=delta)) | |
| last_tick = tick | |
| mid.save(out_path) | |
| print(f"Sparade {out_path}") | |
| # ------------------------------------------------------------------ | |
| # CLI‑exempel / snabbtest | |
| # ------------------------------------------------------------------ | |
| if __name__ == "__main__": | |
| example_text = """ | |
| 5C5e17 5C7f30 5C7f37 5C7f38 5C7f38 3C7f38 | |
| 4C7g4 3A7g4 4C7g4 4C5g4 3G7g4 5C7e14 4C7e43 | |
| 4G7e52 | |
| 4G7f14 4F7f14 3G5f14 4G6f46 4C7f46 | |
| 3H5f12 4G6f35 5G7e42 4G7f43 4C7f43 3G7f45 | |
| 5C7d29 5C7d58 | |
| 4G7d14 4A7d39 | |
| """.replace("*", "") | |
| events = parse_custom(example_text) | |
| build_midi(events, "tmp/example.mid") |