Sonic Pi Coding Class 004

It was agreed to give a small prize to the student who worked the hardest during the class and who also worked the hardest in producing their final piece/s of music based on a brief.  I’ve created links here to the more obscure parts of the prize so that the recipient has a starting point to work out what they can be used for!

Arduino code for the Ultrasonic Ruler that was worked on in class can be found in my GitHub repository, here.

Install pip on OS X via Terminal


GUTE-URLS

Wordpress is loading infos from ahmadawais

Please wait for API server guteurls.de to collect data from
ahmadawais.com/install-pip-ma...

sudo easy_install pip

Get Stock Quote from Yahoo using Node-RED

[
    {
        "id": "d5d3c55c.ff8808",
        "type": "debug",
        "z": "3b163621.c03b8a",
        "name": "",
        "active": true,
        "console": "false",
        "complete": "false",
        "x": 930,
        "y": 180,
        "wires": []
    },
    {
        "id": "39834827.c17818",
        "type": "http request",
        "z": "3b163621.c03b8a",
        "name": "Yahoo Stock",
        "method": "GET",
        "ret": "txt",
        "url": "http://download.finance.yahoo.com/d/quotes.csv?s=^IXIC&f=nsl1opc1p2&e=.csv",
        "tls": "",
        "x": 510,
        "y": 180,
        "wires": [
            [
                "247647e3.b30f88",
                "5db14531.5fb30c"
            ]
        ]
    },
    {
        "id": "5aba260a.74be08",
        "type": "inject",
        "z": "3b163621.c03b8a",
        "name": "Click me to request a stock quote...",
        "topic": "",
        "payload": "",
        "payloadType": "date",
        "repeat": "",
        "crontab": "",
        "once": true,
        "x": 210,
        "y": 180,
        "wires": [
            [
                "39834827.c17818"
            ]
        ]
    },
    {
        "id": "247647e3.b30f88",
        "type": "csv",
        "z": "3b163621.c03b8a",
        "name": "Columns",
        "sep": ",",
        "hdrin": "",
        "hdrout": "",
        "multi": "one",
        "ret": "\\n",
        "temp": "1,2,3,4,5,6,7",
        "x": 720,
        "y": 180,
        "wires": [
            [
                "d5d3c55c.ff8808"
            ]
        ]
    },
    {
        "id": "2f5e310b.99ce8e",
        "type": "comment",
        "z": "3b163621.c03b8a",
        "name": "Readme...",
        "info": "Reference for requesting stock quote...\nhttps://support.klipfolio.com/hc/en-us/articles/215546368-Use-Yahoo-Finance-as-a-data-source-\n\nTo play around, you may be able to change the \nURL in the node that says Yahoo Stock to \nthe URL of your Restful API?\n\nAnyway, aside from that, if you click on the \nlittle square box to the RHS of the node that\nsays \"Click me to request a stock quote...\"\nthat will inject a new timestamp and make a new\nrequest\n\nThere are two outputs.  Check the Debug tab to see them\n\n1 - The raw csv from Yahoo\n2 - The CSV converted to JSON\n\nGood luck!",
        "x": 100,
        "y": 100,
        "wires": []
    },
    {
        "id": "5db14531.5fb30c",
        "type": "debug",
        "z": "3b163621.c03b8a",
        "name": "",
        "active": true,
        "console": "false",
        "complete": "false",
        "x": 730,
        "y": 260,
        "wires": []
    }
]

Install Docker and Compose on Debian 9 Stretch

#!/bin/bash

apt-get update

apt-get install -y \
  apt-transport-https \
  ca-certificates \
  curl \
  gnupg2 \
  software-properties-common

curl -fsSL https://download.docker.com/linux/debian/gpg | apt-key add -

add-apt-repository \
  "deb [arch=amd64] https://download.docker.com/linux/debian \
  $(lsb_release -cs) \
  stable"

apt-get update

apt-get install -y \
  docker-ce \
  docker-compose

If you plan on using the –memory flag when creating containers, you may need to enable memory and swap accounting in the Kernel.

  1. Log into the Debian host as a user with sudo privileges.
  2. Edit the /etc/default/grub file. Add or edit the GRUB_CMDLINE_LINUX line to add the following two key-value pairs:
    GRUB_CMDLINE_LINUX="cgroup_enable=memory swapaccount=1"
  3. Save and close the file.
  4. Update GRUB.
    $ sudo update-grub

    If your GRUB configuration file has incorrect syntax, an error occurs. In this case, repeat steps 3 and 4.

    The changes take effect when the system is rebooted.

