Jami Nishelle's Music and Tech Blog

Read more about my musical process, coding with data structures and algorithms, and using technology to create and market music.

  • Jan 29, 2025

Arrays of Sounds

  • Jami Nishelle
  • 0 comments

Using arrays to store sounds

What are arrays?

Arrays are the most basic form of a data structure. There are actually two types of arrays, static and dynamic arrays. Python’s native array data structure is technically a dynamic array and is called a “list.” All that means is that we can keep adding to it without specifying ahead of time how large it should be. Elements stored in this list start with the index i, the position of the element in the list, of i=0, and the last position has index i=n-1, where n is the number of elements.

Music can be stored as an array in the form of MIDI music, musical samples, numerical representation of octaves, etc.

Basic operations

There are some basic operations that you can perform with a Python list (here specified as lst):

  • lst.append(x): add element x to the end of the list

  • lst.count(x): count the number of x in the list

  • lst.pop(x): returns the element x and removes it from the list

  • lst.remove(x): removes the first matching element x from the list

  • lst.pop(i): removes the element at position i from the list. By default and without specifying position i, it removes the last element.

  • lst.insert(i, x): inserts element x at index i

  • len(lst): provides the length of the list

Let’s listen to an array

  • Here is code to create an array and play it:

    #!/usr/bin/env python
    # coding: utf-8
    
    # In[45]:
    
    
    pip install midiutil # if you don't have midiutil, you have to install it
    
    
    # In[47]:
    
    
    # import the library  
    from midiutil import MIDIFile
    
    degrees  = [60, 62, 64, 65, 67, 69, 71, 72]  # MIDI note number
    track    = 0
    channel  = 0
    time     = 0    # In beats
    duration = 1    # In beats
    tempo    = 60   # In BPM
    volume   = 100  # 0-127, as per the MIDI standard
    
    MyMIDI = MIDIFile(1)  # One track, defaults to format 1 (tempo track is created
                          # automatically)
    MyMIDI.addTempo(track, time, tempo)
    
    for i, pitch in enumerate(degrees):
        MyMIDI.addNote(track, channel, pitch, time + i, duration, volume)
    
    with open("major-scale.mid", "wb") as output_file:
        MyMIDI.writeFile(output_file) # this works!
    
    
    # In[62]:
    
    
    print(degrees) # This is an array! These are the midi note noumbers
    
    
    # In[74]:
    
    
    pip install music21
    
    
    # In[88]:
    
    
    melody = converter.parseFile('major-scale.mid')
    melody.show('midi')
      
  • Here is the link to the Jupyter Notebook to play it in your browser.

  • Here is how the array sounds. It’s a major scale of middle C.

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

0 comments

Sign upor login to leave a comment