Skip to content

A Python tool to convert a computer-code file (js, python, C, etc.) to musical output

License

Notifications You must be signed in to change notification settings

flancast90/Harpsichord

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 

Repository files navigation

Harpsichord

A Python and Piano mashup to create unique music out of a computer-code file (JS, C#, C++, Python, CSS, etc.)

License Latest release

Table of contents

Introduction

Harpsichord is a python implementation of a music-generator that is mathematically-based. The program takes as input a computer-code file of any language (eg C, C#, JS, Python, Html, CSS, etc.), and maps common symbols within them to a randomly-generated note that fits the bounds of the song's random octave.

The below function maps the symbols:

def map_keys(code):
    symbols = ['+', '-', '%', '&', '*', '/', ';', '?', '"', '\'', '!', '$', '{', '}', '=', '_', '(', ')', '[', ']', ',', '.', '#', '`', '\\']
    for item in symbols:
        if item in code:
            play_sound((len(code)/8000)*80)

which then are played with the following code:

def play_sound(length):
    C1 = 32.7032
    note = pick_random_note("note")
    motion = pick_random_note("motion")
    if motion == "up":
        note_loc = (octave*12)+note+4
    elif motion == "down":  
        note_loc = (octave*12)-note+4
    OCTAVE = math.floor((note_loc+8)/12)
    SURPLUS = note_loc - globals()["OCTAVEINT"+str(OCTAVE)]
    sine(frequency=(C1*(2**(OCTAVE-1))*(1.05946371509**(SURPLUS))), duration=length)

NOTE: Harpsichord uses pysine to output the generated notes over computer speakers.

Installation

The non-native dependencies of Harpsichord are PySine. For any speaker or sound-output-related errors, refer to the PySine documentation here

  1. To get started with Harpsichord, first make sure Python is downloaded as per https://www.python.org/downloads/, and then download PIP here
  2. Next, make sure that the necessary libraries are installed using Pip
pip install pysine
  1. Once PySine is downloaded, start Harpsichord as follows:
cd path/to/Harpsichord-main
cd src
python harpsichord.py
  1. That's it! Refer to the Usage Section for the quick-start guide

Usage

Once harpsichord starts, you should see an output similar to the following: [Harpsichord Start Img]

In the Enter File Path: prompt, enter the path to your code file. Note that your current directory is listed above this input by the program, and defaults to the src folder of Harpsichord.

Assuming your program file were located on your desktop, and that the Harpsichord-main folder was also on your desktop, then, a path similar to the following would work:

Enter File Path: ../../code-file.fileType

If everything works, you can expect to see a visual output similar to the following, and if your computer volume is up, then you should hear some soft music playing!: [Harpsichord output img]

Getting help

Hopefully you don't need this section, but in case something goes wrong, feel free to drop me an email at flancast90@gmail.com, or open a new issue on this GitHub Repo. I will do my best to respond ASAP to these problems!

Contributing

Contributions to this file can be done as a Pull Request, or by shooting an email to flancast90@gmail.com. If any Python or Music-Savvy person would like to be added as a Collaborator to this repo, please send an email to the same address given above.

License

This README file is distributed under the terms of the MIT License. The license applies to this file and other files in the GitHub repository hosting this file.

Acknowledgments

Thanks to everyone in the list below! Each of them helped me on my journey to create Harpsichord, and their knowledge and expertise in the subject of music, music-theory, or programming, respectively, taught me something new that is now implemented in Harpsichord. You all are awesome!