More detailed information can be found in the Docker Post-Installation Steps for Linux.

Sonic Pi Coding Class 003

Last week we wrote a function to set the volume based on musical dynamics like ‘ppp’ or ‘fff’

Functions can also be used to return information to us.

An example of a function that returns a value is below. Type it in and see how it works.

define :get_volume do |music_notation|
  # Write your code here to work out volume level
  # corresponding to music_notation like 'ppp'
  
  # Return the volume level like this
  # return volume_level
  
  # Or we could just return a number for this
  # example to see how it works
  return 0.1234
end

# Now write some code to use the function get_volume
my_volume = get_volume("ppp")
print my_volume

What I would like you to do is to modify last weeks function so that when you pass it a musical notation for volume, like ‘ppp’, it returns a number between 0 and 1.0 for Sonic Pi to use as part of an :amp property.

Once you have that function working, make your own piece of music up that uses this function as many times as you can!

Below is one way of achieving the task. If you are stuck, type this in yourself and then make your own piece of music up that uses this function as many times as you can!

# First, we define the function
#
# Function: get_volume
#
# Input:
#          Gets an amp level for a musical notation passed
#          in as a string.
#          Examples of musical notation: ppp, fff, mf etc
#
# Returns:
#          A number between 0 and max_amp
#
# Reference: https://en.wikipedia.org/wiki/Dynamics_(music)
#
define :get_volume do |music_notation|
  max_velocity = 127.0
  max_amp = 1.0
  velocity_ratio = max_amp / max_velocity
  
  case music_notation
  when "ppp"
    return velocity_ratio * 16
  when "pp"
    return velocity_ratio * 33
  when "p"
    return velocity_ratio * 49
  when "mp"
    return velocity_ratio * 64
  when "mf"
    return velocity_ratio * 80
  when "f"
    return velocity_ratio * 96
  when "ff"
    return velocity_ratio * 112
  when "fff"
    return velocity_ratio * 127
  else
    # Unknown music notation so return max volume
    return velocity_ratio * 127
  end
end

# This is where we start to play our music

# Use the variable my_volume to hold the volume level returned from the function get_volume
my_volume = get_volume("mf")

# Play a note and set the amp (amplifier) level to the value stored in the variable my_volume
play 80, amp: my_volume
sleep 1

# Another way we can do it is like this
play 80, amp: get_volume("fff")
sleep 1

 

Choosing a Random Synth in Sonic Pi

Towards the end of our coding class at school today, one of the children asked me how they would choose a random synth in their code.  I couldn’t give them the answer off of the top of my head so I came home and have placed two different solutions below:

# Choosing a Random Synth in Sonic Pi

# Solution 1
10.times do
  use_synth [:beep, :blade, :cnoise, :dpulse].choose
  play :C3
  sleep 1
end

# Solution 2
10.times do
 use_synth choose([:beep, :blade, :cnoise, :dpulse])
 play :C3
 sleep 1
end

 

Check or change the Timezone in Debian

To see or change what timezone your Debian system is configured for:

dpkg-reconfigure tzdata

More detailed information is available in the Debian Wiki

More specifically, I used this in a setup of HypriotOS, a minimal Debian-based operating systems that is optimised to run Docker on ARM devices. In this instance it was an old Raspberry Pi Model B running NodeRed in a Docker Container.

I needed to get the time in the Docker Container to match the time on the host.  Achieved, as below, where /etc/localtime on the host is mapped to /etc/localtime in the container when it is created.

docker run --restart=always -p 1880:1880 -d --name nodered -v /etc/localtime:/etc/localtime:ro -v node_red_user_data:/data nodered/node-red-docker:0.17.4-rpi

I have used NodeRed to subscribe to messages from an MQTT service I have running that publishes various messages from various nodes around our house.  The NodeRed code listens for some of these messages and uses the AWS Polly Service to convert them to speech and read them out to us.

There’s a sample of the speech, below, as well as screen shots of the NodeRed flows.

Sonic Pi Coding Class – 002

