Clapping music: Difference between revisions

From XPUB & Lens-Based wiki
No edit summary
No edit summary
Line 3: Line 3:
See the page on [[Bash]] for help with using loops & variables
See the page on [[Bash]] for help with using loops & variables


This is an exercise to create a Bash version of [[wikipedia:Steve Reich]]'s [[wikipedia:Clapping music]] using Bash and a pipeline using midge and timidity.
This is an exercise to create a Bash version of [[wikipedia:Steve Reich]]'s [[wikipedia:Clapping Music]] using Bash and a pipeline using midge and timidity.


To start, create the first two bars "by hand" as a midge file:
To start, create the first two bars "by hand" as a midge file:
Line 16: Line 16:
         $length 16
         $length 16
         $octave 4
         $octave 4
        $pan 0


     # the main pattern (3, 2, 1, 2 claps with rests in between)
     # the main pattern (3, 2, 1, 2 claps with rests in between)
Line 24: Line 25:
     }
     }
     @channel 2 {
     @channel 2 {
         $patch 2
         $patch 1
         $length 16
         $length 16
         $octave 7
         $octave 5
        $pan 127


     # the 2nd performer starts with the main pattern
     # the 2nd performer starts with the main pattern
Line 34: Line 36:


     }
     }
}</source>
}
</source>


The trivial bashified version, note the need to escape (backslash) the $s.
Now, we convert the midge file to a minimal bash script that does nothing but outputting the text (using a [[heredoc]] and [[cat]]). Note the need to [[backslash]] the $'s.


<source lang="bash">
<source lang="bash">
Line 49: Line 52:
         \$length 16
         \$length 16
         \$octave 4
         \$octave 4
        \$pan 0


     c c c r c c r c r c c r
     c c c r c c r c r c c r
     c c c r c c r c r c c r
     c c c r c c r c r c c r


     }
     }
     @channel 2 {
     @channel 2 {
         \$patch 2
         \$patch 1
         \$length 16
         \$length 16
         \$octave 7
         \$octave 5
        \$pan 127


     c c c r c c r c r c c r
     c c c r c c r c r c c r
     c c r c c r c r c c r c
     c c r c c r c r c c r c


     }
     }
Line 69: Line 72:
</source>
</source>


We will make use of a simple counting loop (see [[Bash#Loops]]) ...
<source lang="bash">
<source lang="bash">
for ((i=0; i<13;i++))
for ((i=0; i<13;i++))
do
do
# ...
done
done
</source>
</source>

Revision as of 14:22, 13 October 2010

... continuing from the sedsongs exercise

See the page on Bash for help with using loops & variables

This is an exercise to create a Bash version of wikipedia:Steve Reich's wikipedia:Clapping Music using Bash and a pipeline using midge and timidity.

To start, create the first two bars "by hand" as a midge file:

@head {
    $tempo 120
    $time_sig 4/4
}
@body {
    @channel 1 {
        $patch 1
        $length 16
        $octave 4
        $pan 0

    # the main pattern (3, 2, 1, 2 claps with rests in between)
    c c c r c c r c r c c r
    # the first performer simply repeats the pattern...
    c c c r c c r c r c c r

    }
    @channel 2 {
        $patch 1
        $length 16
        $octave 5
        $pan 127

    # the 2nd performer starts with the main pattern
    c c c r c c r c r c c r
    # .. and (eventually) shifts the pattern
    c c r c c r c r c c r c

    }
}

Now, we convert the midge file to a minimal bash script that does nothing but outputting the text (using a heredoc and cat). Note the need to backslash the $'s.

cat << END
@head {
    \$tempo 120
    \$time_sig 4/4
}
@body {
    @channel 1 {
        \$patch 1
        \$length 16
        \$octave 4
        \$pan 0

    c c c r c c r c r c c r
    c c c r c c r c r c c r

    }
    @channel 2 {
        \$patch 1
        \$length 16
        \$octave 5
        \$pan 127

    c c c r c c r c r c c r
    c c r c c r c r c c r c

    }
}
END

We will make use of a simple counting loop (see Bash#Loops) ...

for ((i=0; i<13;i++))
do
# ...
done