{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Creating a database of analysed audio files\n",
    "Database objects are used to group audio files and their analyses into a single object in order to perform further operations (such as matching).\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "from sppysound.database import AudioDatabase\n",
    "import analysis_config"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Specify the directory to search recursively for audio files in."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "file_dir = \"./ExampleFiles\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Specify the directory to generate the database in."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "database_dir = \"./ExampleDatabase\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Create a list of analysis trings that determine the descriptors to be generated by the object"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "analysis_list = [\"rms\", \"f0\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "database = AudioDatabase(\n",
    "    file_dir, \n",
    "    database_dir, \n",
    "    analysis_list=analysis_list, \n",
    "    config=analysis_config\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The load_database method will search for any pre-existing analyses and load these, aswell as generating new analyses that aren't already present. These will be organized and stored in the database directory in \"data\" and \"audio\" sub-directories."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "database.load_database(reanalyse=True)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Once analysed, the database object can be used with objects such as the matcher object it's entries to other databases."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "A \"config.py\" file is required to be used as a configuration module for the database. This will contain configurations for initialising audio files contained in the database. The example config.py looks like this:\n",
    "\n",
    "~~~python\n",
    "rms = {\n",
    "    \"window_size\": 100,\n",
    "    \"overlap\": 2,\n",
    "}\n",
    "\n",
    "analysis_dict = {\n",
    "    \"f0\": \"log2_median\",\n",
    "    \"rms\": \"mean\"\n",
    "}\n",
    "\n",
    "analysis = {\n",
    "    \"reanalyse\": False\n",
    "}\n",
    "\n",
    "output_file = {\n",
    "    \"samplerate\": 44100,\n",
    "    \"format\": 131075,\n",
    "    \"channels\": 1\n",
    "}\n",
    "~~~"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.11"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
