#!/usr/bin/env python

"""
Simple script which plays a wave file through a plugin, randomizing parameters
"""

import time
import nosuch.vstutil
import sys
import random
from nosuch.vstutil import *

print "argv len is ",len(sys.argv)
if len(sys.argv) > 1:
	vstdir = sys.argv[1]
else:
	vstdir = "."

print PaGetInfo(output=0)

def vstinfo(v1,doparams=0):
	print "Is VST2 = ",v1.is_vst2()
	print "Is synth = ",v1.is_synth()
	print "numParams = ",v1.numParams()
	print "numInputs = ",v1.numInputs()
	print "numOutputs = ",v1.numOutputs()
	print "can_receive_midi = ",v1.can_receive_midi()
	print "can_send_midi = ",v1.can_send_midi()
	print "product = ",v1.product_string()
	print "can_midi_program_names = ",v1.can_midi_program_names()
	print "numCategories = ",v1.num_categories()
	print "numPrograms = ",v1.numPrograms()
	for i in range(v1.numPrograms()):
		print "program 0,",i," = ",v1.program_name(i,0)
	if doparams:
		for i in range(v1.numParams()):
			print "param",i," name = ",v1.param_name(i)
			print "param",i," label = ",v1.param_label(i)
			print "param",i," display = ",v1.param_display(i)
			print "param",i," isswitch = ",v1.param_properties_isswitch(i)
			print "value",i," value = ",v1.get_param(i)
			v1.set_param(i,1.0)
			print "value",i," 1.0 display = ",v1.param_display(i)
			v1.set_param(i,0.0)
			print "value",i," 0.0 display = ",v1.param_display(i)

def vstrandparam(p):
	for i in range(p.numParams()):
		f = random.random()
		p.set_param(i,f)

a = PaAudioDevice()   # do this first, to get sample rate/blocksize

ring = VstPlugin(dll=vstdir+"\\BJ Ringmodulator.dll")
vstinfo(ring,doparams=0)

sound1 = AudioSnippet(filename="winsound1.wav")
print("sound1 duration = ",sound1.duration())

loop1 = AudioLoop(sound1,loops=-1)   # -1 means loop forever

a.open()
a.start()

ring.setinput(loop1)
a.attach(ring)

for i in range(100):
	vstrandparam(ring)
	time.sleep(1.0)

print("Before sleep")
time.sleep(6)
print("After sleep")

a.stop()
a.close()

sys.exit(0)

