[{"element_type":"user_image","content":"https://cdn-learn.adafruit.com/user_assets/assets/000/000/722/original/IMG_1827.jpeg?1709162685","metadata":{"caption":"A WaveStore Test Screen with Various Waveform and Envelope Icons"}},{"element_type":"text","content":"\n        \u003cp\u003eThe arbitrary waveform capability along with ADSR envelopes and filters has made it very easy to create custom musical voices with \u003cem\u003e\u003cstrong\u003eCircuitPython\u003c/strong\u003e\u003c/em\u003e \u003cstrong\u003e\u003cem\u003esynthio\u003c/em\u003e\u003c/strong\u003e. And if you use an additive synthesis tool like \u003ca href=\"https://adafruit-playground.com/u/CGrover/pages/wavebuilder-construct-a-synthio-wave-table-from-a-list-of-oscillators\" target=\"_blank\"\u003eWaveBuilder\u003c/a\u003e, you'll begin to quickly amass quite a few custom musical voices as you experiment with the nearly infinite number of oscillator combinations.\u003c/p\u003e\n\u003ch3\u003eHearing Voices\u003c/h3\u003e\n\u003cp\u003eUp to this point, I've been keeping track of the numeric specifications for WaveBuilder oscillators and \u003cem\u003esynthio.Envelopes\u003c/em\u003e in project code or scribbled on a note pad. That means that when building a new project that needs to use a previously created voice, I'll cut and paste the voice definition code into the new project. The process works, but isn't ideal. What if a \u003cem\u003esynthio\u003c/em\u003e musical voice could be loaded from a collection in a file folder, kind of like working with a font file?\u003c/p\u003e\n\u003cp\u003eSo a concept for the \u003cem\u003e\u003cstrong\u003eWaveStore\u003c/strong\u003e\u003c/em\u003e project began to develop. Here are the initial requirements.\u003c/p\u003e\n\u003cul\u003e\n\u003cli\u003e\n\u003cstrong\u003eLibrary File Management\u003c/strong\u003e -- We'll make it easy at first and just work with a collection stored on an SD card. No need to tax the brain to conjure up a way to write to the CircuitPython root directory just yet. After creating and storing a voice to a file, we'll manually copy the files from the SD card in order to use and share between projects.\u003c/li\u003e\n\u003cli\u003e\n\u003cstrong\u003eFiles\u003c/strong\u003e -- In the spirit of keeping it simple, only files representing waveforms and envelopes will be created. Those objects form the fundamental elements of a musical voice. Perhaps we can add filters and other effects later.\u003c/li\u003e\n\u003cli\u003e\n\u003cstrong\u003eLibrary File Names\u003c/strong\u003e -- Waveforms will be stored in a standard wave file\u0026nbsp;with a \u003ccode\u003e.wav\u003c/code\u003e extension, \u0026nbsp;thanks to the awesome \u003cem\u003eAdafruit_CircuitPython_Wave\u003c/em\u003e library. Envelopes and filters definitions will be stored in plain text files with \u003ccode\u003e.adsr\u003c/code\u003e and \u003ccode\u003e.fltr\u003c/code\u003e extensions but will transform to \u003cem\u003esynthio.Envelope\u003c/em\u003e and \u003cem\u003esynthio.BiQuad\u003c/em\u003e objects when retrieved.\u003c/li\u003e\n\u003cli\u003e\n\u003cstrong\u003eIcons\u003c/strong\u003e -- Imagine being able to select a musical voice waveform or envelope by touching a screen icon. Functions will be provided to save and retrieve bitmap images of waveforms and envelopes as well as capturing the contents of an entire screen. We'll start with 64x64 pixel bitmaps created by \u003ca href=\"https://adafruit-playground.com/u/CGrover/pages/waveviz-plot-a-synthio-wave-table-or-envelope\" target=\"_blank\"\u003eWaveViz\u003c/a\u003e. A graphical frequency response representation of a bi-quad filter's coefficients is in the works but is a bit beyond today's skillset. There's a potential workaround, but I'd rather derive it directly from the numbers. Hoping for an epiphany soon.\u003c/li\u003e\n\u003c/ul\u003e\n\u003cp\u003eIn the future, more features will likely be added to WaveStore such as support for filters and on-screen icon buttons -- as my experience and Python skill set grows. What would you add?\u003c/p\u003e\n      ","metadata":{}},{"element_type":"text","content":"\n  \n  \n  \n  \n        \u003ch3\u003eEnter WaveStore\u003c/h3\u003e\n\u003cp\u003e\u003ca href=\"https://github.com/CedarGroveStudios/CircuitPython_WaveStore\" target=\"_blank\"\u003eWaveStore GitHub repository\u003c/a\u003e\u003c/p\u003e\n\u003cp\u003eWaveStore is under development but is available for testing. The current alpha version of the WaveStore class provides the following helpers:\u003c/p\u003e\n\u003cul\u003e\n\u003cli\u003eWavetables\n\u003cul\u003e\n\u003cli\u003eread_wavetable -- Read a \u003ccode\u003e.wav\u003c/code\u003e file and return a memory view object.\u003c/li\u003e\n\u003cli\u003eread_wavetable_ulab -- Read a \u003ccode\u003e.wav\u003c/code\u003e file and return a \u003cem\u003eulab.numpy\u003c/em\u003e array object. The \u003cem\u003eulab\u003c/em\u003e array can be mixed with other wavetable array files.\u003c/li\u003e\n\u003cli\u003ewrite_wavetable -- Format a wavetable and write it as a standard \u003ccode\u003e.wav\u003c/code\u003e file.\u003c/li\u003e\n\u003c/ul\u003e\n\u003c/li\u003e\n\u003cli\u003eEnvelopes\n\u003cul\u003e\n\u003cli\u003eread_envelope -- Read an \u003ccode\u003e.adsr\u003c/code\u003e file and return a \u003cem\u003esynthio.Envelope\u003c/em\u003e object.\u003c/li\u003e\n\u003cli\u003ewrite_envelope -- Write a text interpretation of a \u003cem\u003esynthio.Envelope\u003c/em\u003e object to an \u003ccode\u003e.adsr\u003c/code\u003e file.\u003c/li\u003e\n\u003c/ul\u003e\n\u003c/li\u003e\n\u003cli\u003eBitmaps\u003cbr\u003e\n\u003cul\u003e\n\u003cli\u003eread_bitmap -- Read a \u003ccode\u003e.bmp\u003c/code\u003e file and return the bitmap as a \u003cem\u003eTileGrid\u003c/em\u003e object that can be added to a \u003cem\u003edisplayio.Group\u003c/em\u003e object.\u003c/li\u003e\n\u003cli\u003ewrite_bitmap -- Write a bitmap image to a \u003ccode\u003e.bmp\u003c/code\u003e file.\u003c/li\u003e\n\u003cli\u003ewrite_screen -- Write the screen contents to a \u003ccode\u003e.bmp\u003c/code\u003e file on the SD card.\u003c/li\u003e\n\u003c/ul\u003e\n\u003c/li\u003e\n\u003cli\u003eUtilities\n\u003cul\u003e\n\u003cli\u003eget_catalog -- Returns a list of files in a specified folder.\u003c/li\u003e\n\u003c/ul\u003e\n\u003c/li\u003e\n\u003c/ul\u003e\n      \n\n\n\n","metadata":{}},{"element_type":"text","content":"\n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n        \u003cp\u003eWaveStore needs to work with other libraries to build and create voices and graphic icons, so developing a simple test example isn't so simple. Here's a comprehensive test example (still named \"wavestore_simplest.py\" for Community Bundle compatibility) that demonstrates how to store and retrieve icons, waveforms, and envelopes.\u003c/p\u003e\n      \n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n","metadata":{}},{"element_type":"code","content":"# SPDX-FileCopyrightText: Copyright (c) 2024 JG for Cedar Grove Maker Studios\n# SPDX-License-Identifier: MIT\n#\n# wavestore_simpletest.py\n\nimport gc\nimport time\nimport board\nimport displayio\nimport synthio\nimport adafruit_ili9341\n\nfrom cedargrove_wavebuilder import WaveBuilder, WaveShape\nfrom cedargrove_waveviz import WaveViz\nfrom cedargrove_wavestore import WaveStore\n\nDEBUG = True\nSAMP_RATE = 22_050  # samples per second\nTEST_LIST = list(range(0, 20))  # List of tests to perform\n\nFOLDER = \"/sd/voices\"  # The storage folder path (no trailing slash)\n\nsynth = synthio.Synthesizer(sample_rate=SAMP_RATE)\n\n# Instantiate WaveStore to manage SD card contents\nw_store = WaveStore(board.SPI(), board.D20, debug=DEBUG)\n\n# Instantiate the 2.4-inch TFT Wing attached to FeatherS2\ndisplayio.release_displays()  # Release display resources\ndisplay_bus = displayio.FourWire(\n    board.SPI(), command=board.D6, chip_select=board.D5, reset=None\n)\ndisplay = adafruit_ili9341.ILI9341(display_bus, width=320, height=240)\ndisplay.rotation = 0\nsplash = displayio.Group()\ndisplay.root_group = splash\n\n# pylint: disable=no-member\nm0 = gc.mem_free()\nt0 = time.monotonic()\n\n# Create two waveforms, icons, and two envelopes for the tests\nharp_tone = [\n    (WaveShape.Sine, 1.00, 0.10),\n    (WaveShape.Sine, 2.00, 0.48),\n    (WaveShape.Sine, 3.00, 0.28),\n    (WaveShape.Sine, 4.00, 0.02),\n    (WaveShape.Sine, 5.00, 0.12),\n]\nharp = WaveBuilder(oscillators=harp_tone, table_length=512)\nharp_icon = WaveViz(harp.wave_table, 10, 10, 64, 64)\n\nchime_tone = [\n    (WaveShape.Sine, 1.00, -0.60),\n    (WaveShape.Sine, 2.76, 0.20),\n    (WaveShape.Sine, 5.40, 0.10),\n    (WaveShape.Sine, 8.93, 0.07),\n    (WaveShape.Sine, 11.34, 0.01),\n    (WaveShape.Sine, 18.64, 0.01),\n    (WaveShape.Sine, 31.87, 0.01),\n]\nchime = WaveBuilder(oscillators=chime_tone, table_length=512)\nchime_icon = WaveViz(chime.wave_table, 10, 80, 64, 64)\n\nstring_envelope = synthio.Envelope(\n    attack_time=0.0001,\n    attack_level=1.0,\n    decay_time=0.977,\n    release_time=0.200,\n    sustain_level=0.500,\n)\nstring_env_icon = WaveViz(string_envelope, 80, 26, 64, 32)\n\nchime_steel_envelope = synthio.Envelope(\n    attack_time=0.02,\n    attack_level=1.0,\n    decay_time=0.1,\n    release_time=2.0,\n    sustain_level=0.0,\n)\nchime_steel_env_icon = WaveViz(chime_steel_envelope, 80, 96, 64, 32)\n\n# Start testing\n\n# Test 1: Get the SD directory and print list to REPL\nif 1 in TEST_LIST:\n    print(\"\\nTest 1: Get the SD directory and print list to REPL\")\n    print(f\"SD directory: {w_store.get_catalog(path=FOLDER)}\")\n    print(\" completed\")\n\n# Test 2: Write waveform bitmap images to files\nif 2 in TEST_LIST:\n    print(\"\\nTest 2: Write bitmap image to a file\")\n    w_store.write_bitmap(\n        harp_icon.bitmap,\n        harp_icon.pixel_shader,\n        filename=\"harp_icon.bmp\",\n        path=FOLDER,\n        overwrite=True,\n    )\n    w_store.write_bitmap(\n        chime_icon.bitmap,\n        chime_icon.pixel_shader,\n        filename=\"chime_steel_icon.bmp\",\n        path=FOLDER,\n        overwrite=True,\n    )\n    print(\" completed\")\n\n# Test 3: Read and display saved bitmap\nif 3 in TEST_LIST:\n    print(\"\\nTest 3: Read and display saved bitmap\")\n    new_bitmap = w_store.read_bitmap(\"harp_icon.bmp\", path=FOLDER)\n    new_bitmap.x = 10\n    new_bitmap.y = 10\n    splash.append(new_bitmap)\n    print(\" completed\")\n\n# Test 4: Add second icon and envs; save screen in root directory\nif 4 in TEST_LIST:\n    print(\"\\nTest 4: Test 4: Add second icon and envs; save screen in root directory\")\n    splash.append(chime_icon)\n    splash.append(string_env_icon)\n    splash.append(chime_steel_env_icon)\n    w_store.write_screen(display, \"screenshot.bmp\", path=\"/sd\", overwrite=True)\n    print(\" completed\")\n\n# Test 5: Clear the screen and read and display saved screenshot from the root directory\nif 5 in TEST_LIST:\n    print(\n        \"\\nTest 5: Clear the screen and read and display saved screenshot from the root directory\"\n    )\n    splash.pop()\n    splash.pop()\n    time.sleep(1)  # Wait for a moment to show blank screen\n    splash.append(w_store.read_bitmap(\"screenshot.bmp\", path=\"/sd\"))\n    print(\" completed\")\n\n# Test 6: Write wave tables to files\nif 6 in TEST_LIST:\n    print(\"\\nTest 6: Write wave tables to files\")\n    print(harp_icon.wave_table)\n    w_store.write_wavetable(\n        harp_icon.wave_table,\n        \"harp.wav\",\n        path=FOLDER,\n        samp_rate=SAMP_RATE,\n        overwrite=True,\n    )\n    print(chime_icon.wave_table)\n    w_store.write_wavetable(\n        chime_icon.wave_table,\n        \"chime_steel.wav\",\n        path=FOLDER,\n        samp_rate=SAMP_RATE,\n        overwrite=True,\n    )\n    print(\" completed\")\n\n# Test 7: Read wavetable as memory_view object from a file and display\nif 7 in TEST_LIST:\n    print(\"\\nTest 7: Read wavetable as memory_view object from a file and display\")\n    wave_table = w_store.read_wavetable(\"harp.wav\", path=FOLDER)\n    print(f\"w_store.read_wavetable: {wave_table}\")\n    harp_icon.wave_table = w_store.read_wavetable(\"harp.wav\", path=FOLDER)\n    print(\" completed\")\n\n# Test 8: Read wave table as ulab array from a file and display\nif 8 in TEST_LIST:\n    pass\n\n# Test 9: Write envelope objects to files\nif 9 in TEST_LIST:\n    print(\"\\nTest 9: Write envelope objects to files\")\n    w_store.write_envelope(string_envelope, \"string.adsr\", path=FOLDER, overwrite=True)\n    w_store.write_envelope(\n        chime_steel_envelope, \"chime_steel.adsr\", path=FOLDER, overwrite=True\n    )\n    print(\" completed\")\n\n# Test 10: Read envelope object from a file\nif 10 in TEST_LIST:\n    print(\"\\nTest 10: Read envelope object from a file\")\n    new_env = w_store.read_envelope(\"string.adsr\", path=FOLDER)\n    print(\" completed\")\n\n# Test 11: Write envelope bitmap image to a file\nif 11 in TEST_LIST:\n    print(\"\\nTest 11: Write envelope bitmap image to a file\")\n    w_store.write_bitmap(\n        string_env_icon.bitmap,\n        string_env_icon.pixel_shader,\n        filename=\"string_adsr_icon.bmp\",\n        path=FOLDER,\n        overwrite=True,\n    )\n    w_store.write_bitmap(\n        chime_steel_env_icon.bitmap,\n        chime_steel_env_icon.pixel_shader,\n        filename=\"chime_steel_adsr_icon.bmp\",\n        path=FOLDER,\n        overwrite=True,\n    )\n    print(\" completed\")\n\n# Test 12: Write filter object to a file\nif 12 in TEST_LIST:\n    pass\n\n# Test 13: Read filter object from file\nif 13 in TEST_LIST:\n    pass\n\n# Test 14: Display wave table bitmap with transparency\nif 14 in TEST_LIST:\n    print(\"\\nTest 14: Display wave table bitmap with transparency\")\n    new_bitmap = w_store.read_bitmap(\"streetchicken.bmp\", path=\"/sd\")\n    splash.append(\n        displayio.TileGrid(\n            new_bitmap.bitmap, pixel_shader=new_bitmap.pixel_shader, x=170, y=15\n        )\n    )\nprint(\" completed\")\n\n# All tests completed\nprint(\"\\n*** All tests completed ***\")\n# pylint: disable=no-member\nprint(f\"mem_free delta: {gc.mem_free() - m0}\")\nprint(f\"time delta: {time.monotonic() - t0}\")\nwhile True:\n    pass","metadata":{"language":"python","linenums":false}},{"element_type":"text","content":"\n  \n  \n  \n  \n  \n  \n  \n  \n        \u003ch3\u003eSD Card Contents\u003c/h3\u003e\n\u003cp\u003eBefore:\u003c/p\u003e\n      \n\n\n\n\n\n\n\n","metadata":{}},{"element_type":"user_image","content":"https://cdn-learn.adafruit.com/user_assets/assets/000/000/724/original/SD_card_contents_before.png?1709276548","metadata":{"caption":"Contents of SD Card Before Test"}},{"element_type":"text","content":"\n  \n  \n  \n  \n  \n  \n  \n        \u003cp\u003e\u0026nbsp;\u003c/p\u003e\n\u003cp\u003eAfter:\u003c/p\u003e\n      \n\n\n\n\n\n\n","metadata":{}},{"element_type":"user_image","content":"https://cdn-learn.adafruit.com/user_assets/assets/000/000/723/original/SD_card_contents.png?1709276233","metadata":{"caption":"Contents of SD Card After Test Completion"}}]