Blog

Building Chord Progressions using Stacks, Part 2

gray wooden upright pianoPhoto by Nahir Giorgio on Unsplash

Continuing with my previous post on chord progressions, I am going to play a few more chord progression examples. Remember, I am using the music21 Python library to understand and play the music and I am using this musical stack structure:

from music21 import *

class MusicStack:
    def __init__(self):
        self.items = stream.Stream()
    
    def push(self, item):
        self.items.append(item)
    
    def pop(self):
        if self.is_empty():
            raise IndexError("Sorry, your stack is empty")
        else:
            return self.items.pop()
    
    def peek(self):
        if self.is_empty():
            raise IndexError("Sorry, your stack is empty")
        else:
            return self.items[-1]
    
    def is_empty(self):
        return self.items == []

Thanks for reading Learning Data Structures and Algorithms with Music! Subscribe for free to receive new posts and support my work.

Read more

Building Chord Progressions using Stacks, Part 1

black headset on white printer paperPhoto by Kelly Sikkema on Unsplash

In the last post, I wrote about stack data structures and their musical applications. Now, I will show you how stacks can be used to build chord progressions. We will use the music21 Python library to understand and play the music. We’ll use the same musical stack structure from before:

from music21 import *

class MusicStack:
    def __init__(self):
        self.items = stream.Stream()
    
    def push(self, item):
        self.items.append(item)
    
    def pop(self):
        if self.is_empty():
            raise IndexError("Sorry, your stack is empty")
        else:
            return self.items.pop()
    
    def peek(self):
        if self.is_empty():
            raise IndexError("Sorry, your stack is empty")
        else:
            return self.items[-1]
    
    def is_empty(self):
        return self.items == []

Thanks for reading Learning Data Structures and Algorithms with Music! Subscribe for free to receive new posts and support my work.

Read more

Stacks and Applications in Music

a stack of white and black plates on a white tablePhoto by Monika Borys on Unsplash

A stack is a data structure where elements are only added and removed from the same end. This is described as LIFO, or Last In First Out. I like to think about it like a stack of plates, where the last plate in the stack is the first to be picked up.

The three main operations of a stack are:

Read more

Listening to Past The Pain in Reverse

In the last post, I wrote about how we can reverse a linked list of MIDI notes. Now, let’s hear it!

Thanks for reading Learning Data Structures and Algorithms with Music! Subscribe for free to receive new posts and support my work.

First, let’s start with an easy major scale of middle C. We will convert this MIDI to a linked list and then save it back and play it. We will use the music21 library for the playback.

Read more

Reverse a Linked List of MIDI notes

Image by 愚木混株 Yumu (@cdd20)

This Leetcode question asks about how to reverse a linked list. I was thinking that this would be cool to solve with a song! What if we represent a song as a linked list and then reverse it? Then, we could hear the song backwards!

In order to reverse a linked list, we need to keep track of three things:

  1. previous node (we will start it as None; because there is no node before the head)

    Read more

Linked Lists and their Connection to Songs

linked blocks with arrows pointing in one direction

In a previous post on arrays, I wrote about arrays and how you can listen to them by storing sound. Linked lists are an extension of arrays that instead of just storing elements , they store pointers. Linked lists point to the next node which allow for dynamic memory allocation instead of contiguous memory allocation.

For example, a list of six elements would look like this: 1 -> 2 -> 3 -> 4 -> 5 -> 6.

Read more