3

I have a problem with generate png file from stream object in music21. I read documentation and I use ConverterLilypond to do this.

  chords = stream.Stream()

  d7 = chord.Chord(['D4', 'F4', 'A4', 'C5'])
  dmin7 = chord.Chord(['D4', 'F-4', 'A4', 'C5'])
  dmaj7 = chord.Chord(['D4', 'F#4', 'A4', 'C#5'])
  chords.append(d7)
  chords.append(dmin7)
  chords.append(dmaj7)
  conv = converter.subConverters.ConverterLilypond()
  conv.write(chords, fmt='lilypond', fp='file', subformats=['png'])

This code will generate png file, eps file count file, tex and texi file. Why? How can I generate only one file, PNG file?

When I try run this code in jupyter enough that I would use chords.show() to display image, but normally in the script the show generates a file, not a graphic.

EDIT:

Code with environment var

from music21 import *

us = environment.UserSettings()
us['lilypondPath'] = 'C:/Program Files (x86)/LilyPond/usr/bin/lilypond.exe'
us['musescoreDirectPNGPath'] = 'C:/Program Files (x86)/MuseScore 2/bin/MuseScore.exe'
us['musicxmlPath'] = 'C:/Program Files (x86)/MuseScore 2/bin/MuseScore.exe'

d7 = chord.Chord(['D4', 'F4', 'A4', 'C5'])

stream = stream.Stream()
stream.show('musicxml.png')
lukassz
  • 3,135
  • 7
  • 32
  • 72

2 Answers2

0

The best interface is by calling:

chords.show('lily.png')

which will run everything. But the Lilypond interface is not so strong, so better would be to install MuseScore and set the MusescoreDirectPNGPath in .music21rc to point to the mscore executable and the just call chords.show('musicxml.png') and it will do a much better translation via MusicXML.

  • Thanks for reply. I'm working on Windows where can I find `.music21rc`? I add `us['musescoreDirectPNGPath'] = 'C:/Program Files (x86)/MuseScore 2/bin/MuseScore.exe' us['musicxmlPath'] = 'C:/Program Files (x86)/MuseScore 2/bin/MuseScore.exe'` in my script but this not working. Still the same error `cannot support showing in this format yet: png` – lukassz Apr 21 '18 at 05:37
  • Bad me. Bad me. I should be pointing to the python interface for changing environment.UserSettings(). See http://web.mit.edu/music21/doc/usersGuide/usersGuide_24_environment.html – Michael Scott Asato Cuthbert Apr 21 '18 at 14:30
  • And I think in some versions you should do show(‘musicxml.png’) – Michael Scott Asato Cuthbert Apr 21 '18 at 14:31
  • I do `stream.show('musicxml.png')` but it's show black screen in my default photo viewer. I put my code into question. – lukassz Apr 22 '18 at 09:51
  • `music21.base.Music21ObjectException: cannot support showing in this format yet: lily.png` – Cerin May 10 '20 at 19:12
-1

For all those who will struggle with displaying scores from music21 within Jupyter Notebook on Linux (e.g. Ubuntu), follow these steps:

1) Install MuseScore (https://musescore.org/en/download), e.g. using snappy :

sudo snap install musescore

2) Locate musescore in your filesystem:

whereis musescore

In my case it is "usr/bin/musescore".

3) Set environmental variable to tell music21 how can it locate music21. Do it in your Jupyter Notebook:

environment.set("musescoreDirectPNGPath", "/usr/bin/musescore")

4) Now you can use musescore to render music scores from music21:

bwv295 = corpus.parse('bach/bwv295')
bwv295.show()
  • 1
    OP is asking how to generate a png output. Simply installing Musescore and calling `.show()` doesn't generate a png... – Cerin May 10 '20 at 19:13
  • `environment.set()` is `music21.environment.set()` if you are like me confused where that came from. – fireant Feb 01 '22 at 06:36