This file is indexed.

/usr/share/emacs/site-lisp/lilypond-song.el is in lilypond-data 2.14.2-4.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
;;;; lilypond-song.el --- Emacs support for LilyPond singing
;;;;
;;;; This file is part of LilyPond, the GNU music typesetter.
;;;;
;;;; Copyright (C) 2006 Brailcom, o.p.s.
;;;; Author: Milan Zamazal <pdm@brailcom.org>
;;;;
;;;; LilyPond is free software: you can redistribute it and/or modify
;;;; it under the terms of the GNU General Public License as published by
;;;; the Free Software Foundation, either version 3 of the License, or
;;;; (at your option) any later version.
;;;;
;;;; LilyPond is distributed in the hope that it will be useful,
;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;;;; GNU General Public License for more details.
;;;;
;;;; You should have received a copy of the GNU General Public License
;;;; along with LilyPond.  If not, see <http://www.gnu.org/licenses/>.

;;; Commentary:

;; This file adds Emacs support for singing lyrics of LilyPond files.
;; It extends lilypond-mode with the following commands (see their
;; documentation for more information):
;; 
;; - M-x LilyPond-command-sing (C-c C-a)
;; - M-x LilyPond-command-sing-and-play (C-c C-q)
;; - M-x LilyPond-command-sing-last (C-c C-z)
;; 
;; Note these commands are not available from the standard LilyPond mode
;; command menus.

;;; Code:


