{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "colab": {
      "provenance": [],
      "collapsed_sections": [
        "kEg9hg2Di2sW",
        "EHQTLC_FinNH",
        "ng-QBzmEtxWs",
        "qb_X_FHTiFH9",
        "V9WJC1e5jZ4f",
        "Yb3OsPolPBXM",
        "nW72SVI1SKbb",
        "xM1683-1Vy5x",
        "kv1LwdF1WjHK",
        "KPzVMFSXYgRB"
      ]
    },
    "kernelspec": {
      "name": "python3",
      "display_name": "Python 3"
    },
    "language_info": {
      "name": "python"
    }
  },
  "cells": [
    {
      "cell_type": "markdown",
      "source": [
        "\n",
        "**Name:**\n",
        "\n",
        "**Date:**\n",
        "\n",
        "**Description of activity:**"
      ],
      "metadata": {
        "id": "WBWRgm19obVt"
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "# Mount Google Drive\n"
      ],
      "metadata": {
        "id": "ng-QBzmEtxWs"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "from google.colab import drive\n",
        "drive.mount('/drive')\n",
        "\n"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "_3laEwn6werC",
        "outputId": "23444bdd-5a1d-4c24-e027-fc58ebc96db8"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "Mounted at /drive\n"
          ]
        }
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "# Import Libraries"
      ],
      "metadata": {
        "id": "0XSi7YkicN35"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "from IPython.display import Audio, display\n",
        "from scipy.io.wavfile import read, write\n",
        "import numpy as np\n",
        "import matplotlib.pyplot as plt\n"
      ],
      "metadata": {
        "id": "u38vjrZHcCYG"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "# Changing volume\n",
        "\n",
        "Copy and paste your <code>changeVolume</code> function from the previous activity into the Code cell below.\n",
        "\n",
        "**Increase and Decrease**\n",
        "\n",
        "Define a new function, <code>increaseAndDecrease</code> that takes an array of sound data as its parameter and will create an array of data for a new sound that is a variation of the original sound with the volume doubled in the first half of the sound and halved in the second half.  You should create this function by copying your <code>changeVolume</code> function, changing the name, and modifying the code in the loop.  There are two ways to do this:\n",
        " either need to use an in statement to check if the index is in \n",
        "\n",
        "1.   Use an in statement to check if the index is in the first half of the sound.  If it is, double the volume; otherwise halve the volume.  OR\n",
        "2.   Modify the loop to go through the first half of the samples, doubling the volume.  Then add a second loop (not nested inside the first loop) that goues through the second half of the samples, halving the volume.\n",
        "\n",
        "Test this function by:\n",
        "\n",
        "\n",
        "1.   Read in a sound file, saving the sample rate and data in variables.\n",
        "2.   Calculate the number of seconds in the sound.\n",
        "3.  Call the <code>increaseAndDecrease</code> function with the data from your sound file, saving the result in a <code>newData</code> variable.\n",
        "4.  Save your <code>newData</code> as a <code>.wav</code> file.\n",
        "5.  Plot the data from the original file together with the new data.\n",
        "6. Display the audio controls to play both sounds.\n",
        "\n"
      ],
      "metadata": {
        "id": "RQzbMo8wcRbL"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "# paste your function to change volume here\n",
        "\n",
        "\n",
        "\n",
        "# function that doubles volume in first half of the sound, halves volume in second half\n",
        "\n",
        "\n"
      ],
      "metadata": {
        "id": "HOVDWZar2ixH"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "# Test the increaseAndDecrease function here (steps are outlined below)\n",
        "\n",
        "# Set the path to a .wav file in your drive\n",
        "soundFile = \n",
        "\n",
        "# Read the file, getting the sample rate and the array of sample data\n",
        "samplerate, data =\n",
        "\n",
        "# get the number of samples in the sound\n",
        "numsamples = \n",
        "\n",
        "# get the length (time in seconds) of the sound\n",
        "seconds = \n",
        "\n",
        "# play the sound, showing a soundbar on the screen\n",
        "display(Audio(soundFile, autoplay=True))\n",
        "\n",
        "# plot the original sound as a wave\n",
        "time = np.linspace(0., seconds, data.shape[0])\n",
        "plt.plot(time, data, label=\"data\")\n",
        "plt.legend()\n",
        "plt.xlabel(\"Time [s]\")\n",
        "plt.ylabel(\"Amplitude\")\n",
        "plt.show()\n",
        "\n",
        "# call your function \n",
        "newData = \n",
        "\n",
        "# save the new data to a new sound file\n",
        "write(\"pathToANewFileOnYourDrive\", samplerate, newSoundData)\n",
        "\n",
        "# plot the new sound as a wave\n",
        "time = np.linspace(0., seconds, data.shape[0])\n",
        "plt.plot(time, newData, label=\" new data\")\n",
        "plt.legend()\n",
        "plt.xlabel(\"Time [s]\")\n",
        "plt.ylabel(\"Amplitude\")\n",
        "plt.show()\n",
        "\n",
        "# play the original sound, showing a soundbar on the screen\n",
        "display(Audio(soundFile, autoplay=True))\n",
        "\n",
        "# play the new sound, showing a soundbar on the screen\n",
        "display(Audio(\"pathToTheNewFileOnYourDrive\", autoplay=True))"
      ],
      "metadata": {
        "id": "OZaBY15kXJ4g"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "**Gradual Decrease**\n",
        "\n",
        "Define a new function that decreases the volume of the sound over time.  It should decrease the volume of the first third of the sound by 30%, then decrease the volume of the second third by 60%, and finally decreases the volume of the last third by 90%.  You may start writing this function by copying your <code>increaseAndDecrease</code> function and making the appropriate changes.  (**Hint/Reminder:** To decrease the volume by 30%, that means there is 70% of the sound remaining.)\n",
        "\n",
        "**(OPTIONAL CHALLENGE if you have time)**\n",
        "Define a fade function that gradually decreases the volume of a sound over time.  Each sample should be slightly softer than the previous sample."
      ],
      "metadata": {
        "id": "RS_A88Je7kT7"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "# Define the gradual decrease function here\n",
        "\n"
      ],
      "metadata": {
        "id": "jYMewN6D7jxS"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "# Normalize the volume\n",
        "\n",
        "The following function, <code>normalize</code> is from the reading [Introduction to Sounds and Volume](http://www.cs.kzoo.edu/cs103/Readings/IntroSoundsAndVolume.pdf).  It will make a sound as loud as possible, without clipping.  It uses the largest sample value to determine the multiplication factor and then calls on the <code>changeVolume</code> function to create the data for the new sound.  Load this file and test it below."
      ],
      "metadata": {
        "id": "qb_X_FHTiFH9"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "# This function makes a sound as loud as possible\n",
        "# without clipping\n",
        "def normalize(soundData):\n",
        "  # find the largest sample value\n",
        "  largest = 0\n",
        "  for index in range(len(soundData)):\n",
        "    if abs(soundData[index]) > largest:\n",
        "      largest = abs(soundData[index])\n",
        "\n",
        "  # compute the multiplication factor\n",
        "  factor = 32767.0 / largest\n",
        "\n",
        "  new_data = changeVolume(soundData, factor)\n",
        "  return new_data\n"
      ],
      "metadata": {
        "id": "6t4b1xQCXUZy"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "# Test normalize here\n",
        "\n",
        "\n",
        "\n"
      ],
      "metadata": {
        "id": "yKQXPVmnfNos"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "# Saving and Submitting\n",
        "\n",
        "Once you have created results that you are pleased with, skim through your notebook and make sure everything looks the way you expect.  Make sure that your Code cells have been run and that your results for the sound plots are showing.  If it all looks good, save this notebook as a pdf.  (Go to File -> Print -> Save as PDF)\n",
        "\n",
        "Submit your PDF file on Kit."
      ],
      "metadata": {
        "id": "HAUI64Je9cAv"
      }
    }
  ]
}