midililypond

Create MIDI files from Lilypond snippet


This is not a duplicate question on how to create a MIDI file from Lilypond. I have done that many times.

I have created some lilypond snippets, and I want to extract MIDI from them. Normally I would do that in the \score block like this:

\score {
  \music
  \layout { }
  \midi { }
}

But, my snippet does not contain a \score block; it is written like this:

\version "2.18.2"
\include "lilypond-book-preamble.ly"

\paper {
  indent = 2\mm
  line-width = 210\mm
}

\layout {
  indent = #0
  \context {
    \Score
    \remove "Bar_number_engraver"
  }
}

\relative c'
{
   \tempo 4 = 60
   \clef treble \key d \major g8^\markup { C } g8 b4   b8 b8 d4   cis8 cis8 e4  b8 b8 d4
}

This code creates a perfect PDF snippet. But I don't know where to put the \midi { } block to create a MIDI file.


Solution

  • I think the easiest solution is to assign your music to a variable and create a \score block. Is there a reason why you are avoiding to use a \score block in your example? For instance, the code below generates both MIDI and pdf files:

    \version "2.18.2"
    \include "lilypond-book-preamble.ly"
    
    \paper {
      indent = 2\mm
      line-width = 210\mm
    }
    
    \layout {
      indent = #0
      \context {
        \Score
        \remove "Bar_number_engraver"
      }
    }
    
    music = \relative c'
    {
       \tempo 4 = 60
       \clef treble \key d \major g8^\markup { C } g8 b4   b8 b8 d4   cis8 cis8 e4  b8 b8 d4
    }
    
    \score{
      \music
      \layout{}
      \midi{}
    }