Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Count-in beats #45

Open
kalegood opened this issue Feb 14, 2013 · 5 comments
Open

Count-in beats #45

kalegood opened this issue Feb 14, 2013 · 5 comments
Milestone

Comments

@kalegood
Copy link

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

@kalegood
Copy link
Author

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

@kalegood kalegood reopened this Feb 14, 2013
@kalegood
Copy link
Author

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

@kalegood
Copy link
Author

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

@kalegood
Copy link
Author

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

@kalegood
Copy link
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant