Tuesday, July 26, 2016

Learn music for programmers with Sonci Pi - 02 - Random Chinese music generator

If you play just black keys on the piano in randomly you'll generate a Chinese music.

Pentatonic scale


Now this time it makes sense Penta tonic = 5 tones. Which is exactly what it is.
There are several pentatonic scales, but as suggested lets start with black piano keys which are:
C#, D#, F#, G#, A#, c#, d#, f#, g#, a#
However most important is the difference between them if we take the number for those tones from Sonic Pi it's:
62, 64, 67, 69, 71, (74, 76, 79, 81, 83)
Hence differences are:
2, 3, 2, 2, (3, 2, 3, 2, 2)
I guess you can see the pattern there, so if you choose any tone and just do the differences, you got pentatonic scale.

Sonic Pi

OK so musical theory we have, what we will need to create simple generator. Endless loop and randomly choose tone and duration in each go run through in the loop.


Loop

Loop just do commands in endless loop.

loop do
  commands
end

Field

Field is the list of items in square brackets separated by comma(s)
[62, 64, 67, 69, 71]
pentatonic_scale = [62, 64, 67, 69, 71]

Choose

Randomly choose one of the item in the give field
choose(field)
choose([62, 64, 67, 69, 71])

One_in

This returns true with probability of 1/X
one_in(X)
For example gives true with probability of 1/5
one_in(5)

Function/Procedure

Just use define :name do |prametr1, parametr2| to define procedure or function, if you want it to return some value then before the end use name = value

define :gchoose_note do
  note = 67
  note = 64 if one_in(5)
  note = 69 if one_in(5)
  note = 62 if one_in(10)
  note = 71 if one_in(10)
  gchoose_note = note
end

Random Chines music generator v1


loop do
  play choose([62, 64, 67, 69, 71])
  sleep choose([0.125, 0.25, 0.5, 1])
end

The problem with this is that it's too random and it doesn't sound as much as Chines music as if you really play the black keys your self randomly.
The reason is that people often misunderstand the randomness and if you ask group of the people to stay in the room randomly they will distribute them self's more or less evenly.
So they do when the play black keys, they tend to play one tone (in the middle often) and going around back and forth.

Random Chines music generator v2


So let's try a Gaussian distribution of probability with which we choose the tone to play.

# Welcome to Sonic Pi v2.10
use_bpm 60
use_synth :dull_bell

define :gchoose_note do
  note = 67
  note = 64 if one_in(5)
  note = 69 if one_in(5)
  note = 62 if one_in(10)
  note = 71 if one_in(10)
  gchoose_note = note
end

define :gchoose_duration do
  duration = 0.25
  duration = 0.5 if one_in(3)
  duration = 1 if one_in(6)
  duration = 0.125 if one_in(6)
  gchoose_duration = duration
end

loop do
  play gchoose_note
  sleep gchoose_duration
end

Saturday, July 16, 2016

Learn music for programmers with Sonic Pi - 01 - Brother John

I my self don't have any musical education and they try to teach me in school it was completely nonsense for me. The reason is that it's basically substitution cipher for basic physics. So let's start with few basic substitutions.

Tones/Notes

There is 12 half-tones which musicians call octave(Latin name for 8)
It's actually 7 tones (C,D,E,F,G,A,B) + 5 Sharp tones(C#, D#, F#, G#, A#). - To be more complicated German notation use H instead of B.
Reference tone is A = 440Hz, A octave higher have frequency 2*A (880Hz) and octave lower is 1/2*A (220Hz)

Now if we check the notation we can find where it's C, D, E etc. based on the position, as you can see on the we start with C on the line below "standard lines". D is under first line, E on the first line F between 1st and 2nd, etc. If you would like to ask where are the # (Sharps), there are not there if the note is sharp would see symbol # in front  of the note. (If you try to find logic in it I'm not able to help sorry, it's a cipher, really it is Bb read as B flat = A# read as A Sharp or Ais, 4 names for one frequency, see not only simple substitution it's almost like enigma)


Duration

Tones have duration, whole, half, quarter, 1/8, 1/16 etc.



This is what every musician explains, but still you don't know anything. Half of what? Second, minute, year.... ?
The answer is it depends on tempo. Tempo is number of whole notes which you can play in the minute, BPM, beats per minute.
Hence if tempo is 60 bpm it 60 wholes per minute, hence whole note plays for 1 second,  or half note 0.5 second, etc.

Tones/Notes in Sonic Pi


One more substitution we need to learn this time easy for Sonic Pi. Sonic Pi don't use names of tones it use simple integers.
1 is lowest C
2 is C#
3 is D
4 is D#
etc.
This is however way too low, to get something which we can hear we will start at 61 so

CC#DD#EFF#GG#AA#B
616263646566676869707172

Let's play in Sonic Pi


It's easy we will use command play to play a note and sleep to say how long.
Hence

play 61
sleep 0.25

plays quarter C

Now we just need to define what we are going to play and how fast (tempo). We will use piano and tempo 60 BPM

use_bpm 60
use_synth :piano

And last for today we need to learn to repeat certain parts for that we will use X. times do cyckle it have easy syntax:

X. times do
  commands
end

Will do X times commands between do and end. In below song we use 2 times do.

Brother John

An finally let's play very easy song, which have mutation in various languages, Brother Jacob.

Sonic Pi code:

# Welcome to Sonic Pi v2.10
use_bpm 60
use_synth :piano

#Let first define basic tones
C = 61
CSharp = 62
D = 63
DSharp = 64
E = 65
F = 66
FSharp = 67
G = 68
GSharp = 69
A = 70
ASharp = 71
H = 72
B = 72

