{ "metadata": { "name": "", "signature": "sha256:3853130ab9a1d87525b3ef76574d3eca7239bf456b8a16f0960c6da33b0668a4" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "This notebook takes a txt file where each line is of the form (\"%s,%s\" % (G5,1.25)) and plays it to stdout.\n", "\n", "NOTE: You need to write a function to convert a music21 length to a mingus length, given the time signature." ] }, { "cell_type": "code", "collapsed": false, "input": [ "import sys, time, re\n", "import music21\n", "import mingus.core.notes as notes\n", "import mingus.core.value as value\n", "sys.path.append('/usr/local/lib/python2.7/dist-packages/fluidsynth/')\n", "from mingus.midi import fluidsynth\n", "from mingus.containers.Note import Note\n", "from mingus.containers.Bar import Bar\n", "fluidsynth.init('/usr/share/sounds/sf2/FluidR3_GM.sf2',\"alsa\")" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "True\n" ] } ], "prompt_number": 1 }, { "cell_type": "code", "collapsed": false, "input": [ "# Convert music21 note to mingus note.\n", "def mingify(note):\n", " accidental = re.compile(\"[A-Z](-|#)[0-9]\")\n", " if accidental.match(note):\n", " if '-' not in note: note = Note(\"%s%s-%s\" % (note[0], note[1], note[2]))\n", " else: note = Note(note.replace('-', 'b-'))\n", " else: note = Note(\"%s-%s\" % (note[0], note[1]))\n", " return note\n", " \n", "# Get the notes. Note that you need to convert \"E-\" to \"Eb\" since you're converting music21 to mingus.\n", "with open('oscar2trigrams.txt', 'rb') as f:\n", " allnotes = [] # list of Note objects\n", " alldurations = []\n", " for line in f:\n", " items = line.split(',')\n", " note = items[0].rstrip()\n", " allnotes.append(mingify(note))\n", " alldurations.append(float(items[1]))\n", "\n", "# Get the durations. \n", "# print allnotes\n", "# print alldurations\n", "print len(allnotes)\n", "print sum(alldurations)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "489\n", "258.75\n" ] } ], "prompt_number": 2 }, { "cell_type": "code", "collapsed": false, "input": [ "# Put notes into one huge bar.\n", "b = Bar()\n", "b.set_meter((len(allnotes) * 4, 4))\n", "for note in allnotes:\n", " b.place_notes(note, value.eighth)\n", "\n", "# Assert # of notes in bar == total # of notes.\n", "print len(b)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "489\n" ] } ], "prompt_number": 3 }, { "cell_type": "code", "collapsed": false, "input": [ "# For now, just adjust tempo to do faster/slower.\n", "fluidsynth.play_Bar(b, 1, 375)" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 4, "text": [ "{'bpm': 375}" ] } ], "prompt_number": 4 }, { "cell_type": "code", "collapsed": false, "input": [], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 4 } ], "metadata": {} } ] }