Skip to main content

Generates randomized music tunes with Python, with MIDI export support.

Project description

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.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pytunegen-1.1.1.tar.gz (8.4 kB view details)

Uploaded Source

Built Distribution

pytunegen-1.1.1-py3-none-any.whl (7.6 kB view details)

Uploaded Python 3

File details

Details for the file pytunegen-1.1.1.tar.gz.

File metadata

  • Download URL: pytunegen-1.1.1.tar.gz
  • Upload date:
  • Size: 8.4 kB
  • Tags: Source
  • Uploaded using 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

File hashes

Hashes for pytunegen-1.1.1.tar.gz
Algorithm Hash digest
SHA256 fcab530d81629b8b50933960fc30062e9a587072a57b7bd13e4f24e19b8954b8
MD5 d9411392935b20920572508c3d306c3e
BLAKE2b-256 a1ec92213e01a17a6bcbe2d3fd552c20f26914ccaa563ffbd18bb60c32a84db3

See more details on using hashes here.

File details

Details for the file pytunegen-1.1.1-py3-none-any.whl.

File metadata

  • Download URL: pytunegen-1.1.1-py3-none-any.whl
  • Upload date:
  • Size: 7.6 kB
  • Tags: Python 3
  • Uploaded using 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

File hashes

Hashes for pytunegen-1.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 22626d96b1f52dea1dac27ab513ca48d0b457b18bd50f9e817a4578f78cef319
MD5 44fbf10989c3d5543d9deeaa0e036e0d
BLAKE2b-256 e3915fe2d64b02f9e1f034cc74aef29675ad4de4d272178354270ef42bf2b9fc

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page