def __init__(self, payload): from abjad import abjad_configuration if isinstance(payload, str): self._payload = payload else: payload = copy.deepcopy(payload) lilypond_file = iotools.insert_expr_into_lilypond_file(payload) lilypond_file.file_initial_system_comments[:] = [] lilypond_version_token = lilypondfiletools.LilyPondVersionToken( abjad_configuration.get_lilypond_minimum_version_string(), ) lilypond_file.file_initial_system_includes[0] = lilypond_version_token lilypond_format = lilypond_file.lilypond_format self._payload = lilypond_format
def write_expr_to_ly( expr, file_name, print_status=False, tagline=False, docs=False, ): r'''Writes `expr` to `file_name`. :: >>> note = Note("c'4") >>> iotools.write_expr_to_ly(note, '/home/user/foo.ly') # doctest: +SKIP Returns none. ''' from abjad.tools import iotools file_name = os.path.expanduser(file_name) if not file_name.endswith('.ly'): file_name += '.ly' try: outfile = open(file_name, 'w') if docs: expr = documentationtools.make_reference_manual_lilypond_file(expr) lilypond_file = iotools.insert_expr_into_lilypond_file( expr, tagline=tagline) # the following line is necessary for Windows *not* to keep # outfile open after writing; # why this should be the case is, however, a complete mystery. output = lilypond_file.lilypond_format outfile.write(output) outfile.close() except IOError: print 'ERROR: cound not open file %s' % file_name dirname = os.path.dirname(file_name) if dirname: print 'Make sure "%s" exists in your system.' % dirname if print_status: print 'LilyPond file written to %r ...' % os.path.basename(file_name)
def play(expr): r'''Plays `expr`. :: >>> note = Note("c'4") :: >>> iotools.play(note) # doctest: +SKIP This input creates and opens a one-note MIDI file. Abjad outputs MIDI files of the format ``filename.mid`` under Windows. Abjad outputs MIDI files of the format ``filename.midi`` under other operating systems. Returns none. ''' from abjad import abjad_configuration from abjad.tools import iotools ABJADOUTPUT = abjad_configuration['abjad_output'] iotools.verify_output_directory(ABJADOUTPUT) os.chdir(ABJADOUTPUT) name = iotools.get_next_output_file_name() outfile = open(name, 'w') lilypond_file = iotools.insert_expr_into_lilypond_file(expr) lilypond_file.score_block.append(lilypondfiletools.MIDIBlock()) outfile.write(lilypond_file.lilypond_format) outfile.close() iotools.run_lilypond(name, abjad_configuration['lilypond_path']) if os.name == 'nt': extension = 'mid' else: extension = 'midi' midi_player = abjad_configuration['midi_player'] iotools.open_file('%s.%s' % (name[:-3], extension), midi_player)