{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "ccc61334-38ac-4c86-a86a-bd2975d5a60d",
   "metadata": {},
   "source": [
    "# lib_audio_dsp pipeline designer\n",
    "\n",
    "In this file you can generate the DSP pipeline of your choice.\n",
    "\n",
    "Below you will find 3 cells which can be modified and executed to configure, tune and run the desired pipeline.\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0acbc09d",
   "metadata": {},
   "source": [
    "1. This is the pipeline design cell. Here you must break the DSP pipeline down into threads and use the provided DSP stages to create a pipeline. Running this cell will produce a diagram showing your pipeline. Make sure to capture each stage in your pipeline as a variable, as it will be needed in the next step.\n",
    "Note that every time the pipeline cell is changed, the app must be regenerated before the tuning stage can work correctly as the stage indices used for communication may have changed."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f3806bd0-99e0-42b6-a084-e9a2f17ba7dc",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Pipeline design stage\n",
    "\n",
    "from audio_dsp.design.pipeline import Pipeline\n",
    "from audio_dsp.stages import *\n",
    "\n",
    "p, inputs = Pipeline.begin(1, fs=48000)\n",
    "\n",
    "# i is a list of pipeline inputs. \"lowshelf\" is a label for this instance of Biquad.\n",
    "# The new variable x is the output of the lowshelf Biquad\n",
    "x = p.stage(Biquad, inputs, \"lowshelf\")\n",
    "\n",
    "# The output of lowshelf \"x\" is passed as the input to the\n",
    "# highshelf. The variable x is reassigned to the outputs of the new Biquad.\n",
    "x = p.stage(Biquad, x, \"highshelf\")\n",
    "\n",
    "# Connect highshelf to the limiter. Labels are optional, however they are required\n",
    "# if the stage will be tuned later.\n",
    "x = p.stage(LimiterPeak, x)\n",
    "\n",
    "# Finally connect to the output of the pipeline.\n",
    "p.set_outputs(x)\n",
    "\n",
    "p.draw()\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "27e9d385",
   "metadata": {},
   "source": [
    "2. This is the tuning cell. First time through this can be ignored, but once your pipeline is running on a connected device, this cell can be updated and executed to update each pipeline stage live."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "89d245d6-b908-4815-ae22-fa10e40d65dc",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Make a low shelf with a centre frequency of 200 Hz, q of 0.7 and gain of +6 dB\n",
    "p[\"lowshelf\"].make_lowshelf(200, 0.7, 6)\n",
    "p[\"lowshelf\"].plot_frequency_response()\n",
    "\n",
    "# Make a high shelf with a centre frequency of 4000 Hz, q of 0.7 and gain of +6 dB\n",
    "p[\"highshelf\"].make_highshelf(4000, 0.7, 6)\n",
    "p[\"highshelf\"].plot_frequency_response()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2113ddc3",
   "metadata": {},
   "source": [
    "3. This is the build and run cell. This stage generates an application which uses your pipeline. The tuning parameters set in the previous cell are baked in the application."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "32492495-d82a-45ba-87e3-80b01de38981",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Build and run\n",
    "\n",
    "from audio_dsp.design.pipeline import generate_dsp_main\n",
    "from audio_dsp.design.build_utils import XCommonCMakeHelper\n",
    "\n",
    "b = XCommonCMakeHelper()\n",
    "\n",
    "generate_dsp_main(p)\n",
    "b.configure_build_run()\n"
   ]
  }
 ],
 "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",
   "version": "3.12.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
