Close

A Python library for the Fluxamasynth Pi

A project log for Fluxamasynth Modules

The Fluxamasynth is a module that makes it easy to add high quality MIDI sound to any Arduino or Raspberry Pi project.

fluxlyFluxly 10/19/2018 at 22:320 Comments

The Raspberry Pi version of the Fluxamasynth can't use an Arduino library, so I ported that library to Python. Python libraries are called modules which at their simplest are a collection of function definitions in a file fluxamasynth.py:

import serial
port = "/dev/ttyS0"
serialPort = serial.Serial()

def setPort(p):
    port = p
    
def init():
    serialPort.baudrate = 31250
    serialPort.port = port
    serialPort.open()
    serialPort.flushInput

def noteOn(channel, pitch, velocity):
    packet = [ 0x90 | (channel & 0x0f), pitch, velocity];
    serialPort.write(bytearray(packet))

def noteOff(channel, pitch):
    packet = [ 0x80 | (channel & 0x0f), pitch, 0x00 ];
    serialPort.write(bytearray(packet))

...

To us the module, call import at the beginning of your script:

import fluxamasynth
from time import sleep
import random

fluxamasynth.init()

while (1): 
    note = random.randint(0, 127)
    fluxamasynth.noteOn(0, note, 127)
    sleep(float(random.randint(0, 250))/1000)
    fluxamasynth.noteOff(0, note)

The Fluxamasynth Python library code is hosted on Github. You can clone or download the code from there, but I also packaged the module so a user can install the Fluxamasynth module using pip from the command line:

pip install fluxamasynth

The pip utility will grab the latest version of the Fluxamasynth module from the Python Package Index, a central repository for Python modules. It will also install the pyserial module dependency. 

To package your module for distribution you need to create a README and LICENSE file, and a setup.py script that provides some metadata and points at your module's source code:

import setuptools

with open("README.md", "r") as fh:
    long_description = fh.read()

setuptools.setup(
    name="fluxamasynth",
    version="1.0",
    author="Shawn Wallace",
    author_email="fluxama@gmail.com",
    description="A library for the Raspberry Pi variant of the Fluxamasynth board.",
    long_description=long_description,
    long_description_content_type="text/markdown",
    url="https://github.com/fluxly/fluxamasynthPi",
    packages=setuptools.find_packages(),
    classifiers=(
        "Programming Language :: Python :: 3",
        "License :: OSI Approved :: MIT License",
        "Operating System :: OS Independent",
    ),
)

 After that you need to create an account on the Python Package Index. Follow the full instructions for using pip and the Python setup tools in the Packaging Python Modules tutorial

Discussions