<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>codehop &#187; function</title>
	<atom:link href="http://codehop.com/tag/function/feed/" rel="self" type="application/rss+xml" />
	<link>http://codehop.com</link>
	<description>#code #art #music</description>
	<lastBuildDate>Mon, 23 Apr 2012 18:37:35 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>https://wordpress.org/?v=4.2.38</generator>
	<item>
		<title>x Trigger Notation</title>
		<link>http://codehop.com/x-trigger-notation/</link>
		<comments>http://codehop.com/x-trigger-notation/#comments</comments>
		<pubDate>Sat, 17 Apr 2010 15:10:59 +0000</pubDate>
		<dc:creator><![CDATA[Jacob Joaquin]]></dc:creator>
				<category><![CDATA[News]]></category>
		<category><![CDATA[docstring]]></category>
		<category><![CDATA[function]]></category>
		<category><![CDATA[generator]]></category>
		<category><![CDATA[import]]></category>
		<category><![CDATA[notation]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[string]]></category>
		<category><![CDATA[syntax]]></category>

		<guid isPermaLink="false">http://slipmat.noisepages.com/?p=166</guid>
		<description><![CDATA[In Roll You Own Syntax, I theorized how users could construct their own systems of notation using strings. I&#8217;ve constructed a working function, called trig(), to show how it&#8217;s done. First, let&#8217;s see trig() in action. The following is complete &#8230; <a href="http://codehop.com/x-trigger-notation/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
				<content:encoded><![CDATA[<p>In <a href="http://slipmat.noisepages.com/2010/04/roll-your-own-score-syntax/">Roll You Own Syntax</a>, I theorized how users could construct their own systems of notation using strings. I&#8217;ve constructed a working function, called trig(), to show how it&#8217;s done.</p>
<p>First, let&#8217;s see trig() in action. The following is complete conceptual prototype Slipmat program that generates a rock drum groove with 8th note hats:</p>
<pre style="font-family: 'Courier New', courier, monaco, monospace, sans-serif; font-size: 1.2em; padding-bottom: 16px">
#!/usr/bin/env slipmat

from JakeLib.Generators import trig
from EasyKit import hat, snare, kick
    
@trig('x.x. x.x. x.x. x.x.') hat()
@trig('.... x... .... x...') snare()
@trig('x... .... x... ....') kick()
</pre>
<p>This horizontal system for notating triggers, and others like it, can greatly improve the legibility of a piece, while catering to a composer&#8217;s preferred style of working. A composer quickly scans this and comprehends it without having to reconstruct it in their head from a list of individual events:</p>
<pre style="font-family: 'Courier New', courier, monaco, monospace, sans-serif; font-size: 1.2em; padding-bottom: 16px">
@0    hat()
@0    kick()
@0.5  hat()
@1    hat()
@1    snare()
@1.5  hat()
@2    hat()
@2    kick()
@2.5  hat()
@3    hat()
@3    snare()
@3.5  hat()
</pre>
<p>The form is lost in translation.</p>
<p>Building the function is pretty straight forward if you&#8217;re somewhat experienced with Python. I put together the following function definition, with <a href="http://slipmat.noisepages.com/2010/03/on-the-importance-of-docstrings/">docstrings</a>, in roughly 15 minutes:</p>
<pre style="font-family: 'Courier New', courier, monaco, monospace, sans-serif; font-size: 1.2em; padding-bottom: 16px">
def trig(seq, res=0.25):
    '''
    Creates a numeric sequence from a string and returns a list.

    Description:
    A string trigger sequencer, where an 'x' creates a trigger, and a
    '.' creates a rest. All other glyphs are ignored. The resolution
    of triggers and rests are determined by the argument res.

    Input:
    seq -- A string containing a sequence of 'x' triggers and '.' rests
    res -- Resolution of note triggers and rests
        
    Output:
    return -- A numeric list
    '''    
    
    L = []  # The return list
    p = 0   # Position in sequence

    for c in seq:
        if c == 'x':
            L.append(p * res)
            p += 1            
        elif c == '.':
            p += 1

    return L
</pre>
<p>The trig() function accepts a string formatted in what I call &#8216;x trigger notation.&#8217; The function parses the string and <a href="http://slipmat.noisepages.com/2010/04/auto-generating-lists/">auto-generates a list</a> of numbers representing trigger times. A trigger is denoted by an &#8216;x&#8217;, while a rest is a &#8216;.&#8217;. All other glyphs are ignored. I use a single space between beats for clarity. The default resolution is a 16th note, though trig() accepts an optional argument for changing the resolution, increasing its usefulness.</p>
<p><strong>Import, Reuse, Remix</strong></p>
<p>The best part about custom functions is that they can be reused multiple times in multiple programs by multiple people with the use of the <a href="http://slipmat.noisepages.com/2010/03/importing-modules-and-reusing-code/">import</a>. No refactoring of code, no copy and paste, no reinventing of the wheel. Just import, reuse, remix.</p>
]]></content:encoded>
			<wfw:commentRss>http://codehop.com/x-trigger-notation/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Functions as Reusable Score Clips</title>
		<link>http://codehop.com/functions-as-reusable-score-clips/</link>
		<comments>http://codehop.com/functions-as-reusable-score-clips/#comments</comments>
		<pubDate>Fri, 16 Apr 2010 16:05:06 +0000</pubDate>
		<dc:creator><![CDATA[Jacob Joaquin]]></dc:creator>
				<category><![CDATA[News]]></category>
		<category><![CDATA[clip]]></category>
		<category><![CDATA[function]]></category>
		<category><![CDATA[range]]></category>
		<category><![CDATA[score]]></category>

		<guid isPermaLink="false">http://slipmat.noisepages.com/?p=158</guid>
		<description><![CDATA[I&#8217;m not a fan of the copy and paste method for composing computer music scores. I&#8217;d rather write something once, and reuse it as needed. Slipmat uses function definitions as one method for creating reusable score clips. The following function &#8230; <a href="http://codehop.com/functions-as-reusable-score-clips/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
				<content:encoded><![CDATA[<p>I&#8217;m not a fan of the copy and paste method for composing computer music scores. I&#8217;d rather write something once, and reuse it as needed. Slipmat uses function definitions as one method for creating reusable score clips.</p>
<p>The following function rock_drums() stores a simple rock drum pattern with 8th note hats:</p>
<pre style="font-family: 'Courier New', courier, monaco, monospace, sans-serif; font-size: 1.2em; padding-bottom: 16px">
def rock_drums():
    @[0, 0.5, 1, 1.5, 2, 2.5, 3, 3.5] hat()
    @[1, 3]                           snare()
    @[0, 2]                           kick()
</pre>
<p>Here, the rock_drum() clip is played 4 times:</p>
<pre style="font-family: 'Courier New', courier, monaco, monospace, sans-serif; font-size: 1.2em; padding-bottom: 16px">
@0  rock_drums()
@4  rock_drums()
@8  rock_drums()
@12 rock_drums()
</pre>
<p>Here are 16 bars using the <a href="http://slipmat.noisepages.com/2010/04/auto-generating-lists/">range() shorthand method</a>:</p>
<pre style="font-family: 'Courier New', courier, monaco, monospace, sans-serif; font-size: 1.2em; padding-bottom: 16px">
@range(0, 64, 4) rock_drums()
</pre>
<p>Using functions as clips can save time and keystrokes while greatly improving legibility. The easier your code is to read, the more likely others will remix your work. If they can&#8217;t make heads or tails out of it, they&#8217;re more likely to move on.</p>
]]></content:encoded>
			<wfw:commentRss>http://codehop.com/functions-as-reusable-score-clips/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Custom Sequence List Generators</title>
		<link>http://codehop.com/custom-sequence-list-generators/</link>
		<comments>http://codehop.com/custom-sequence-list-generators/#comments</comments>
		<pubDate>Tue, 13 Apr 2010 16:50:27 +0000</pubDate>
		<dc:creator><![CDATA[Jacob Joaquin]]></dc:creator>
				<category><![CDATA[News]]></category>
		<category><![CDATA[docstring]]></category>
		<category><![CDATA[function]]></category>
		<category><![CDATA[interpreter]]></category>
		<category><![CDATA[module]]></category>
		<category><![CDATA[pattern]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[scheduler]]></category>

		<guid isPermaLink="false">http://slipmat.noisepages.com/?p=142</guid>
		<description><![CDATA[Python&#8217;s built-in range() list generator is full of possibilities when used in conjunction with Slipmat&#8217;s @ scheduler. And that&#8217;s just one generator out of, dare I say, a million possibilities. Imagine being able to conveniently import generators from a massive &#8230; <a href="http://codehop.com/custom-sequence-list-generators/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
				<content:encoded><![CDATA[<p>Python&#8217;s built-in <a href="http://slipmat.noisepages.com/2010/04/auto-generating-lists/">range() list generator</a> is full of possibilities when used in conjunction with Slipmat&#8217;s <a href="http://slipmat.noisepages.com/2010/03/coding-in-time-with-the-scheduler/">@ scheduler</a>. And that&#8217;s just one generator out of, dare I say, a million possibilities.</p>
<p>Imagine being able to conveniently import generators from a massive user library of modules. This will be a reality for Slipmat for two reasons:</p>
<ul style="padding-bottom: 16px">
<li>Users can easily write their own generators</li>
<li>Users can easily <a href="http://slipmat.noisepages.com/2010/03/importing-modules-and-reusing-code/">share, import and remix</a> generators</li>
</ul>
<p>I built a list generator called range_plus(), which takes Python&#8217;s range() function a few steps farther. range() is limited to integers; range_plus() removes this restriction and allows for floats. range() supports a single increment step value; range_plus() additionally allows users to pass a list of increment step values that are chosen at random.</p>
<p>Here&#8217;s the working Python defintion for range_plus():</p>
<pre style="font-family: 'Courier New', courier, monaco, monospace, sans-serif; font-size: 1.2em; padding-bottom: 16px">
def range_plus(start, stop, random_steps=1):
    '''
    Returns a list of successive incremented values
    chosen randomly from a list.

    Input:
    start -- Starting value
    stop --  End point
    random_steps -- A list of incremental values chosen at random or a
        single numeric value
        
    Output:
    return -- A numeric list
    '''
    
    if stop < start:  # Avoid infinite loop from bad input
        return []

    if not isinstance(random_steps, list):
        random_steps = [random_steps]

    L = []

    while start < stop:
        L.append(start)
        start = start + random.choice(random_steps)

    return L
</pre>
<p>I made sure to write the <a href="http://slipmat.noisepages.com/2010/03/on-the-importance-of-docstrings/">docstrings</a>, to make life easier for anyone who may import my code later. Here's a Python interpreter session to test out some numbers:</p>
<pre style="font-family: 'Courier New', courier, monaco, monospace, sans-serif; font-size: 1.2em; padding-bottom: 16px">
>>> from MyListGenerators import range_plus
>>> range_plus(0, 4, 0.5)
[0, 0.5, 1.0, 1.5, 2.0, 2.5, 3.0, 3.5]
>>> range_plus(0, 4, [0.25, 0.25, 0.5, 0.75])
[0, 0.5, 1.0, 1.5, 1.75, 2.5, 3.0, 3.25, 3.75]
</pre>
<p>As applied to music, range_plus() would be a perfect fit for randomly generating rhythmic patterns, for things such as hi-hats.</p>
<pre style="font-family: 'Courier New', courier, monaco, monospace, sans-serif; font-size: 1.2em; padding-bottom: 16px">
@0  @range_plus(0, 4, [0.25, 0.25, 0.5, 0.75]) hat()
@4  @range_plus(0, 4, [0.25, 0.25, 0.5, 0.75]) hat()
@12 @range_plus(0, 4, [0.25, 0.25, 0.5, 0.75]) hat()
@16 @range_plus(0, 4, [0.25, 0.25, 0.5, 0.75]) hat()
</pre>
<p>Here's is one scenario for what those patterns would look like in trigger notation:</p>
<pre style="font-family: 'Courier New', courier, monaco, monospace, sans-serif; font-size: 1.2em; padding-bottom: 16px">
Bar 1:  x.x. x.xx ..x. xx.x
Bar 2:  xx.. x..x ..x. .xx.
Bar 3:  x..x ..x. xx.x .x..
Bar 4:  x.x. .xxx .xxx x..x
</pre>
<p>The 'x' is a 16th note trigger, the '.' is a 16th note rest, and spaces are used to distinguish between each quarter note.</p>
]]></content:encoded>
			<wfw:commentRss>http://codehop.com/custom-sequence-list-generators/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Auto-Generating Lists</title>
		<link>http://codehop.com/auto-generating-lists/</link>
		<comments>http://codehop.com/auto-generating-lists/#comments</comments>
		<pubDate>Mon, 12 Apr 2010 12:57:28 +0000</pubDate>
		<dc:creator><![CDATA[Jacob Joaquin]]></dc:creator>
				<category><![CDATA[News]]></category>
		<category><![CDATA[function]]></category>
		<category><![CDATA[generator]]></category>
		<category><![CDATA[goa]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[range]]></category>

		<guid isPermaLink="false">http://slipmat.noisepages.com/?p=137</guid>
		<description><![CDATA[In yesterday&#8217;s blog, Lists as Micro-Sequencers, we discussed writing lists instead of individual events. Today, we&#8217;ll generate lists instead of writing list values. Python comes with the built-in range() function that generates and returns a list of integers, which can &#8230; <a href="http://codehop.com/auto-generating-lists/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
				<content:encoded><![CDATA[<p>In yesterday&#8217;s blog, <a href="http://slipmat.noisepages.com/2010/04/lists-as-micro-sequencers/">Lists as Micro-Sequencers</a>, we discussed writing lists instead of individual events. Today, we&#8217;ll generate lists instead of writing list values.</p>
<p>Python comes with the built-in <a href="http://docs.python.org/library/functions.html#range">range()</a> function that generates and returns a list of integers, which can be used to schedule events. The following python interpreter session demos range() with three sets of args along with their respective outputs.</p>
<pre style="font-family: 'Courier New', courier, monaco, monospace, sans-serif; font-size: 1.2em; padding-bottom: 16px">
>>> range(4)
[0, 1, 2, 3]
>>> range(5, 9)
[5, 6, 7, 8]
>>> range(0, 16, 4)
[0, 4, 8, 12]
</pre>
<p>The range() function can be a huge saver of time and keystrokes. Especially if you&#8217;re composing <a href="http://en.wikipedia.org/wiki/Goa_trance">goa trance</a>:</p>
<pre style="font-family: 'Courier New', courier, monaco, monospace, sans-serif; font-size: 1.2em; padding-bottom: 16px">
@range(1024) goa_kick()
</pre>
<p><em>&#8220;The kicks are done, man.&#8221;</em> Just to be absolutely clear, that generates 1024 goa_kick() events, one per beat.</p>
]]></content:encoded>
			<wfw:commentRss>http://codehop.com/auto-generating-lists/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>
