+++ /dev/null
-"""This module allows python programs to use GNU gettext message catalogs.
-
-Author: James Henstridge <james@daa.com.au>
-(This is loosely based on gettext.pl in the GNU gettext distribution)
-
-The best way to use it is like so:
- import gettext
- gettext.bindtextdomain(PACKAGE, LOCALEDIR)
- gettext.textdomain(PACKAGE)
- _ = gettext.gettext
- print _('Hello World')
-
-where PACKAGE is the domain for this package, and LOCALEDIR is usually
-'$prefix/share/locale' where $prefix is the install prefix.
-
-If you have more than one catalog to use, you can directly create catalog
-objects. These objects are created as so:
- import gettext
- cat = gettext.Catalog(PACKAGE, localedir=LOCALEDIR)
- _ = cat.gettext
- print _('Hello World')
-
-The catalog object can also be accessed as a dictionary (ie cat['hello']).
-
-There are also some experimental features. You can add to the catalog, just
-as you would with a normal dictionary. When you are finished, you can call
-its save method, which will create a new .mo file containing all the
-translations:
- import gettext
- cat = Catalog()
- cat['Hello'] = 'konichiwa'
- cat.save('./tmp.mo')
-
-Once you have written an internationalized program, you can create a .po file
-for it with "xgettext --keyword=_ fillename ...". Then do the translation and
-compile it into a .mo file, ready for use with this module. Note that you
-will have to use C style strings (ie. use double quotes) for proper string
-extraction.
-"""
-import os, string
-
-prefix = '/usr/local'
-localedir = prefix + '/share/locale'
-
-def _expandLang(str):
- langs = [str]
- # remove charset ...
- if '.' in str:
- langs.append(string.split(str, '.')[0])
- # also add 2 character language code ...
- if len(str) > 2:
- langs.append(str[:2])
- return langs
-
-lang = []
-for env in 'LANGUAGE', 'LC_ALL', 'LC_MESSAGES', 'LANG':
- if os.environ.has_key(env):
- lang = string.split(os.environ[env], ':')
- lang = map(_expandLang, lang)
- lang = reduce(lambda a, b: a + b, lang)
- break
-if 'C' not in lang:
- lang.append('C')
-
-# remove duplicates
-i = 0
-while i < len(lang):
- j = i + 1
- while j < len(lang):
- if lang[i] == lang[j]:
- del lang[j]
- else:
- j = j + 1
- i = i + 1
-del i, j
-
-if os.environ.has_key('PY_XGETTEXT'):
- xgettext = os.environ['PY_XGETTEXT']
-else:
- xgettext = None
-
-del os, string
-
-error = 'gettext.error'
-
-def _lsbStrToInt(str):
- return ord(str[0]) + \
- (ord(str[1]) << 8) + \
- (ord(str[2]) << 16) + \
- (ord(str[3]) << 24)
-def _msbStrToInt(str):
- return (ord(str[0]) << 24) + \
- (ord(str[1]) << 16) + \
- (ord(str[2]) << 8) + \
- ord(str[3])
-def _intToLsbStr(int):
- return chr(int & 0xff) + \
- chr((int >> 8) & 0xff) + \
- chr((int >> 16) & 0xff) + \
- chr((int >> 24) & 0xff)
-
-def _getpos(levels = 0):
- """Returns the position in the code where the function was called.
- The function uses some knowledge about python stack frames."""
- import sys
- # get access to the stack frame by generating an exception.
- try:
- raise RuntimeError
- except RuntimeError:
- frame = sys.exc_traceback.tb_frame
- frame = frame.f_back # caller's frame
- while levels > 0:
- frame = frame.f_back
- levels = levels - 1
- return (frame.f_globals['__name__'],
- frame.f_code.co_name,
- frame.f_lineno)
-
-class Catalog:
- def __init__(self, domain=None, localedir=localedir):
- self.domain = domain
- self.localedir = localedir
- self.cat = {}
- if not domain: return
- for self.lang in lang:
- if self.lang == 'C':
- return
- catalog = "%s//%s/LC_MESSAGES/%s.mo" % (
- localedir, self.lang, domain)
- try:
- f = open(catalog, "rb")
- buffer = f.read()
- del f
- break
- except IOError:
- pass
- else:
- return # assume C locale
-
- strToInt = _lsbStrToInt
- if strToInt(buffer[:4]) != 0x950412de:
- # catalog is encoded with MSB offsets.
- strToInt = _msbStrToInt
- if strToInt(buffer[:4]) != 0x950412de:
- # magic number doesn't match
- raise error, 'Bad magic number in %s' % (catalog,)
-
- self.revision = strToInt(buffer[4:8])
- nstrings = strToInt(buffer[8:12])
- origTabOffset = strToInt(buffer[12:16])
- transTabOffset = strToInt(buffer[16:20])
- for i in range(nstrings):
- origLength = strToInt(buffer[origTabOffset:
- origTabOffset+4])
- origOffset = strToInt(buffer[origTabOffset+4:
- origTabOffset+8])
- origTabOffset = origTabOffset + 8
- origStr = buffer[origOffset:origOffset+origLength]
-
- transLength = strToInt(buffer[transTabOffset:
- transTabOffset+4])
- transOffset = strToInt(buffer[transTabOffset+4:
- transTabOffset+8])
- transTabOffset = transTabOffset + 8
- transStr = buffer[transOffset:transOffset+transLength]
-
- self.cat[origStr] = transStr
-
- def gettext(self, string):
- """Get the translation of a given string"""
- if self.cat.has_key(string):
- return self.cat[string]
- else:
- return string
- # allow catalog access as cat(str) and cat[str] and cat.gettext(str)
- __getitem__ = gettext
- __call__ = gettext
-
- # this is experimental code for producing mo files from Catalog objects
- def __setitem__(self, string, trans):
- """Set the translation of a given string"""
- self.cat[string] = trans
- def save(self, file):
- """Create a .mo file from a Catalog object"""
- try:
- f = open(file, "wb")
- except IOError:
- raise error, "can't open " + file + " for writing"
- f.write(_intToLsbStr(0x950412de)) # magic number
- f.write(_intToLsbStr(0)) # revision
- f.write(_intToLsbStr(len(self.cat))) # nstrings
-
- oIndex = []; oData = ''
- tIndex = []; tData = ''
- for orig, trans in self.cat.items():
- oIndex.append((len(orig), len(oData)))
- oData = oData + orig + '\0'
- tIndex.append((len(trans), len(tData)))
- tData = tData + trans + '\0'
- oIndexOfs = 20
- tIndexOfs = oIndexOfs + 8 * len(oIndex)
- oDataOfs = tIndexOfs + 8 * len(tIndex)
- tDataOfs = oDataOfs + len(oData)
- f.write(_intToLsbStr(oIndexOfs))
- f.write(_intToLsbStr(tIndexOfs))
- for length, offset in oIndex:
- f.write(_intToLsbStr(length))
- f.write(_intToLsbStr(offset + oDataOfs))
- for length, offset in tIndex:
- f.write(_intToLsbStr(length))
- f.write(_intToLsbStr(offset + tDataOfs))
- f.write(oData)
- f.write(tData)
-
-_cat = None
-_cats = {}
-
-if xgettext:
- class Catalog:
- def __init__(self, domain, localedir):
- self.domain = domain
- self.localedir = localedir
- self._strings = {}
- def gettext(self, string):
- # there is always one level of redirection for calls
- # to this function
- pos = _getpos(2) # get this function's caller
- if self._strings.has_key(string):
- if pos not in self._strings[string]:
- self._strings[string].append(pos)
- else:
- self._strings[string] = [pos]
- return string
- __getitem__ = gettext
- __call__ = gettext
- def __setitem__(self, item, data):
- pass
- def save(self, file):
- pass
- def output(self, fp):
- import string
- fp.write('# POT file for domain %s\n' % (self.domain,))
- for str in self._strings.keys():
- pos = map(lambda x: "%s(%s):%d" % x,
- self._strings[str])
- pos.sort()
- length = 80
- for p in pos:
- if length + len(p) > 74:
- fp.write('\n#:')
- length = 2
- fp.write(' ')
- fp.write(p)
- length = length + 1 + len(p)
- fp.write('\n')
- if '\n' in str:
- fp.write('msgid ""\n')
- lines = string.split(str, '\n')
- lines = map(lambda x:
- '"%s\\n"\n' % (x,),
- lines[:-1]) + \
- ['"%s"\n' % (lines[-1],)]
- fp.writelines(lines)
- else:
- fp.write('msgid "%s"\n' % (str,))
- fp.write('msgstr ""\n')
-
- import sys
- if hasattr(sys, 'exitfunc'):
- _exitchain = sys.exitfunc
- else:
- _exitchain = None
- def exitfunc(dir=xgettext, _exitchain=_exitchain):
- # actually output all the .pot files.
- import os
- for file in _cats.keys():
- fp = open(os.path.join(dir, file + '.pot'), 'w')
- cat = _cats[file]
- cat.output(fp)
- fp.close()
- if _exitchain: _exitchain()
- sys.exitfunc = exitfunc
- del sys, exitfunc, _exitchain, xgettext
-
-def bindtextdomain(domain, localedir=localedir):
- global _cat
- if not _cats.has_key(domain):
- _cats[domain] = Catalog(domain, localedir)
- if not _cat: _cat = _cats[domain]
-
-def textdomain(domain):
- global _cat
- if not _cats.has_key(domain):
- _cats[domain] = Catalog(domain)
- _cat = _cats[domain]
-
-def gettext(string):
- if _cat == None: raise error, "No catalog loaded"
- return _cat.gettext(string)
-
-_ = gettext
-
-def dgettext(domain, string):
- if domain is None:
- return gettext(string)
- if not _cats.has_key(domain):
- raise error, "Domain '" + domain + "' not loaded"
- return _cats[domain].gettext(string)
-
-def test():
- import sys
- global localedir
- if len(sys.argv) not in (2, 3):
- print "Usage: %s DOMAIN [LOCALEDIR]" % (sys.argv[0],)
- sys.exit(1)
- domain = sys.argv[1]
- if len(sys.argv) == 3:
- bindtextdomain(domain, sys.argv[2])
- textdomain(domain)
- info = gettext('') # this is where special info is often stored
- if info:
- print "Info for domain %s, lang %s." % (domain, _cat.lang)
- print info
- else:
- print "No info given in mo file."
-
-if __name__ == '__main__':
- test()
-
+++ /dev/null
-# lilylib.py -- options and stuff
-#
-# source file of the GNU LilyPond music typesetter
-
-import os
-from __main__ import *
-
-try:
- import gettext
- gettext.bindtextdomain ('lilypond', '@localedir@')
- gettext.textdomain ('lilypond')
- _ = gettext.gettext
-except:
- def _ (s):
- return s
-
-program_version = '@TOPLEVEL_VERSION@'
-if program_version == '@' + 'TOPLEVEL_VERSION' + '@':
- program_version = '1.5.17'
-
-
-original_dir = os.getcwd ()
-temp_dir = os.path.join (original_dir, '%s.dir' % program_name)
-
-errorport = sys.stderr
-keep_temp_dir_p = 0
-verbose_p = 0
-
-
-def identify ():
- sys.stdout.write ('%s (GNU LilyPond) %s\n' % (program_name, program_version))
-
-def warranty ():
- identify ()
- sys.stdout.write ('\n')
- sys.stdout.write (_ ('Copyright (c) %s by' % ' 2001'))
- sys.stdout.write ('\n')
- sys.stdout.write (' Han-Wen Nienhuys')
- sys.stdout.write (' Jan Nieuwenhuizen')
- sys.stdout.write ('\n')
- sys.stdout.write (_ (r'''
-Distributed under terms of the GNU General Public License. It comes with
-NO WARRANTY.'''))
- sys.stdout.write ('\n')
-
-def progress (s):
- errorport.write (s + '\n')
-
-def warning (s):
- progress (_ ("warning: ") + s)
-
-def error (s):
-
-
- '''Report the error S. Exit by raising an exception. Please
- do not abuse by trying to catch this error. If you do not want
- a stack trace, write to the output directly.
-
- RETURN VALUE
-
- None
-
- '''
-
- progress (_ ("error: ") + s)
- raise _ ("Exiting ... ")
-
-def getopt_args (opts):
- '''Construct arguments (LONG, SHORT) for getopt from list of options.'''
- short = ''
- long = []
- for o in opts:
- if o[1]:
- short = short + o[1]
- if o[0]:
- short = short + ':'
- if o[2]:
- l = o[2]
- if o[0]:
- l = l + '='
- long.append (l)
- return (short, long)
-
-def option_help_str (o):
- '''Transform one option description (4-tuple ) into neatly formatted string'''
- sh = ' '
- if o[1]:
- sh = '-%s' % o[1]
-
- sep = ' '
- if o[1] and o[2]:
- sep = ','
-
- long = ''
- if o[2]:
- long= '--%s' % o[2]
-
- arg = ''
- if o[0]:
- if o[2]:
- arg = '='
- arg = arg + o[0]
- return ' ' + sh + sep + long + arg
-
-
-def options_help_str (opts):
- '''Convert a list of options into a neatly formatted string'''
- w = 0
- strs =[]
- helps = []
-
- for o in opts:
- s = option_help_str (o)
- strs.append ((s, o[3]))
- if len (s) > w:
- w = len (s)
-
- str = ''
- for s in strs:
- str = str + '%s%s%s\n' % (s[0], ' ' * (w - len(s[0]) + 3), s[1])
- return str
-
-def help ():
- ls = [(_ ("Usage: %s [OPTION]... FILE") % program_name),
- ('\n\n'),
- (help_summary),
- ('\n\n'),
- (_ ("Options:")),
- ('\n'),
- (options_help_str (option_definitions)),
- ('\n\n'),
- (_ ("Report bugs to %s") % 'bug-lilypond@gnu.org'),
- ('\n')]
- map (sys.stdout.write, ls)
-
-def setup_temp ():
- """
- Create a temporary directory, and return its name.
- """
- global temp_dir
- if not keep_temp_dir_p:
- temp_dir = tempfile.mktemp (program_name)
- try:
- os.mkdir (temp_dir, 0777)
- except OSError:
- pass
-
- return temp_dir
-
-
-def system (cmd, ignore_error = 0):
- """Run CMD. If IGNORE_ERROR is set, don't complain when CMD returns non zero.
-
- RETURN VALUE
-
- Exit status of CMD
- """
-
- if verbose_p:
- progress (_ ("Invoking `%s\'") % cmd)
- st = os.system (cmd)
- if st:
- name = re.match ('[ \t]*([^ \t]*)', cmd).group (1)
- msg = name + ': ' + _ ("command exited with value %d") % st
- if ignore_error:
- warning (msg + ' ' + _ ("(ignored)") + ' ')
- else:
- error (msg)
-
- return st
-
-
-def cleanup_temp ():
- if not keep_temp_dir_p:
- if verbose_p:
- progress (_ ("Cleaning %s...") % temp_dir)
- shutil.rmtree (temp_dir)
-
-
-def strip_extension (f, ext):
- (p, e) = os.path.splitext (f)
- if e == ext:
- e = ''
- return p + e
-
-# END Library
-
+++ /dev/null
-# title makefile for midi2ly
-# file midi2ly/Makefile
-
-depth = ..
-
-NAME = midi2ly-old
-MODULE_NAME = midi2ly-old
-
-SUBDIRS = include
-MODULE_LIBS=$(depth)/flower
-HELP2MAN_EXECS = midi2ly
-STEPMAKE_TEMPLATES=c++ executable po help2man
-
-include $(depth)/make/stepmake.make
-
-# explicit dependencies: (how to do auto?)
-#
-midi-lexer.l: $(outdir)/midi-parser.hh
-
-
-$(outdir)/midi2ly-version.o: $(outdir)/version.hh
-
+++ /dev/null
-/*
- duration-convert.cc -- implement Duration_convert
-
- source file of the LilyPond music typesetter
-
- (c) 1997--2001 Han-Wen Nienhuys <hanwen@cs.uu.nl>
- Jan Nieuwenhuizen <janneke@gnu.org>
-*/
-#include <assert.h>
-#include "duration-convert.hh"
-#include "warn.hh"
-
-// statics Duration_convert
-bool Duration_convert::no_quantify_b_s = false;
-bool Duration_convert::no_double_dots_b_s = false;
-bool Duration_convert::no_tuplets_b_s = false;
-int Duration_convert::no_smaller_than_i_s = 0;
-Array<Duration> Duration_convert::dur_array_s;
-
-String
-Duration_convert::dur2_str (Duration dur)
-{
- if (dur.ticks_i_)
- return String ("[") + to_str (dur.ticks_i_) + "]";
-
- String str;
- if (dur.durlog_i_ >= 0)
- str = to_str ( type2_i (dur.durlog_i_) );
- else if (dur.durlog_i_ == -1)
- str = "\\breve";
- else if (dur.durlog_i_ <= -2)
- {
- str = "\\longa";
- if (dur.durlog_i_ < -2)
- {
- dur.plet_.iso_i_ *= 1 << (-2 - dur.durlog_i_);
- }
- }
- str += to_str ('.', dur.dots_i_);
- if (dur.plet_b ())
- {
- str += String ("*") + to_str (dur.plet_.iso_i_);
- if (dur.plet_.type_i_ != 1)
- str += String ("/") + to_str (dur.plet_.type_i_);
- }
- return str;
-}
-
-int
-Duration_convert::dur2ticks_i (Duration dur)
-{
- if (dur.ticks_i_)
- return dur.ticks_i_;
- return dur2_mom (dur) * Rational (Duration::division_1_i_s);
-}
-
-int
-Duration_convert::i2_type (int i)
-{
- int t=0;
- while (i && !(i & 1)) {
- i >>= 1;
- t++;
- }
- return t;
-}
-
-int
-Duration_convert::type2_i (int type)
-{
- if (type<0)
- return 0;
- else
- return 1 << type;
-}
-
-Rational
-Duration_convert::dur2_mom (Duration dur)
-{
- if (dur.ticks_i_)
- return Rational (dur.ticks_i_, Duration::division_1_i_s);
-
- // or simply assert?
- if (dur.durlog_i_<-10)
- return Rational (0);
- Rational mom;
- if (dur.durlog_i_<0)
- mom = Rational (type2_i (-dur.durlog_i_), 1);
- else
- mom = Rational (1 , type2_i (dur.durlog_i_));
-
- Rational delta = mom;
- while (dur.dots_i_--)
- {
- delta /= 2.0;
- mom += delta;
- }
-
- return mom * plet_factor_mom (dur);
-}
-
-Duration
-Duration_convert::mom2_dur (Rational mom)
-{
- if (!mom)
- {
- Duration dur;
- dur.set_plet (0,1);
- return dur;
- }
-
- return mom2standardised_dur (mom);
-}
-
-
-Duration
-Duration_convert::mom2standardised_dur (Rational mom)
-{
- Duration dur;
-
- if (mom == Rational (0))
- return dur;
-
- int d = no_smaller_than_i_s ? no_smaller_than_i_s : 7;
- int i = type2_i (d);
- int n = (mom / Rational (1, i)).to_int ();
-
- int tuplet = 1;
- if (!no_tuplets_b_s)
- {
- // ugh: 8
- int m = n;
- int tup = 1;
- while (tup < 8 &&
- mom != Rational (m, i * tup))
- {
- tup += 2;
- m = (mom / Rational (1, i * tup)).to_int ();
- }
-
- if (tuplet < 8)
- {
- n = m;
- tuplet = tup;
- }
- }
-
- if (!n)
- return dur;
-
- if (mom - Rational (n, i)
- > Rational (1, i * 2 * tuplet))
- n++;
-
- while (!(n & 1))
- {
- n >>= 1;
- d--;
- }
-
- dur.durlog_i_ = d;
- dur.plet_.iso_i_ = n;
- dur.plet_.type_i_ = tuplet;
- return dur;
-}
-
-Rational
-Duration_convert::plet_factor_mom (Duration dur)
-{
- return dur.plet_.mom ();
-}
-
-Real
-Duration_convert::sync_f (Duration dur, Rational mom)
-{
- return mom / dur2_mom (dur);
-}
-
-Duration
-Duration_convert::ticks2_dur (int ticks_i)
-{
- Rational mom (ticks_i, Duration::division_1_i_s);
- return mom2standardised_dur (mom);
-}
-
-Duration
-Duration_convert::ticks2standardised_dur (int ticks_i)
-{
- Rational mom (ticks_i, Duration::division_1_i_s);
- Duration dur = mom2standardised_dur (mom);
- return dur;
-}
+++ /dev/null
-/*
- duration.cc -- implement Duration, Plet,
-
- source file of the LilyPond music typesetter
-
- (c) 1997--2001 Jan Nieuwenhuizen <janneke@gnu.org>
- Han-Wen Nienhuys <hanwen@cs.uu.nl>
-
-
- UGH. Duration is broken.
-*/
-
-#include <assert.h>
-
-#include "flower-proto.hh"
-#include "string.hh"
-#include "source-file.hh"
-#include "source.hh"
-#include "rational.hh"
-#include "duration.hh"
-#include "duration-convert.hh"
-
-// statics Duration
-int Duration::division_1_i_s = 384 * 4;
-
-
-Duration::Duration ()
-{
- durlog_i_ = 0;
- dots_i_ = 0;
- ticks_i_ = 0;
-}
-
-bool
-Duration::duration_type_b (int t)
-{
- /*
- ugh. Assuming behavior of conversion funcs on broken input.
- */
- return t == Duration_convert::type2_i (Duration_convert::i2_type (t));
-}
-
-void
-Duration::compress (Rational m)
-{
- plet_.iso_i_ *= m.num ();
- plet_.type_i_ *= m.den ();
-}
-
-Rational
-Duration::length_mom () const
-{
- return Duration_convert::dur2_mom (*this);
-}
-
-void
-Duration::set_plet (int i, int t)
-{
- plet_.iso_i_ = i;
- plet_.type_i_ = t;
-}
-
-/*
-void
-Duration::set_plet (Duration d)
-{
- plet_.iso_i_ = d.plet_.iso_i_;
- plet_.type_i_ = d.plet_.type_i_;
-}
-*/
-
-void
-Duration::set_ticks (int ticks_i)
-{
- assert (durlog_i_ <10);
- assert (!dots_i_);
- ticks_i_ = ticks_i;
-}
-
-String
-Duration::str () const
-{
- return Duration_convert::dur2_str (*this);
-}
-
-
-bool
-Duration::plet_b ()
-{
- return !plet_.unit_b ();
-}
-
+++ /dev/null
-# lib/include/Makefile
-
-depth = ../..
-STEPMAKE_TEMPLATES=c++
-include $(depth)/make/stepmake.make
-
+++ /dev/null
-/*
- duration-convert.hh -- declare Duration_convert
-
- source file of the LilyPond music typesetter
-
- (c) 1997--2001 Han-Wen Nienhuys <hanwen@cs.uu.nl>
-*/
-
-
-#ifndef DURATION_CONVERT_HH
-#define DURATION_CONVERT_HH
-#include "duration.hh"
-#include "string.hh"
-#include "array.hh"
-
-/**
- Duration_convert handles all conversions to -n fro Duration (dur).
- That is including (integer + division) representation for MIDI,
- and conversion from unexact time representation (best guess :-).
-
- A Rational (mom) is a Rational that holds the time fraction
- compared to a whole note (before also called wholes).
-
- [todo]
- move all statics to real members, instantiate Duration_convert
- object (s).
-*/
-struct Duration_convert {
-
- /* Urgh. statics.
- */
- static bool no_quantify_b_s;
- static bool no_double_dots_b_s;
- static bool no_tuplets_b_s;
- static int no_smaller_than_i_s;
- static Array<Duration> dur_array_s;
-
- /// Return number of ticks in (ticks, division_1) representation
- static int dur2ticks_i (Duration dur );
-
- /// Return the type_i representation of note length i
- static int i2_type (int i);
-
- /// Return the note length corresponding to the type_i representation
- /// Return 0 if longer than whole note.
- static int type2_i (int type);
-
- /// Return Rational representation (fraction of whole note).
- static Rational dur2_mom (Duration dur );
-
- /// Return Lilypond string representation.
- static String dur2_str (Duration dur );
-
- /// Return duration from Rational (fraction of whole) representation.
- static Duration mom2_dur (Rational mom );
-
- /// Return standardised duration, best guess if not exact.
- static Duration mom2standardised_dur (Rational mom );
-
- /// Return plet factor (not a Rational: should use Rational?).
- static Rational plet_factor_mom (Duration dur );
-
- static void set_array ();
-
- /** Return synchronisation factor for mom, so that
- mom2_dur (mom / sync_f ) will return the duration dur.
- */
- static Real sync_f (Duration dur, Rational mom );
-
- /// Return exact duration, in midi-ticks if not-exact.
- static Duration ticks2_dur (int ticks_i );
-
- /// Return standardised duration, best guess if not exact.
- static Duration ticks2standardised_dur (int ticks_i );
-};
-
-
-#endif // DURATION_CONVERT_HH
+++ /dev/null
-/*
- duration.hh -- declare Duration
-
- source file of the LilyPond music typesetter
-
- (c) 1997--2001 Jan Nieuwenhuizen <janneke@gnu.org>
-
-*/
-
-// split into 4?
-
-#ifndef DURATION_HH
-#define DURATION_HH
-
-#include "flower-proto.hh"
-#include "rational.hh"
-#include "plet.hh"
-
-/**
- Handle "musical" durations. This means: balltype 1,2,4,etc. and dots.
-
- (dur)
- */
-struct Duration {
- Duration ();
- /// is the "plet factor" of this note != 1 ?
- bool plet_b ();
- String str () const;
- void set_plet (int,int );
- void compress (Rational);
-
- static bool duration_type_b (int t);
- void set_ticks (int ticks_i );
- Rational length_mom () const ;
- static int division_1_i_s;
-
- /// Logarithm of the base duration.
- int durlog_i_;
- int dots_i_;
- Plet plet_;
- int ticks_i_;
-};
-#endif // DURATION_HH
-
+++ /dev/null
-//
-// lilypond-column.hh -- declare Lilypond_column
-//
-// (c) 1997--2001 Jan Nieuwenhuizen <janneke@gnu.org>
-
-#ifndef LILYPOND_COLUMN_HH
-#define LILYPOND_COLUMN_HH
-
-#include "flower-proto.hh"
-#include "midi2ly-proto.hh"
-#include "rational.hh"
-#include "cons.hh"
-
-/// (lilypond_column)
-class Lilypond_column
-{
-public:
- Lilypond_column (Lilypond_score* lilypond_score_l, Rational mom);
-
- void add_item (Lilypond_item* lilypond_item_l);
- Rational at_mom ();
-
- Cons_list<Lilypond_item> lilypond_item_l_list_;
- Rational at_mom_;
- Lilypond_score* lilypond_score_l_;
-};
-
-#endif // LILYPOND_COLUMN_HH
-
+++ /dev/null
-//
-// lilypond-item.hh -- declare lilypond_item
-//
-// (c) 1997--2001 Jan Nieuwenhuizen <janneke@gnu.org>
-
-#ifndef LILYPOND_ITEM_HH
-#define LILYPOND_ITEM_HH
-
-#include "midi2ly-proto.hh"
-#include "string.hh"
-#include "rational.hh"
-#include "duration.hh"
-
-// should these:
-// * be Lilypond_items
-// * be Voice_elements/requests
-// * get a name-change
-// ?
-
-/// (lilypond_item)
-class Lilypond_item
-{
-public:
- Lilypond_item (Lilypond_column* lilypond_column_l);
- virtual ~Lilypond_item ();
-
- virtual Rational at_mom ();
- virtual Rational duration_mom ();
- void output (Lilypond_stream& lilypond_stream_r);
- virtual String str () = 0;
-
- Lilypond_column* lilypond_column_l_;
-};
-
-class Lilypond_key : public Lilypond_item
-{
-public:
- Lilypond_key (int accidentals_i, int minor_i);
-
- String notename_str (int pitch_i);
- virtual String str ();
-
- //private:
- int accidentals_i_;
- int minor_i_;
-};
-
-class Lilypond_time_signature : public Lilypond_item
-{
-public:
- Lilypond_time_signature (int num_i, int den_i, int division_4_i, int count_32_i);
-
- Duration i2_dur (int time_i, int division_1_i);
- int clocks_1_i ();
- int den_i ();
- int num_i ();
- virtual String str ();
- Rational bar_mom ();
-
-private:
- Real sync_f_;
- Duration sync_dur_;
- int clocks_1_i_;
- int num_i_;
- int den_i_;
-};
-
-class Lilypond_note : public Lilypond_item
-{
-public:
- Lilypond_note (Lilypond_column* lilypond_column_l, int channel_i, int pitch_i, int dyn_i);
-
- Duration duration ();
- virtual Rational duration_mom ();
- virtual String str ();
-
- // int const c0_pitch_i_c_ = 60; // huh?
- static int const c0_pitch_i_c_ = 48;
-
- static bool const simple_plet_b_s = false;
- int channel_i_;
- int pitch_i_;
- Lilypond_column* end_column_l_;
-};
-
-class Lilypond_skip : public Lilypond_item
-{
-public:
- Lilypond_skip (Lilypond_column* lilypond_column_l, Rational skip_mom);
-
- Duration duration ();
- virtual Rational duration_mom ();
- virtual String str ();
-
-private:
- Rational mom_;
-};
-
-
-class Lilypond_tempo : public Lilypond_item
-{
-public:
- Lilypond_tempo (int useconds_per_4_i);
-
- int get_tempo_i (Rational rational);
- virtual String str ();
- int useconds_per_4_i ();
-
-private:
- int useconds_per_4_i_;
- Rational seconds_per_1_mom_;
-};
-
-class Lilypond_text : public Lilypond_item
-{
-public:
- enum Type {
- TEXT = 1, COPYRIGHT, TRACK_NAME, INSTRUMENT_NAME, LYRIC,
- MARKER, CUE_POINT
- };
- Lilypond_text (Lilypond_text::Type type, String str);
- virtual String str ();
-
- //private:
- Type type_;
- String text_str_;
-};
-
-#endif // LILYPOND_ITEM_HH
-
+++ /dev/null
-//
-// lilypond-score.hh -- declare Lilypond_score
-//
-// (c) 1997--2001 Jan Nieuwenhuizen <janneke@gnu.org>
-
-#ifndef LILYPOND_SCORE_HH
-#define LILYPOND_SCORE_HH
-
-#include "midi2ly-proto.hh"
-#include "flower-proto.hh"
-#include "cons.hh"
-#include "parray.hh"
-
-/// (lilypond_score)
-class Lilypond_score {
-public:
- Lilypond_score (int format_i, int tracks_i, int tempo_i);
- ~Lilypond_score ();
-
- void add_item (Lilypond_item* lilypond_item_p);
- void add_staff (Lilypond_staff* lilypond_staff_p);
-
- Lilypond_column* find_column_l (Rational mom);
- Lilypond_column* get_column_l (Rational mom);
-
- void output (String filename_str);
- void process ();
-
- // ugh
- Lilypond_key* lilypond_key_l_;
- Lilypond_time_signature* lilypond_time_signature_l_;
- Lilypond_tempo* lilypond_tempo_l_;
- Lilypond_staff * last_staff_l_;
-private:
- void filter_tempo ();
- void quantify_columns ();
- void quantify_durations ();
- void settle_columns ();
-
- Cons_list<Lilypond_staff> lilypond_staff_p_list_;
- Link_array<Lilypond_column> column_l_array_;
-
- // ugh, ugh, ugh
-public:
- int format_i_;
- int tracks_i_;
- int tempo_i_;
-};
-
-#endif // LILYPOND_SCORE_HH
-
+++ /dev/null
-//
-// lilypond-staff.hh -- declare lilypond_staff
-//
-// (c) 1997--2001 Jan Nieuwenhuizen <janneke@gnu.org>
-
-#ifndef LILYPOND_STAFF_HH
-#define LILYPOND_STAFF_HH
-
-#include "midi2ly-proto.hh"
-#include "flower-proto.hh"
-#include "cons.hh"
-#include "string.hh"
-
-/// (lilypond_staff)
-class Lilypond_staff
-{
-public:
- Lilypond_staff (int number_i, String copyright_str, String track_name_str, String instrument_str);
-
- void add_item (Lilypond_item* lilypond_item_p);
- void eat_voice (Cons_list<Lilypond_item>& items);
- String id_str ();
- String name_str ();
- void output (Lilypond_stream& lilypond_stream_r);
- void process ();
-
- String copyright_str_;
- String instrument_str_;
- String name_str_;
- Lilypond_key* lilypond_key_l_;
- Lilypond_time_signature* lilypond_time_signature_l_;
- Lilypond_tempo* lilypond_tempo_l_;
- int number_i_;
-
-private:
- void output_lilypond_begin_bar (Lilypond_stream& lilypond_stream_r, Rational now_mom, int bar_i);
-
- Cons_list<Lilypond_voice> lilypond_voice_p_list_;
- Cons_list<Lilypond_item> lilypond_item_p_list_;
-};
-
-#endif // LILYPOND_STAFF_HH
-
+++ /dev/null
-//
-// lilypond-stream.hh -- part of LilyPond
-//
-// (c) 1997--2001 Jan Nieuwenhuizen <janneke@gnu.org>
-
-// should i be named Lilypond_stream?
-
-#ifndef LILYPOND_STREAM_HH
-#define LILYPOND_STREAM_HH
-
-#include "midi2ly-proto.hh"
-#include "string.hh"
-//#include "scalar.hh"
-
-/// Lily output
-class Lilypond_stream {
-public:
- Lilypond_stream (String filename_str);
- ~Lilypond_stream();
-
- Lilypond_stream& operator << (char c);
- Lilypond_stream& operator << (String s);
- Lilypond_stream& operator << (Lilypond_item& lilypond_item_r);
-
-private:
- void handle_pending_indent();
- void header();
- void open();
- void output (String str);
- void output_wrapped (String str);
-
- ostream* os_p_;
- String filename_str_;
- int indent_i_;
- int column_i_;
- int pending_indent_i_;
- int wrap_column_i_;
- bool comment_mode_b_;
-};
-
-#endif // LILYPOND_STREAM_HH
-
+++ /dev/null
-//
-// lilypond-voice.hh -- declare Lilypond_voice
-//
-// (c) 1997--2001 Jan Nieuwenhuizen <janneke@gnu.org>
-
-#ifndef LILYPOND_VOICE_HH
-#define LILYPOND_VOICE_HH
-
-#include "midi2ly-proto.hh"
-//#include "flower-proto.hh"
-#include "parray.hh"
-#include "cons.hh"
-
-/// (lilypond_voice)
-class Lilypond_voice
-{
-public:
- Lilypond_voice (Lilypond_staff* lilypond_staff_l);
- void add_items (Link_array<Lilypond_item>& items);
- void output (Lilypond_stream& lilypond_stream_r);
- String get_clef () const;
-
-private:
- Lilypond_staff* lilypond_staff_l_;
- Link_array < Cons_list<Lilypond_item> > threads_;
- Rational mom_;
-};
-
-#endif // LILYPOND_VOICE_HH
-
+++ /dev/null
-/*
- midi-parser.hh -- declare Midi_parser
-
- source file of the GNU LilyPond music typesetter
-
- (c) 1997--1998 Jan Nieuwenhuizen <janneke@gnu.org>
-*/
-
-
-#ifndef MIDI_PARSER_HH
-#define MIDI_PARSER_HH
-
-// must, gcc 2.7.2{,.1} hits ico on midi-track-parser.cc:134 (@Midi_note)
-#define INLINES
-
-#ifdef INLINES
-
-#define next_byte() (inline_next_byte (__FUNCTION__))
-#define peek_byte() (inline_peek_byte (__FUNCTION__))
-#define forward_byte_L(n) (inline_forward_byte_L (__FUNCTION__, n))
-
-#else
-
-#define next_byte()\
- ((info_l_->byte_L_ < info_l_->end_byte_L_ ?\
- *info_l_->byte_L_++\
- : (Byte const)exit (__FUNCTION__": unexpected EOF")));
-
-#define peek_byte()\
- ((info_l_->byte_L_ < info_l_->end_byte_L_ ?\
- *info_l_->byte_L_\
- : (Byte const)exit (__FUNCTION__": unexpected EOF")));
-
-#define forward_byte_L(n) (inline_forward_byte_L (__FUNCTION__, n))
-
-#endif
-
-#include "flower-proto.hh"
-#include "rational.hh"
-#include "midi2ly-proto.hh"
-
-struct Midi_parser_info
-{
- Midi_parser_info();
- int division_1_i_;
- int format_i_;
- int tracks_i_;
- int errorlevel_i_;
- Byte const* byte_L_;
- Byte const* end_byte_L_;
- Source_file* source_l_;
- Lilypond_score* score_l_;
- Rational bar_mom_;
-};
-
-#include "string.hh"
-
-class Midi_parser
-{
-public:
- Midi_parser ();
-
- Midi_parser_info* info_l_;
-
-protected:
- Byte const* inline_forward_byte_L (char const* fun, int n)
- {
- if (info_l_->byte_L_ + n < info_l_->end_byte_L_ )
- {
- Byte const* p = info_l_->byte_L_;
- info_l_->byte_L_ += n;
- return p;
- }
- exit (String (fun) + ": unexpected EOF");
- return 0;
- }
-
-#ifdef INLINES
- Byte inline_next_byte (char const* fun)
- {
- if (info_l_->byte_L_ < info_l_->end_byte_L_)
- return *info_l_->byte_L_++;
- exit (String (fun) + ": unexpected EOF");
- return 0;
- }
-
- Byte inline_peek_byte (char const* fun)
- {
- if (info_l_->byte_L_ < info_l_->end_byte_L_)
- return *info_l_->byte_L_;
- exit (String (fun) + ": unexpected EOF");
- return 0;
- }
-#endif
-
- int get_i (int);
- String get_str (int);
- unsigned get_u (int);
- int get_var_i ();
- int exit (String);
- void error (String);
- String message (String);
- void warning (String);
-};
-
-#endif // MIDI_PARSER_HH
+++ /dev/null
-/*
- midi-score-parser.hh -- declare Midi_score_parser
-
- source file of the GNU LilyPond music typesetter
-
- (c) 1997--1998 Jan Nieuwenhuizen <janneke@gnu.org>
-*/
-
-
-#ifndef MIDI_SCORE_PARSER_HH
-#define MIDI_SCORE_PARSER_HH
-
-#include "midi-parser.hh"
-#include "flower-proto.hh"
-#include "midi2ly-proto.hh"
-#include "parray.hh"
-
-class Midi_score_parser : public Midi_parser
-{
-public:
- Lilypond_score* parse (String filename_str, Sources*);
-
-private:
- void open (String filename_str, Sources*);
-
- void parse_header ();
- int find_earliest_i (Link_array<Midi_track_parser>& tracks);
- Lilypond_score* parse_score ();
-};
-
-#endif // MIDI_SCORE_PARSER_HH
+++ /dev/null
-/*
- midi-track-parser.hh -- declare
-
- source file of the GNU LilyPond music typesetter
-
- (c) 1997--1998 Jan Nieuwenhuizen <janneke@gnu.org>
-*/
-
-
-#ifndef MIDI_TRACK_PARSER_HH
-#define MIDI_TRACK_PARSER_HH
-
-#include "flower-proto.hh"
-#include "cons.hh"
-#include "rational.hh"
-#include "midi2ly-proto.hh"
-#include "midi-parser.hh"
-
-class Midi_track_parser : public Midi_parser
-{
-public:
-
- Midi_track_parser (Midi_parser_info* info_l, int i);
- ~Midi_track_parser ();
-
- Rational at_mom ();
- Lilypond_staff* parse (Lilypond_column* col_l);
-
-private:
- bool eot ();
- void note_end (Lilypond_column* col_l, int channel_i, int pitch_i, int aftertouch_i );
- void note_end_all (Lilypond_column* col_l) ;
- void parse_delta_time ();
- Lilypond_item* parse_event (Lilypond_column* col_l);
- void parse_header ();
-
- Rational at_mom_;
- Byte running_byte_;
- Cons_list<Lilypond_note> open_note_l_list_;
- Lilypond_staff* lilypond_staff_p_;
- Midi_parser_info* track_info_p_;
-};
-
-#endif // MIDI_TRACK_PARSER_HH
+++ /dev/null
-//
-// midi2ly-global.hh -- declare global stuff for midi2ly
-//
-// (c) 1997--2001 Jan Nieuwenhuizen <janneke@gnu.org>
-
-#ifndef MIDI2LY_GLOBAL_HH
-#define MIDI2LY_GLOBAL_HH
-
-#include <iostream.h>
-
-#include "string.hh"
-#include "flower-proto.hh"
-
-#define monitor_p_g &cout
-enum Verbose { QUIET_ver, BRIEF_ver, NORMAL_ver, VERBOSE_ver, DEBUG_ver };
-extern Verbose level_ver;
-#if 0 // NPRINT
- // not what i want, all output goes through tors.
- // set verbosity level.
- #define LOGOUT(threshold) if (0) *monitor_p_g
-#else
- #define LOGOUT(threshold) if (level_ver >= threshold) *monitor_p_g
-#endif
-
-extern Sources* source_l_g;
-// huh?
-void message (String message_str); //, char const* context_ch_C);
-void warning (String message_str); //, char const* context_ch_C);
-void error (String message_str); //, char const* context_ch_C);
-
-String midi2ly_version_str();
-extern bool no_timestamps_b_g;;
-extern bool no_rests_b_g;;
-
-#endif // MIDI2LY_GLOBAL_HH
-
+++ /dev/null
-/*
- midi2ly-proto.hh -- declare type names in midi2ly
-
- source file of midi2ly, part of the GNU LilyPond package,
-
- (c) 1997--1998 Han-Wen Nienhuys <hanwen@stack.nl>
-*/
-
-
-#ifndef MIDI2LY_PROTO_HH
-#define MIDI2LY_PROTO_HH
-
-class Midi_parser;
-struct Midi_parser_info;
-class Midi_score_parser;
-class Midi_track_parser;
-class Lilypond_stream;
-class Lilypond_item;
-class Lilypond_key;
-class Lilypond_time_signature;
-class Lilypond_note;
-class Lilypond_tempo;
-class Lilypond_text;
-class Lilypond_score;
-class Lilypond_staff;
-class Lilypond_voice;
-class Lilypond_column;
-
-#endif // MIDI2LY_PROTO_HH
+++ /dev/null
-/*
- plet.hh -- declare Plet
-
- source file of the GNU LilyPond music typesetter
-
- (c) 1997--2001 Han-Wen Nienhuys <hanwen@cs.uu.nl>
-*/
-
-
-#ifndef PLET_HH
-#define PLET_HH
-#include "rational.hh"
-
-/**
- The type and replacement value of a plet (triplet, quintuplet.) Conceptually the same as a rational, but 4/6 != 2/3.
-
- (plet)
- */
-struct Plet {
- Plet ();
- Rational mom () const;
- bool unit_b () const;
- int iso_i_; // 2/3; 2 is not duration, maar of count!
- int type_i_;
-};
-
-#endif // PLET_HH
+++ /dev/null
-//
-// lilypond-column.cc -- implement Lilypond_column
-//
-// (c) 1997--2001 Jan Nieuwenhuizen <janneke@gnu.org>
-
-#include "lilypond-column.hh"
-
-Lilypond_column::Lilypond_column (Lilypond_score* lilypond_score_l, Rational mom)
-{
- lilypond_score_l_ = lilypond_score_l;
- at_mom_ = mom;
-}
-
-void
-Lilypond_column::add_item (Lilypond_item* lilypond_item_l)
-{
- lilypond_item_l_list_.append (new Cons<Lilypond_item> (lilypond_item_l, 0));
-}
-
-Rational
-Lilypond_column::at_mom()
-{
- return at_mom_;
-}
+++ /dev/null
-//
-// lilypond-item.cc -- implement Lilypond_item
-//
-// (c) 1997--2001 Jan Nieuwenhuizen <janneke@gnu.org>
-
-#include <string.h>
-#include <assert.h>
-#include "midi2ly-global.hh"
-#include "string-convert.hh"
-#include "duration-convert.hh"
-#include "lilypond-column.hh"
-#include "lilypond-item.hh"
-#include "lilypond-stream.hh"
-#include "lilypond-score.hh"
-
-Lilypond_item::Lilypond_item (Lilypond_column* lilypond_column_l)
-{
- lilypond_column_l_ = lilypond_column_l;
-}
-
-Lilypond_item::~Lilypond_item ()
-{
-}
-
-Rational
-Lilypond_item::at_mom ()
-{
- return lilypond_column_l_->at_mom ();
-}
-
-Rational
-Lilypond_item::duration_mom ()
-{
- return Rational (0);
-}
-
-void
-Lilypond_item::output (Lilypond_stream& lilypond_stream_r)
-{
- lilypond_stream_r << str () << " ";
-}
-
-Lilypond_key::Lilypond_key (int accidentals_i, int minor_i)
- : Lilypond_item (0)
-{
- accidentals_i_ = accidentals_i;
- minor_i_ = minor_i;
-}
-
-char const *accname[] = {"eses", "es", "", "is" , "isis"};
-
-String
-Lilypond_key::str ()
-{
- int key_i = accidentals_i_ >= 0
- ? ((accidentals_i_ % 7) ["cgdaebf"] - 'a' - 2 -2 * minor_i_ + 7) % 7
- : ((-accidentals_i_ % 7) ["cfbeadg"] - 'a' - 2 -2 * minor_i_ + 7) % 7;
-
- String notename_str = !minor_i_
- ? to_str ((char) ((key_i + 2) % 7 + 'a'))
- : to_str ((char) ((key_i + 2) % 7 + 'a'));
-
- // fis cis gis dis ais eis bis
- static int sharps_i_a [7] = { 2, 4, 6, 1, 3, 5, 7 };
- // bes es as des ges ces fes
- static int flats_i_a [7] = { 6, 4, 2, 7, 5, 3, 1 };
- int accidentals_i = accidentals_i_ >= 0
- ? sharps_i_a [key_i] <= accidentals_i_ ? 1 : 0
- : flats_i_a [key_i] <= -accidentals_i_ ? -1 : 0;
-
- if (accidentals_i)
- notename_str += String (accname [accidentals_i + 2]);
-
- return "\\key " + notename_str + (minor_i_ ? "\\minor" : "\\major") + "\n";
-}
-
-String
-Lilypond_key::notename_str (int pitch_i)
-{
- // this may seem very smart,
- // but it-s only an excuse not to read a notename table
-
- // major scale: do-do
- // minor scale: la-la (= + 5)
- static int notename_i_a [12] = { 0, 0, 1, 1, 2, 3, 3, 4, 4, 5, 5, 6 };
- int notename_i = notename_i_a [pitch_i % 12];
-
- static int accidentals_i_a [12] = { 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 0 };
- int accidental_i = accidentals_i_a [(minor_i_ * 5 + pitch_i) % 12];
- if (accidental_i && (accidentals_i_ < 0))
- {
- accidental_i *= -1;
- notename_i = (notename_i + 1) % 7;
- }
-
- String notename_str = to_str ((char)(((notename_i + 2) % 7) + 'a'));
- if (accidental_i)
- notename_str += String (accname [accidental_i + 2]);
-
- /*
- By tradition, all scales now consist of a sequence of 7 notes each
- with a distinct name, from amongst a b c d e f g. But, minor scales
- have a wide second interval at the top - the 'leading note' is
- sharped. (Why? it just works that way! Anything else doesn't sound as
- good and isn't as flexible at saying things. In medieval times,
- scales only had 6 notes to avoid this problem - the hexachords.)
-
- So, the d minor scale is d e f g a b-flat c-sharp d - using d-flat
- for the leading note would skip the name c and duplicate the name d.
- Why isn't c-sharp put in the key signature? Tradition. (It's also
- supposedly based on the Pythagorean theory of the cycle of fifths,
- but that really only applies to major scales...)
-
- Anyway, g minor is g a b-flat c d e-flat f-sharp g, and all the other
- flat minor keys end up with a natural leading note. And there you
- have it.
-
- John Sankey <bf250@freenet.carleton.ca>
-
- Let's also do a-minor: a b c d e f gis a
-
- --jcn
-
- */
-
- /* ok, bit ugly, but here we go -- jcn */
-
-
- if (minor_i_)
- {
- if ((accidentals_i_ == 0) && (notename_str == "as"))
- notename_str = "gis";
- else if ((accidentals_i_ == -1) && (notename_str == "des"))
- notename_str = "cis";
- else if ((accidentals_i_ == -2) && (notename_str == "ges"))
- notename_str = "fis";
- else if ((accidentals_i_ == 5) && (notename_str == "g"))
- notename_str = "fisis";
- else if ((accidentals_i_ == 6) && (notename_str == "d"))
- notename_str = "cisis";
- else if ((accidentals_i_ == 7) && (notename_str == "a"))
- notename_str = "gisis";
-
- if ((accidentals_i_ <= -6) && (notename_str == "b"))
- notename_str = "ces";
- if ((accidentals_i_ <= -7) && (notename_str == "e"))
- notename_str = "fes";
- }
-
- String de_octavate_str = to_str (',', (Lilypond_note::c0_pitch_i_c_ + 11 - pitch_i) / 12);
- String octavate_str = to_str ('\'', (pitch_i - Lilypond_note::c0_pitch_i_c_) / 12);
- return notename_str +de_octavate_str + octavate_str;
-}
-
-Lilypond_time_signature::Lilypond_time_signature (int num_i, int den_i, int clocks_4_i, int count_32_i)
- : Lilypond_item (0)
-{
- sync_dur_.durlog_i_ = 3;
- sync_f_ = 1.0;
- if (count_32_i != 8)
- warning (_f ("#32 in quarter: %d", count_32_i));
- num_i_ = num_i;
- den_i_ = den_i;
- clocks_1_i_ = clocks_4_i * 4;
-}
-
-Rational
-Lilypond_time_signature::bar_mom ()
-{
- Duration d;
- d.durlog_i_ = den_i_;
- return Rational (num_i_) * Duration_convert::dur2_mom (d);
-}
-
-int
-Lilypond_time_signature::clocks_1_i ()
-{
- return clocks_1_i_;
-}
-
-int
-Lilypond_time_signature::den_i ()
-{
- return den_i_;
-}
-
-int
-Lilypond_time_signature::num_i ()
-{
- return num_i_;
-}
-
-String
-Lilypond_time_signature::str ()
-{
- String str = "\\time "
- + to_str (num_i_) + "/" + to_str (1 << den_i_)
- + "\n";
- return str;
-}
-
-
-// statics Lilypond_note
-/*
- this switch can be used to write simple plets like
- c4*2/3
- as
- \plet 2/3; c4 \plet 1/1;
- */
-/*
- UGH: .hh says false, .cc says true.
- FIXME.
- */
-bool const Lilypond_note::simple_plet_b_s;
-
-Lilypond_note::Lilypond_note (Lilypond_column* lilypond_column_l,
- int channel_i, int pitch_i, int dyn_i)
- : Lilypond_item (lilypond_column_l)
-{
- // junk dynamics
- (void)dyn_i;
- channel_i_ = channel_i;
- pitch_i_ = pitch_i;
- end_column_l_ = 0;
-}
-
-Duration
-Lilypond_note::duration ()
-{
- assert (end_column_l_);
- Rational mom = end_column_l_->at_mom () - at_mom ();
- return Duration_convert::mom2_dur (mom);
-}
-
-Rational
-Lilypond_note::duration_mom ()
-{
- assert (end_column_l_);
- return end_column_l_->at_mom () - at_mom ();
-}
-
-String
-Lilypond_note::str ()
-{
- Duration dur = duration ();
- if (dur.durlog_i_ < -10)
- return "";
-
- String name_str
- = lilypond_column_l_->lilypond_score_l_->lilypond_key_l_->notename_str (pitch_i_);
-
- if (simple_plet_b_s)
- return name_str + Duration_convert::dur2_str (dur) + " ";
-
- String str;
-
- //ugh
- if (dur.plet_b () && dur.plet_.type_i_ != 1)
- {
- {
- str += String ("\\times ")
- + String_convert::i2dec_str (dur.plet_.iso_i_, 0, 0)
- + "/"
- + String_convert::i2dec_str (dur.plet_.type_i_, 0, 0)
- + " { ";
- }
- }
-
- str += name_str;
-
- Duration tmp = dur;
- tmp.set_plet (1,1);
- str += Duration_convert::dur2_str (tmp);
-
- if (dur.plet_b ())
- {
- if (dur.plet_.type_i_ != 1)
- str += String (" }");
- else
- str += String ("*") + to_str (dur.plet_.iso_i_);
- }
-
- /*
- note of zero duration is nonsense,
- but let's output anyway for convenient debugging
- */
- if (!duration_mom ())
- return String ("\n% ") + str + "\n";
-
- return str + " ";
-}
-
-Lilypond_skip::Lilypond_skip (Lilypond_column* lilypond_column_l, Rational skip_mom)
- : Lilypond_item (lilypond_column_l)
-{
- mom_ = skip_mom;
-}
-
-Duration
-Lilypond_skip::duration ()
-{
- return Duration_convert::mom2_dur (mom_);
-}
-
-Rational
-Lilypond_skip::duration_mom ()
-{
- return Duration_convert::dur2_mom (duration ());
-}
-
-String
-Lilypond_skip::str ()
-{
- String str;
- Rational m = mom_;
- if (m.to_int () >= 1)
- {
- int n = m.to_int ();
- str += "\\skip 1";
- if (n > 1)
- {
- str += "*";
- str += to_str (n);
- }
- str += " ";
- m -= n;
- }
-
- if (m > Rational (0))
- {
-
- Duration dur = Duration_convert::mom2_dur (m);
- str += "\\skip ";
- str += Duration_convert::dur2_str (dur);
- str += " ";
- }
- return str;
-}
-
-Lilypond_tempo::Lilypond_tempo (int useconds_per_4_i)
- : Lilypond_item (0)
-{
- useconds_per_4_i_ = useconds_per_4_i;
- seconds_per_1_mom_ = Rational(useconds_per_4_i_ *4, 1e6);
-}
-
-String
-Lilypond_tempo::str ()
-{
- String str = "\\tempo 4=";
- str += to_str (get_tempo_i (Rational (1, 4)));
- str += "\n";
- return str;
-}
-
-int
-Lilypond_tempo::useconds_per_4_i ()
-{
- return useconds_per_4_i_;
-}
-
-int
-Lilypond_tempo::get_tempo_i (Rational rational)
-{
- Rational m1 = Rational (60) / rational;
- Rational m2 = seconds_per_1_mom_;
- return m1 / m2;
-}
-
-Lilypond_text::Lilypond_text (Lilypond_text::Type type, String text_str)
- : Lilypond_item (0)
-{
- type_ = type;
- text_str_ = text_str;
-}
-
-String
-Lilypond_text::str ()
-{
- if (!text_str_.length_i ()
- || (text_str_.length_i () != (int)strlen (text_str_.ch_C ())))
- return "";
-
- return "% " + text_str_ + "\n";
-}
+++ /dev/null
-//
-// lilypond-score.cc -- implement Lilypond_score
-//
-// (c) 1997--2001 Jan Nieuwenhuizen <janneke@gnu.org>
-
-#include <assert.h>
-#include "rational.hh"
-#include "duration.hh"
-#include "duration-convert.hh"
-#include "midi2ly-global.hh"
-#include "lilypond-column.hh"
-#include "lilypond-item.hh"
-#include "lilypond-score.hh"
-#include "lilypond-staff.hh"
-#include "lilypond-stream.hh"
-
-#include "killing-cons.tcc"
-
-//static Lilypond_key key_c (0, 0);
-static Lilypond_time_signature time_sig_4 (4, 2, 24, 8);
-// useconds per 4: 250000 === 60 4 per minute
-static Lilypond_tempo tempo_60 (1000000);
-
-Lilypond_score::Lilypond_score (int format_i, int tracks_i, int tempo_i)
-{
- last_staff_l_ =0;
- format_i_ = format_i;
- tracks_i_ = tracks_i;
- tempo_i_ = tempo_i;
- column_l_array_.push (new Lilypond_column (this, Rational (0)));
- // lilypond_key_l_ = &key_c;
- lilypond_key_l_ = 0;
- lilypond_time_signature_l_ = &time_sig_4;
- lilypond_tempo_l_ = &tempo_60;
-}
-
-Lilypond_score::~Lilypond_score ()
-{
-}
-
-void
-Lilypond_score::add_item (Lilypond_item* lilypond_item_p)
-{
- last_staff_l_->add_item (lilypond_item_p);
-}
-
-void
-Lilypond_score::add_staff (Lilypond_staff* lilypond_staff_p)
-{
- lilypond_staff_p_list_.append (new Killing_cons<Lilypond_staff> (lilypond_staff_p, 0));
- last_staff_l_ = lilypond_staff_p;
-}
-
-Lilypond_column*
-Lilypond_score::find_column_l (Rational mom)
-{
- int upper_i = max (0, column_l_array_.size () - 1);
- int lower_i = 0;
- int i = 0; //upper_i;
- while (1)
- {
- Rational i_mom = column_l_array_ [i]->at_mom ();
- if (i_mom == mom)
- return column_l_array_ [i];
- if (mom < i_mom)
- upper_i = i;
- else
- lower_i = i;
- if ( (upper_i == lower_i) || (i == column_l_array_.size () - 1))
- {
- // we don't do inserts
- assert (0);
- Lilypond_column* col_p = new Lilypond_column (this, mom);
- column_l_array_.push (col_p);
- return col_p;
- }
- i = (upper_i + lower_i + 1 ) / 2;
- }
- assert (0);
- return 0;
-}
-
-Lilypond_column*
-Lilypond_score::get_column_l (Rational mom)
-{
- int i;
- Lilypond_column *c=0;
- for (i=column_l_array_.size () - 1; !c && i >=0; i--)
- {
- if (column_l_array_ [i]->at_mom () == mom )
- c = column_l_array_[i];
- else if (column_l_array_[i]->at_mom () < mom)
- break;
- }
- if (!c)
- {
- c = new Lilypond_column (this, mom);
- column_l_array_.insert (c, i+1);
- }
-
- assert (c->at_mom () == mom);
- return c;
-}
-
-void
-Lilypond_score::output (String filename_str)
-{
- LOGOUT (NORMAL_ver) << _f ("LY output to `%s'...", filename_str) << endl;
-
- // ugh, ugly midi type 1 fix
- if ( (lilypond_staff_p_list_.size_i () == 1)
- && !lilypond_staff_p_list_.head_->car_->number_i_)
- lilypond_staff_p_list_.head_->car_->number_i_ = 1;
-
- int track_i = 0;
- Lilypond_stream lilypond_stream (filename_str);
- for (Cons<Lilypond_staff>* i = lilypond_staff_p_list_.head_; i; i = i->next_)
- {
- LOGOUT (NORMAL_ver) << _f ("track %d:", track_i++) << flush;
- i->car_->output (lilypond_stream);
- lilypond_stream << '\n';
- LOGOUT (NORMAL_ver) << endl;
- }
-
- lilypond_stream << "\\score{\n";
- if (lilypond_staff_p_list_.size_i () > 1)
- lilypond_stream << "< \n";
- for (Cons<Lilypond_staff>* i = lilypond_staff_p_list_.head_; i; i = i->next_)
- {
- if ( (lilypond_staff_p_list_.size_i () != 1)
- && (i->car_ == lilypond_staff_p_list_.head_->car_))
- continue;
- lilypond_stream << "\\context Staff = \"" << i->car_->id_str () << "\" ";
- lilypond_stream << String ("\\" + i->car_->id_str ()) << '\n';
- }
- if (lilypond_staff_p_list_.size_i () > 1)
- lilypond_stream << ">\n";
-
- lilypond_stream << "\\paper{}\n";
-
-#if 1
- lilypond_stream << "\\midi{\n";
-
- // let's not use silly 0 track
- last_cons (lilypond_staff_p_list_.head_)->car_->lilypond_tempo_l_->output (lilypond_stream);
- lilypond_stream << "}\n";
-#endif
-
- lilypond_stream << "}\n";
-}
-
-void
-Lilypond_score::process ()
-{
- LOGOUT (NORMAL_ver) << '\n' << _ ("Processing...") << endl;
-
- LOGOUT (DEBUG_ver) << "columns\n";
-
- settle_columns ();
- filter_tempo ();
- quantify_columns ();
- quantify_durations ();
-
- LOGOUT (NORMAL_ver) << '\n' << _ ("Creating voices...") << endl;
- int track_i = 0;
- for (Cons<Lilypond_staff>* i = lilypond_staff_p_list_.head_; i; i = i->next_)
- {
- LOGOUT (NORMAL_ver) << _ ("track ") << track_i++ << ": " << flush;
- i->car_->process ();
- LOGOUT (NORMAL_ver) << endl;
- }
-}
-
-void
-Lilypond_score::filter_tempo ()
-{
- LOGOUT (NORMAL_ver) << '\n' << _ ("NOT Filtering tempo...") << endl;
-}
-
-void
-Lilypond_score::quantify_columns ()
-{
- // ugh
- if (Duration_convert::no_quantify_b_s)
- {
- LOGOUT (NORMAL_ver) << '\n' << _ ("NOT Quantifying columns...") << endl;
- return;
- }
-
- LOGOUT (NORMAL_ver) << '\n' << _ ("Quantifying columns...") << endl;
-
- int current_bar_i = 0;
- Rational bar_mom = lilypond_time_signature_l_->bar_mom ();
-
- int n = 7 >? Duration_convert::no_smaller_than_i_s;
- n = Duration_convert::type2_i (n);
- Rational s = Rational (1, n);
- for (int i = 0; i < column_l_array_.size (); i++)
- {
- column_l_array_ [i]->at_mom_ =
- s * Rational ( (int) ( (column_l_array_ [i]->at_mom ()) / s));
-
- int bar_i = (int) (column_l_array_ [i]->at_mom () / bar_mom) + 1;
- if (bar_i > current_bar_i)
-
- {
- LOGOUT (NORMAL_ver) << "[" << bar_i << "]" << flush;
- current_bar_i = bar_i;
- }
- }
- LOGOUT (NORMAL_ver) << endl;
-}
-
-void
-Lilypond_score::quantify_durations ()
-{
-
-}
-
-void
-Lilypond_score::settle_columns ()
-{
- LOGOUT (NORMAL_ver) << '\n' << _ ("Settling columns...") << endl;
-
- int n = column_l_array_.size ();
-
- int start_i = 0;
- int end_i = 0;
- Rational start_mom = 0;
-
- Duration smallest_dur;
- smallest_dur.durlog_i_ = 6;
- Rational const noise_mom = Duration_convert::dur2_mom (smallest_dur)
- / Rational (2);
- for (int i = 0; i < n; i++)
- {
- if (!start_i)
- {
- start_i = end_i = i;
- start_mom = column_l_array_ [i]->at_mom ();
- continue;
- }
-
- // find all columns within noise's distance
- while ( (i < n)
- && (column_l_array_ [i]->at_mom () - start_mom < noise_mom))
- end_i = ++i;
-
- // bluntly set all to time of first in group
- for (int j = start_i; j < end_i; j++)
- column_l_array_ [j]->at_mom_ = start_mom;
-
- start_i = end_i = 0;
- }
-}
-
+++ /dev/null
-//
-// lilypond-staff.cc -- implement Lilypond_staff
-//
-// (c) 1997--2001 Jan Nieuwenhuizen <janneke@gnu.org>
-
-#include <assert.h>
-#include <ctype.h>
-#include "rational.hh"
-#include "duration-convert.hh"
-#include "string-convert.hh"
-#include "midi2ly-proto.hh"
-#include "midi2ly-global.hh"
-#include "lilypond-column.hh"
-#include "lilypond-item.hh"
-#include "lilypond-staff.hh"
-#include "lilypond-stream.hh"
-#include "lilypond-voice.hh"
-#include "lilypond-score.hh"
-
-#include "killing-cons.tcc"
-
-extern Lilypond_score* lilypond_score_l_g;
-
-Lilypond_staff::Lilypond_staff (int number_i, String copyright_str, String track_name_str, String instrument_str)
-{
- number_i_ = number_i;
- copyright_str_ = copyright_str;
- instrument_str_ = instrument_str;
- name_str_ = track_name_str;
- lilypond_key_l_ = 0;
- lilypond_time_signature_l_ = 0;
- lilypond_tempo_l_ = 0;
-}
-
-void
-Lilypond_staff::add_item (Lilypond_item* lilypond_item_p)
-{
- lilypond_item_p_list_.append (new Killing_cons <Lilypond_item> (lilypond_item_p, 0));
- if (lilypond_item_p->lilypond_column_l_)
- lilypond_item_p->lilypond_column_l_->add_item (lilypond_item_p);
-}
-/**
- Walk ITEMS and find voices. Remove categorised items.
-
- TODO:
-
- * collect all channels into separate voices. Use chords for sim
- notes on same channel.
- * assume voices/assume chords modes.
-
- */
-void
-Lilypond_staff::eat_voice (Cons_list<Lilypond_item>& items)
-{
- Lilypond_voice* voice_p = new Lilypond_voice (this);
- lilypond_voice_p_list_.append (new Killing_cons<Lilypond_voice> (voice_p, 0));
-
- Rational mom = 0;
-
- Link_array<Lilypond_item> now_items;
- for (Cons<Lilypond_item>** i = &items.head_; *i;)
- {
- while (*i && (*i)->car_->at_mom () < mom)
- i = &(*i)->next_;
-
- Lilypond_note* last_note = 0;
- Link_array<Lilypond_item> now_items;
- if (*i)
- mom = (*i)->car_->at_mom ();
- while (*i && (*i)->car_->at_mom () == mom)
- {
- Lilypond_note* note = dynamic_cast<Lilypond_note*> ((*i)->car_);
- if (note && last_note
- /* ugh, should sort out (whether to) channel before */
- && (note->channel_i_ != last_note->channel_i_
- || (note->duration_mom ()
- != last_note->duration_mom ())))
- break;
- Cons<Lilypond_item>* c = items.remove_cons (i);
- now_items.push (c->car_);
- if (note)
- last_note = note;
- delete c;
- }
-
- if (now_items.size ())
- mom = now_items.top ()->at_mom ();
- if (last_note)
- mom += last_note->duration_mom ();
-
- voice_p->add_items (now_items);
- }
-}
-
-String
-Lilypond_staff::id_str ()
-{
- String id (name_str ());
- char *cp = id.ch_l ();
- char *end = cp + id.length_i ();
- for (;cp < end; cp++)
- {
- if (!isalpha (*cp))
- {
- *cp = 'X';
- }
- }
- return id;
-}
-
-String
-Lilypond_staff::name_str ()
-{
- if (name_str_.length_i ())
- return name_str_;
- return String ("track") + to_str (char ('A' - 1 + number_i_));
-}
-
-
-
-void
-Lilypond_staff::output (Lilypond_stream& lilypond_stream_r)
-{
- int c =0;
-
- String trackbody = "";
- for (Cons<Lilypond_voice>* i = lilypond_voice_p_list_.head_; i; i = i->next_)
- {
- String voicename = id_str () + "voice" + to_str (char (c + 'A'));
-
- lilypond_stream_r << voicename << " = \\notes ";
-
- trackbody += "\\context Voice = " + voicename + " \\" + voicename + "\n";
- lilypond_stream_r << '\n';
- i->car_->output (lilypond_stream_r);
- c++;
- lilypond_stream_r << '\n';
- }
-
- lilypond_stream_r << '\n';
- lilypond_stream_r << _ ("% MIDI copyright:") << copyright_str_ << '\n';
- lilypond_stream_r << _ ("% MIDI instrument:") << instrument_str_ << '\n';
- lilypond_stream_r << id_str () << " = ";
- lilypond_stream_r << "<\n" << trackbody << ">\n";
-
- lilypond_stream_r << " % " << name_str () << '\n';
-}
-
-void
-Lilypond_staff::output_lilypond_begin_bar (Lilypond_stream& lilypond_stream_r, Rational now_mom, int bar_i)
-{
- Rational bar_mom = lilypond_time_signature_l_->bar_mom ();
- Rational into_bar_mom = now_mom - Rational (bar_i - 1) * bar_mom;
- if (bar_i > 1)
- {
- if (!into_bar_mom)
- lilypond_stream_r << "|\n";
- }
- lilypond_stream_r << "% " << String_convert::i2dec_str (bar_i, 0, ' ');
- if (into_bar_mom)
- lilypond_stream_r << ":" << Duration_convert::dur2_str (Duration_convert::mom2_dur (into_bar_mom));
- lilypond_stream_r << '\n';
-}
-
-
-void
-Lilypond_staff::process ()
-{
- /*
- group items into voices
- */
-
- assert (lilypond_score_l_g);
- lilypond_key_l_ = lilypond_score_l_g->lilypond_key_l_;
- lilypond_time_signature_l_ = lilypond_score_l_g->lilypond_time_signature_l_;
- lilypond_tempo_l_ = lilypond_score_l_g->lilypond_tempo_l_;
-
- Cons_list<Lilypond_item> items;
- for (Cons<Lilypond_item>* i = lilypond_item_p_list_.head_; i; i = i->next_)
- items.append (new Cons<Lilypond_item> (i->car_, 0));
-
- while (items.size_i ())
- eat_voice (items);
-}
+++ /dev/null
-//
-// lilypond-stream.cc
-//
-// source file of the LilyPond music typesetter
-//
-// (c) 1997--1998, 1998 Jan Nieuwenhuizen <janneke@gnu.org>
-
-#include <assert.h>
-#include <time.h>
-#include <fstream.h>
-#include "midi2ly-global.hh"
-#include "lilypond-item.hh"
-#include "lilypond-stream.hh"
-#include "string-convert.hh"
-
-extern String filename_str_g;
-
-static int const INDENT_i = 8;
-
-Lilypond_stream::Lilypond_stream (String filename_str)
-{
- filename_str_ = filename_str;
- pending_indent_i_ = 0;
- os_p_ = 0;
- indent_i_ = 0;
- comment_mode_b_ = false;
- column_i_ = 0;
- wrap_column_i_ = 68;
- open();
- header();
-}
-
-Lilypond_stream::~Lilypond_stream ()
-{
- delete os_p_;
- if (indent_i_)
- warning (_f ("lily indent level: %d", indent_i_));
-}
-
-Lilypond_stream&
-Lilypond_stream::operator << (char c)
-{
- *this << to_str (c);
- return *this;
-}
-
-Lilypond_stream&
-Lilypond_stream::operator << (String s)
-{
- static String word_sep_str = "{} \t\n";
- while (s.length_i())
- {
- int i = s.index_any_i (word_sep_str) + 1;
- if (!i)
- i = s.length_i();
- String word = s.left_str (i);
- s = s.cut_str (i, s.length_i());
- output_wrapped (word);
- }
- return *this;
-}
-
-Lilypond_stream&
-Lilypond_stream::operator << (Lilypond_item& lilypond_item_r)
-{
- lilypond_item_r.output (*this);
- *os_p_ << flush;
- return *this;
-}
-
-void
-Lilypond_stream::handle_pending_indent()
-{
- *os_p_ << String_convert::char_str ('\t', pending_indent_i_);
- column_i_ += pending_indent_i_ * INDENT_i;
- pending_indent_i_ = 0;
-}
-
-void
-Lilypond_stream::header()
-{
- /* Maybe better not to translate these? */
- *os_p_ << _ ("% Creator: ");
- if (no_timestamps_b_g)
- *os_p_ << "GNU LilyPond\n";
- else
- *os_p_ << midi2ly_version_str() << '\n';
- *os_p_ << _ ("% Automatically generated");
- if (no_timestamps_b_g)
- *os_p_ << ".\n";
- else
- {
- *os_p_ << _ (", at ");
- time_t t (time (0));
- *os_p_ << ctime (&t) << "%\n";
- }
- *os_p_ << _ ("% from input file: ");
- // *os_p_ << midi_parser_l_g->filename_str_;
- // ugh
- *os_p_ << filename_str_g;
- *os_p_ << "\n\n";
- // ugh
- *os_p_ << "\\version \"1.4.0\"\n";
-}
-
-void
-Lilypond_stream::open()
-{
- os_p_ = new ofstream (filename_str_.ch_C ());
- if (!*os_p_)
- error (_f ("can't open file: `%s'", filename_str_));
-}
-
-void
-Lilypond_stream::output (String str)
-{
- for (int i = 0; i < str.length_i(); i++)
- {
- char c = str[ i ];
- switch (c)
- {
- case '{' :
- case '<' :
- handle_pending_indent();
- if (column_i_ == indent_i_ * INDENT_i)
- output ("\t");
- indent_i_++;
- *os_p_ << c;
- column_i_++;
- break;
- case '}' :
- case '>' :
- assert (indent_i_);
- indent_i_--;
- if (pending_indent_i_)
- pending_indent_i_--;
- handle_pending_indent();
- *os_p_ << c;
- column_i_++;
- break;
- case '%' :
- handle_pending_indent();
- comment_mode_b_ = true;
- *os_p_ << c;
- column_i_++;
- break;
- case '\t' :
- handle_pending_indent();
- *os_p_ << c;
- column_i_ += INDENT_i;
- break;
- case '\n' :
- *os_p_ << endl;
- pending_indent_i_ = indent_i_;
- column_i_ = 0;
- comment_mode_b_ = false;
- break;
- default :
- handle_pending_indent();
- *os_p_ << c;
- column_i_++;
- break;
- }
- }
-}
-
-void
-Lilypond_stream::output_wrapped (String str)
-{
- // enough room left -> doit
- if (column_i_ + str.length_i() <= wrap_column_i_)
- {
- output (str);
- return;
- }
-
- // we're at BOL already; this will never fit -> doit
- if (column_i_ == indent_i_ * INDENT_i)
- {
- output (str);
- return;
- }
-
- // ok, let's wrap
- // preserve comment mode
- if (comment_mode_b_)
- output (String ("\n%"));
- else
- output (String ("\n"));
-
- output (str);
-}
+++ /dev/null
-//
-// lilypond-voice.cc -- implement Lilypond_voice
-//
-// (c) 1997--2001 Jan Nieuwenhuizen <janneke@gnu.org>
-
-#include "string-convert.hh"
-#include "midi2ly-global.hh"
-#include "lilypond-column.hh"
-#include "lilypond-item.hh"
-#include "lilypond-staff.hh"
-#include "lilypond-stream.hh"
-#include "lilypond-voice.hh"
-#include "lilypond-score.hh"
-
-extern Lilypond_score* lilypond_score_l_g;
-
-Lilypond_voice::Lilypond_voice (Lilypond_staff* lilypond_staff_l)
-{
- lilypond_staff_l_ = lilypond_staff_l;
- threads_.push (new Cons_list<Lilypond_item>);
- mom_ = 0;
-}
-
-void
-Lilypond_voice::add_items (Link_array<Lilypond_item>& items)
-{
- int thread = 0;
- for (int i = 0; i < items.size (); i++)
- {
- Lilypond_item* item = items[i];
-
- int to_thread;
- if (Lilypond_note* n = dynamic_cast<Lilypond_note*> (item))
- to_thread = thread++;
- else
- to_thread = 0;
-
- if (to_thread >= threads_.size ())
- threads_.push (new Cons_list<Lilypond_item>);
-
- if (to_thread == 0 && item->at_mom () > mom_)
- {
- /* urg: skip should use refer to end-colum, not separate moment */
- Rational r = item->at_mom () - mom_;
- Lilypond_column* start = lilypond_score_l_g->find_column_l (mom_);
- threads_[to_thread]->append (new Cons<Lilypond_item> (new Lilypond_skip (start, r), 0));
- mom_ = item->at_mom ();
- }
-
- threads_[to_thread]->append (new Cons<Lilypond_item> (item, 0));
- if (to_thread == 0)
- mom_ += item->duration_mom ();
- }
-}
-
-/**
- analyse pitches to determine clef.
- */
-String
-Lilypond_voice::get_clef () const
-{
- Lilypond_note * n =0;
-
- for (Cons<Lilypond_item> *cp = threads_[0]->head_; !n && cp; cp = cp->next_)
- {
- n = dynamic_cast<Lilypond_note*> (cp->car_);
- }
-
- if (!n)
- return "";
-
- int p = n->pitch_i_;
-
- if (p < 56)
- return "\\clef \"bass\"\n";
- else if (p > 67)
- return "\\clef \"treble\"\n";
- else
- return "";
-}
-
-static int const FAIRLY_LONG_VOICE_i = 6;
-
-void
-Lilypond_voice::output (Lilypond_stream& lilypond_stream_r)
-{
- lilypond_stream_r << "{ ";
- if (threads_[0]->size_i () > FAIRLY_LONG_VOICE_i)
- lilypond_stream_r << '\n';
-
-
- lilypond_stream_r << get_clef () << '\n';
-
- int current_bar_i = 0;
- Rational bar_mom = lilypond_staff_l_->lilypond_time_signature_l_->bar_mom ();
-
- Link_array <Cons<Lilypond_item> > heads;
- for (int i = 1; i < threads_.size (); i++)
- heads.push (threads_[i]->head_);
- for (Cons<Lilypond_item>* i = threads_[0]->head_; i; i = i->next_)
- {
- Rational at_mom = i->car_->lilypond_column_l_->at_mom ();
- int bar_i = (int) (at_mom / bar_mom) + 1;
- if (bar_i > current_bar_i)
- {
- if (current_bar_i)
- {
- if (at_mom == Rational (bar_i - 1) * bar_mom)
- lilypond_stream_r << "|";
- lilypond_stream_r << "\n% ";
- lilypond_stream_r << String_convert::i2dec_str (bar_i, 0, ' ');
- lilypond_stream_r << '\n';
- }
- LOGOUT (NORMAL_ver) << "[" << bar_i << "]" << flush;
- current_bar_i = bar_i;
- }
-
- if (dynamic_cast<Lilypond_note*> (i->car_)
- && heads.size ()
- && heads[0]
- && heads[0]->car_->at_mom () == at_mom)
- {
- lilypond_stream_r << '<';
-
- lilypond_stream_r << *i->car_;
-
- for (int h = 0;
- h < heads.size ()
- && heads[h]
- && heads[h]->car_->at_mom () == at_mom;
- h++)
- {
- lilypond_stream_r << *heads[h]->car_;
- heads[h] = heads[h]->next_;
- }
- lilypond_stream_r << '>';
- }
- else
- lilypond_stream_r << *i->car_;
-
- if (Lilypond_key* k = dynamic_cast<Lilypond_key*> (i->car_))
- lilypond_staff_l_->lilypond_key_l_ = lilypond_score_l_g->lilypond_key_l_ = k;
- }
-
- if (threads_[0]->size_i () > FAIRLY_LONG_VOICE_i)
- lilypond_stream_r << '\n';
-
- lilypond_stream_r << "} ";
-}
-
-
+++ /dev/null
-//
-// main.cc -- implement main () entry point
-//
-// (c) 1997--2001 Jan Nieuwenhuizen <janneke@gnu.org>
-
-#include <stdlib.h>
-#include <iostream.h>
-#include <assert.h>
-#include <locale.h>
-#include "config.h"
-#include "string-convert.hh"
-#include "getopt-long.hh"
-#include "file-path.hh"
-#include "duration-convert.hh"
-#include "source.hh"
-
-#include "midi2ly-global.hh"
-#include "midi-score-parser.hh"
-#include "lilypond-item.hh"
-#include "lilypond-score.hh"
-
-#if HAVE_GETTEXT
-#include <libintl.h>
-#endif
-
-bool testing_level_global;
-
-// ugh
-String filename_str_g;
-
-// ugh
-Lilypond_score* lilypond_score_l_g = 0;
-
-bool no_timestamps_b_g = false;
-bool no_rests_b_g = false;
-
-Sources source;
-
-static File_path path;
-
-Verbose level_ver = NORMAL_ver;
-
-
-void
-identify()
-{
-cout << midi2ly_version_str() << endl;
-
-}
-
-void
-version ()
-{
- identify ();
- cout << '\n';
- cout << _f (""
- "This is free software. It is covered by the GNU General Public License,\n"
- "and you are welcome to change it and/or distribute copies of it under\n"
- "certain conditions. Invoke as `%s --warranty' for more information.\n",
- "midi2ly");
- cout << endl;
-
- cout << _f ("Copyright (c) %s by", "1996--2001");
- cout << "Han-Wen Nienhuys <hanwen@cs.uu.nl>\n"
- << "Jan Nieuwenhuizen <janneke@gnu.org>\n";
-}
-
-void
-notice()
-{
- cout << _ (
- " This program is free software; you can redistribute it and/or\n"
- "modify it under the terms of the GNU General Public License version 2\n"
- "as published by the Free Software Foundation.\n"
- "\n"
- " This program is distributed in the hope that it will be useful,\n"
- "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
- "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU\n"
- "General Public License for more details.\n"
- "\n"
- " You should have received a copy (refer to the file COPYING) of the\n"
- "GNU General Public License along with this program; if not, write to\n"
- "the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139,\n"
- "USA.\n");
-}
-
-/*
- Internationalisation kludge in two steps:
- * use _i () to get entry in POT file
- * call gettext () explicitely for actual "translation"
- */
-Long_option_init long_option_init_a[] =
-{
- {0, "no-quantify", 'b', _i ("write exact durations, e.g.: a4*385/384")},
- {0, "debug", 'd', _i ("enable debugging output")},
- {0, "help", 'h', _i ("this help")},
- {_i ("ACC[:MINOR]"), "key", 'k', _i ("set key: ACC +sharps/-flats; :1 minor")},
- {0, "no-silly", 'n', _i ("don't output tuplets, double dots or rests, smallest is 32")},
- {_i ("FILE"), "output", 'o', _i ("set FILE as default output")},
- {0, "no-tuplets", 'p', _i ("don't output tuplets")},
- {0, "quiet", 'q', _i ("be quiet")},
- {0, "no-rests", 'r', _i ("don't output rests or skips")},
- {_i ("DUR"), "smallest", 's', _i ("set smallest duration")},
- {0, "no-timestamps", 'T', _i ("don't timestamp the output")},
- {0, "version", 'V', _i ("print version number")},
- {0, "verbose", 'v', _i ("be verbose")},
- {0, "warranty", 'w', _i ("show warranty and copyright")},
- {0, "no-double-dots", 'x', _i ("assume no double dotted notes")},
- {0,0,0, 0}
-};
-
-void
-usage()
-{
- cout << _f ("Usage: %s [OPTION]... [FILE]", "midi2ly");
- cout << '\n';
- cout << _ ("Translate MIDI-file to lilypond");
- cout << '\n';
- cout << '\n';
- cout << _ ("Options:");
- cout << '\n';
- cout << Long_option_init::table_str (long_option_init_a) << endl;
-
- cout << _f ("Report bugs to %s", "bug-gnu-music@gnu.org") << endl;
-}
-
-void
-show_settings ()
-{
- LOGOUT (VERBOSE_ver) << "\n";
- LOGOUT (VERBOSE_ver) << _f ("no_double_dots: %d\n",
- Duration_convert::no_double_dots_b_s);
- LOGOUT (VERBOSE_ver) << _f ("no_rests: %d\n",
- no_rests_b_g);
- LOGOUT (VERBOSE_ver) << _f ("no_quantify_b_s: %d\n",
- Duration_convert::no_quantify_b_s);
- LOGOUT (VERBOSE_ver) << _f ("no_smaller_than: %d (1/%d)\n",
- Duration_convert::no_smaller_than_i_s,
- Duration_convert::type2_i (Duration_convert::no_smaller_than_i_s));
- LOGOUT (VERBOSE_ver) << _f ("no_tuplets: %d\n",
- Duration_convert::no_tuplets_b_s);
-}
-
-int
-main (int argc_i, char* argv_sz_a[])
-{
-
-#if HAVE_GETTEXT
- setlocale (LC_ALL, ""); /* enable locales */
- setlocale (LC_NUMERIC, "C"); /* musn't have comma's in output */
- String name (PACKAGE);
- name.to_lower ();
- bindtextdomain (name.ch_C (), DIR_LOCALEDIR);
- textdomain (name.ch_C ()) ;
-#endif
-
- bool key_override_b = false;
- Lilypond_key key (0, 0);
-
-
- Getopt_long getopt_long (argc_i, argv_sz_a, long_option_init_a);
-
- String output_str;
- while (Long_option_init const* long_option_init_p = getopt_long())
- switch (long_option_init_p->shortname_ch_)
- {
- case 'b':
- Duration_convert::no_quantify_b_s = true;
- break;
- case 'd':
- level_ver = DEBUG_ver;
- break;
- case 'h':
- usage();
- exit (0);
- break;
- // case 'I':
- // path->push (getopt_long.optional_argument_ch_C_);
- // break;
- case 'k':
- {
- String str = getopt_long.optional_argument_ch_C_;
- int i = str.index_i (':');
- i = (i >=0 ? i : str.length_i ());
- key.accidentals_i_ = String_convert::dec2_i (str.left_str (i));
- key.minor_i_ = (int)(bool)String_convert::dec2_i (str.cut_str (i + 1, str.length_i ()));
- key_override_b = true;
- break;
- }
- case 'n':
- Duration_convert::no_double_dots_b_s = true;
- Duration_convert::no_tuplets_b_s = true;
- Duration_convert::no_smaller_than_i_s = 5;
- no_rests_b_g = true;
- break;
- case 'o':
- output_str = getopt_long.optional_argument_ch_C_;
- break;
- case 'p':
- Duration_convert::no_tuplets_b_s = true;
- break;
- case 'q':
- level_ver = QUIET_ver;
- break;
- case 'r':
- no_rests_b_g = true;
- break;
- case 'T':
- no_timestamps_b_g = true;
- break;
- case 's':
- {
- int i = String_convert::dec2_i (getopt_long.optional_argument_ch_C_);
- if (!i)
- {
- identify();
- usage();
- exit (2); //usage
- }
- Duration_convert::no_smaller_than_i_s =
- Duration_convert::i2_type(i);
- }
- break;
- case 'v':
- level_ver = VERBOSE_ver;
- break;
-
- case 'V':
- version ();
- exit (0);
- break;
- case 'w':
- identify();
- notice();
- exit (0);
- break;
- case 'x':
- Duration_convert::no_double_dots_b_s = true;
- break;
- default:
- assert (0);
- break;
- }
-
- // flag -q must be checked first
- identify();
-
- path.add ("");
- source.set_binary (true);
- source.set_path (&path);
-
- char const* arg_sz = 0;
- while ( (arg_sz = getopt_long.get_next_arg ()))
- {
- show_settings ();
- filename_str_g = arg_sz;
- Midi_score_parser midi_parser;
- Lilypond_score* score_p = midi_parser.parse (arg_sz, &source);
-
- if (!score_p)
- return 1;
-
- // if given on command line: override
- if (key_override_b || !score_p->lilypond_key_l_)
- score_p->lilypond_key_l_ = &key;
- lilypond_score_l_g = score_p;
- score_p->process();
-
- if (!output_str.length_i ())
- {
- Path p = split_path (arg_sz);
-
- output_str = p.base + p.ext + ".ly";
- }
-
- score_p->output (output_str);
- delete score_p;
- }
- return 0;
-}
+++ /dev/null
-/*
- midi-parser.cc -- implement Midi_parser[_info]
-
- source file of the GNU LilyPond music typesetter
-
- (c) 1997--1998 Jan Nieuwenhuizen <janneke@gnu.org>
-*/
-
-#include <assert.h>
-#include "string-convert.hh"
-#include "source-file.hh"
-#include "midi2ly-global.hh"
-#include "midi-parser.hh"
-
-Midi_parser_info::Midi_parser_info ()
-{
- division_1_i_ = 0;
- format_i_ = 0;
- tracks_i_ = 0;
- errorlevel_i_ = 0;
- byte_L_ = 0;
- end_byte_L_ = 0;
- score_l_ = 0;
-}
-
-Midi_parser::Midi_parser ()
-{
- info_l_ = 0;
-}
-
-int
-Midi_parser::exit (String str)
-{
- error (str);
- ::exit (1);
- return 0;
-}
-
-void
-Midi_parser::error (String str)
-{
- ::message (message (str));
-}
-
-int
-Midi_parser::get_i (int n)
-{
- assert (n <= (int)sizeof(int));
- return String_convert::bin2_i (get_str (n));
-}
-
-unsigned
-Midi_parser::get_u (int n)
-{
- assert (n <= (int)sizeof(int));
- return String_convert::bin2_u (get_str (n));
-}
-
-String
-Midi_parser::get_str (int n)
-{
- assert (n >= 0);
- if (!n)
- warning (_ ("zero length string encountered"));
-
- Byte const* p = forward_byte_L (n);
- return String (p, n);
-}
-
-int
-Midi_parser::get_var_i ()
-{
- int var_i = 0;
-
- while (1)
- {
- Byte byte = next_byte ();
- var_i <<= 7;
- var_i += byte & 0x7f;
- if (!(byte & 0x80))
- return var_i;
- }
- exit ("get_var_i:");
- return 0;
-}
-
-String
-Midi_parser::message (String str)
-{
- return String ("midi2ly: ")
- + info_l_->source_l_->name_str () + ": "
- + String_convert::i2dec_str (info_l_->source_l_->line_i ((char const*)info_l_->byte_L_), 0, 0) + ": "
- + str + "\n"
- + info_l_->source_l_->error_str ((char const*)info_l_->byte_L_);
-}
-
-void
-Midi_parser::warning (String str)
-{
- ::message (message (String (_ ("warning: ")) + str));
-}
+++ /dev/null
-/*
- midi-score-parser.cc -- implement
-
- source file of the GNU LilyPond music typesetter
-
- (c) 1997--1998 Jan Nieuwenhuizen <janneke@gnu.org>
-*/
-
-#include "rational.hh"
-#include "source-file.hh"
-#include "source.hh"
-#include "midi2ly-global.hh"
-#include "midi-score-parser.hh"
-#include "midi-track-parser.hh"
-#include "lilypond-item.hh"
-#include "lilypond-score.hh"
-
-
-void
-Midi_score_parser::open (String filename_str, Sources* sources_l)
-{
- info_l_->source_l_ = sources_l->get_file_l (filename_str);
- if (!info_l_->source_l_)
- ::error (_f ("can't find file: `%s'", filename_str));
- info_l_->byte_L_ = (Byte const*)info_l_->source_l_->ch_C ();
- info_l_->end_byte_L_ = info_l_->byte_L_ + info_l_->source_l_->length_i () + 1;
-}
-
-Lilypond_score*
-Midi_score_parser::parse (String filename_str, Sources* sources_l)
-{
- Midi_parser_info info;
- info_l_ = &info;
- open (filename_str, sources_l);
- parse_header ();
- return parse_score ();
-}
-
-void
-Midi_score_parser::parse_header ()
-{
- String str = get_str (4);
- if ( str != "MThd" )
- exit (_ ("MIDI header expected"));
-
- int length_i = get_i (4);
- // is this signed?
- if (length_i < 6)
- exit (_ ("invalid header length"));
- info_l_->format_i_ = get_i (2);
- if (info_l_->format_i_ != 0 && info_l_->format_i_ != 1)
- exit (_("invalid MIDI format"));
- info_l_->tracks_i_ = get_i (2);
- if (info_l_->tracks_i_ < 0 || info_l_->tracks_i_ > 32 )
- exit (_("invalid number of tracks"));
- info_l_->division_1_i_ = get_i (2) * 4;
- if (info_l_->division_1_i_ < 0)
- exit (_ ("can't handle non-metrical time"));
- // ugh
- Duration::division_1_i_s = info_l_->division_1_i_;
- forward_byte_L (length_i - 6);
-}
-
-int
-Midi_score_parser::find_earliest_i (Link_array<Midi_track_parser>& tracks)
-{
- int earliest_i = 0;
- Rational earliest_mom = infinity_rat;
- for (int i = 0; i < tracks.size(); i++)
- {
- if ( tracks [i]->at_mom () < earliest_mom )
- {
- earliest_mom = tracks [i]->at_mom ();
- earliest_i = i;
- }
- }
- return earliest_i;
-}
-
-Lilypond_score*
-Midi_score_parser::parse_score ()
-{
- int current_bar_i = 0;
- Lilypond_time_signature m4 (4, 2, 24, 8);
- Rational bar4_mom = m4.bar_mom ();
-
- Lilypond_score* score_p = new Lilypond_score( 1, 1, 1 );
- info_l_->score_l_ = score_p;
-
- Link_array<Midi_track_parser> tracks;
- for (int i = 0; i < info_l_->tracks_i_; i++)
- tracks.push (new Midi_track_parser (info_l_, i));
-
- LOGOUT (NORMAL_ver) << _ ("Parsing...");
- LOGOUT (NORMAL_ver) << "\n";
- while (tracks.size ())
- {
- int i = find_earliest_i (tracks);
- Rational at_mom = tracks [i]->at_mom ();
- Lilypond_column* column_l = score_p->get_column_l (at_mom);
- Lilypond_staff* staff_p = tracks [i]->parse (column_l);
- if ( staff_p )
- {
- score_p->add_staff (staff_p);
- delete tracks [i];
- tracks.del (i);
- }
-
- // brr, musta have some progress
- for (int ii = 0; !info_l_->bar_mom_ && ii < tracks.size (); ii++)
- info_l_->bar_mom_ = tracks [ii]->info_l_->bar_mom_;
-
- int bar_i = (int) (at_mom
- / (info_l_->bar_mom_ ? info_l_->bar_mom_ : bar4_mom)) + 1;
- if (bar_i > current_bar_i)
- {
- LOGOUT (NORMAL_ver) << '[' << bar_i << ']' << flush;
- current_bar_i = bar_i;
- }
- }
- return score_p;
-}
+++ /dev/null
-/*
- midi-track-parser.cc -- implement
-
- source file of the GNU LilyPond music typesetter
-
- (c) 1997--1998 Jan Nieuwenhuizen <janneke@gnu.org>
-*/
-
-#include <assert.h>
-#include "string-convert.hh"
-#include "midi2ly-global.hh"
-#include "midi-track-parser.hh"
-#include "lilypond-column.hh"
-#include "lilypond-item.hh"
-#include "lilypond-score.hh"
-#include "lilypond-staff.hh"
-
-Midi_track_parser::Midi_track_parser (Midi_parser_info* info_l, int i)
-{
- info_l_ = info_l;
- at_mom_ = 0;
- track_info_p_ = 0;
- lilypond_staff_p_ = new Lilypond_staff (i, "", "", "");
- parse_header ();
- parse_delta_time ();
-}
-
-Midi_track_parser::~Midi_track_parser ()
-{
- delete lilypond_staff_p_;
- delete track_info_p_;
-}
-
-Rational
-Midi_track_parser::at_mom ()
-{
- return at_mom_;
-}
-
-bool
-Midi_track_parser::eot ()
-{
- if ( info_l_->byte_L_ < info_l_->end_byte_L_ )
- return false;
- return true;
-}
-
-void
-Midi_track_parser::note_end (Lilypond_column* col_l, int channel_i, int pitch_i, int aftertouch_i )
-{
- // junk dynamics
- (void)aftertouch_i;
-
- assert (col_l);
-
- for (Cons<Lilypond_note>** pp = &open_note_l_list_.head_; *pp;)
- {
- Cons<Lilypond_note>* i = *pp;
- if ((i->car_->pitch_i_ == pitch_i) && (i->car_->channel_i_ == channel_i))
- {
- i->car_->end_column_l_ = col_l;
- delete open_note_l_list_.remove_cons (pp);
- return;
- }
- else
- pp = &i->next_;
- }
- warning (_f ("Junking note-end event: channel = %d, pitch = %d",
- channel_i, pitch_i));
-}
-
-void
-Midi_track_parser::note_end_all (Lilypond_column* col_l)
-{
- // find
- assert (col_l);
- for (Cons<Lilypond_note>* i = open_note_l_list_.head_; i; i = i->next_)
- {
- i->car_->end_column_l_ = col_l;
- }
- // UGH UGH. MEMORY LEAK.
- open_note_l_list_.init ();
-}
-
-Lilypond_staff*
-Midi_track_parser::parse (Lilypond_column* col_l)
-{
- Rational mom = at_mom ();
- while (!eot () && (mom == at_mom ()))
- {
- Lilypond_item* p = parse_event (col_l);
- if (p)
- lilypond_staff_p_->add_item (p);
- }
-
- if (!eot())
- return 0;
-
- // catch-all
- note_end_all (col_l);
-
- Lilypond_staff* p = lilypond_staff_p_;
- lilypond_staff_p_ = 0;
- return p;
-}
-
-void
-Midi_track_parser::parse_delta_time ()
-{
- if (eot ())
- return;
- int delta_i = get_var_i ();
- at_mom_ += Rational (delta_i, info_l_->division_1_i_);
-}
-
-Lilypond_item*
-Midi_track_parser::parse_event (Lilypond_column* col_l)
-{
- Byte byte = peek_byte ();
- // RUNNING_STATUS [\x00-\x5f]
- if (byte <= 0x5f)
- {
- if (running_byte_ <= 0x5f)
- exit (_ ("invalid running status"));
- /*
- 'running status' rather means 'missing status'.
- we'll just pretend we read the running status byte.
- */
- byte = running_byte_;
- }
- else
- byte = next_byte ();
-
- Lilypond_item* item_p = 0;
- // DATA_ENTRY [\x60-\x79]
- if ((byte >= 0x60) && (byte <= 0x79))
- {
- next_byte ();
- }
- // ALL_NOTES_OFF [\x7a-\x7f]
- else if ((byte >= 0x7a) && (byte <= 0x7f))
- {
- next_byte ();
- next_byte ();
- note_end_all (col_l);
- }
- // NOTE_OFF [\x80-\x8f]
- else if ((byte >= 0x80) && (byte <= 0x8f))
- {
- running_byte_ = byte;
- int channel_i = byte & ~0x90;
- int pitch_i = (int)next_byte ();
- int dyn_i = (int)next_byte ();
- note_end (col_l, channel_i, pitch_i, dyn_i);
- }
- // NOTE_ON [\x90-\x9f]
- else if ((byte >= 0x90) && (byte <= 0x9f))
- {
- running_byte_ = byte;
- int channel_i = byte & ~0x90;
- int pitch_i = (int)next_byte ();
- int dyn_i = (int)next_byte ();
- /*
- sss: some broken devices encode NOTE_OFF as
- NOTE_ON with zero volume
- */
- if (dyn_i)
- {
- Lilypond_note* p = new Lilypond_note (col_l, channel_i, pitch_i, dyn_i);
- item_p = p;
- open_note_l_list_.append (new Cons<Lilypond_note> (p, 0));
- }
- else
- {
- note_end (col_l, channel_i, pitch_i, dyn_i);
- }
- }
-
- // POLYPHONIC_AFTERTOUCH [\xa0-\xaf]
- else if ((byte >= 0xa0) && (byte <= 0xaf))
- {
- running_byte_ = byte;
- next_byte ();
- next_byte ();
- }
- // CONTROLMODE_CHANGE [\xb0-\xbf]
- else if ((byte >= 0xb0) && (byte <= 0xbf))
- {
- running_byte_ = byte;
- next_byte ();
- next_byte ();
- }
- // PROGRAM_CHANGE [\xc0-\xcf]
- else if ((byte >= 0xc0) && (byte <= 0xcf))
- {
- running_byte_ = byte;
- next_byte ();
- }
- // CHANNEL_AFTERTOUCH [\xd0-\xdf]
- else if ((byte >= 0xd0) && (byte <= 0xdf))
- {
- running_byte_ = byte;
- next_byte ();
- next_byte ();
- }
- // PITCHWHEEL_RANGE [\xe0-\xef]
- else if ((byte >= 0xe0) && (byte <= 0xef))
- {
- running_byte_ = byte;
- next_byte ();
- next_byte ();
- }
- // SYSEX_EVENT1 [\xf0]
- else if (byte == 0xf0)
- {
- int length_i = get_var_i ();
- String str = get_str (length_i);
- }
- // SYSEX_EVENT2 [\xf7]
- else if (byte == 0xf7)
- {
- int length_i = get_var_i ();
- String str = get_str (length_i);
- }
- // META_EVENT [\xff]
- else if (byte == 0xff)
- {
- // SEQUENCE [\x00][\x02]
- byte = next_byte ();
- if (byte == 0)
- {
- next_byte ();
- get_i (2);
- }
- // YYTEXT [\x01]
- // YYCOPYRIGHT [\x02]
- // YYTRACK_NAME [\x03]
- // YYINSTRUMENT_NAME [\x04]
- // YYLYRIC [\x05]
- // YYMARKER [\x06]
- // YYCUE_POINT [\x07]
- else if ((byte >= 0x01) && (byte <= 0x07))
- {
- // LOGOUT (DEBUG_ver) << "\n% Text(" << (int)byte << "):" << flush;
- int length_i = get_var_i ();
- String str = get_str (length_i);
- // LOGOUT (DEBUG_ver) << str << endl;
- Lilypond_text::Type t = (Lilypond_text::Type)byte;
- Lilypond_text* p = new Lilypond_text (t, str);
- item_p = p;
- if (t == Lilypond_text::COPYRIGHT)
- lilypond_staff_p_->copyright_str_ = p->text_str_;
- else if (t == Lilypond_text::TRACK_NAME)
- lilypond_staff_p_->name_str_ = p->text_str_;
- else if (t == Lilypond_text::INSTRUMENT_NAME)
- lilypond_staff_p_->instrument_str_ = p->text_str_;
- }
- // END_OF_TRACK [\x2f][\x00]
- else
- {
- Byte next = peek_byte ();
- if ((byte == 0x2f) && (next == 0x00))
- {
- next_byte ();
- info_l_->byte_L_ = info_l_->end_byte_L_;
- }
- // TEMPO [\x51][\x03]
- else if ((byte == 0x51) && (next == 0x03))
- {
- next_byte ();
- unsigned useconds_per_4_u = get_u (3);
- // $$ = new Lilypond_tempo ( ($2 << 16) + ($3 << 8) + $4);
- // LOGOUT (DEBUG_ver) << $$->str() << endl;
- Lilypond_tempo* p = new Lilypond_tempo ( useconds_per_4_u );
- item_p = p;
- info_l_->score_l_->lilypond_tempo_l_ = p;
- lilypond_staff_p_->lilypond_tempo_l_ = p;
- }
- // SMPTE_OFFSET [\x54][\x05]
- else if ((byte == 0x54) && (next == 0x05))
- {
- next_byte ();
- (int)next_byte ();
- (int)next_byte ();
- (int)next_byte ();
- (int)next_byte ();
- (int)next_byte ();
- }
- // TIME [\x58][\x04]
- else if ((byte == 0x58) && (next == 0x04))
- {
- next_byte ();
- int num_i = (int)next_byte ();
- int den_i = (int)next_byte ();
- int clocks_4_i = (int)next_byte ();
- int count_32_i = (int)next_byte ();
- Lilypond_time_signature* p = new Lilypond_time_signature ( num_i, den_i, clocks_4_i, count_32_i );
- item_p = p;
- info_l_->score_l_->lilypond_time_signature_l_ = p;
- info_l_->bar_mom_ = p->bar_mom ();
- lilypond_staff_p_->lilypond_time_signature_l_ = p;
- }
- // KEY [\x59][\x02]
- else if ((byte == 0x59) && (next == 0x02))
- {
- next_byte ();
- int accidentals_i = (int)(signed char)next_byte ();
- int minor_i = (int)(bool)next_byte ();
- Lilypond_key* p = new Lilypond_key (accidentals_i, minor_i);
- item_p = p;
-#if 0
- info_l_->score_l_->lilypond_key_l_ = p;
- lilypond_staff_p_->lilypond_key_l_ = p;
-#endif
- }
- // SSME [\0x7f][\x03]
- else if ((byte == 0x7f) && (next == 0x03))
- {
- next_byte ();
- int length_i = get_var_i ();
- String str = get_str (length_i);
- item_p = new Lilypond_text ((Lilypond_text::Type)byte, str);
- }
- else
- {
- next_byte ();
- next_byte ();
- warning (_ ("unimplemented MIDI meta-event"));
- }
- }
- }
- else
- exit (_ ("invalid MIDI event"));
-
- if (item_p)
- item_p->lilypond_column_l_ = col_l;
-
- parse_delta_time ();
-
- return item_p;
-}
-
-void
-Midi_track_parser::parse_header ()
-{
- String str = get_str (4);
- if ( str != "MTrk" )
- exit (_ ("MIDI track expected"));
-
- int length_i = get_i (4);
- // is this signed?
- if (length_i < 0)
- exit (_ ("invalid track length"));
- assert (!track_info_p_);
- track_info_p_ = new Midi_parser_info (*info_l_);
- track_info_p_->end_byte_L_ = track_info_p_->byte_L_ + length_i;
- forward_byte_L (length_i);
- // forward_byte_L (length_i-1);
- info_l_ = track_info_p_;
-}
+++ /dev/null
-//
-// version.cc -- implement inexpensive versioning
-//
-// (c) 1997--2001 Jan Nieuwenhuizen <janneke@gnu.org>
-
-#include <stdio.h>
-#include "config.h"
-#include "version.hh"
-
-#define VERSION_SZ MAJOR_VERSION "." MINOR_VERSION "." PATCH_LEVEL "." MY_PATCH_LEVEL
-
-static char *s = "midi2ly " VERSION_SZ;
-
-
-const char *
-midi2ly_version_sz()
-{
- return s;
-}
-
+++ /dev/null
-/*
- plet.cc -- implement Plet
-
- source file of the GNU LilyPond music typesetter
-
- (c) 1997--2001 Han-Wen Nienhuys <hanwen@cs.uu.nl>
-*/
-
-#include "plet.hh"
-
-
-Plet::Plet ()
-{
- type_i_ = 1;
- iso_i_ = 1;
-}
-
-Rational
-Plet::mom () const
-{
- return Rational (iso_i_, type_i_);
-}
-
-bool
-Plet::unit_b () const
-{
- return type_i_ == 1 && iso_i_ == 1;
-}
-
+++ /dev/null
-#include "flower-proto.hh"
-#include "string.hh"
-
-const char * midi2ly_version_sz();
-
-String
-midi2ly_version_str()
-{
- return String (midi2ly_version_sz ());
-}
-
+++ /dev/null
-/*
- midi.c -- implement Python midi parser module
-
- source file of the GNU LilyPond music typesetter
-
- (c) 2001 Han-Wen Nienhuys <hanwen@cs.uu.nl>
- Jan Nieuwenhuizen <janneke@gnu.org>
-
-*/
-
-/*
-
-python2
-import midi
-s = open ("s.midi").read ()
-midi.parse_track (s)
-midi.parse (s)
-
-*/
-
-#include "config.h"
-
-/* urg */
-#if HAVE_PYTHON2_PYTHON_H
-#include <python2/Python.h>
-#elif HAVE_PYTHON2_1_PYTHON_H
-#include <python2.1/Python.h>
-#elif HAVE_PYTHON2_0_PYTHON_H
-#include <python2.0/Python.h>
-#elif HAVE_PYTHON1_5_PYTHON_H
-#include <python1.5/Python.h>
-#elif HAVE_PYTHON_PYTHON_H
-#define assert(x)
-#include <python/Python.h>
-#elif HAVE_PYTHON_H
-#define assert(x)
-#include <Python.h>
-#else
-#error Need Python.h
-#endif
-
-#if 0
-int x = 0;
-int *track = &x;
-#define debug_print(f, args...) fprintf (stderr, "%s:%d: track: %p :" f, __FUNCTION__, __LINE__, *track, ##args)
-#else
-#define debug_print(f, args...)
-#endif
-
-static PyObject *Midi_error;
-static PyObject *Midi_warning;
-
-static PyObject *
-midi_error (char *s)
-{
- PyErr_SetString (Midi_error, s);
- return 0;
-}
-
-static PyObject *
-midi_warning (char *s)
-{
- PyErr_SetString (Midi_warning, s);
- return 0;
-}
-
-
-typedef struct message {
- unsigned char msg;
- char * description;
-} message_t;
-
-message_t channelVoiceMessages[] = {
- 0x80, "NOTE_OFF",
- 0x90, "NOTE_ON",
- 0xA0, "POLYPHONIC_KEY_PRESSURE",
- 0xB0, "CONTROLLER_CHANGE",
- 0xC0, "PROGRAM_CHANGE",
- 0xD0, "CHANNEL_KEY_PRESSURE",
- 0xE0, "PITCH_BEND",
- 0,0
-};
-
-message_t channelModeMessages[] = {
- 0x78, "ALL_SOUND_OFF",
- 0x79, "RESET_ALL_CONTROLLERS",
- 0x7A, "LOCAL_CONTROL",
- 0x7B, "ALL_NOTES_OFF",
- 0x7C, "OMNI_MODE_OFF",
- 0x7D, "OMNI_MODE_ON",
- 0x7E, "MONO_MODE_ON",
- 0x7F, "POLY_MODE_ON",
- 0,0
-};
-
-message_t metaEvents[] = {
- 0x00, "SEQUENCE_NUMBER",
- 0x01, "TEXT_EVENT",
- 0x02, "COPYRIGHT_NOTICE",
- 0x03, "SEQUENCE_TRACK_NAME",
- 0x04, "INSTRUMENT_NAME",
- 0x05, "LYRIC",
- 0x06, "MARKER",
- 0x07, "CUE_POINT",
- 0x20, "MIDI_CHANNEL_PREFIX",
- 0x21, "MIDI_PORT",
- 0x2F, "END_OF_TRACK",
- 0x51, "SET_TEMPO",
- 0x54, "SMTPE_OFFSET",
- 0x58, "TIME_SIGNATURE",
- 0x59, "KEY_SIGNATURE",
- 0x7F, "SEQUENCER_SPECIFIC_META_EVENT",
- 0xFF, "META_EVENT",
- 0,0
-};
-
-void
-add_constants (PyObject *dict)
-{
- message_t * p[] = {metaEvents, channelModeMessages, channelVoiceMessages ,0};
- int i,j;
- for ( j =0; p[j]; j++)
- for ( i = 0; p[j][i].description; i++)
- PyDict_SetItemString (dict, p[j][i].description, Py_BuildValue ("i", p[j][i].msg));
-}
-
-unsigned long int
-get_number (unsigned char ** str, unsigned char * end_str, int length)
-{
- /* # MIDI uses big-endian for everything */
- long sum = 0;
- int i = 0;
-
- for (; i < length; i++)
- sum = (sum << 8) + (unsigned char) (*str)[i];
-
- *str += length;
- debug_print ("%d:\n", sum);
- return sum;
-}
-
-unsigned long int
-get_variable_length_number (unsigned char **str, unsigned char * end_str)
-{
- long sum = 0;
- int i = 0;
- while (*str < end_str)
- {
- unsigned char x = **str;
- (*str) ++;
- sum = (sum << 7) + (x & 0x7F);
- if (!(x & 0x80))
- break;
- }
- debug_print ("%d:\n", sum);
- return sum;
-}
-
-PyObject *
-read_one_byte (unsigned char **track, unsigned char *end,
- unsigned char x)
-{
- PyObject *pyev = Py_BuildValue ("(i)", x);
- debug_print ("%x:%s", x, "event\n");
-
- return pyev;
-}
-
-PyObject *
-read_two_bytes (unsigned char **track, unsigned char *end,
- unsigned char x)
-{
- PyObject *pyev = Py_BuildValue ("(ii)", x, (*track)[0]);
- *track += 1;
- debug_print ("%x:%s", x, "event\n");
- return pyev;
-}
-
-PyObject *
-read_three_bytes (unsigned char **track, unsigned char *end,
- unsigned char x)
-{
- PyObject *pyev = Py_BuildValue ("(iii)", x, (*track)[0],
- (*track)[1]);
-
- *track += 2;
- debug_print ("%x:%s", x, "event\n");
- return pyev;
-}
-
-PyObject *
-read_string (unsigned char **track, unsigned char *end)
-{
- unsigned long length = get_variable_length_number (track, end);
- if (length > end - *track)
- length = end - *track;
-
- *track += length;
- return Py_BuildValue ("s#", ((*track) -length), length);
-}
-
-typedef PyObject* (*Read_midi_event)
- (unsigned char **track, unsigned char *end,
- unsigned char x);
-
-
-static PyObject *
-read_f0_byte (unsigned char **track, unsigned char *end,
- unsigned char x)
-
-{
- debug_print ("%x:%s", x, "event\n");
- if (x == 0xff)
- {
- unsigned char z = (*track)[0 ];
- *track += 1;
- debug_print ("%x:%s", z, "f0-event\n");
-
- return Py_BuildValue ("(iiO)", x, z, read_string (track, end));
- }
-
- return Py_BuildValue ("(iO)", x, read_string (track, end));
-}
-
-Read_midi_event read_midi_event [16] =
-{
- read_one_byte, // 0
- read_one_byte, // 10
- read_one_byte, // 20
- read_one_byte, // 30
- read_one_byte, // 40
- read_one_byte, // 50
- read_one_byte, // 60 data entry.
- read_two_bytes, // 70 all notes off
- read_three_bytes, // 80 note off
- read_three_bytes, // 90 note on
- read_three_bytes, // a0 poly aftertouch
- read_three_bytes, // b0 control
- read_two_bytes, // c0 prog change
- read_two_bytes, // d0 ch aftertouch
- read_three_bytes, // e0 pitchwheel range
- read_f0_byte, // f0
-};
-
-
-static PyObject *
-read_event (unsigned char **track, unsigned char *end, PyObject *time,
- unsigned char *running_status)
-{
- int rsb_skip = ((**track & 0x80)) ? 1 :0;
-
- unsigned char x = (rsb_skip) ? (*track)[0]: *running_status;
-
- PyObject * bare_event = 0;
- debug_print ("%x:%s", x, "event\n");
- *running_status = x;
- *track += rsb_skip;
-
- // printf ("%x %x %d next %x\n", x, (*track)[0], rsb_skip, (*track)[1]);
- bare_event = (*read_midi_event[x >> 4]) (track, end, x);
- if (bare_event)
- return Py_BuildValue ("(OO)", time, bare_event);
- else
- return NULL;
-}
-
-static PyObject *
-midi_parse_track (unsigned char **track, unsigned char *track_end)
-{
- unsigned int time = 0;
- unsigned char running_status;
- unsigned long track_len, track_size;
- PyObject *pytrack = 0;
-
- debug_print ("%s", "\n");
-
- track_size = track_end - *track;
-
- debug_print ("%s", "\n");
- if (strcmp (*track, "MTrk"))
- return midi_error (__FUNCTION__ ": MTrk expected");
-
- *track += 4;
-
- track_len = get_number (track, *track + 4, 4);
-
-
- debug_print ("track_len: %u\n", track_len);
- debug_print ("track_size: %u\n", track_size);
- debug_print ("track begin: %p\n", track);
- debug_print ("track end: %p\n", track + track_len);
-
- if (track_len > track_size)
- return midi_error (__FUNCTION__ ": track size corrupt");
-
- pytrack = PyList_New (0);
-
- track_end = *track + track_len;
-
- {
- PyObject *pytime = PyInt_FromLong (0L);
- while (*track < track_end)
- {
- long dt = get_variable_length_number(track, track_end);
- PyObject *pyev = 0;
-
- time += dt;
- if (dt)
- pytime = PyInt_FromLong (time);
-
- pyev = read_event (track, track_end, pytime,
- &running_status);
- if (pyev)
- PyList_Append (pytrack, pyev);
- }
- }
-
- *track = track_end;
- return pytrack;
-}
-
-
-static PyObject *
-pymidi_parse_track (PyObject *self, PyObject *args)
-{
- unsigned char *track, *track_end;
- unsigned long track_size, track_len;
-
- PyObject * sobj = PyTuple_GetItem (args, 0);
-
- debug_print ("%s", "\n");
- if (!PyArg_ParseTuple (args, "s#", &track, &track_size))
- return 0;
-
- if (track_size < 0)
- return midi_error (__FUNCTION__ ": negative track size");
-
- track_end = track + track_size;
-
- return midi_parse_track (&track, track_end);
-}
-
-static PyObject *
-midi_parse (unsigned char **midi,unsigned char *midi_end)
-{
- PyObject *pymidi = 0;
- unsigned long header_len;
- unsigned format, tracks;
- int division;
- int i;
-
- debug_print ("%s", "\n");
-
- /* Header */
- header_len = get_number (midi, *midi + 4, 4);
-
-
- if (header_len < 6)
- return midi_error (__FUNCTION__ ": header too short");
-
- format = get_number (midi, *midi + 2, 2);
- tracks = get_number (midi, *midi + 2, 2);
-
- if (tracks > 32)
- return midi_error (__FUNCTION__ ": too many tracks");
-
- division = get_number (midi, *midi + 2, 2) * 4;
-
-
- if (division < 0)
- /* return midi_error ("can't handle non-metrical time"); */
- ;
- *midi += header_len - 6;
-
- pymidi = PyList_New (0);
-
- /* Tracks */
- for (i = 0; i < tracks; i++)
- PyList_Append (pymidi, midi_parse_track (midi, midi_end));
-
- pymidi = Py_BuildValue ("(OO)", Py_BuildValue ("(ii)", format, division),
- pymidi);
- return pymidi;
-}
-
-static PyObject *
-pymidi_parse (PyObject *self, PyObject *args)
-{
- unsigned char *midi, *midi_end;
- unsigned long midi_size, midi_len;
-
- PyObject *sobj = PyTuple_GetItem (args, 0);
-
- debug_print ("%s", "\n");
- if (!PyArg_ParseTuple (args, "s#", &midi, &midi_size))
- return 0;
-
- if (strcmp (midi, "MThd"))
- return midi_error (__FUNCTION__ ": MThd expected");
-
- midi += 4;
-
- midi_end = midi + midi_size;
-
- return midi_parse (&midi, midi_end);
-}
-
-
-static PyMethodDef MidiMethods[] =
-{
- {"parse", pymidi_parse, 1},
- {"parse_track", pymidi_parse_track, 1},
- {0, 0} /* Sentinel */
-};
-
-initmidi ()
-{
- PyObject *m, *d;
- m = Py_InitModule ("midi", MidiMethods);
- d = PyModule_GetDict (m);
-
- Midi_error = PyString_FromString ("midi.error");
- PyDict_SetItemString (d, "error", Midi_error);
- add_constants (d);
- Midi_warning = PyString_FromString ("midi.warning");
- PyDict_SetItemString (d, "warning", Midi_warning);
-}