added ipython notebook examples

This commit is contained in:
2016-03-17 22:31:29 +00:00
parent bbda99edef
commit 4c7458cbbc
15 changed files with 656 additions and 0 deletions
@@ -0,0 +1,203 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 15,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"from sppysound.database import AudioDatabase, Matcher\n",
"import matching_config"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"source_dir = \"./ExampleDatabase\"\n",
"target_dir = \"./ExampleTarget\"\n",
"output_dir = \"./ExampleOutput\"\n",
"analysis_list = [\"rms\", \"f0\"]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Load source and target databases for matching..."
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"source_database = AudioDatabase(source_dir, analysis_list=analysis_list, config=matching_config)\n",
"source_database.load_database(reanalyse=False)\n",
"target_database = AudioDatabase(target_dir, analysis_list=analysis_list, config=matching_config)\n",
"target_database.load_database(reanalyse=False)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"An output database must also be defined. This is to store matching results and synthesis results generated later.\n",
"Note that an analysis list was not defined for this as it will not be analysed"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"output_database = AudioDatabase(output_dir, config=matching_config)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The database must still be loaded to check for previous HDF5 files to use for results"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"output_database.load_database(reanalyse=False)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"A matcher object is then created using the loaded databases, ready to perform matching. The rematch argument can be set to discard any previously found matches from pre-existing HDF5 files, otherwise previously found matches will cause the program to terminate for their preservation."
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"matcher = Matcher(\n",
" source_database,\n",
" target_database,\n",
" output_db=output_database,\n",
" config=matching_config,\n",
" rematch=True\n",
")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The matching is then run using the brute force matcher method. Other methods are not currently available.\n",
"\n",
"Warnings may be produced during this process. These will be silenced in a future revision but do not affect results."
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"matcher.match(\n",
" matcher.brute_force_matcher,\n",
")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The output database will now contain a HDF5 file containing matching data for the two databases. This can be used to synthesize results."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The matching_config file for this demo is:\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",
"matcher_weightings = {\n",
" \"f0\" : 1.,\n",
" \"rms\": 1.\n",
"}\n",
"\n",
"analysis = {\n",
" \"reanalyse\": False\n",
"}\n",
"\n",
"matcher = {\n",
" \"rematch\": False,\n",
" \"grain_size\": 100,\n",
" \"overlap\": 2,\n",
" # Defines the number of matches to keep for synthesis.\n",
" \"match_quantity\": 20\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.10"
}
},
"nbformat": 4,
"nbformat_minor": 0
}
@@ -0,0 +1,164 @@
{
"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": 3,
"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": 4,
"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": 5,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"analysis_list = [\"rms\", \"f0\"]"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"database = AudioDatabase(file_dir, database_dir, analysis_list=analysis_list, config=analysis_config)"
]
},
{
"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": 8,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"database.load_database(reanalyse=False)"
]
},
{
"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.10"
}
},
"nbformat": 4,
"nbformat_minor": 0
}
@@ -0,0 +1,206 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"from sppysound.database import AudioDatabase, Synthesizer\n",
"import synthesis_config"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"source_dir = \"./ExampleDatabase\"\n",
"target_dir = \"./ExampleTarget\"\n",
"output_dir = \"./ExampleOutput\""
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Load source database.\n",
"Also load the F0, RMS and Peak analyses for use with amplitude and pitch enforcement."
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"source_database = AudioDatabase(\n",
" source_dir,\n",
" config=synthesis_config,\n",
" analysis_list={\"f0\", \"rms\", \"peak\"}\n",
")\n",
"source_database.load_database(reanalyse=False)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Load database used to generate matches to source database. \n",
"This is used when enforcing analyses such as RMS and F0. (Original grains are needed to calculate the ratio to alter the synthesized grain by)"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"target_database = AudioDatabase(\n",
" target_dir,\n",
" config=synthesis_config,\n",
" analysis_list={\"f0\", \"rms\", \"peak\"}\n",
")\n",
"target_database.load_database(reanalyse=False)\n",
"\n",
"output_database = AudioDatabase(\n",
" output_dir,\n",
" config=synthesis_config\n",
")\n",
"output_database.load_database(reanalyse=False)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Initialise the synthesizer object used for generating the final output."
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"synthesizer = Synthesizer(source_database, output_database, target_db=target_database, config=synthesis_config)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Run synthesis. As with the matching, warnings may be generated. These have all been accounted for and will be silenced in a future release. The output audio can now be found in the audio folder of ./ExampleOutput"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"collapsed": false,
"scrolled": true
},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"/Users/sam/PerryPerrySource/pysource/pysound/src/sppysound/audiofile.py:665: FutureWarning: comparison to `None` will result in an elementwise object comparison in the future.\n",
" if self.times == None:\n",
"/Users/sam/PerryPerrySource/pysource/pysound/src/sppysound/audiofile.py:297: UserWarning: write_frames::warning::audio data has been clipped while writing to file ./.shift_input.wav.\n",
" return self.pysndfile_object.write_frames(input)\n",
"/Users/sam/.pyenv/versions/2.7.10/lib/python2.7/site-packages/numpy/core/_methods.py:59: RuntimeWarning: Mean of empty slice.\n",
" warnings.warn(\"Mean of empty slice.\", RuntimeWarning)\n",
"/Users/sam/.pyenv/versions/2.7.10/lib/python2.7/site-packages/numpy/core/_methods.py:71: RuntimeWarning: invalid value encountered in double_scalars\n",
" ret = ret.dtype.type(ret / rcount)\n"
]
}
],
"source": [
"synthesizer.synthesize()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The synthesis_config.py file for this demo is:\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",
"\n",
"synthesizer = {\n",
" \"enforce_rms\": True,\n",
" \"enf_rms_ratio_limit\": 5.,\n",
" \"enforce_f0\": True,\n",
" \"enf_f0_ratio_limit\": 10.,\n",
" \"grain_size\": 100,\n",
" \"overlap\": 2,\n",
" \"normalize\" : True,\n",
" # Defines the number of potential grains to choose from matches when\n",
" # synthesizing output.\n",
" \"match_quantity\": 20\n",
"}\n",
"~~~"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
}
],
"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.10"
}
},
"nbformat": 4,
"nbformat_minor": 0
}
+19
View File
@@ -0,0 +1,19 @@
rms = {
"window_size": 100,
"overlap": 2,
}
analysis_dict = {
"f0": "log2_median",
"rms": "mean"
}
analysis = {
"reanalyse": False
}
output_file = {
"samplerate": 44100,
"format": 131075,
"channels": 1
}
+32
View File
@@ -0,0 +1,32 @@
rms = {
"window_size": 100,
"overlap": 2,
}
analysis_dict = {
"f0": "log2_median",
"rms": "mean"
}
matcher_weightings = {
"f0" : 1.,
"rms": 1.
}
analysis = {
"reanalyse": False
}
matcher = {
"rematch": False,
"grain_size": 100,
"overlap": 2,
# Defines the number of matches to keep for synthesis.
"match_quantity": 20
}
output_file = {
"samplerate": 44100,
"format": 131075,
"channels": 1
}
@@ -0,0 +1,32 @@
rms = {
"window_size": 100,
"overlap": 2,
}
analysis_dict = {
"f0": "log2_median",
"rms": "mean"
}
analysis = {
"reanalyse": False
}
output_file = {
"samplerate": 44100,
"format": 131075,
"channels": 1
}
synthesizer = {
"enforce_rms": True,
"enf_rms_ratio_limit": 5.,
"enforce_f0": True,
"enf_f0_ratio_limit": 10.,
"grain_size": 100,
"overlap": 2,
"normalize" : True,
# Defines the number of potential grains to choose from matches when
# synthesizing output.
"match_quantity": 20
}