VocalEasel/mma/MMA/notelen.py

100 lines
2.8 KiB
Python
Raw Permalink Normal View History

2006-11-10 08:07:56 +00:00
# notelen.py
"""
2007-04-29 06:47:40 +00:00
This module is an integeral part of the program
2006-11-10 08:07:56 +00:00
MMA - Musical Midi Accompaniment.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
2007-04-29 06:47:40 +00:00
Bob van der Poel <bob@mellowood.ca>
2006-11-10 08:07:56 +00:00
"""
2009-05-17 22:34:44 +00:00
2006-11-10 08:07:56 +00:00
import gbl
from MMA.common import *
noteLenTable = {
2011-07-26 22:49:39 +00:00
'0' : 1, # special 0==1 midi tick
'1' : gbl.BperQ * 4, # whole note
'2' : gbl.BperQ * 2, # 1/2
2007-04-29 06:47:40 +00:00
'23' : gbl.BperQ * 4 / 3, # 1/2 triplet
2011-07-26 22:49:39 +00:00
'4' : gbl.BperQ, # 1/4
2007-04-29 06:47:40 +00:00
'43' : gbl.BperQ * 2 / 3, # 1/4 triplet
2011-07-26 22:49:39 +00:00
'8' : gbl.BperQ / 2, # 1/8
'81' : None, # short 1/8, set by swing skew
'82' : None, # long 1/8, set by swing skew
2007-04-29 06:47:40 +00:00
'16' : gbl.BperQ / 4, # 1/16
'32' : gbl.BperQ / 8, # 1/32
'64' : gbl.BperQ / 16, # 1/64
2011-07-26 22:49:39 +00:00
'6' : gbl.BperQ / 6, # 1/16 note triplet
'3' : gbl.BperQ / 3, # 1/8 note triplet
'5' : gbl.BperQ / 5 } # 1/8 note quintuplet
2006-11-10 08:07:56 +00:00
2007-04-29 06:47:40 +00:00
def getNoteLen(n):
""" Convert a Note to a midi tick length.
Notes are 1==Whole, 4==Quarter, etc.
Notes can be dotted or double dotted.
Notes can be combined: 1+4 == 5 beats, 4. or 4+8 == dotted 1/4
2006-11-10 08:07:56 +00:00
1-4 == 3 beats, 1-0 == 4 beats less a midi tick
2007-04-29 06:47:40 +00:00
"""
length = 0
2011-07-26 22:49:39 +00:00
if n.endswith('T') or n.endswith('t'):
n=n[:-1]
if not n:
error("Specifying a note length in MIDI Ticks requires a value")
n=stoi(n)
if n == 0:
n = 1
if n<1:
error("MIDI note length cannot be negative.")
return n
2007-04-29 06:47:40 +00:00
n=n.replace('-', '+-') # change "2-4" to "2+-4" for easier parsing
n=n.replace('++-', '+-') # and in case we already used "+-", take out 2nd "+"
for a in str(n).split('+'):
if a.endswith('..'):
dot = 2
a=a[:-2]
elif a.endswith('.'):
dot = 1
a=a[:-1]
else:
dot = 0
try:
if a.startswith('-'):
i = noteLenTable[a[1:]] * -1
else:
i = noteLenTable[a]
except:
error("Unknown note duration %s" % n )
if dot == 2:
i += i/2 + i/4
elif dot == 1:
i += i/2
length += i
return length