{ "cells": [ { "cell_type": "code", "execution_count": 3, "id": "b7c95bcb", "metadata": {}, "outputs": [], "source": [ "from omusic.midi import Instrument, port, Player, play, change_instrument\n", "from time import sleep\n", "import omusic.chord as chord\n", "import omusic.modes as modes" ] }, { "cell_type": "markdown", "id": "bc16188e", "metadata": {}, "source": [ "# Playing Notes\n", "\n", "OMusic comes with a MIDI player. The player is based on RtMidi and accepts notes as input. Being parallelised means that several notes or chords can be played at the same time.\n", "\n", "The `omusic.midi` module has several components of note:\n", "\n", "* `change_instrument` changes the current instrument to one of `omusic.midi.Instrument`. All standard instruments are supported.\n", "\n", "* `Player` creates a player. This player controls a MIDI port and acts as a context manager.\n", "\n", "* `play` plays notes.\n", "\n", "* `port` returns the default MIDI port. If something goes wrong, check if the default MIDI port is available.\n", "\n", "While the `play` function only accept strings, you can play MIDI notes by first passing them to `omusic.notes_i2s`." ] }, { "cell_type": "code", "execution_count": null, "id": "336b1199", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "closing\n" ] } ], "source": [ "with Player() as player:\n", " play(player, sound=chord.triad(\"C5\", modes.MAJOR))\n", " sleep(0.4)\n", " play(player, sound=chord.triad(\"E5\", modes.MINOR))\n", " sleep(0.4)\n", " play(player, sound=chord.triad(\"G5\", modes.MINOR_MELODIC))" ] }, { "cell_type": "markdown", "id": "c4e6ba9b", "metadata": {}, "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.12.10" } }, "nbformat": 4, "nbformat_minor": 5 }