Synthesize musical notes (with piano sounds) in Python
Asked Answered
S

2

22

I would like to have a python implementation of a musical instrument library (for instance, a piano object) that I can use to convert a list of notes and a duration into sound. For instance, something like:

import Piano

pn = Piano()
pn.play([note, note, ..., note], duration)

Does something like this exist for python 2.7? I would like to implement it if it doesn't. I currently have something that uses audiere, but its just sine waves so it sounds horrible. Is there any way to hook into a midi piano or something like that- I am using windows 7? Are there any implementing steps that I might not expect?

Sorilda answered 26/6, 2011 at 22:37 Comment(1)
possible duplicate of How to synthesize sounds?Stultz
G
13

A subset of @Marcelos answer: http://code.google.com/p/mingus/

mingus is a package for Python used by programmers, musicians, composers and researchers to make and investigate music. At the core of mingus is music theory, which includes topics like intervals, chords, scales and progressions.

The MIDI package can save and load MIDI files, and -last but not least- provides a general purpose sequencer for all the containers and a FluidSynth sequencer subclass. This allows you to play all your data structures straight from Python in just a couple of lines. Most of the icky timing and MIDI code has been abstracted away for you, leaving a clean, relatively simple API.

Greig answered 26/6, 2011 at 22:45 Comment(1)
Thank you, that does indeed answer the question. For interested parties, I ended up building what I wanted with midiutilSorilda
O
23

A student of mine has just started using mingus to do just this so here's quick guide on how to get going on linux (ubuntu):

Install fluidsynth and mingus if you don't have them already:

$ sudo apt-get install fluidsynth

$ sudo easy_install mingus

Now you should be able to open python and type:

>>> from mingus.midi import fluidsynth   
>>> fluidsynth.init('/usr/share/sounds/sf2/FluidR3_GM.sf2',"alsa")

This imports the necessary stuff from mingus and initialises fluidsynth to play through alsa (not jack which is the default). Then:

>>> fluidsynth.play_Note(64,0,100)

...and you should hear a note played on the piano (arguments are: note number, channel number and velocity).

For more information go here:

https://code.google.com/p/mingus/wiki/tutorialFluidsynth

Origan answered 4/4, 2013 at 12:27 Comment(1)
for ubuntu, I used "pip install mingus". Do not use python3/pip3, seems not to work.Oech
G
13

A subset of @Marcelos answer: http://code.google.com/p/mingus/

mingus is a package for Python used by programmers, musicians, composers and researchers to make and investigate music. At the core of mingus is music theory, which includes topics like intervals, chords, scales and progressions.

The MIDI package can save and load MIDI files, and -last but not least- provides a general purpose sequencer for all the containers and a FluidSynth sequencer subclass. This allows you to play all your data structures straight from Python in just a couple of lines. Most of the icky timing and MIDI code has been abstracted away for you, leaving a clean, relatively simple API.

Greig answered 26/6, 2011 at 22:45 Comment(1)
Thank you, that does indeed answer the question. For interested parties, I ended up building what I wanted with midiutilSorilda

© 2022 - 2024 — McMap. All rights reserved.