Python Note Data Generating Script Tutorial

Setting Up Your Environment

Get/Access a Shell

You will need a shell to run the Python script.

  • MacOS: The Terminal app is installed by default.
  • Windows: PowerShell is installed by default on newer computers or can be installed by following the instructions on Microsoft's Powershell installation page.

Install Python

You will also need to install Python to run the script. Installers for various operating systems can be found on Python's download page. If you don't want to install python on your computer, you can run the script/code in a Jupyter Notebook, using something such as Anaconda or Google's Colab.

Download the Script

You may need to right-click and "Save as...".

Running the Script

  1. Open your shell and use the cd command to change directories into the same directory as the Python script node_data_gen.py. You can use the ls command to list the subdirectories available from your current directory to help you navigate.
  2. Once you're in the same directory as note_data_gen.py, use the following command to view the script's help message.
    python3 note_data_gen.py -h
  3. The script accepts 3-4 command-line arguments, which specify the desired note, amplitude, duration, and output filename. This should be entered as follows, with the bracketed parts replaced with the values you want:
    python3 note_data_gen.py [note] [amplitude] [duration] [filename]
    1. You can find information on [note] names and their corresponding frequencies at various online resources (e.g., sengpielaudio).
    2. The [amplitude] is artifically scaled from 1 (soft) to 10 (max).
    3. [duration] is in seconds. At the audio codec rate of 48000 Hz, that means every second of duration generates 48000 data points!
    4. On successful execution (i.e., no error messages), the specified [filename] (or note_data.mif by default) will be created (or overwritten).

    Here is the transcript of an example usage, with prompts in blue, user input in yellow, and terminal output in white:

    user$ ls
     note_data_gen.py
    user$ python3 note_data_gen.py -h
    usage: note_data_gen.py [-h] note amplitude duration [filename]

    Generate a MIF file with audio data with the specified characteristics

    positional arguments:
      note        Specify note as either a numerical frequency in Hz (e.g.,
                  440.0) or an English 12-tone chromatic note on a piano (e.g.,
                  C#3, Ab7)
      amplitude   Relative amplitude/volume (from 1.0 to 10.0)
      duration    Note duration in seconds
      filename    output filename (include the extention .mif)

    optional arguments:
      -h, --help  show this help message and exit
    user$ python3 note_data_gen.py C4 5 1
    Audio characteristics:
     - frequency: 261.63 Hz
     - amplitude: 5.0 out of 10
     - duration: 1.0 seconds = 261.63 wavelengths
     - sample rate of 48000.0 Hz (same as Audio CODEC)

    File note_data.mif generated.
    user$ ls
     note_data.mif   note_data_gen.py