Musings of a dad with too much time on his hands and not enough to do. Wait. Reverse that.

Month: January 2021

Learning Guitar with Python, Part 2

Here’s another installment in my occasional series on using Python to help me learn guitar.

At the start of my practice sessions, I play scales to both help me get warmed up and help me know where the notes in a given key are in relation to each other. I’m learning that scale patterns are important to soloing, so I’m starting to commit a lot of these scales to memory. Having them easily available and all together in a Jupyter Notebook makes this process a lot easier for me.

So, here’s the code I wrote and accompanying image of the modal scales I practice on a regular basis:

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from IPython.display import display_html

%matplotlib inline


Ionian = {'Low E': ['', '1', '', '2'], 'A': ['3', '4', '', '5'], 'D': ['6', '', '7', '1'],
          'G': ['2', '', '3', '4'], 'B': ['', '5', '', '6'], 'High E': ['7', '1', '', '']}
Dorian = {'Low E': ['', '2', '', '3', '4'], 'A': ['', '5', '', '6', ''], 'D': ['7', '1', '', '2', ''],
          'G': ['3', '4', '', '5', ''], 'B': ['', '6', '', '7', '1'], 'High E': ['', '2', '', '', '']}
Phrygian = {'Low E': ['3', '4', '', '5'], 'A': ['6', '', '7', '1'], 'D': ['2', '', '3', '4'],
            'G': ['5', '', '6', ''], 'B': ['7', '1', '', '2'], 'High E': ['3', '', '', '']}
Lydian = {'Low E': ['', '4', '', '5'], 'A': ['6', '', '7', '1'], 'D': ['2', '', '3', '4'],
          'G': ['5', '', '6', ''], 'B': ['7', '1', '', '2'], 'High E': ['3', '4', '', '']}
Myxolydian = {'Low E': ['', '5', '', '6', ''], 'A': ['7', '1', '', '2', ''], 'D': ['3', '4', '', '5', ''],
          'G': ['6', '', '7', '1', ''], 'B': ['', '2', '', '3', '4'], 'High E': ['', '5', '', '', '']}
Aeolian = {'Low E': ['', '6', '', '7', '1'], 'A': ['', '2', '', '3', '4'], 'D': ['', '5', '', '6', ''],
          'G': ['7', '1', '', '2', ''], 'B': ['', '3', '4', '', '5'], 'High E': ['', '6', '', '', '']}
Locrian = {'Low E': ['7', '1', '', '2'], 'A': ['3', '4', '', '5'], 'D': ['6', '', '7', '1'],
            'G': ['2', '', '3', '4'], 'B': ['', '5', '', '6'], 'High E': ['7', '', '', '']}

modes = {'Ionian':Ionian, 'Dorian':Dorian, 'Phrygian':Phrygian, 'Lydian':Lydian, 'Myxolydian':Myxolydian, 
         'Aeolian':Aeolian, 'Locrian':Locrian}
modes_html = '<h3>Scales for Each Mode</h3>'

for i, mode_name in enumerate(modes):
    nbr_of_frets = len(modes[mode_name]['Low E'])
    df_mode = pd.DataFrame(modes[mode_name], index=np.arange(1, nbr_of_frets+1))
    # https://stackoverflow.com/a/50899244
    df_mode_styler = df_mode.style.set_table_attributes("style='display:inline'").set_caption(mode_name)
    modes_html += df_mode_styler._repr_html_() + '&nbsp;&nbsp;&nbsp;'
    if (i+1) % 3 == 0:
        modes_html += '<p></p>'

display_html(modes_html, raw=True)

This code in a Jupyter Notebook produces this graphic:

The numbers for each note represent the note’s position in the key in which you’re playing. Ionian pushes the first note of the key. For example, if you’re playing in the Key of C major, the “1” would be C. You’d find C on your Low E string (8th fret) and start playing the scale. If you wanted to play the Dorian mode of that key–D Dorian–you’d find D on your Low E string–a whole step up to the 10th fret–and start your Dorian scale there.

When I practice these scales, I’ve been taking a few approaches. For one, I play the scale going up and then turning around and coming back. I’ll start with the first note on the Low E string and play all the way up to the last note on the High E string and then play back down to where I started. To keep things interesting, sometimes I’ll reverse this process so that I start on that last High E string note, play down to the first note on the Low E and then turn around and play back to where I started.

Next, I’ll play each scale for every fret up the neck until I reach the 12th fret. So, for the Ionian scale, I’ll start the scale on the 2nd fret, play it through, move to the 3rd fret, play it through, and repeat this process until I reach the 12th fret. I do this for all the scales, so I play each scale about 10 or so times in a given practice session.

Finally, as I get these scales under my belt, I’m trying to play them faster and faster. I’ll usually try to power through the first four modes as fast and accurate as I can, take a small break, and then finish the last three. Then, I’ll move on to the rest of my practice.

More Python and music notes to follow!

End of year gotcha in Java

Some of the data I work with sends dates in epoch or unix time. This works fine for software, but it is often helpful to convert those numbers to year, month, and day values for human consumption.

Consider the epoch time value: 1608552000.000 (the decimal places being for milliseconds)

Epoch Converter converts this value to: December 21, 2020 12:00:00 PM (GMT)

In Java, I might write code like this to convert the value:

import java.time.*;
import java.time.format.DateTimeFormatter;


class MyApplication{
    public static void main(String[] args){
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("YYYY-MM-dd");
        String uTime = "1608552000.000";
        Float fTime = Float.parseFloat(uTime);
        String sTime = LocalDateTime.ofInstant(Instant.ofEpochMilli((long) (fTime*1000)), ZoneOffset.ofHours(-5)).format(dtf);

        System.out.println(sTime);
    }
}

This code writes out the date: 2020-12-21

Job well done, right? Not so fast.

Consider this epoch value: 1609070400.000

Epoch Converter converts this value to: December 27, 2020 12:00:00 PM (GMT)

However, when I run this number through the code above, the output is: 2021-12-27

The year is wrong! What’s going on here? Well, it turns out the problem is with the date formatting pattern (line 7). In the code, I’m using “Y” (capital Y) to convert the year. The documentation defines this format as a “week-based-year”. December 27 was a Sunday. The first day of 2021 was the following Friday. My best guess is that, since part of that week changed to the year 2021, use of that particular format pattern must apply the year 2021 to all days in the week. Interesting.

Thankfully, if you want to get the actual year for that date, just use “y” (lowercase y). In code, that looks like this:

import java.time.*;
import java.time.format.DateTimeFormatter;


class MyApplication{
    public static void main(String[] args){
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        String uTime = "1609070400.000";
        Float fTime = Float.parseFloat(uTime);
        String sTime = LocalDateTime.ofInstant(Instant.ofEpochMilli((long) (fTime*1000)), ZoneOffset.ofHours(-5)).format(dtf);

        System.out.println(sTime);
    }
}

And now the output is as I expect: 2020-12-27

© 2024 DadOverflow.com

Theme by Anders NorenUp ↑