User:Lieven Van Speybroeck/Prototyping/2-CPUtter: Difference between revisions

From XPUB & Lens-Based wiki
No edit summary
Line 30: Line 30:


''The output looks a bit like this:''
''The output looks a bit like this:''
[log]


<source lang="text">
<source lang="text">

Revision as of 02:35, 22 October 2010


CPUtter

Description

The CPU... the brain of our computer systems. And yet, it's so quiet while making it's calculations. Not like those fans, hard disks and DVD-drives that tend to manifest themselves with all sorts of bleeps and buzzes. Therefore, i thought about making a voice for the processor. An intuitive representation of all it's hard work in the background. This is my attempt.


Structure

  • Check user cpu usage for an amount of time and log it in a file.
  • Filter the logfile so the result is a list of numbers that represent the cpu-usage over time.
  • Create a scale from "low usage" to "high usage", compare each line of the file to that scale and convert it into a tone with pitch value.
  • Use bending to create an ongoing sequence between the tones and their different pitch values.
  • Let it sing!


Source

For making a log-file of the cpu-usage, i used "iostat", which does pretty much the same as "top" does, but you can filter a bit more:

iostat -c 1 250 > log

This will put 250 cpu-usage (-c) reports at 1 second intervals in "log". You can change the speed and amount of lines (amount of statistics) by changing these numbers. This way you could run it as long as you want to get better results.

The output looks a bit like this:

[log]

Linux 2.6.32-25-generic (Lieven) 	10/21/2010 	_x86_64_	(2 CPU)

avg-cpu:  %user   %nice %system %iowait  %steal   %idle
           1.52    0.10    0.69    0.28    0.00   97.42

avg-cpu:  %user   %nice %system %iowait  %steal   %idle
           2.55    0.00    0.00    0.51    0.00   96.94

avg-cpu:  %user   %nice %system %iowait  %steal   %idle
           3.98    0.00    0.00    0.00    0.00   96.02

avg-cpu:  %user   %nice %system %iowait  %steal   %idle
           4.10    0.00    2.05    0.00    0.00   93.85

avg-cpu:  %user   %nice %system %iowait  %steal   %idle
           7.61    0.00    1.52    0.00    0.00   90.86

avg-cpu:  %user   %nice %system %iowait  %steal   %idle
           4.02    0.00    1.01    0.00    0.00   94.97

avg-cpu:  %user   %nice %system %iowait  %steal   %idle
           2.01    0.00    2.01    0.00    0.00   95.98

avg-cpu:  %user   %nice %system %iowait  %steal   %idle
           1.05    0.00    0.00    0.00    0.00   98.95

avg-cpu:  %user   %nice %system %iowait  %steal   %idle
           5.42    0.00    1.48    0.00    0.00   93.10

... and so on


Code

With sed regular expressions i get rid of all the info that is useless.

[filter.sed]

1d
s/[a-z]//g
s/%//g
s/^[ ]*//g
s/[-:]//g
s/\.//g
s/   /r/g
s/rrrr//g
s/r /r/g
/^$/d
s/r.*//

s/^0\(.*\)/\1/
s/^0\(.\)/\1/

Use bash to prepare everything for midge:

[cputter.sh]

cat <<END
@head {
	\$time_sig 4/4
	\$tempo 250
}
@body {
	@channel 1 {
		\$patch 53
		\$length 60
		\$octave 5
		\$strum 8
		%bend e {

END

	cat log | sed -f filter.sed > score.txt

# define the scale of 'low usage' to 'high usage'

	lowest=400
	low=1200
	normal=2400
	heigh=4000


# go through every line, compare it to the scale and convert it to a tone + pitch (the heigher the tone, the heigher the CPU-usage)

	while read line;
		do
			if [ "$line" -lt "$lowest" ];
				then echo "4-2"
			elif [ "$line" -lt "$low" ];
				then echo "4-1"
			elif [ "$line" -lt "$normal" ];
				then echo "4+0"
			elif [ "$line" -lt "$heigh" ];
				then echo "4+1"
			elif [ "$line" -gt "$heigh" ]; 
				then echo "4+2"
			fi
		done < score.txt

cat <<END
		}
	}
}
END


