Melopy (Melo-pee)

A Python library for playing with sound.



View the Project on GitHub jdan/Melopy

Getting started
melopy.py
scales.py
utility.py
Scoresexamples

Getting Started

Install

$ pip install melopy

Fire it up

$ python
Python 2.7.2 (default, Jun 20 2012, 16:23:33)
[GCC 4.2.1 Compatible Apple Clang 4.0 (tags/Apple/clang-418.0.60)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import melopy
>>> melopy.major_scale('C5')
['C5', 'D5', 'E5', 'F5', 'G5', 'A5', 'B5']
>>>

Development

Grab a copy:

$ git clone git://github.com/jdan/Melopy
$ cd Melopy
$ python setup.py install

To run tests:

$ cd Melopy
$ python tests/melopy_tests.py

For examples, check out the examples directory:

$ python examples/canon.py
$ python examples/parser.py entertainer < examples/scores/entertainer.mlp

Organization

Melopy is broken down into 3 subcategories - melopy, scales, and utility.

melopy.py

>>> from melopy import Melopy
>>> m = Melopy('mysong')
>>> m.add_quarter_note('A4')
>>> m.add_quarter_note('C#5')
>>> m.add_quarter_note('E5')
>>> m.render()
[==================================================] 100%
Done

Produces the follow result



scales.py

>>> from melopy.scales import *
>>> major_scale('C4')
['C4', 'D4', 'E4', 'F4', 'G4', 'A4', 'B4']
>>> major_scale('C4','dict')
{0: 'C4', 1: 'D4', 2: 'E4', 3: 'F4', 4: 'G4', 5: 'A4', 6: 'B4'}
>>> major_scale('C4','tuple')
('C4', 'D4', 'E4', 'F4', 'G4', 'A4', 'B4')
>>> minor_scale('D#5')  # has some bugs
['D#5', 'F5', 'F#5', 'G#5', 'A#5', 'B5', 'C#6']
>>> major_triad('A4')
['A4', 'C#5', 'E5']
>>> major_triad('A4', 'tuple')
('A4', 'C#5', 'E5')

utility.py

>>> from melopy.utility import *
>>> key_to_frequency(49)
440.0
>>> note_to_frequency('A4')
440.0
>>> note_to_frequency('C5')
523.2511306011972
>>> note_to_key('Bb5')
62
>>> key_to_note(65)
'C#6'
>>> key_to_note(304) # even something stupid
'C26'
>>> frequency_to_key(660)
56
>>> frequency_to_note(660)
'E5'

Scores

Scores are files with a .mlp extension that Melopy can easily process into music. They have a basic, comprehensible syntax that makes creating noise a painless process.

m.parse('5EDCDEE[E]')

Is translated as...

m.octave = 5
m.add_quarter_note('E')
m.add_quarter_note('D')
m.add_quarter_note('C')
m.add_quarter_note('D')
m.add_quarter_note('E')
m.add_quarter_note('E')
m.add_half_note('E')


In this syntax...

Quick reference

Symbol Description
A, B, C, D, E, F, or GPlace a note
Any number (i.e. 4, 5)Change the octave
+, -, ^, vShift the octave
whitespaceIgnored
/Begin or end a comment
&&&Begin a new track
(notes)Place one or more notes at half the speed
[notes]Place one or more notes at double the speed
_Rest (can be wrapped in () or [])

Examples

Up and Down
2 C+ C+ C+ C+ [[C]]
C - C - C - [[C]]


Fur Elise
5
(ED#ED#E
  vB
  ^DC)
  vA(_)


The Entertainer
4 (DD#)
    (E)^Cv
    (E)^Cv
    (E)^[C]_

5 (CDD#E CD)Ev(B)^D[C]_

&&&

_
3 C^C
2 G^Bb
2 F^A
2 E^G

2 G4C
2 G^B
2 C4CC