(require 'cl)
(require 'lilypond-mode)

(ignore-errors (require 'ecasound))


;;; User options


(defcustom LilyPond-synthesize-command "lilysong"
  "Command used to sing LilyPond files."
  :group 'LilyPond
  :type 'string)

(defcustom LilyPond-play-command (or (executable-find "ecaplay") "play")
  "Command used to play WAV files."
  :group 'LilyPond
  :type 'string)

;; In case you would like to use fluidsynth (not recommended as fluidsynth
;; can perform wave file synthesis only in real time), you can use the
;; following setting:
;; (setq LilyPond-midi->wav-command "fluidsynth -nil -a file soundfont.sf2 '%s' && sox -t raw -s -r 44100 -w -c 2 fluidsynth.raw '%t'")
(defcustom LilyPond-midi->wav-command "timidity -Ow %m -s %r -o '%t' '%s'"
  "Command used to make a WAV file from a MIDI file.
%s in the string is replaced with the source MIDI file name,
%t is replaced with the target WAV file name.
%r is replaced with rate.
%m is replaced with lilymidi call."
  :group 'LilyPond
  :type 'string)

(defcustom LilyPond-voice-rates
  '((".*czech.*" . 44100)
    (".*\\<fi\\(\\>\\|nnish\\).*" . 22050)
    (".*" . 16000))
  "Alist of regexps matching voices and the corresponding voice rates.
It may be necessary to define proper voice rates here in order to
avoid ecasound resampling problems."
  :group 'LilyPond
  :type '(alist :key-type regexp :value-type integer))

(defcustom LilyPond-use-ecasound (and (featurep 'ecasound)
                                      (executable-find "ecasound")
                                      t)
  "If non-nil, use ecasound for mixing and playing songs."
  :group 'LilyPond
  :type 'boolean)

(defcustom LilyPond-voice-track-regexp "voice"
  "Perl regexp matching names of MIDI tracks to be ignored on sing&play."
  :group 'LilyPond
  :type 'string)

(defcustom LilyPond-lilymidi-command "\"`lilymidi --prefix-tracks -Q --filter-tracks '%s' '%f'`\""
  "Command to insert into LilyPond-midi->wav-command calls.
%f is replaced with the corresponding MIDI file name.
%s is replaced with `LilyPond-voice-track-regexp'."
  :group 'LilyPond
  :type 'string)


;;; Lyrics language handling


(defvar lilysong-language nil)
(make-variable-buffer-local 'lilysong-language)

(defvar lilysong-last-language nil)
(make-variable-buffer-local 'lilysong-last-language)

(defvar lilysong-languages '("cs" "en"))

(defvar lilysong-voices nil)

(defun lilysong-voices ()
  (or lilysong-voices
      (with-temp-buffer
        (call-process "lilysong" nil t nil "--list-voices")
        (call-process "lilysong" nil t nil "--list-languages")
        (goto-char (point-min))
        (while (not (eobp))
          (push (buffer-substring-no-properties
                 (line-beginning-position) (line-end-position))
                lilysong-voices)
          (forward-line))
        lilysong-voices)))
  
(defun lilysong-change-language ()
  "Change synthesis language or voice of the current document."
  (interactive)
  (setq lilysong-language
        (completing-read "Lyrics language or voice: "
                         (mapcar 'list (lilysong-voices)))))

(defun lilysong-update-language ()
  (unless lilysong-language
    (lilysong-change-language)))


;;; Looking for \festival* and \midi commands


(defun lilysong-document-files ()
  (let ((resulting-files ())
        (stack (list (LilyPond-get-master-file))))
    (while (not (null stack))
      (let ((file (expand-file-name (pop stack))))
        (when (and (file-exists-p file)
                   (not (member file resulting-files)))
          (push file resulting-files)
          (save-excursion
            (save-restriction
              (set-buffer (find-file-noselect file nil))
              (widen)
              (goto-char (point-min))
              (while (re-search-forward "^[^%\n]*\\\\include +\"\\([^\"]+\\)\"" nil t)
                (push (match-string 1) stack)))))))
    (nreverse resulting-files)))
     
(defvar lilysong-festival-command-regexp
  "^[^%\n]*\\\\festival\\(syl\\)? +#\"\\([^\"]+\\)\"")

(defun lilysong-find-song (direction)
  "Find XML file name of the nearest Festival command in the given DIRECTION.
DIRECTION is one of the symbols `forward' or `backward'.
If no Festival command is found in the current buffer, return nil.
The point is left at the position where the command occurrence was found."
  (save-match-data
    (when (funcall (if (eq direction 'backward)
                       're-search-backward
                     're-search-forward)
                   lilysong-festival-command-regexp nil t)
      (match-string-no-properties 2))))

(defun lilysong-current-song ()
  "Return the XML file name corresponding to the song around current point.
If there is none, return nil."
  (save-excursion
    (or (progn (end-of-line) (lilysong-find-song 'backward))
        (progn (beginning-of-line) (lilysong-find-song 'forward)))))

(defun lilysong-all-songs (&optional limit-to-region)
  "Return list of XML file names of the song commands in the current buffer.
If there are none, return an empty list.
If LIMIT-TO-REGION is non-nil, look for the commands in the current region
only."
  (let ((result '())
        (current nil))
    (save-excursion
      (save-restriction
        (when limit-to-region
          (narrow-to-region (or (mark) (point)) (point)))
        (goto-char (point-min))
        (while (setq current (lilysong-find-song 'forward))
          (push current result))))
    (nreverse result)))

(defun lilysong-walk-files (collector)
  (save-excursion
    (mapcar (lambda (f)
              (set-buffer (find-file-noselect f))
              (funcall collector))
            (lilysong-document-files))))

(defun lilysong-all-songs* ()
  "Return list of XML file names of the song commands in the current document."
  (remove-duplicates (apply #'append (lilysong-walk-files #'lilysong-all-songs))
                     :test #'equal))

(defvar lilysong-song-history nil)
(make-variable-buffer-local 'lilysong-song-history)

(defvar lilysong-last-song-list nil)
(make-variable-buffer-local 'lilysong-last-song-list)

(defvar lilysong-last-command-args nil)
(make-variable-buffer-local 'lilysong-last-command-args)

(defun lilysong-song-list (multi)
  (cond
   ((eq multi 'all)
    (lilysong-all-songs*))
   (multi
    (lilysong-select-songs))
   (t
    (lilysong-select-single-song))))

(defun lilysong-select-single-song ()
  (let ((song (lilysong-current-song)))
    (if song
        (list song)
      (error "No song found"))))

(defun lilysong-select-songs ()
  (let* ((all-songs (lilysong-all-songs*))
         (available-songs all-songs)
         (initial-songs (if (or (not lilysong-last-song-list)
                                (eq LilyPond-command-current
                                    'LilyPond-command-region))
                            (lilysong-all-songs t)
                          lilysong-last-song-list))
         (last-input (completing-read
                      (format "Sing file%s: "
                              (if initial-songs
                                  (format " (default `%s')"
                                          (mapconcat 'identity initial-songs
                                                     ", "))
                                ""))
                      (mapcar 'list all-songs)
                      nil t nil
                      'lilysong-song-history)))
    (if (equal last-input "")
        initial-songs
      (let ((song-list '())
            default-input)
        (while (not (equal last-input ""))
          (push last-input song-list)
          (setq default-input (second (member last-input available-songs)))
          (setq available-songs (remove last-input available-songs))
          (setq last-input (completing-read "Sing file: "
                                            (mapcar #'list available-songs)
                                            nil t default-input
                                            'lilysong-song-history)))
        (setq lilysong-last-song-list (nreverse song-list))))))

(defun lilysong-count-midi-words ()
  (count-rexp (point-min) (point-max) "^[^%]*\\\\midi"))

(defun lilysong-midi-list (multi)
  (if multi
      (let ((basename (file-name-sans-extension (buffer-file-name)))
            (count (apply #'+ (save-match-data
                                (lilysong-walk-files #'lilysong-count-midi-words))))
            (midi-files '()))
        (while (> count 0)
          (setq count (1- count))
          (if (= count 0)
              (push (concat basename ".midi") midi-files)
            (push (format "%s-%d.midi" basename count) midi-files)))
        midi-files)
    (list (LilyPond-string-current-midi))))


;;; Compilation


(defun lilysong-file->wav (filename &optional extension)
  (format "%s.%s" (save-match-data
                    (if (string-match "\\.midi$" filename)
                        filename
                      (file-name-sans-extension filename)))
          (or extension "wav")))

(defun lilysong-file->ewf (filename)
  (lilysong-file->wav filename "ewf"))

(defstruct lilysong-compilation-data
  command
  makefile
  buffer
  songs
  midi
  in-parallel)
(defvar lilysong-compilation-data nil)
(defun lilysong-sing (songs &optional midi-files in-parallel)
  (setq lilysong-last-command-args (list songs midi-files in-parallel))
  (lilysong-update-language)
  (add-to-list 'compilation-finish-functions 'lilysong-after-compilation)
  (setq songs (mapcar #'expand-file-name songs))
  (let* ((makefile (lilysong-makefile (current-buffer) songs midi-files))
         (command (format "make -f %s" makefile)))
    (setq lilysong-compilation-data
          (make-lilysong-compilation-data
           :command command
           :makefile makefile
           :buffer (current-buffer)
           :songs songs
           :midi midi-files
           :in-parallel in-parallel))
    (save-some-buffers (not compilation-ask-about-save))
    (unless (equal lilysong-language lilysong-last-language)
      (mapc #'(lambda (f) (when (file-exists-p f) (delete-file f)))
            (append songs (mapcar 'lilysong-file->wav midi-files))))
    (if (lilysong-up-to-date-p makefile)
        (lilysong-process-generated-files lilysong-compilation-data)
      (compile command))))

(defun lilysong-up-to-date-p (makefile)
  (equal (call-process "make" nil nil nil "-f" makefile "-q") 0))

(defun lilysong-makefile (buffer songs midi-files)
  (let ((temp-file (make-temp-file "Makefile.lilysong-el"))
        (language lilysong-language))
    (with-temp-file temp-file
      (let ((source-files (save-excursion
                            (set-buffer buffer)
                            (lilysong-document-files)))
            (master-file (save-excursion
                           (set-buffer buffer)
                           (LilyPond-get-master-file)))
            (lilyfiles (append songs midi-files)))
        (insert "all:")
        (dolist (f (mapcar 'lilysong-file->wav (append songs midi-files)))
          (insert " " f))
        (insert "\n")
        (when lilyfiles
          (dolist (f songs)
            (insert f " "))
          (when midi-files
            (dolist (f midi-files)
              (insert f " ")))
          (insert ": " master-file "\n")
          (insert "\t" LilyPond-lilypond-command " " master-file "\n")
          (dolist (f songs)
            (insert (lilysong-file->wav f) ": " f "\n")
            (insert "\t" LilyPond-synthesize-command " $< " (or language "") "\n"))
          ;; We can't use midi files in ecasound directly, because setpos
          ;; doesn't work on them.
          (let ((lilymidi LilyPond-lilymidi-command)
                (voice-rate (format "%d" (or (cdr (assoc-if (lambda (key) (string-match key language))
                                                            LilyPond-voice-rates))
                                             16000))))
            (when (string-match "%s" lilymidi)
              (setq lilymidi (replace-match LilyPond-voice-track-regexp nil nil lilymidi)))
            (dolist (f midi-files)
              (insert (lilysong-file->wav f) ": " f "\n")
              (let ((command LilyPond-midi->wav-command)
                    (lilymidi* lilymidi))
                (when (string-match "%s" command)
                  (setq command (replace-match f nil nil command)))
                (when (string-match "%t" command)
                  (setq command (replace-match (lilysong-file->wav f) nil nil command)))
                (when (string-match "%r" command)
                  (setq command (replace-match voice-rate nil nil command)))
                (when (string-match "%f" lilymidi*)
                  (setq lilymidi (replace-match f nil nil lilymidi*)))
                (when (string-match "%m" command)
                  (setq command (replace-match lilymidi nil nil command)))
                (insert "\t" command "\n")))
            ))))
    temp-file))

(defun lilysong-after-compilation (buffer message)
  (let ((data lilysong-compilation-data))
    (when (and data
               (equal compile-command
                      (lilysong-compilation-data-command data)))
      (unwind-protect
          (when (lilysong-up-to-date-p (lilysong-compilation-data-makefile data))
            (lilysong-process-generated-files data))
        (delete-file (lilysong-compilation-data-makefile data))))))

(defun lilysong-process-generated-files (data)
  (with-current-buffer (lilysong-compilation-data-buffer data)
    (setq lilysong-last-language lilysong-language))
  (lilysong-play-files (lilysong-compilation-data-in-parallel data)
                       (lilysong-compilation-data-songs data)
                       (lilysong-compilation-data-midi data)))


;;; Playing files


(defun lilysong-play-files (in-parallel songs midi-files)
  (funcall (if LilyPond-use-ecasound
               'lilysong-play-with-ecasound
             'lilysong-play-with-play)
           in-parallel songs midi-files))

(defun lilysong-call-play (files)
  (apply 'start-process "lilysong-el" nil LilyPond-play-command files))

(defun lilysong-play-with-play (in-parallel songs midi-files)
  (let ((files (mapcar 'lilysong-file->wav (append songs midi-files))))
    (if in-parallel
        (dolist (f files)
          (lilysong-call-play (list f)))
      (lilysong-call-play files))))

(defun lilysong-make-ewf-files (files)
  (let ((offset 0.0))
    (dolist (f files)
      (let* ((wav-file (lilysong-file->wav f))
             (length (with-temp-buffer
                       (call-process "ecalength" nil t nil "-s" wav-file)
                       (goto-char (point-max))
                       (forward-line -1)
                       (read (current-buffer)))))
        (with-temp-file (lilysong-file->ewf f)
          (insert "source = " wav-file "\n")
          (insert (format "offset = %s\n" offset))
          (insert "start-position = 0.0\n")
          (insert (format "length = %s\n" length))
          (insert "looping = false\n"))
        (setq offset (+ offset length))))))

(when (and (featurep 'ecasound)
           (not (fboundp 'eci-cs-set-param)))
  (defeci cs-set-param ((parameter "sChainsetup option: " "%s"))))

(defun lilysong-play-with-ecasound (in-parallel songs midi-files)
  (ecasound)
  (eci-cs-add "lilysong-el")
  (eci-cs-select "lilysong-el")
  (eci-cs-remove)
  (eci-cs-add "lilysong-el")
  (eci-cs-select "lilysong-el")
  (eci-cs-set-param "-z:mixmode,sum")
  (unless in-parallel
    (lilysong-make-ewf-files songs)
    ;; MIDI files should actually start with each of the songs
    (mapc 'lilysong-make-ewf-files (mapcar 'list midi-files)))
  (let* ((file->wav (if in-parallel 'lilysong-file->wav 'lilysong-file->ewf))
         (files (mapcar file->wav (append songs midi-files))))
    (dolist (f files)
      (eci-c-add f)
      (eci-c-select f)
      (eci-ai-add f))
    (eci-c-select-all)
    (eci-ao-add-default)
    (let* ((n (length songs))
           (right (if (<= n 1) 50 0))
           (step (if (<= n 1) 0 (/ 100.0 (1- n)))))
      (dolist (f songs)
        (let ((chain (funcall file->wav f)))
          (eci-c-select chain)
          (eci-cop-add "-erc:1,2")
          (eci-cop-add (format "-epp:%f" (min right 100)))
          (incf right step))))
    (eci-start)))


;;; User commands


(defun lilysong-arg->multi (arg)
  (cond
   ((not arg)
    nil)
   ((or
     (numberp arg)
     (equal arg '(4)))
    t)
   (t
    'all)))

(defun lilysong-command (arg play-midi?)
  (let* ((multi (lilysong-arg->multi arg))
         (song-list (lilysong-song-list multi))
         (midi-list (if play-midi? (lilysong-midi-list multi))))
    (message "Singing %s" (mapconcat 'identity song-list ", "))
    (lilysong-sing song-list midi-list (if play-midi? t (listp arg)))))

(defun LilyPond-command-sing (&optional arg)
  "Sing lyrics of the current LilyPond buffer.
Without any prefix argument, sing current \\festival* command.
With the universal prefix argument, ask which parts to sing.
With a double universal prefix argument, sing all the parts.
With a numeric prefix argument, ask which parts to sing and sing them
sequentially rather than in parallel."
  (interactive "P")
  (lilysong-command arg nil))

(defun LilyPond-command-sing-and-play (&optional arg)
  "Sing lyrics and play midi of the current LilyPond buffer.
Without any prefix argument, sing and play current \\festival* and \\midi
commands.
With the universal prefix argument, ask which parts to sing and play.
With a double universal prefix argument, sing and play all the parts."
  (interactive "P")
  (lilysong-command arg t))

(defun LilyPond-command-sing-last ()
  "Repeat last LilyPond singing command."
  (interactive)
  (if lilysong-last-command-args
      (apply 'lilysong-sing lilysong-last-command-args)
    (error "No previous singing command")))

(defun LilyPond-command-clean ()
  "Remove generated *.xml and *.wav files used for singing."
  (interactive)
  (flet ((delete-file* (file)
           (when (file-exists-p file)
             (delete-file file))))
    (dolist (xml-file (lilysong-song-list 'all))
      (delete-file* xml-file)
      (delete-file* (lilysong-file->wav xml-file)))
    (mapc 'delete-file* (mapcar 'lilysong-file->wav (lilysong-midi-list 'all)))))

(define-key LilyPond-mode-map "\C-c\C-a" 'LilyPond-command-sing)
(define-key LilyPond-mode-map "\C-c\C-q" 'LilyPond-command-sing-and-play)
(define-key LilyPond-mode-map "\C-c\C-x" 'LilyPond-command-clean)
(define-key LilyPond-mode-map "\C-c\C-z" 'LilyPond-command-sing-last)

(easy-menu-add-item LilyPond-command-menu nil
  ["Sing Current" LilyPond-command-sing t])
(easy-menu-add-item LilyPond-command-menu nil
  ["Sing Selected" (LilyPond-command-sing '(4)) t])
(easy-menu-add-item LilyPond-command-menu nil
  ["Sing All" (LilyPond-command-sing '(16)) t])
(easy-menu-add-item LilyPond-command-menu nil
  ["Sing Selected Sequentially" (LilyPond-command-sing 1) t])
(easy-menu-add-item LilyPond-command-menu nil
  ["Sing and Play Current" LilyPond-command-sing-and-play t])
(easy-menu-add-item LilyPond-command-menu nil
  ["Sing and Play Selected" (LilyPond-command-sing-and-play '(4)) t])
(easy-menu-add-item LilyPond-command-menu nil
  ["Sing and Play All" (LilyPond-command-sing-and-play '(16)) t])
(easy-menu-add-item LilyPond-command-menu nil
  ["Sing Last" LilyPond-command-sing-last t])


;;; Announce

(provide 'lilypond-song)


;;; lilypond-song.el ends here