# svsdval/video2midi architecture

> Architecture diagram of the GitHub repository svsdval/video2midi, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-05-14). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/svsdval/video2midi
- Repository: https://github.com/svsdval/video2midi

## Overview

##### Project Type and Purpose
- Type: A standalone, full-stack command-line tool (monolithic application) with an interactive UI layer for parameter tuning, and a video-to-MIDI conversion pipeline.
- Purpose: Read Synthesia (or similar) piano-roll videos, detect key presses frame by frame, and emit a corresponding multi-channel MIDI file.

##### File Structure
Top-level components:
- v2m.py – Entry point / command-line interface
- video2midi/ – Core processing package
– gl.py – OpenGL/pygame-based visualization & input handling
– midi.py – MIDI event creation (wraps midiutil)
– settings.py & prefs.py – Configuration & user preferences loader
- docs/ – Install & manual guides, screenshots
- tests/ – Unit tests for settings, sample INI files
- midis/ – Sample input/output data (not part of runtime)
- CI config (GitHub workflows), requirements.txt / pyproject.toml

##### README Insights
- Lists required libraries: python-opencv, python-pygame, python-opengl, python-midiutil.
- Describes the interactive controls (key mapping, frame ranges, channel splitting).
- Mentions optional multi-channel & instrument mapping via INI.

##### Main Components
A. CLI / Orchestration Layer
– v2m.py: argument parsing, loading settings, initializing UI and processing loop.
B. Configuration Store
– settings.py / prefs.py: load/override logic (home directory vs. working dir).
C. Video Input & Frame Grabber
– OpenCV module: open video file, read frames, handle start/end frame.
D. UI / Visualization & Control
E. Processing Pipeline
– Note Tracker: track active notes per channel, handle overlap/ignore/min-duration logic.
F. MIDI Generator
– midi.py (midiutil wrapper): translate tracked note events into MIDI tracks, apply program changes.
– Output Writer: write .mid file to disk when processing completes or on abort.
G. External Dependencies
– Libraries: OpenCV (cv2), pygame, PyOpenGL, midiutil.
– Operating system: reads/writes local files.

##### Relationships & Data Flow
1. User invokes v2m.py with a video path.
2. v2m.py loads settings (INI) → instantiates UI and processing components.
3. Video Input (OpenCV) supplies frames to Frame Processor.
4. Frame Processor sends results to Note Tracker.
5. Tracker emits note-on/note-off events to MIDI Generator.
6. UI Layer renders the live keyboard + assists in realtime parameter tuning.
7. MIDI Generator accumulates events, then writes the .mid file to disk.

##### Architectural Patterns & Design Principles
- Modular layering: separate I/O, processing, UI, and output responsibilities.
- Configuration-driven behavior: all thresholds, mappings, and instrument settings live in INI.
- Event-driven UI: asynchronous user commands modify runtime behavior (start/end frame, channel split).
- Single process, no services/microservices.

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% Layers
    subgraph "CLI & Configuration" 
        direction TB
        CLI["v2m.py"]:::cli
        INI["v2m.ini / ~/.v2m.ini"]:::cli
        Settings["video2midi/settings.py"]:::cli
        Prefs["video2midi/prefs.py"]:::cli
    end

    subgraph "Processing Pipeline"
        direction TB
        VideoReader["OpenCV Video Reader"]:::processing
        FrameProcessor["Frame Processor"]:::processing
        NoteTracker["Note Tracker"]:::processing
    end

    subgraph "UI & Visualization"
        direction TB
        UI["video2midi/gl.py"]:::ui
    end

    subgraph "MIDI Generation & Output"
        direction TB
        MIDIGen["video2midi/midi.py"]:::output
        Writer["Output Writer (.mid)"]:::output
    end

    subgraph "External & Resources"
        direction TB
        Libs["External Libraries"]:::external
        Docs["docs/"]:::resource
        Tests["tests/test_settings.py"]:::resource
        TestINI["tests/spin.mp4.ini"]:::resource
        Samples["midis/"]:::resource
    end

    %% Connections
    CLI -->|"loads INI"| INI
    CLI -->|"parses args & settings"| Settings
    CLI --> UI
    CLI --> VideoReader
    INI --> Settings
    INI --> Prefs
    Settings --> VideoReader
    Prefs --> UI
    VideoReader -->|"frames"| FrameProcessor
    FrameProcessor -->|"key events"| NoteTracker
    NoteTracker -->|"note on/off"| MIDIGen
    UI -->|"renders keyboard & events"| FrameProcessor
    MIDIGen --> Writer
    CLI --> Docs
    CLI --> Tests
    CLI --> Samples

    %% External dependencies
    Libs -.-> VideoReader
    Libs -.-> UI
    Libs -.-> MIDIGen

    %% Click events
    click CLI "https://github.com/svsdval/video2midi/blob/master/v2m.py"
    click INI "https://github.com/svsdval/video2midi/blob/master/v2m.ini"
    click Settings "https://github.com/svsdval/video2midi/blob/master/video2midi/settings.py"
    click Prefs "https://github.com/svsdval/video2midi/blob/master/video2midi/prefs.py"
    click UI "https://github.com/svsdval/video2midi/blob/master/video2midi/gl.py"
    click MIDIGen "https://github.com/svsdval/video2midi/blob/master/video2midi/midi.py"
    click Docs "https://github.com/svsdval/video2midi/tree/master/docs/"
    click Tests "https://github.com/svsdval/video2midi/blob/master/tests/test_settings.py"
    click TestINI "https://github.com/svsdval/video2midi/blob/master/tests/spin.mp4.ini"
    click Samples "https://github.com/svsdval/video2midi/tree/master/midis/"

    %% Styles
    classDef cli fill:#a8d5a2,stroke:#227a12,stroke-width:2px
    classDef processing fill:#abd1f9,stroke:#195caa,stroke-width:2px
    classDef ui fill:#f9c290,stroke:#c56c1e,stroke-width:2px
    classDef output fill:#d8b4f2,stroke:#733e9f,stroke-width:2px
    classDef external fill:#dddddd,stroke:#888888,stroke-width:1px,stroke-dasharray: 5 5
    classDef resource fill:#f2e2b3,stroke:#a67c00,stroke-width:2px
```
