aspiers/ly2video

Count-in beats

Opened this issue · 5 comments

Create frames displaying beat before music to count in with via something that creates number of frames ((60/tempo) * fps) for each beat desired.

Working on this, but I don't know any python. Trying to use the generateTitle block as a template for generateBeat. I stripped things down and was hoping this would run, but alas, it does not. Still working on it.

#!/usr/bin/env python

coding=utf-8

import collections
import copy
import os
import re
import shutil
import subprocess
import sys
import urllib
from collections import namedtuple
from distutils.version import StrictVersion
from optparse import OptionParser
from struct import pack

from PIL import Image, ImageDraw, ImageFont
from pyPdf import PdfFileWriter, PdfFileReader
import midi

from pprint import pprint, pformat

DEBUG = False # --debug sets to True

def generateBeat1(beatText, width, height, ttfFile, fps, tempo):
"""
Generates frames with beat number 1.

Params:
- width:        pixel width of frames (and video)
- height:       pixel height of frames (and video)
- ttfFile:      path to TTF file to use for title text
- fps:          frame rate (frames per second) of final video
- tempo:        tempo
"""

# create image of title screen
beatScreen = Image.new("RGB", (width, height), (255,255,255))
# it will draw text on titleScreen
drawer = ImageDraw.Draw(titleScreen)
# save folder for frames
if not os.path.exists("beats"):
    os.mkdir("beats")

totalFrames = int(round(("60" / 100) * 30))
progress("Beats: ly2video will generate approx. %d frames." % totalFrames)

# font for beat 1, args - font type, size
beatFont = ImageFont.truetype(ttfFile, height / 15)

# args - position of left upper corner of rectangle (around text), text, font and color (black)
drawer.text(((width - nameFont.getsize("1")[0]) / 2,
             (height - nameFont.getsize("1")[1]) / 2 - height / 25),
            "1", font=nameFont, fill=(0,0,0))

# generate needed number of frames (= (60/tempo) * fps)
for frameNum in xrange(totalFrames):
    beatScreen.save("frame%d.png" % frameNum)

progress("Beats: Generating title screen has ended. (%d/%d)" %
         (totalFrames, totalFrames))
return 0

generateBeat1

A hack of generate title to create beat 1 image.

def generateTitle(titleText, width, height, ttfFile, fps, titleLength):
"""
Generates frames with name of song and its author.

Params:
- width:        pixel width of frames (and video)
- height:       pixel height of frames (and video)
- ttfFile:      path to TTF file to use for title text
- fps:          frame rate (frames per second) of final video
- tempo:        tempo
"""

# create image of title screen
titleScreen = Image.new("RGB", (width, height), (255,255,255))
# it will draw text on titleScreen
drawer = ImageDraw.Draw(titleScreen)
# save folder for frames
if not os.path.exists("beats"):
    os.mkdir("beats")

totalFrames = int(round((60.000/100.000) * fps))
progress("Beats: ly2video will generate approx. %d frames." % totalFrames)

# font for beat 1, args - font type, size
nameFont = ImageFont.truetype(ttfFile, height / 15)

# args - position of left upper corner of rectangle (around text), text, font and color (black)
drawer.text(((width - nameFont.getsize("1")[0]) / 2,
             (height - nameFont.getsize("1")[1]) / 2 - height / 25),
            "1", font=nameFont, fill=(0,0,0))

# generate needed number of frames (= (60/tempo) * fps)
for frameNum in xrange(totalFrames):
    titleScreen.save(tmpPath("beats", "frame%d.png" % frameNum))

progress("Beats: Generating title screen has ended. (%d/%d)" %
         (totalFrames, totalFrames))
return 0

again, hacking the title font. my best though is to have ly2video pull the time signature to figure out how many beats to have. It would have to be able to recognize compound meters. It would also be good to have a manual override to multiple measures of count-in, as two beats for 2/2, 2/4, 6/8, etc would not be enough.

I think I'm just going to get this working for 4/4 for myself and manually edit the file when I need more/ less beats. If someone could figure out how to cycle this process, inserting the appropriate beat into "beat" variable each time, it should do the trick.

def generateTitle(titleText, width, height, ttfFile, fps, titleLength):

# create image of title screen
titleScreen = Image.new("RGB", (width, height), (255,255,255))
# it will draw text on titleScreen
drawer = ImageDraw.Draw(titleScreen)
# save folder for frames
if not os.path.exists("beats"):
    os.mkdir("beats")

totalFrames = int(round((60.000/100.000) * fps))
progress("Beats: ly2video will generate approx. %d frames for each beat." % totalFrames)

#this variable needs to increase with each cycle of the process.
beat = 1

# font for beat 1, args - font type, size
nameFont = ImageFont.truetype(ttfFile, height / 15)

# args - position of left upper corner of rectangle (around text), text, font and color (black)
drawer.text(((width - nameFont.getsize(beat)[0]) / 2,
             (height - nameFont.getsize(beat)[1]) / 2 - height / 25),
            beat, font=nameFont, fill=(0,0,0))

# generate needed number of frames (= (60/tempo) * fps)
for frameNum in xrange(totalFrames):
    titleScreen.save(tmpPath("beats", "frame%d.png" % (((beat-1)*totalFrames)+frameNum)))

progress("Beats: Generating title screen has ended. (%d/%d)" %
         (totalFrames, totalFrames))
return 0

might be nice to have an option to make the last beat a different color as well (might be more hassle than its worth)

This does the trick for generating the screens; now I need to create a call for it in the main function and cat it together. For now, I think I'll try to make some command line options rather than figure out how to pull data from the .ly file.

def generateTitle(titleText, width, height, ttfFile, fps, titleLength):
"""
Generates frames with name of song and its author.

Params:
- width:        pixel width of frames (and video)
- height:       pixel height of frames (and video)
- ttfFile:      path to TTF file to use for title text
- fps:          frame rate (frames per second) of final video
- tempo:        tempo
"""


# save folder for frames
if not os.path.exists("beats"):
    os.mkdir("beats")

#grab the number of beats
beats = 4

totalFrames = int(round((60.000/100.000) * fps))

for currentBeat in range(beats):

    printBeat = currentBeat + 1 

    # create image of title screen
    titleScreen = Image.new("RGB", (width, height), (255,255,255))
    # it will draw text on titleScreen
    drawer = ImageDraw.Draw(titleScreen)

    progress("Beats: ly2video will generate approx. %d frames." % totalFrames)
    # font for beat args - font type, size
    nameFont = ImageFont.truetype(ttfFile, height / 15)

    # args - position of left upper corner of rectangle (around text), text, font and color (black)
    drawer.text(((width - nameFont.getsize("%d" % printBeat)[0]) / 2,
             (height - nameFont.getsize("%d" % printBeat)[1]) / 2 - height / 25),
            "%d" % printBeat, font=nameFont, fill=(0,0,0))

    # generate needed number of frames (= (60/tempo) * fps)
    for frameNum in xrange(totalFrames):
        titleScreen.save(tmpPath("beats", "frame%d.png" % ((currentBeat*totalFrames)+frameNum)))

    progress("Beats: Generating title screen has ended. (%d/%d)" %
             (totalFrames, totalFrames))


return 0