Permalink
Browse files

Improved csd.sco.preprocessor.value_to_carry().

  • Loading branch information...
1 parent 540ed72 commit c0c4fd43914e42283b21e735cba6e97b4a48879d @jacobjoaquin committed Jul 28, 2009
@@ -18,7 +18,7 @@
'''Preprocessor functions.'''
from csd.sco import event
-#from csd.sco import element
+from csd.sco import element
#from csd.sco import selection
def carry_to_value(score):
@@ -27,7 +27,16 @@ def carry_to_value(score):
pass
def value_to_carry(score):
- '''Replaces subsequent repeated values with a carry (.)'''
+ '''Replaces subsequent repeated values with a carry (.)
+
+ Identical expressions do no carry, as a carry only copies the first
+ value output from an expression. This breaks the form when multiple
+ random evaluations are part of the score.
+
+ Macros do no carry as they may contain expressions.
+
+ No-carrys are not carried.
+ '''
event_list = score.splitlines(True)
last_identifier = None
@@ -36,29 +45,48 @@ def value_to_carry(score):
# Explicitly state pfield 3 instead a magic number. Carry
# statements only substitute for pfields 3 or higher.
- pfield_3 = 3
+ PFIELD_3 = 3
+
+ size = 0
+
+ # Excluded element token types
+ elements = [element.EXPRESSION, element.MACRO, element.NO_CARRY];
for e in event_list:
if event.match(e, {0: 'i', 1: last_identifier}):
- for i in range(pfield_3, event.number_of_pfields(e)):
- if event.match(e, {i: last_values[i]}):
+ lv = len(last_values)
+
+ for i in range(PFIELD_3, max(event.number_of_pfields(e), lv)):
+ this_pfield = event.get(e, i)
+
+ if element.token_type(this_pfield) == element.NO_CARRY:
+ last_values[i] = this_pfield
+ break
+
+ elif element.token_type(last_values[i]) == element.NO_CARRY:
+ break
+
+ elif (this_pfield == last_values[i] and
+ element.token_type(this_pfield) not in elements):
+
+ # Replace pfield with carry
e = event.set(e, i, '.')
- else:
- last_values[i] = event.get(e, i)
+ elif this_pfield == None:
+ # Add a carry if one does not exist
+ e = event.push(e, '.')
+
+ else:
+ last_values[i] = this_pfield
+
output.append(e)
-# elif event.get(e, 0) == None:
-# output.append(e)
-
else:
-# last_identifier = ''
last_identifier = event.get(e, 1)
-# last_values = []
last_values = event.get_pfield_list(e)
output.append(e)
return ''.join(output)
-
+
@@ -0,0 +1,50 @@
+#!/usr/bin/env csound -g -odac
+
+Tests if i-events auto carry without the carry preprocessor.
+
+Conclusion:
+
+When not using the no-carry, carry is implicit for everything except
+for a blank i-event, i.e., 'i ; nothing else'. And, pfield 2 requires
+at least one '+' in order to carry.
+
+<CsoundSynthesizer>
+<CsInstruments>
+sr = 44100
+kr = 4410
+ksmps = 10
+nchnls = 1
+
+0dbfs = 1.0
+
+instr 1
+ iamp = p4
+ ifreq = p5
+
+ aenv line iamp, p3, 0
+ asig oscil aenv, ifreq, 1
+ out asig
+endin
+
+</CsInstruments>
+<CsScore>
+f 1 0 8192 10 1
+
+i 1 0 1 1 440
+i 1 + . . .
+i 1 . . .
+i 1 . .
+i 1 .
+i 1
+i .
+i .
+;i ; breaks
+s
+
+i 1 0 1 1 880
+i . + ; At least on '+' required to implicitly carry
+i .
+
+</CsScore>
+</CsoundSynthesizer>
+
@@ -0,0 +1,42 @@
+#!/usr/bin/env csound -g -odac
+
+Tests if the blank i-event in the Csound manual really does what is
+advertised.
+
+ i1 0 .5 100
+ i . +
+ i
+
+Should translate to this:
+
+ i1 0 .5 100
+ i1 .5 .5 100
+ i1 1 .5 100
+
+Conclusion:
+
+Just sent this to the list and waiting for a response. However, it
+seems that the translation does not match what is written in the
+manual.
+
+
+<CsoundSynthesizer>
+<CsInstruments>
+sr = 44100
+kr = 4410
+ksmps = 10
+nchnls = 1
+
+instr 1
+ ; do nothing
+endin
+
+</CsInstruments>
+<CsScore>
+i1 0 .5 100
+i . +
+i
+
+</CsScore>
+</CsoundSynthesizer>
+
@@ -88,8 +88,84 @@ def test(n, score, expect):
'''
expect = '''\
i 1 2 $foo
+i 1 2 $foo
+i 1 2 $foo
+'''
+print test(4, score, expect)
+
+score = '''\
+i 1 2 [~ * 440 + 440]
+i 1 2 [~ * 440 + 440]
+'''
+expect = '''\
+i 1 2 [~ * 440 + 440]
+i 1 2 [~ * 440 + 440]
+'''
+print test(5, score, expect)
+
+score = '''\
+i 1 2 3
+i 1 2
+i 1 2
+'''
+expect = '''\
+i 1 2 3
i 1 2 .
i 1 2 .
'''
-print test(4, score, expect)
+print test(6, score, expect)
+
+score = '''\
+i 1 2 3
+i 1 2 !
+i 1 2 !
+'''
+expect = '''\
+i 1 2 3
+i 1 2 !
+i 1 2 !
+'''
+print test(7, score, expect)
+
+score = '''\
+i 1 2 3
+i 1 2 !
+i 1 2
+'''
+expect = '''\
+i 1 2 3
+i 1 2 !
+i 1 2
+'''
+print test(8, score, expect)
+
+score = '''\
+i 1 2 3 4 5
+i 1 2 3 4 5
+i 1 2 3 !
+i 1 2 3
+'''
+expect = '''\
+i 1 2 3 4 5
+i 1 2 . . .
+i 1 2 . !
+i 1 2 .
+'''
+print test(9, score, expect)
+
+score = '''\
+i 1 2 3 4 5
+i 1 2 3 4 5
+i 1 2 3 !
+i 1 2 3
+i 1 2 3 4 5
+'''
+expect = '''\
+i 1 2 3 4 5
+i 1 2 . . .
+i 1 2 . !
+i 1 2 .
+i 1 2 . 4 5
+'''
+print test(10, score, expect)

0 comments on commit c0c4fd4

Please sign in to comment.