Metadata-Version: 2.4
Name: harmonization-env
Version: 1.0
Summary: Harmonization environment implementation via reinforcement learning
Home-page: https://github.com/pietrobegotti/harmonization
Author: Begotti Pietro, Bianchi Luigi Amedeo, Cordoni Francesco Giuseppe
Author-email: pietro.begotti@gmail.it
Classifier: Programming Language :: Python :: 3
Classifier: License :: OSI Approved :: MIT License
Classifier: Operating System :: OS Independent
Requires-Python: >=3.6
Description-Content-Type: text/markdown
Requires-Dist: torch>=2.5.1
Requires-Dist: numpy<2.1,>=1.22
Requires-Dist: midiutil>=1.2.1
Requires-Dist: midi2audio>=0.1.1
Requires-Dist: synthviz>=0.0.2
Requires-Dist: tqdm
Dynamic: author
Dynamic: author-email
Dynamic: classifier
Dynamic: description
Dynamic: description-content-type
Dynamic: home-page
Dynamic: requires-dist
Dynamic: requires-python
Dynamic: summary

# Music Harmonization Package

A Reinforcement Learning package to generate musical harmonies, starting from a general sequence of notes in the scale of C major. Outputs musical harmonies, MIDI files, and mp4 videos.

## Prerequisites

- **ffmpeg** must be installed on your system.
  - [Download ffmpeg](https://ffmpeg.org/download.html)
  

## Examples

Here are a few example outputs generated by the package. The top line is the melody, given as input. The other notes played are generated. In this case a simple C major scale. The following videos are two different outputs generated by the agent.

<video src = "https://github.com/pietrobegotti/harmonization/main/examples/Sequence_#1.mp4"></video>
<video src = "https://github.com/pietrobegotti/harmonization/main/examples/Sequence_#2.mp4"></video>


https://github.com/user-attachments/assets/e7a52a7e-48b0-4076-8552-e5e392ad850d

https://github.com/user-attachments/assets/9b4d98df-2e92-45ec-9ef5-695c0d947d09



## Installation

Install this package via `pyPI`: 

```bash
pip install harmonization-env
```

https://pypi.org/project/harmonization-env/

## Basic Usage

Here's a complete example showing the main features:

```python

import torch
from harmonization_env import *

path_to_params = "..."
env = HarmonizationEnv(device = 'cpu')

net = NetM.load_from_checkpoint(path_to_params, device = 'cpu')

# Create a melody sequence
# Notes are represented as MIDI numbers (60 = middle C)
melody = torch.tensor([64, 64, 65, 65, 
                       67, 69, 69, 62,
                       67, 67, 68, 71,
                       69, 71, 72, 65,
                       64, 64, 62, 62,
                       60, 60, 60, 60,
                       60], dtype = torch.int32)

melody += 12


# get output
chords, reward = agent.get(
    melody = melody,
)

run = chords[0, :]


v = Voicer(melody, run)
voices = v.get()

player = MIDIGenerator(tempo = 80) 

# get and play midi output
player.generate(voices, filenames = 'test0.mid')
player.play(filename = 'test0.mid')

# Create visualization video
# Download soundfont from https://member.keymusician.com/Member/FluidR3_GM/index.html
vg = VideoGenerator('test0.mid', soundfont_path='FluidR3_GM.sf2')
vg.get_video(output_filename='output.mp4')

```
