Skip to main content

pyTuneGen

Generates music tunes using Python. Sometimes it isn't terrible at it.

You can customize the generation by choosing your favorite scale, a suitable bpm, an interesting time signature, or adjust some of the unorthodox variables including the randomization seed; or you can simply sit back and let the lords of random number generation do their own thing!

pyTuneGen can also export these grand works of art into MIDI files for convenient handling.

The basic introduction below is probably all you need, but full documentation is also available.

Installation

pip install pytunegen

Usage

If you want to start creating random MIDIs right away, it is this simple!

from pytunegen.midigen import MIDIgen
MIDIgen().export()

Although it is simple enough and does the job, the example above is extremely bare-bones and you might want to customize the kind of music generated. To acheive this, you can adjust some or all of the many parameters (this example shows all parameters):

from pytunegen.midigen import MIDIgen

midi_exporter = MIDIgen(seed=18811938, music_length=100, scale="F Minor", bpm=20, time_sig="3/8",
                        note_jump_limit=1.8, silence_percent=2, non_repeat_percent=75)
midi_exporter.export("my_MIDI_file.mid")

You don't have to deal with MIDI files if you want to use the generated music in Python scripts. Instead, you can use TuneGen! TuneGen creates a list of bars, which themselves are lists of notes, with properties pitch, length and silence (bool value to show whether that note actually denotes a silence).

Here is a simple player that prints out the characteristics of the generated music, and uses the 'synthesizer' package to play it.

from synthesizer import Player, Synthesizer, Waveform
from pytunegen.tunegen import TuneGen
import time

# initialize the player
player = Player()
player.open_stream()
synth1 = Synthesizer(osc1_waveform = Waveform.sine, osc1_volume=1.0,
    use_osc2=True, osc2_waveform=Waveform.square, osc2_volume=0.3)

# initialize the tune generator
tunegen = TuneGen()

# print the generator details
print(f"Seed: {tunegen.randseed}")
print(f"BPM: {str(tunegen.bpm_current)}")
print("Time Signature:", tunegen.time_sig_display)
print(f"Scale: {tunegen.scale_current_name}")
print(f"Length: {tunegen.music_length} bars")

music = tunegen.generate()

for bar in music:
    print(str(bar.note_names))
    print(str(bar.durations))
    print(f"Bar Repeat: {bar.bar_repeat}\n")
    for note in bar.notes:
        if not note.silence:
            player.play_wave(synth1.generate_constant_wave(note.pitch, note.duration * 60/tunegen.bpm_current))
        else:
            time.sleep(note.duration * 60/tunegen.bpm_current)

SOS

If you need to generate the SOS morse code tune for any reason, you may simply enter "sos" or any of the following as the seed; "SOS", "s.o.s", "S.O.S", "s.o.s.", "S.O.S."

# for use with a custom music player,
# such as the one in the example above
from pytunegen.tunegen import TuneGen
tunegen = TuneGen("sos")
# to generate a MIDI file
from pytunegen.midigen import MIDIgen
MIDIgen("sos").export()

This functionality is hard-coded into Tunegen (tunegen.py) and will disregard any other inputs.

Metadata

Release files for pytunegen 1.1.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pytunegen 1.1.1
File Size Uploaded
pytunegen-1.1.1.tar.gz 8.4 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for pytunegen 1.1.1
File Interpreter ABI Platform
pytunegen-1.1.1-py3-none-any.whl Python 3 none any Details

Total release size: 15.9 kB

Release files / pytunegen-1.1.1.tar.gz

Download URL pytunegen-1.1.1.tar.gz
Size 8.4 kB
Tags Source
SHA-256 checksum
How to use checksums
fcab530d81629b8b50933960fc30062e9a587072a57b7bd13e4f24e19b8954b8
BLAKE2b-256 checksum
How to use checksums
a1ec92213e01a17a6bcbe2d3fd552c20f26914ccaa563ffbd18bb60c32a84db3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.4.2 importlib_metadata/4.6.4 pkginfo/1.7.1 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.62.2 CPython/3.9.2

Release files / pytunegen-1.1.1-py3-none-any.whl

Download URL pytunegen-1.1.1-py3-none-any.whl
Size 7.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
22626d96b1f52dea1dac27ab513ca48d0b457b18bd50f9e817a4578f78cef319
BLAKE2b-256 checksum
How to use checksums
e3915fe2d64b02f9e1f034cc74aef29675ad4de4d272178354270ef42bf2b9fc
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.4.2 importlib_metadata/4.6.4 pkginfo/1.7.1 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.62.2 CPython/3.9.2

Release history Release notifications | RSS feed

This release

1.1.1 This release

2 release files

1.1.0

2 release files

1.0.1

2 release files

1.0.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page