Blog

Connecting the Leetcode Valid Parentheses Problem to Parsing MusicXML files

While exploring stacks and their connection to music, I have learned that stacks are great data structures to parse files and can be applied to parse MusicXML files. MusicXML is a standard open format for exchanging digital sheet music. MusicXML was based primarily on two academic music formats to capture how music is formatted on the printed page. MusicXML differs from MIDI, another music notation interchange format, in that MIDI is useful for performance applications, but not musical notation. MIDI does not represent stem direction, beams, repeats, slurs, measures, and many other aspects of notation.

Read more

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