<?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; frame</title>
	<atom:link href="http://codehop.com/tag/frame/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>Control-Rate Envelope Generator</title>
		<link>http://codehop.com/control-rate-envelope-generator/</link>
		<comments>http://codehop.com/control-rate-envelope-generator/#comments</comments>
		<pubDate>Thu, 29 Apr 2010 19:07:10 +0000</pubDate>
		<dc:creator><![CDATA[Jacob Joaquin]]></dc:creator>
				<category><![CDATA[News]]></category>
		<category><![CDATA[class]]></category>
		<category><![CDATA[csound]]></category>
		<category><![CDATA[frame]]></category>
		<category><![CDATA[generator]]></category>
		<category><![CDATA[iterator]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://slipmat.noisepages.com/?p=205</guid>
		<description><![CDATA[I wanted to generate a control-rate signal with my Python-Slipmat prototype. To my surprise, doing so was fairly straight forward; Python iterator classes have a control-rate mechanism built right in. Read and download the full script at snipt. I designed &#8230; <a href="http://codehop.com/control-rate-envelope-generator/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
				<content:encoded><![CDATA[<p>I wanted to generate a control-rate signal with my Python-Slipmat prototype. To my surprise, doing so was fairly straight forward; Python iterator classes have a control-rate mechanism built right in.</p>
<p>Read and download the full script at <a href="http://snipt.org/LpL">snipt</a>.</p>
<p>I designed a simple envelope that generates a rise-fall shape. By default, the rise and fall times are identical, though users can specify a peak value relative to the duration. Here&#8217;s the code:</p>
<pre style="font-family: 'Courier New', courier, monaco, monospace, sans-serif; font-size: 1.2em; padding-bottom: 16px">
class RiseFall:
    '''A rise-fall envelope generator.'''
    
    def __init__(self, dur, peak=0.5):
        self.frames = int(dur * sr / float(ksmps))
        self.rise = int(peak * self.frames)
        self.fall = int(self.frames - self.rise)
        self.inc = 0
        self.v = 0
        
    def __iter__(self):
        self.index = 0
        
        if self.inc <= self.rise and self.rise != 0:
            self.v = self.inc / float(self.rise)
        else:
            self.v = (self.fall - (self.inc - self.rise)) / float(self.fall)
            
        self.inc += 1
        return self
    
    def next(self):
        if self.index >= ksmps:
            raise StopIteration

        self.index += 1          
        return self.v
</pre>
<p>To make sense of this, I&#8217;m going to compare this Python iterator class to Csound. More or less, Csound works at three rates: init (i), control (k) and audio (a). All three of these are represented in class RiseFall. The i-rate of RiseFall is __init__(), the k-rate is __iter__() and the a-rate is next(). </p>
<p>What makes RiseFall a k-rate unit generator is that the code that calculates the current value of the envelope resides in __iter__(), which gets executed at the beginning of each new frame of audio. If you look at class Sine, you&#8217;ll see that the code responsible for generating the sine wave is in the class method next().</p>
<p>And here is RiseFall added to our graph:</p>
<pre style="font-family: 'Courier New', courier, monaco, monospace, sans-serif; font-size: 1.2em; padding-bottom: 16px">
if __name__ == "__main__":
    t = 0.002
    a1 = Sine(0.5, 440)
    a2 = Sine(0.5, 440 * 2 ** (7 / 12.0))
    amix = Sum(a1, a2)
    aenv = RiseFall(t, 0.5)
    aout = Multiply(amix, aenv)

    for frame in Run(t):
        print '%d:' % frame
        for i in aout:
            print 't%.8f' % i
</pre>
<p>I also refactored class Mixer; It is now known as Sum, which can now sum two or more signals. Additionally, I added the class Multiply, which takes after Sum.</p>
]]></content:encoded>
			<wfw:commentRss>http://codehop.com/control-rate-envelope-generator/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Rendering 1 Second of Audio Data</title>
		<link>http://codehop.com/rendering-1-second-of-audio-data/</link>
		<comments>http://codehop.com/rendering-1-second-of-audio-data/#comments</comments>
		<pubDate>Wed, 28 Apr 2010 18:08:42 +0000</pubDate>
		<dc:creator><![CDATA[Jacob Joaquin]]></dc:creator>
				<category><![CDATA[News]]></category>
		<category><![CDATA[class]]></category>
		<category><![CDATA[frame]]></category>
		<category><![CDATA[generator]]></category>
		<category><![CDATA[iterator]]></category>
		<category><![CDATA[oscillator]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://slipmat.noisepages.com/?p=194</guid>
		<description><![CDATA[Yesterday&#8217;s python script rendered 1 frame of a rudimentary additive synthesizer. Today&#8217;s script renders 1 second. You can download the complete example at textsnip or at snipt. To render multiple frames of audio, I added two classes: Mixer and Run. &#8230; <a href="http://codehop.com/rendering-1-second-of-audio-data/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
				<content:encoded><![CDATA[<p>Yesterday&#8217;s <a href="http://slipmat.noisepages.com/2010/04/python-iterator-as-a-sine-oscillator/">python script</a> rendered 1 frame of a rudimentary additive synthesizer. Today&#8217;s script renders 1 second. You can download the complete example at <a href="http://textsnip.com/9e138b/python">textsnip</a> or at <a href="http://snipt.org/Lnlo">snipt</a>.</p>
<p>To render multiple frames of audio, I added two classes: Mixer and Run. </p>
<pre style="font-family: 'Courier New', courier, monaco, monospace, sans-serif; font-size: 1.2em; padding-bottom: 16px">
class Mixer:
    '''A simple mixer.'''
    
    def __init__(self, source1, source2):
        self.s1 = source1
        self.s2 = source2
    
    def __iter__(self):
        self.index = 0
        self.iter_1 = (i for i in self.s1)
        self.iter_2 = (i for i in self.s2)
        return self
    
    def next(self): 
        if self.index >= ksmps:
            raise StopIteration

        self.index += 1
        return self.iter_1.next() + self.iter_2.next()
    
class Run:
    '''Render frames over time.'''
    
    def __init__(self, dur=1.0):
        self.dur = dur  
    
    def __iter__(self):
        self.index = 0
        return self
    
    def next(self): 
        if self.index >= (self.dur * sr) / ksmps:
            raise StopIteration

        self.index += 1
        return self.index
</pre>
<p>Mixer is an iterator class that takes two iterator objects as its input. The values yielded by the inputs are summed together. Yesterday&#8217;s method was only good for one frame; A Mixer object does not have this restriction.</p>
<p>The Run iterator class is designed to loop through the iterator/audio graph over a user-defined duration of time, creating multiple frames of audio data.</p>
<p>The follow snippet of code resembles yesterday&#8217;s example as it creates a graph of two sine waves (a1 &#038; a2) being fed into a mixer (amix.) The Run object is given a duration of 1 second, which produces 4410 frames (sr / ksmps) and 44100 samples (10 samples per frame.)</p>
<pre style="font-family: 'Courier New', courier, monaco, monospace, sans-serif; font-size: 1.2em; padding-bottom: 16px">
if __name__ == "__main__":
    a1 = Sine(0.5, 440)
    a2 = Sine(0.5, 440 * 2 ** (7 / 12.0))
    amix = Mixer(a1, a2)
    
    for frame in Run(1.0):
        print frame, ':'
        for sample in amix:
            print 't', sample
</pre>
<p>Still no output to an audio file. What it does output is a printed list of frames and samples. Here&#8217;s frame 4276:</p>
<pre style="font-family: 'Courier New', courier, monaco, monospace, sans-serif; font-size: 1.2em; padding-bottom: 16px">
4276 :
    0.128148365438
    0.138541849949
    0.14709747835
    0.153592896452
    0.157826911985
    0.15962183375
    0.158825589584
    0.155313602303
    0.148990404968
    0.139790979215
</pre>
<p>BTW, I&#8217;m trying a new service, <a href="http://textsnip.com/">textsnip.com</a>, for storing and sharing my scripts online. If you have a better recommendation, let me know. Update: textsnip seems to add a couple of gremlins to the autodocs, so I&#8217;m trying out <a href="http://snipt.org/Lnlo">snipt.org</a> as well.</p>
]]></content:encoded>
			<wfw:commentRss>http://codehop.com/rendering-1-second-of-audio-data/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
