{ "cells": [ { "cell_type": "markdown", "id": "0", "metadata": {}, "source": [ "# Types\n", "\n", "Here we examine the key data types and demonstrate the rich reprs." ] }, { "cell_type": "code", "execution_count": null, "id": "1", "metadata": {}, "outputs": [], "source": [ "from pyabc2 import PitchClass, Pitch, Note, Key, Tune" ] }, { "cell_type": "markdown", "id": "2", "metadata": {}, "source": [ "## Pitch class\n", "\n", "A pitch class is a pitch without octave.\n", "For {class}`~pyabc2.PitchClass`, the key attribute is {class}`~pyabc2.PitchClass.value`,\n", "which represents the distance (in half steps) from C.\n", "\n", "We can initialize it that way:" ] }, { "cell_type": "code", "execution_count": null, "id": "3", "metadata": {}, "outputs": [], "source": [ "PitchClass(6)" ] }, { "cell_type": "markdown", "id": "4", "metadata": {}, "source": [ "Or we can initialize it using a name string:" ] }, { "cell_type": "code", "execution_count": null, "id": "5", "metadata": {}, "outputs": [], "source": [ "Fb = PitchClass.from_name(\"Fb\")\n", "Fb" ] }, { "cell_type": "markdown", "id": "6", "metadata": {}, "source": [ "{class}`~pyabc2.PitchClass`s have various useful attributes derived from the {class}`~pyabc2.PitchClass.value`." ] }, { "cell_type": "code", "execution_count": null, "id": "7", "metadata": {}, "outputs": [], "source": [ "Fb.equivalent_sharp" ] }, { "cell_type": "markdown", "id": "8", "metadata": {}, "source": [ "## Pitch\n", "\n", "For {class}`~pyabc2.Pitch`, the key attribute similarly is {class}`~pyabc2.Pitch.value`,\n", "but now that represents the distance (in half steps) from Cā‚€ (four octaves below Cā‚„, \"middle C\")." ] }, { "cell_type": "code", "execution_count": null, "id": "9", "metadata": {}, "outputs": [], "source": [ "Pitch(39)" ] }, { "cell_type": "code", "execution_count": null, "id": "10", "metadata": {}, "outputs": [], "source": [ "Pitch.from_name(\"Fb4\")" ] }, { "cell_type": "markdown", "id": "11", "metadata": {}, "source": [ "{class}`~pyabc2.Pitch`s have various useful attributes derived from the {class}`~pyabc2.Pitch.value`." ] }, { "cell_type": "code", "execution_count": null, "id": "12", "metadata": {}, "outputs": [], "source": [ "p = Pitch.from_name(\"A4\")\n", "p" ] }, { "cell_type": "code", "execution_count": null, "id": "13", "metadata": {}, "outputs": [], "source": [ "p.equal_temperament_frequency" ] }, { "cell_type": "code", "execution_count": null, "id": "14", "metadata": {}, "outputs": [], "source": [ "p.piano_key_number" ] }, { "cell_type": "code", "execution_count": null, "id": "15", "metadata": {}, "outputs": [], "source": [ "p.octave" ] }, { "cell_type": "markdown", "id": "16", "metadata": {}, "source": [ "## Note\n", "\n", "A note ({class}`~pyabc2.Note`) has a pitch and a duration.\n", "The easiest way to create one is using ABC notation:" ] }, { "cell_type": "code", "execution_count": null, "id": "17", "metadata": {}, "outputs": [], "source": [ "Note.from_abc(\"G2\")" ] }, { "cell_type": "code", "execution_count": null, "id": "18", "metadata": {}, "outputs": [], "source": [ "Note.from_abc(\"f24\")" ] }, { "cell_type": "code", "execution_count": null, "id": "19", "metadata": {}, "outputs": [], "source": [ "Note.from_abc(\"D,,3/\")" ] }, { "cell_type": "markdown", "id": "20", "metadata": {}, "source": [ "{class}`~pyabc2.Pitch` attributes work here too." ] }, { "cell_type": "code", "execution_count": null, "id": "21", "metadata": {}, "outputs": [], "source": [ "n = Note.from_abc(\"A2\")\n", "n" ] }, { "cell_type": "code", "execution_count": null, "id": "22", "metadata": {}, "outputs": [], "source": [ "n.equal_temperament_frequency" ] }, { "cell_type": "markdown", "id": "23", "metadata": {}, "source": [ "## Interval\n", "\n", "A {class}`~pyabc2.pitch.SignedInterval` is created when two pitches are subtracted." ] }, { "cell_type": "code", "execution_count": null, "id": "24", "metadata": { "tags": [ "scroll-output" ] }, "outputs": [], "source": [ "p = Pitch.from_name(\"D3\")\n", "\n", "for dv in range(-14, 32):\n", " pn = Pitch(p.value + dv)\n", " i = pn - p\n", " print(p.unicode(), \"→\", pn.unicode(), \"\\t\", i)" ] }, { "cell_type": "markdown", "id": "25", "metadata": {}, "source": [ "## Key\n", "\n", "A {class}`~pyabc2.Key` is most easily created by passing an ABC notation key/mode spec string." ] }, { "cell_type": "code", "execution_count": null, "id": "26", "metadata": {}, "outputs": [], "source": [ "Key(\"C\")" ] }, { "cell_type": "code", "execution_count": null, "id": "27", "metadata": {}, "outputs": [], "source": [ "Key(\"Dmaj\")" ] }, { "cell_type": "code", "execution_count": null, "id": "28", "metadata": {}, "outputs": [], "source": [ "Key(\"Ador\")" ] }, { "cell_type": "code", "execution_count": null, "id": "29", "metadata": {}, "outputs": [], "source": [ "Key(\"Gm\")" ] }, { "cell_type": "markdown", "id": "30", "metadata": {}, "source": [ "The {attr}`~pyabc2.Key.tonic` is a {class}`~pyabc2.PitchClass`." ] }, { "cell_type": "code", "execution_count": null, "id": "31", "metadata": {}, "outputs": [], "source": [ "Key(\"Ebmix\").tonic" ] }, { "cell_type": "markdown", "id": "32", "metadata": {}, "source": [ "(tune_type)=\n", "\n", "## Tune\n", "\n", "Pass an ABC string to create a {class}`~pyabc2.Tune`." ] }, { "cell_type": "code", "execution_count": null, "id": "33", "metadata": {}, "outputs": [], "source": [ "from pyabc2.sources import load_example_abc\n", "\n", "abc = load_example_abc(\"For the Love of Music\")\n", "\n", "print(abc)" ] }, { "cell_type": "code", "execution_count": null, "id": "34", "metadata": {}, "outputs": [], "source": [ "tune = Tune(abc)\n", "tune" ] }, { "cell_type": "code", "execution_count": null, "id": "35", "metadata": {}, "outputs": [], "source": [ "tune.key" ] }, { "cell_type": "markdown", "id": "36", "metadata": {}, "source": [ "{class}`~pyabc2.Tune` initialization (currently) parses the ABC and expands repeats/endings into measures of notes, here from 12 written measures into 16." ] }, { "cell_type": "code", "execution_count": null, "id": "37", "metadata": {}, "outputs": [], "source": [ "len(tune.measures)" ] }, { "cell_type": "code", "execution_count": null, "id": "38", "metadata": {}, "outputs": [], "source": [ "tune.measures[8]" ] }, { "cell_type": "code", "execution_count": null, "id": "39", "metadata": {}, "outputs": [], "source": [ "display(*tune.measures[8])" ] }, { "cell_type": "code", "execution_count": null, "id": "40", "metadata": {}, "outputs": [], "source": [ "tune.print_measures(n=9)" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "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" } }, "nbformat": 4, "nbformat_minor": 5 }