aboutsummaryrefslogtreecommitdiff
path: root/bpe.py
blob: 7a54b2c006e65d5673dac4baf1a5b18660714916 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# $Id: bpe.py,v 1.2 2005-12-17 17:52:01 timblech Exp $
#
# Copyright (C) 2005  Tim Blechmann
# 
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program; see the file COPYING.  If not, write to
# the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
# Boston, MA 02111-1307, USA.


import pyext

def parse_arguments(*args):
	duration = args[0]
	if duration == 0:
		return args[1:]
	
	length = len(args)
	durations = []
	breakpoints = []
	for i in range(1,length):
		if i % 2 == 0:
			durations.append(args[i])
		else:
			breakpoints.append(args[i])
	duration_factor = duration / reduce(lambda x,y: x + y, durations)
	
	durations = map(lambda x: x*duration_factor, durations)

	ret = []

	for i in range(len(durations)):
		ret.append(breakpoints[i])
		ret.append(durations[i])
	ret[-2] = ret[0]
	return ret
	
class Bpe(pyext._class):
	_inlets = 1
	_outlets = 1

	def list_1(self, *args):
		if args[0] != 0:
			self._outlet(1,parse_arguments(*args))

class VolBpe(pyext._class):
	_inlets = 3
	_outlets = 1

	def __init__(self, dollar1):
		try:
			self.duration = 1000 / dollar1
		except:
			self.duration = 1000
		self._detach = 1
		self._priority(-20)

		self.bpe = []
		self.running = False
		
	def list_1(self, *args):
		arglist = list(args)
		arguments = [self.duration] + arglist
		self.bpe = arglist
		self._outlet(1,parse_arguments(*arguments))
		if not self.running:
			self._outlet(1, None)
			self.running = True
			
	def float_2(self, freq):
		self.duration = 1000 / freq;
		arguments = self.duration + self.bpe
		self._outlet(1,parse_arguments(*arguments))

	
	def float_3(self, switch):
		if switch == 0:
			self._outlet(1, self.bpe[0], 0, self.bpe[0], self.duration)
		else:
			self._outlet(1, self.bpe)