#!/usr/bin/env python

import bootstrap
import sys
import openpluginlib
import openimagelib
import openmedialib
import common

def open_clips( clips ):
	inputs = []
	for clip in clips:
		input = common.input_wrapper( openmedialib.create_input( clip[ 0 ] ) )
		input.properties( ).get_property( "frequency" ).set( 44100 )
		input.properties( ).get_property( "fps_num" ).set( 25 )
		input.properties( ).get_property( "fps_den" ).set( 1 )
		input.set_process_flags( openmedialib.process_flags.audio )
		inputs.append( input )
	return inputs

openpluginlib.init( "" )

if len( sys.argv ) > 1:
	sys.argv.pop( 0 )
	openal = None
	clips = common.parse_args( sys.argv )
	inputs = open_clips( clips )
	input = inputs.pop( 0 )
	try:
		i = 0
		while i < input.get_frames( ):
			input.seek( i, False )
			frame = input.fetch( )
			audio = frame.get_audio( )
			for other in inputs:
				other.seek( i, False )
				audio = openmedialib.audio_mix( audio, other.fetch( ).get_audio( ) )
			frame.set_audio( audio )
			if openal is None:
				openal = openmedialib.create_store( "openal:", frame )
				if openal is not None: openal.init( )
			openal.push( frame )
			i += 1
	except KeyboardInterrupt:
		pass
