Index About Installation Source Code (Github) PyPi Page SOS

Code Reference

Quickstart with Example Code

pytunegen.constants

pytunegen.constants holds... well... constants. You won't need to communicate with this module unless you want to pull off a really crazy trick. It includes:

  - Note pitches

   All notes from C3 to B5 are present.
   Variables representing full notes are written as c3, d3, f3, ..., a4, b4, ... and so on.
   Variables representing half notes are written as c_4, f_4, ... and so on. The underscore represents "#", so c_4 is actually C#4.

   pytunegen.constants.c5 would give you the pitch of the note C5 as an integer, which is 523. (Due to C5's frequency being 523 Hertz.)


  - Scales (dicts that show which notes are used in which scale)

   In a scale dict, keys are the usual way notes are written, and values are the pitch values of the notes.
   For example: a_major = {"C#3":c_3, "D3":d3, "E3":e3, "F#3":f_3, "G#3":g_3, "A3":a3, "B3":b3, ... }
   Following scales are available: c_major, d_major, e_major, f_major, g_major, a_major, b_major, c_minor, d_minor, e_minor, f_minor, g_minor, a_minor, b_minor

   "full_notes" is a dict (or a special scale) that includes all notes.

   "scales" is a dict that holds all scales + full_notes. You will probably want to use this to get a scale.
   (pytunegen.constants.scales.get("F Minor") gets you the f_minor dict)


  - Preset bpm values (the script randomly chooses among presets if the user does not provide a custom bpm)
   bpm_slowest, bpm_slow, bpm_slowish, bpm_normal, bpm_fastish, bpm_fast, bpm_fastest

   There is also a list of all preset bpms, called "bpms".
   bpms = [bpm_slowest, bpm_slow ... ]


  - Note durations

   There is a dict that holds possible note durations from 1/8 to 4.
   note_durations = {"1/8": 1/8, "1/4":1/4, "1/2": 1/2, "1":1, "2":2, "4":4}


  - Bar repeats

   A list holds possible times a bar can repeat.
   bar_repeats = [2, 4]

pytunegen.tunegen

This is where the actual music generation happens.

  - class Tunegen(seed = None, music_length = 50, scale = None, bpm = None, time_sig = None, note_jump_limit = 2.2, silence_percent = 1, non_repeat_percent = 65)

  Tunegen class is the tune generator object.

  Attributes:
  seed: seed to seed Python's 'random' module with, at the start of the generation. OPTIONAL.
  music_length: Integer representing total length of the generated music in number of bars. OPTIONAL.
  scale: Music scale to pick notes from. OPTIONAL.
  bpm: Music playing speed in beats per minute. OPTIONAL.
  time_sig: String to customize the time signature used for music generation. Must be written in "a/b" format where b is a positive even integer, and a is a postive integer. OPTIONAL.
  note_jump_limit: The factor which prevents "jumping" between too high and too low notes. OPTIONAL.
  silence_percent: Ratio of silences to all notes throughout the whole tune. OPTIONAL.
  non_repeat_percent: Percentage of bars that are not repeated. OPTIONAL.

  Methods:
  generate(): Generates the music with the parameters provided to the Tunegen instance. Returns a list of Bar objects (see below).

  - class Bar(self, note_names, durations, bar_repeat)

  Attributes:
  note_names: A list of names of the notes in the bar. (such as ["C4", "D#4", ...])
  durations: A list of note durations in tha bar. (such as [0.25, 1, 0.25, 0.25, ...])
  bar_repeat: Number of times the bar is to be repeated. Integer value.
  notes: A list of Note objects (see below).

  - class Note(self, pitch, duration, silence)

  Attributes:
  pitch: The pitch (frequency) of the note, in Hertz.
  duration: Duration of the note.
  silence: Bool value that denotes whether the note is a silence or an actual note.

pytunegen.midigen

This is the MIDI creation and exporting tool.

  - class MIDIgen(seed = None, music_length = 50, scale = None, bpm = None, time_sig = None, note_jump_limit = 2.2, silence_percent = 1, non_repeat_percent = 65)

  MIDIgen class is the MIDI generation and exporting object. Uses same attributes as Tunegen (because it feeds these values into Tunegen itself, making it easier to use).

  Attributes:
  seed: seed to seed Python's 'random' module with, at the start of the generation. OPTIONAL.
  music_length: Integer representing total length of the generated music in number of bars. OPTIONAL.
  scale: Music scale to pick notes from. OPTIONAL.
  bpm: Music playing speed in beats per minute. OPTIONAL.
  time_sig: String to customize the time signature used for music generation. Must be written in "a/b" format where b is a positive even integer, and a is a postive integer. OPTIONAL.
  note_jump_limit: The factor which prevents "jumping" between too high and too low notes. OPTIONAL.
  silence_percent: Ratio of silences to all notes throughout the whole tune. OPTIONAL.
  non_repeat_percent: Percentage of bars that are not repeated. OPTIONAL.

  Methods:
  export(export_filename = None): Converts the generated music into MIDI format and exports it as a .mid file.
   Arguments:
   export_filename: File name for the exported .mid file. OPTIONAL. (If not provided, it will be created as "<randomization_seed>.mid".)