#!/usr/bin/python
#
# moodbar.py -- Read and visualize "moodbar" files
# Copyright 2007 Thomas Perl <thp@thpinfo.com>
# Initial version: 2007-09-29
#
# This program reads in ".mood" files that have been
# created with the "moodbar" utility from
#
#   http://amarok.kde.org/wiki/Moodbar
#
# The moodbar file is a simple binary file with 
# three bytes per frame: red, green and blue.
#

import sys
import os.path

import pygame
from pygame.locals import *

HEIGHT = 100
fn = sys.argv[-1]

if not os.path.exists( fn) or len(sys.argv) < 2:
    print 'Usage: moodbar.py my-audiofile.mood'
    sys.exit( -1)

def moodbar_from_file( fn):
    fp = open( fn, 'rb')
    result = []
    s = fp.read( 3)
    while s:
        result.append( ( ord( s[0]), ord( s[1]), ord( s[2])))
        s = fp.read( 3)
    fp.close()
    return result

bars = moodbar_from_file( fn)

pygame.init()

pygame.display.set_mode( (len(bars), HEIGHT))
pygame.display.set_caption('Moodbar: %s' % os.path.basename( fn))
screen = pygame.display.get_surface()

for x in range( len(bars)):
    pygame.draw.line( screen, bars[x], (x,0),(x,HEIGHT))

pygame.display.flip()

while True:
    for event in pygame.event.get():
        if event.type == QUIT:
            sys.exit( 0)