#Now lets play the song Brother Jacob

#1st Part 2 times
#Are you slee - pin'
2. times do
  play C
  sleep 0.50
  play D
  sleep 0.50
  play E
  sleep 0.50
  play C
  sleep 0.50
end

#Second part 2 times
#Bro-ther John
2. times do
  play E
  sleep 0.5
  play F
  sleep 0.5
  play G
  sleep 1
end

#3rd part 2 times
#Mor- nin bells are ring-ing
2. times do
  play G
  sleep 0.25
  play A
  sleep 0.25
  play G
  sleep 0.25
  play F
  sleep 0.25
  play E
  sleep 0.5
  play C
  sleep 0.5
end

#4th and last part 2 times
# Bim bam boom
2. times do
  play C
  sleep 0.5
  play G
  sleep 0.5
  play C
  sleep 1
end

Saturday, July 9, 2016

False alerts - Dropped packets

Most of the monitoring is based on same fact it does some kind of probe, ICMP, SNMP, SSH, WinRM, WMI, or some kind of API. But most importantly it because it needs to pull all data from all devices it cannot afford more then few tries. So it's understandable that from time to time something is missed, not reply. It would be fine if it doesn't occur often and ideally on the random bases.

Usually the problem lies in TCP/IP implementation. Each network interface (no matter if virtual, physical, on switch, server, printer) have incoming and outgoing queue (buffer) which have some size. Packets goes first to this buffer and then they are processed one by one by TCP/IP layers.

When there is lot of traffic in very short period of time, typically when you start big file transfer, this buffer fills and newly incoming packets are dropped. For ongoing TCP connection it's no problem, because dropped packets as they are not acknowledged are resend. But new connections and state less protocols are not able recover and it "timeouts".

In monitoring we usually consider such device as down because it's not responding.
So if you have irregularly flapping devices or more specifically packet loss and your problem might be dropped packets and you shall consider extending those buffers.
This is known issue of VMWare by the way
https://kb.vmware.com/selfservice/microsites/search.do?language=en_US&cmd=displayKC&externalId=2039495
If you want to prove this the problem you just need to get the dropped packet counters so here are tips how you can get it.

For Windows


From Zenoss


winrm -r http://hotsname:5985 -u service-acount@example.net -a kerberos --dcip 10.10.10.10 -f "select * from Win32_PerfRawData_Tcpip_NetworkInterface" -d

From Windows Powershell


Get-WmiObject -computername hostname -Query "select Name,PacketsOutboundDiscarded,PacketsReceivedDiscarded from Win32_PerfRawData_Tcpip_NetworkInterface"

Output example:
__GENUS                  : 2
__CLASS                  : Win32_PerfRawData_Tcpip_NetworkInterface
__SUPERCLASS             :
__DYNASTY                :
__RELPATH                : Win32_PerfRawData_Tcpip_NetworkInterface.Name="vmxnet3 Ethernet Adapter _2"
__PROPERTY_COUNT         : 3
__DERIVATION             : {}
__SERVER                 :
__NAMESPACE              :
__PATH                   :
Name                     : vmxnet3 Ethernet Adapter _2
PacketsOutboundDiscarded : 0
PacketsReceivedDiscarded : 50
PacketsReceivedDiscarded ( https://msdn.microsoft.com/en-us/library/aa394340(v=vs.85).aspx )
Data type: uint32
Access type: Read-only
Qualifiers: DisplayName ("Packets Received Discarded") ,
CounterType (65536) ,
DefaultScale (0) ,
PerfDetail(200)
Number of inbound packets that were chosen to be discarded even though no errors had been detected to prevent delivery to a higher-layer protocol. One possible reason for discarding such a packet could be to free up buffer space.
  

For SNMP Devices

Snmpwalk/get for below OIDs
ifInDiscards .1.3.6.1.2.1.2.2.1.13 
ifOutDiscards .1.3.6.1.2.1.2.2.1.19
The number of in/out bound packets which were chosen to be discarded even though no errors had been detected to prevent their being deliverable to a higher-layer protocol. One possible reason for discarding such a packet could be to free up buffer space

root@raspberrypi:/home/pi# snmpwalk -v 2c -c public 10.0.0.40 .1.3.6.1.2.1.2.2.1.2
iso.3.6.1.2.1.2.2.1.2.1 = STRING: "eth0"
iso.3.6.1.2.1.2.2.1.2.2 = STRING: "LOOPBACK"
root@raspberrypi:/home/pi# snmpwalk -v 2c -c public 10.0.0.40 .1.3.6.1.2.1.2.2.1.13
iso.3.6.1.2.1.2.2.1.13.1 = Counter32: 0
iso.3.6.1.2.1.2.2.1.13.2 = Counter32: 0
root@raspberrypi:/home/pi# snmpwalk -v 2c -c public 10.0.0.40 .1.3.6.1.2.1.2.2.1.19
iso.3.6.1.2.1.2.2.1.19.1 = Counter32: 0
iso.3.6.1.2.1.2.2.1.19.2 = Counter32: 0

For Linux base devices

On linux based devices you can use ifconfig command nad check for dropped counters
root@raspberrypi:/var/www# ifconfig
eth0      Link encap:Ethernet  HWaddr b8:27:eb:1d:89:fe
          inet addr:10.0.0.37  Bcast:10.0.0.255  Mask:255.255.255.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:19462418 errors:0 dropped:43030 overruns:0 frame:0
          TX packets:12891339 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000

          RX bytes:4260766110 (3.9 GiB)  TX bytes:2835848497 (2.6 GiB)