My little boy, James, has been taking piano lessons for two terms now. Last week, his teacher asked him to do an assignment on a minuet.

James knows I’ve been running a coding group at school with Sonic Pi so he asked his teacher if he could write his minuet in Sonic Pi as part of his work.  It was his first coding in a non block type language like Scratch.  I think he did a super job!  James’ version is on his own web page, here.

I thought it would be a good opportunity for me to have a go too so that I could become more familiar with what I am talking to the children about in the class.  Here’s my version which includes the student and the teachers parts played as a duet.  It took me a couple of hours to figure it all out so it was a good challenge!

Press the play button, below, to see how it turned out.

#
# Petit Minuet
#
# 170704
#
# From page 47 of Piano Adventures by Nancy and Randall Faber
# Lesson Book : Primer Level
#

# I have no idea how I originally came up with this number!
speed_multiplier = 0.5 / 8

q = 4 * speed_multiplier      # Quaver          1/8
c = 8 * speed_multiplier      # Crotchet        1/4
m = 16 * speed_multiplier     # Minim           1/2
md = 24 * speed_multiplier    # Dotted minim
s = 32 * speed_multiplier     # Semibreve       1/1

# Extend the default release time of 1 second to
# give a slightly more pleasant sound
use_synth_defaults attack: 0.0125, release: s * 8

# Student
in_thread do
  use_synth :piano
  
  play_pattern_timed [:c5, :c5, :b4], [c, c, c]
  play_pattern_timed [:c5, :d5], [m, c]
  play_pattern_timed [:e5, :e5, :d5], [c, c, c]
  play_pattern_timed [:e5, :f5], [m, c]
  play_pattern_timed [:g5, :g5, :c5], [c, c, c]
  play_pattern_timed [:b4, :c5], [m, c]
  play_pattern_timed [:f5, :e5, :d5], [c, c, c]
  play_pattern_timed [:c5], [md]
end

# Teacher right hand
in_thread do
  use_synth :piano
  
  # Bar 1
  play_pattern_timed [:e4, :e4, :d4], [c, c, c]
  
  # Bar 2
  play_pattern_timed [:e4, :d4], [m, c]
  
  # Bar 3
  2.times do
    play_chord [:c4, :a3]
    sleep c
  end
  
  play_pattern_timed [:b3], [c]
  
  # Bar 4
  play_pattern_timed [:c4, :f4, :e4, :d4], [c, q, q, c]
  
  # Bar 5
  play_pattern_timed [:e4, :e4, :e4], [c, c, c]
  
  # Bar 6
  play_pattern_timed [:d4, :e4], [m, c]
  
  # Bar 7
  play_chord [:f3, :a3]
  sleep q
  
  play :d4
  sleep q
  
  play_chord [:g3, :c4]
  sleep c
  
  play_pattern_timed [:b3], [c]
  
  # Bar 8
  play_pattern_timed [:c4], [md]
end

# Teacher left hand
in_thread do
  use_synth :piano
  
  # Bar 1
  play_pattern_timed [:c3, :c3, :g3], [c, c, c]
  
  # Bar 2
  play_pattern_timed [:c3, :d3, :e3, :f3, :g3], [q, q, q, q, c]
  
  # Bar 3
  play_pattern_timed [:r, :r, :e3], [c, c, c]
  
  # Bar 4
  play_pattern_timed [:a2, :b2], [m, c]
  
  # Bar 5
  play_pattern_timed [:c3, :r, :c3], [c, c, c]
  
  # Bar 6
  play_pattern_timed [:g2, :b2, :c3], [c, c, c]
  
  # Bar 7
  play_pattern_timed [:r, :r, :g2], [c, c, c]
  
  # Bar 8
  play_pattern_timed [:c3, :g2, :c2], [c, c, c]
end

Get Spell Checking Working in Libre Office on a Raspberry Pi

Spell checking never seems to work, by default, in Libre Office on a Raspberry Pi.

To get it working:

# Go to:
https://extensions.libreoffice.org/extensions/english-dictionaries/

# Click on the Current Release or go to the below URL which
# is current as of now!
https://extensions.libreoffice.org/extensions/english-dictionaries/2017-01.01

# Download the .oxt file

# In Libre Office go to Tools / Extension Manager

# Add the .oxt file

# Spell checking now works!