#!/usr/bin/env ruby

require 'curses'
require './TrueLegacyGraphicsCairo'

include Math

# $ ffmpeg -y -r 24 -i metronome.d/%06d.png -i midi/metronome.wav -vcodec libx264 -profile baseline -s 480x272 -r 24 -b 600k -acodec libfaac -ac 2 -ar 48000 -ab 125k metronome120_psp.mp4

Dir.mkdir(path = 'metronome.d/') rescue true

tempo = 120										# tempo
fpt = (fps = 24) * 60 / tempo					# Frame Par Turn

cx = 360; w1 =  30 / 2; w2 = 120 / 2			# 板の仕様
cy = 136; h1 = 200 / 2; h2 =  60; w3 = w2 - (w2 - w1) * h2 / (h1 * 2.0)
ax = cx; ay = cy + 60; al = 140					# 腕の仕様

ps1 = { :font_family => 'default.bmf', :tx => 1, :ty => 1, :layout_width => 120, :layout_alignment => 'right' }
ps2 = { :font_family => 'default.bmf', :tx => 2, :ty => 2, :layout_width => 120, :layout_alignment => 'right' }

(1602 * fps).times {|n|
#	n > 36 and break							# for DEBUG
	print(n % (10 * fps) == 0 ? n / fps : '.')

	# PSP - 480 x 272 pixel
	win = LegacyGraphics.new(0, 0, 0, 0, 272, 480, 16, 8, 0, { :file => path + '/%06d' % n, :type => 'png' })	# png/pdf/svg/ps

	# 奥板
	win.polygon([[cx - w2, cy + h1], [cx + w2, cy + h1], [cx + w1, cy - h1     ], [cx - w1, cy - h1     ]], 2, 'f')	# 左下->右下->右上->左上

	# 腕
	ap = cos(rad(n * 180 / fpt)); ar = rad(90 + ap * 30)
	win.line(ax, ay, ax + cos(ar) * al, ay - sin(ar) * al, 7, '', 10)

	# 手前板
	win.polygon([[cx - w2, cy + h1], [cx + w2, cy + h1], [cx + w3, cy + h1 - h2], [cx - w3, cy + h1 - h2]], 6, 'f')

	# 文字情報
	ty = 16
	win.text(80, ty, '%6d' % [n / fpt], 7, ps2); ty += 48
	(5..20).each {|xn|
		xn != 10 and (	win.text( 0, ty, 'X %3.1f:'	% [xn / 10.0],			7, ps1);
						win.text(80, ty, '%3d'		% [tempo * xn / 10],	7, ps1); ty += 8 + 4)
		xn == 10 and (	win.text( 0, ty, 'TEMPO:',							7, ps2);
						win.text(80, ty, '%3d'		% [tempo],				7, ps2); ty += 16 + 4)
	}
	win.text(360, 248, '%d:%02d ' % [(it = n / fps) / 60, it % 60], 7, ps2)

	win.refresh									# ページ生成(pdf/ps)
	win.close
}

__END__