Start recording your processor activity. Browse, code, watch some (flash) movies, read some mails and get a drink. Do some 3D rendering if you wish! (set the last iostat value high enough then though):

iostat -c 1 250 > log

When that's done:

bash cputter.sh | midge -o cputter.mid
timidity cputter.mid

enjoy!


ADJUSTMENTS & CORRECTIONS (IMPORTANT)

I found out there were some problems with my script. Mostly the bending wasn't handled in a right way and it caused trouble for different logfiles. I searched for a way where the bending was handled properly and creates an exact 'image' of the cpu process. It was a bit of a struggle, but eventually i came up with this:

cat <<END
@head {
	\$time_sig 4/4
	\$tempo 120
}
@body {
	@channel 1 {
		\$patch 53
		\$length 24
		\$octave 4
		\$reverb 100

END
# Use %bend to create a sequential sound that mimics an ongoing and changing process.
# The higher the tone gets, the higher the CPU usage 
cat <<END

		%bend f {
		
END

		# Pipe the log file to the filter with the regexp and store the output in a textfile

		cat log | sed -f filter.sed > score.txt

		# Define the scale of 'low usage' to 'high usage'.

		lowest=400
		low=1200
		normal=2400
		heigh=4000
		heighest=10001

		# Create variables to define on what pitch level the tones are

		minpitch=active
		lowpitch=nonactive
		normpitch=nonactive
		highpitch=nonactive
		maxpitch=nonactive

	# This is where most corrections/additions take place:	
	# Go through every line of the score.txt file
	# Compare it to the scale
	# Check which pitch is active
	# Echo a tone with the right bend value

	while read line;
		do

		# All checks are necessary to get the bending handled properly and get a correct 'image' of the cpu usage

		# MINIMAL PITCH LEVEL
		# -------------------
	
			if [[ "$line" -lt "$lowest" && "$minpitch" != "active" && "$lowpitch" == "active" ]]
				then 
				minpitch=active
				lowpitch=nonactive
				normpitch=nonactive
				highpitch=nonactive
				maxpitch=nonactive				
				echo "4-10"
			elif [[ "$line" -lt "$lowest" && "$minpitch" != "active" && "$normpitch" == "active" ]]
				then 
				minpitch=active
				lowpitch=nonactive
				normpitch=nonactive
				highpitch=nonactive
				maxpitch=nonactive				
				echo "4-20"
			elif [[ "$line" -lt "$lowest" && "$minpitch" != "active" && "$highpitch" == "active" ]]
				then 
				minpitch=active
				lowpitch=nonactive
				normpitch=nonactive
				highpitch=nonactive
				maxpitch=nonactive				
				echo "4-30"
			elif [[ "$line" -lt "$lowest" && "$minpitch" != "active" && "$maxpitch" == "active" ]]
				then 
				minpitch=active
				lowpitch=nonactive
				normpitch=nonactive
				highpitch=nonactive
				maxpitch=nonactive				
				echo "4-40"
			elif [[ "$line" -lt "$lowest" && "$minpitch" == "active" ]]
				then echo "4+0"

		# LOW PITCH LEVEL
		# ---------------

			elif [[ "$line" -lt "$low" && "$lowpitch" != "active" && "$minpitch" == "active" ]]
				then 
				minpitch=nonactive
				lowpitch=active
				normpitch=nonactive
				highpitch=nonactive
				maxpitch=nonactive				
				echo "4+10"
			elif [[ "$line" -lt "$low" && "$lowpitch" != "active" && "$normpitch" == "active" ]]
				then 
				minpitch=nonactive
				lowpitch=active
				normpitch=nonactive
				highpitch=nonactive
				maxpitch=nonactive				
				echo "4-10"
			elif [[ "$line" -lt "$low" && "$lowpitch" != "active" && "$highpitch" == "active" ]]
				then 
				minpitch=nonactive
				lowpitch=active
				normpitch=nonactive
				highpitch=nonactive
				maxpitch=nonactive				
				echo "4-20"
			elif [[ "$line" -lt "$low" && "$lowpitch" != "active" && "$maxpitch" == "active" ]]
				then 
				minpitch=nonactive
				lowpitch=active
				normpitch=nonactive
				highpitch=nonactive
				maxpitch=nonactive				
				echo  "4-30"
			elif [[ "$line" -lt "$low" && "$lowpitch" == "active" ]]
				then echo "4+0"
		
		# NORMAL PITCH LEVEL
		# ------------------

			elif [[ "$line" -lt "$normal" && "$normpitch" != "active" && "$minpitch" == "active" ]]
				then
				minpitch=nonactive
				lowpitch=nonactive
				normpitch=active
				highpitch=nonactive
				maxpitch=nonactive
				echo "4+20"
			elif [[ "$line" -lt "$normal" && "$normpitch" != "active" && "$lowpitch" == "active" ]]
				then
				minpitch=nonactive
				lowpitch=nonactive
				normpitch=active
				highpitch=nonactive
				maxpitch=nonactive
				echo "4+10"
			elif [[ "$line" -lt "$normal" && "$normpitch" != "active" && "$highpitch" == "active" ]]
				then
				minpitch=nonactive
				lowpitch=nonactive
				normpitch=active
				highpitch=nonactive
				maxpitch=nonactive
				echo "4-10"
			elif [[ "$line" -lt "$normal" && "$normpitch" != "active" && "$maxpitch" == "active" ]]
				then
				minpitch=nonactive
				lowpitch=nonactive
				normpitch=active
				highpitch=nonactive
				maxpitch=nonactive
				echo "4-20"	
			elif [[ "$line" -lt "$normal" && "$normpitch" == "active" ]]
				then echo "4+0"

		# HIGH PITCH LEVEL
		# ----------------

			elif [[ "$line" -lt "$heigh" && "$highpitch" != "active" && "$minpitch" == "active" ]]
				then
				minpitch=nonactive
				lowpitch=nonactive
				normpitch=nonactive
				highpitch=active
				maxpitch=nonactive
				echo "4+30"
			elif [[ "$line" -lt "$heigh" && "$highpitch" != "active" && "$lowpitch" == "active" ]]
				then
				minpitch=nonactive
				lowpitch=nonactive
				normpitch=nonactive
				highpitch=active
				maxpitch=nonactive
				echo "4+20"
			elif [[ "$line" -lt "$heigh" && "$highpitch" != "active" && "$normpitch" == "active" ]]
				then
				minpitch=nonactive
				lowpitch=nonactive
				normpitch=nonactive
				highpitch=active
				maxpitch=nonactive
				echo "4+10"
			elif [[ "$line" -lt "$heigh" && "$highpitch" != "active" && "$maxpitch" == "active" ]]
				then
				minpitch=nonactive
				lowpitch=nonactive
				normpitch=nonactive
				highpitch=active
				maxpitch=nonactive
				echo "4-10"
			elif [[ "$line" -lt "$heigh" && "$highpitch" == "active" ]]
				then echo "4+0"

		# MAXIMUM PITCH LEVEL
		# -------------------

			elif [[ "$line" -gt "$heigh" && "$maxpitch" != "active" && "$minpitch" == "active" ]] 
				then
				minpitch=nonactive
				lowpitch=nonactive
				normpitch=nonactive
				highpitch=nonactive
				maxpitch=active
				echo "4+40"
			elif [[ "$line" -gt "$heigh" && "$maxpitch" != "active" && "$lowpitch" == "active" ]] 
				then
				minpitch=nonactive
				lowpitch=nonactive
				normpitch=nonactive
				highpitch=nonactive
				maxpitch=active
				echo "4+30"
			elif [[ "$line" -gt "$heigh" && "$maxpitch" != "active" && "$normpitch" == "active" ]]
				then
				minpitch=nonactive
				lowpitch=nonactive
				normpitch=nonactive
				highpitch=nonactive
				maxpitch=active
				echo "4+20"
			elif [[ "$line" -gt "$heigh" && "$maxpitch" != "active" && "$highpitch" == "active" ]]
				then
				minpitch=nonactive
				lowpitch=nonactive
				normpitch=nonactive
				highpitch=nonactive
				maxpitch=active
				echo "4+10"
			elif [[ "$line" -gt "$heigh" && "$maxpitch" == "active" ]]
				then echo "4+0"
			fi		
		done < score.txt

cat <<END

		 }
	}
}
END


Output file

For some reason, the soundfile doesn't play when i upload it to the wiki. It plays from the terminal with timidity, but not in any other player it seems... Any suggestions?