User:Laurier Rochon/prototyping/clappingmusic: Difference between revisions
(Created page with "== Attempt #1 == ARRAYS : shifting positions into a loop Trying to move <source lang="bash"> 1 2 3 4 5 6 </source> into <source lang="bash"> 2 3 4 5 6 1 </source> Need to hav...") |
|||
Line 42: | Line 42: | ||
== Attempt #2 == | == Attempt #2 == | ||
ARRAYS : creating an empty array that we refill and then override the original array with the new values (avoids values overriding/shifting by creating this 'buffer'). Not very elegant! | ARRAYS : creating an empty array that we refill and then override the original array with the new values (avoids values overriding/shifting by creating this 'buffer'). | ||
'''Not very elegant!''' | |||
<source lang="bash"> | |||
#declare values | |||
notes=( a a+ b b+ c c+ d d+ e e+ f f+ g g+ ) #fun to toggle! | |||
notes=( c c c r c c r c r c c r) #fun to toggle! | |||
#check the length | |||
len=${#notes[@]} | |||
#create an empty array 'newnotes' | |||
declare -a newnotes | |||
#loop1, do this 13 times | |||
for((a=0;a<$len;a++));do | |||
loop2, another 13 times (13x13 total) | |||
for (( i=0;i<$len;i++)); do | |||
#check the last value | |||
last=${notes[$len-1]} | |||
#throw that into the 1st position | |||
newnotes[0]=$last | |||
#if the position of loop is smaller than total elements | |||
if [ $i -lt $((len-1)) ]; then | |||
#grab the value of the old array, put into new | |||
newnotes[$((i+1))]=${notes[$i]} | |||
fi | |||
done | |||
#then squash the new array, so this works within a loop | |||
for (( i=0;i<$len;i++)); do | |||
#reassign | |||
notes[$i]=${newnotes[$i]} | |||
done | |||
#echo | |||
echo ${notes[*]} | |||
done | |||
</source> | |||
Which creates a nice shifting matrix like so | |||
<source lang="bash"> | |||
g+ a a+ b b+ c c+ d d+ e e+ f f+ g | |||
g g+ a a+ b b+ c c+ d d+ e e+ f f+ | |||
f+ g g+ a a+ b b+ c c+ d d+ e e+ f | |||
f f+ g g+ a a+ b b+ c c+ d d+ e e+ | |||
e+ f f+ g g+ a a+ b b+ c c+ d d+ e | |||
e e+ f f+ g g+ a a+ b b+ c c+ d d+ | |||
d+ e e+ f f+ g g+ a a+ b b+ c c+ d | |||
d d+ e e+ f f+ g g+ a a+ b b+ c c+ | |||
c+ d d+ e e+ f f+ g g+ a a+ b b+ c | |||
c c+ d d+ e e+ f f+ g g+ a a+ b b+ | |||
b+ c c+ d d+ e e+ f f+ g g+ a a+ b | |||
b b+ c c+ d d+ e e+ f f+ g g+ a a+ | |||
a+ b b+ c c+ d d+ e e+ f f+ g g+ a | |||
a a+ b b+ c c+ d d+ e e+ f f+ g g+ | |||
</source> | |||
== End result == | |||
<source lang="bash"> | <source lang="bash"> | ||
cat << STOP | cat << STOP |
Latest revision as of 15:06, 12 October 2010
Attempt #1
ARRAYS : shifting positions into a loop
Trying to move
1 2 3 4 5 6
into
2 3 4 5 6 1
Need to have something like
array[currentposition]=array[currentposition+1]
But then we need to have a reminder of what the first value was before we override it with (2) in this case.
start loop
firstvalue=array[positionone]
if loopindex <smaller lastarrayindex
array[currentposition]=array[currentposition+1]
endif
array[lastvalue]=array[firstvalue]
end loop
The main problem is that firstvalue=array[positionone] changes as you move through the loop. Meaning that you lose the value and after looping this once, you get
1 3 4 5 6 6
Insteda of
2 3 4 5 6 1
Attempt #2
ARRAYS : creating an empty array that we refill and then override the original array with the new values (avoids values overriding/shifting by creating this 'buffer'). Not very elegant!
#declare values
notes=( a a+ b b+ c c+ d d+ e e+ f f+ g g+ ) #fun to toggle!
notes=( c c c r c c r c r c c r) #fun to toggle!
#check the length
len=${#notes[@]}
#create an empty array 'newnotes'
declare -a newnotes
#loop1, do this 13 times
for((a=0;a<$len;a++));do
loop2, another 13 times (13x13 total)
for (( i=0;i<$len;i++)); do
#check the last value
last=${notes[$len-1]}
#throw that into the 1st position
newnotes[0]=$last
#if the position of loop is smaller than total elements
if [ $i -lt $((len-1)) ]; then
#grab the value of the old array, put into new
newnotes[$((i+1))]=${notes[$i]}
fi
done
#then squash the new array, so this works within a loop
for (( i=0;i<$len;i++)); do
#reassign
notes[$i]=${newnotes[$i]}
done
#echo
echo ${notes[*]}
done
Which creates a nice shifting matrix like so
g+ a a+ b b+ c c+ d d+ e e+ f f+ g
g g+ a a+ b b+ c c+ d d+ e e+ f f+
f+ g g+ a a+ b b+ c c+ d d+ e e+ f
f f+ g g+ a a+ b b+ c c+ d d+ e e+
e+ f f+ g g+ a a+ b b+ c c+ d d+ e
e e+ f f+ g g+ a a+ b b+ c c+ d d+
d+ e e+ f f+ g g+ a a+ b b+ c c+ d
d d+ e e+ f f+ g g+ a a+ b b+ c c+
c+ d d+ e e+ f f+ g g+ a a+ b b+ c
c c+ d d+ e e+ f f+ g g+ a a+ b b+
b+ c c+ d d+ e e+ f f+ g g+ a a+ b
b b+ c c+ d d+ e e+ f f+ g g+ a a+
a+ b b+ c c+ d d+ e e+ f f+ g g+ a
a a+ b b+ c c+ d d+ e e+ f f+ g g+
End result
cat << STOP
@head {
\$time_sig 4/4
\$tempo 120
}
@body {
@channel 1 {
\$patch 1
\$octave 4
\$length 16
STOP
#just the original loop outputting non-variable pattern
notes=( c c c r c c r c r c c r)
len=${#notes[@]}
for((a=0;a<$len;a++));do
echo ${notes[*]}
done
cat << STOP
}
@channel 2 {
\$patch 1
\$length 16
\$octave 6
STOP
#declare values
notes=( a a+ b b+ c c+ d d+ e e+ f f+ g g+ ) #fun to toggle!
notes=( c c c r c c r c r c c r) #fun to toggle!
#check the length
len=${#notes[@]}
#create an empty array 'newnotes'
declare -a newnotes
#loop1, do this 13 times
for((a=0;a<$len;a++));do
loop2, another 13 times (13x13 total)
for (( i=0;i<$len;i++)); do
#check the last value
last=${notes[$len-1]}
#throw that into the 1st position
newnotes[0]=$last
#if the position of loop is smaller than total elements
if [ $i -lt $((len-1)) ]; then
#grab the value of the old array, put into new
newnotes[$((i+1))]=${notes[$i]}
fi
done
#then squash the new array, so this works within a loop
for (( i=0;i<$len;i++)); do
#reassign
notes[$i]=${newnotes[$i]}
done
#echo
echo ${notes[*]}
done
cat << STOP
}
}
STOP