{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "Dependencies\n", "===============" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[nltk_data] Downloading package timit to /home/boisgera/nltk_data...\n", "[nltk_data] Package timit is already up-to-date!\n" ] } ], "source": [ "from __future__ import division\n", "\n", "from IPython.display import *\n", "\n", "from numpy import *; seterr(all=\"ignore\")\n", "from numpy import linalg\n", "from numpy import random\n", "\n", "%matplotlib notebook\n", "from matplotlib.pyplot import *\n", "\n", "import wish\n", "\n", "import audio.wave as wave\n", "from audio.filters import FIR, AR\n", "import audio.frames\n", "import audio.index; import nltk; nltk.download(\"timit\")\n", "from audio.lp import lp\n", "from audio.quantizers import Quantizer" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "df = 16000\n", "dt = 1.0 / df" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Sandbox\n", "========" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "FIR\n", "---" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([0., 1., 2., 3., 4.])" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "fir = FIR([0.0, 1.0])\n", "fir([1,2,3,4,5])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Linear Prediction" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "/home/boisgera/miniconda2/envs/audio/lib/python2.7/site-packages/audio/lp.py:244: FutureWarning: `rcond` parameter will change to the default of machine precision times ``max(M, N)`` where M and N are the input matrix dimensions.\n", "To use the future default and silence this warning we advise to pass `rcond=None`, to keep using the old, explicitly pass `rcond=-1`.\n", " a_, _, _ ,_ = np.linalg.lstsq(A, b) # can't trust the residues (may be [])\n" ] }, { "data": { "text/plain": [ "array([0., 1.])" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x = 4*[-1.0, 1.0]\n", "#figure()\n", "#plot(x, \"k+\"); grid()\n", "lp(x,order=2)#, method=\"autocorrelation\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Audio Source (NLTK)\n", "=============" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "brush fires are common in the dry underbrush of nevada" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "\n", " \n", " " ], "text/plain": [ "" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "utterances = audio.index.search(type=audio.index.Utterance)\n", "utterance = utterances[16]\n", "display(utterance)\n", "data = utterance.audio\n", "audio.wave.write(data, \"voice.wav\", df=16000)\n", "Audio(\"voice.wav\")" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "phone: h# (brush fires are common in the dry underbrush of nevada).\n", "word: brush [b-r-ah-sh]\n", "word: fires [f-ay-axr-z]\n", "word: are [axr]\n", "word: common [kcl-k-aa-m-ix-n]\n", "word: in [ix-n]\n", "word: the [dh-ix]\n", "word: dry [dcl-d-r-ay]\n", "phone: q (brush fires are common in the dry underbrush of nevada).\n", "word: underbrush [ah-n-axr-bcl-b-r-ah-sh]\n", "word: of [ax-v]\n", "word: nevada [n-ax-v-aa-dx-ax]\n", "phone: h# (brush fires are common in the dry underbrush of nevada).\n" ] } ], "source": [ "for item in utterance:\n", " print type(item).__name__.lower() + \": \" + str(item)" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/plain": [ " 0. ey (away [ix-w-ey]).\n", " 1. ey (taken [tcl-t-ey-kcl-k-ix-nx]).\n", " 2. ey (spray [s-pcl-p-r-ey]).\n", " 3. ey (exchanged [ix-kcl-s-sh-ey-n-zh]).\n", " 4. ey (vaguely [v-ey-gcl-g-l-iy]).\n", " 5. ey (make [m-ey-kcl-k]).\n", " 6. ey (occasionally [ow-kcl-k-ey-zh-l-ix]).\n", " 7. ey (monday [m-ah-n-d-ey]).\n", " 8. ey (always [ao-l-w-ey-z]).\n", " 9. ey (situation [s-ix-tcl-ch-uw-ey-sh-en]).\n", " 10. ey (clay [kcl-k-l-ey]).\n", " 11. ey (straightforward [s-tcl-t-r-ey-q-f-ao-w-axr-dx]).\n", " 12. ey (rachel's [r-ey-tcl-ch-el-z]).\n", " 13. ey (maybe [m-ey-bcl-b-iy]).\n", " 14. ey (they [dh-ey]).\n", " 15. ey (take [tcl-t-ey-kcl-k]).\n", " 16. ey (traits [tcl-t-r-ey-tcl-s]).\n", " 17. ey (today's [tcl-t-ix-dx-ey-z]).\n", " 18. ey (made [m-ey-dx]).\n", " 19. ey (arbitrate [q-aa-r-bcl-b-ix-tcl-t-r-ey-tcl-t]).\n", " 20. ey (cases [kcl-k-ey-s-ix-z]).\n", " 21. ey (straight [s-tcl-t-r-ey-tcl]).\n", " 22. ey (play [pcl-p-l-ey]).\n", " 23. ey (repainted [r-iy-pcl-p-ey-nx-ix-dcl]).\n", " 24. ey (waste [w-ey-s-tcl-t]).\n", " 25. ey (masquerade [m-ae-s-kcl-k-axr-r-ey-dcl]).\n", " 26. ey (imagination [ix-m-ae-dcl-jh-ix-n-ey-sh-ix-n]).\n", " 27. ey (grade [gcl-g-r-ey-dx]).\n", " 28. ey (a [ey]).\n", " 29. ey (birthday [b-er-th-dcl-dh-ey]).\n", " 30. ey (cupcakes [kcl-k-ah-pcl-k-ey-kcl-k-s]).\n", " 31. ey (pays [pcl-p-ey-z]).\n", " 32. ey (instantaneous [ih-n-s-tcl-t-ix-n-tcl-t-ey-n-iy-ix-s]).\n", " 33. ey (contained [kcl-k-ix-n-tcl-t-ey-n-dcl]).\n", " 34. ey (that [dh-ey-eh-tcl-t]).\n", " 35. ey (slavery [s-l-ey-v-r-iy]).\n", " 36. ey (change [ch-ey-n-dcl-jh]).\n", " 37. ey (that [dh-ey-eh-tcl-t]).\n", " 38. ey (unappreciated [ah-n-ax-pcl-p-r-iy-sh-iy-ey-dx-ix-dcl]).\n", " 39. ey (today [t-ax-h-dcl-d-ey]).\n", " 40. ey (coeducational [kcl-k-ow-eh-dcl-jh-ax-h-kcl-k-ey-sh-epi-n-el]).\n", " 41. ey (holiday [hv-aa-l-eh-dcl-d-ey]).\n", " 42. ey (aprons [ey-pcl-p-r-ix-n-z]).\n", " 43. ey (available [ax-v-ey-l-ax-bcl-b-el]).\n", " 44. ey (data [dcl-d-ey-dx-ix]).\n", " 45. ey (famous [f-ey-m-ax-s]).\n", " 46. ey (names [n-ey-m-z]).\n", " 47. ey (a [ey]).\n", " 48. ey (made [m-ey-dcl]).\n", " 49. ey (agency [ey-dcl-jh-ix-n-s-iy]).\n", " 50. ey (a [ey]).\n", " 51. ey (great [gcl-g-r-ey-tcl]).\n", " 52. ey (bluejay [bcl-b-l-ux-dcl-jh-ey]).\n", " 53. ey (makes [m-ey-kcl-k-s]).\n", " 54. ey (names [n-ey-m-z]).\n", " 55. ey (interchangeably [q-ih-n-tcl-t-axr-tcl-ch-ey-n-dcl-jh-ix-bcl-b-l-iy]).\n", " 56. ey (able [q-ey-bcl-el]).\n", " 57. ey (hesitate [hv-eh-z-ix-tcl-t-ey-tcl]).\n", " 58. ey (they [dh-ey]).\n", " 59. ey (remained [r-iy-m-ey-n-dcl-d]).\n", " 60. ey (may [m-ey]).\n", " 61. ey (overweight [ow-v-ax-w-ey-tcl]).\n", " 62. ey (display [dcl-d-ih-s-pcl-p-l-ey]).\n", " 63. ey (misplaced [m-ix-s-pcl-p-l-ey-s-tcl-t]).\n", " 64. ey (ate [ey-tcl]).\n", " 65. ey (face [f-ey-s]).\n", " 66. ey (they [dh-ey]).\n", " 67. ey (paper [pcl-p-ey-pcl-p-ax]).\n", " 68. ey (place [pcl-p-l-ey-s]).\n", " 69. ey (they [dh-ey]).\n", " 70. ey (eyestrain [ay-s-tcl-t-r-ey-n]).\n", " 71. ey (a [q-ey]).\n", " 72. ey (upgrade [q-ah-pcl-g-r-ey-dcl-jh]).\n", " 73. ey (status [tcl-t-ey-dcl-d-ix-s]).\n", " 74. ey (may [m-ey]).\n", " 75. ey (cooperated [kcl-k-ow-ao-pcl-p-er-ey-dx-ix-dcl]).\n", " 76. ey (make [m-ey-kcl]).\n", " 77. ey (operates [aa-pcl-p-axr-r-ey-tcl-s]).\n", " 78. ey (daytime [dcl-d-ey-tcl-t-ay-m]).\n", " 79. ey (save [s-ey-v]).\n", " 80. ey (they've [dh-ey-v]).\n", " 81. ey (agency [ey-dcl-jh-ih-n-s-iy]).\n", " 82. ey (alien [ey-l-iy-ix-n]).\n", " 83. ey (interpretation [ix-n-tcl-t-er-v-axr-tcl-t-ey-sh-en]).\n", " 84. ey (hatred [hv-ey-tcl-t-r-ix-dcl]).\n", " 85. ey (celebrates [s-eh-l-ax-bcl-b-r-ey-tcl-s]).\n" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "audio.index.search(\"ey\", type=audio.index.Phone)" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", " \n", " " ], "text/plain": [ "" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data = wave.read(\"sounds/whisper.wav\")[0]\n", "Audio(\"sounds/whisper.wav\")" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [], "source": [ "audio.wave.write(data, \"voice.wav\", df=16000)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Short-Term Prediction\n", "======================" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [], "source": [ "class STP(Quantizer):\n", " \"Short-Term Predictor\"\n", " def __init__(self, order=16, method=\"autocorrelation\"):\n", " self.fir = FIR(a=r_[1.0, zeros(order)]) \n", " self.ar = AR(a=zeros(order))\n", " self.order = order\n", " self.method = method\n", " def encode(self, data):\n", " if self.method == \"covariance\" and self.order >= len(data):\n", " raise ValueError(\"not enough data samples\")\n", " a = lp(data, order=self.order, method=self.method)\n", " self.fir.a[:] = r_[1.0, -a]\n", " error = self.fir(data)\n", " return (a, error) \n", " def decode(self, data):\n", " a, error = data\n", " self.ar.a[:] = a\n", " return self.ar(error)" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [], "source": [ "def stp_error(data, T=0.02, order=16, method=\"autocorrelation\"):\n", " length = len(data)\n", " n = int(T * df) # number of samples for T s at the given frequency.\n", " frames = audio.frames.split(data, n, pad=True)\n", " stp = STP(order=order, method=method)\n", " error = zeros(n*len(frames))\n", " for i, frame in enumerate(frames):\n", " a, error_frame = stp.encode(frame)\n", " error[i*n:(i+1)*n] = error_frame\n", " return error[:length]" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "data": { "application/javascript": [ "/* Put everything inside the global mpl namespace */\n", "window.mpl = {};\n", "\n", "\n", "mpl.get_websocket_type = function() {\n", " if (typeof(WebSocket) !== 'undefined') {\n", " return WebSocket;\n", " } else if (typeof(MozWebSocket) !== 'undefined') {\n", " return MozWebSocket;\n", " } else {\n", " alert('Your browser does not have WebSocket support.' +\n", " 'Please try Chrome, Safari or Firefox ≥ 6. ' +\n", " 'Firefox 4 and 5 are also supported but you ' +\n", " 'have to enable WebSockets in about:config.');\n", " };\n", "}\n", "\n", "mpl.figure = function(figure_id, websocket, ondownload, parent_element) {\n", " this.id = figure_id;\n", "\n", " this.ws = websocket;\n", "\n", " this.supports_binary = (this.ws.binaryType != undefined);\n", "\n", " if (!this.supports_binary) {\n", " var warnings = document.getElementById(\"mpl-warnings\");\n", " if (warnings) {\n", " warnings.style.display = 'block';\n", " warnings.textContent = (\n", " \"This browser does not support binary websocket messages. \" +\n", " \"Performance may be slow.\");\n", " }\n", " }\n", "\n", " this.imageObj = new Image();\n", "\n", " this.context = undefined;\n", " this.message = undefined;\n", " this.canvas = undefined;\n", " this.rubberband_canvas = undefined;\n", " this.rubberband_context = undefined;\n", " this.format_dropdown = undefined;\n", "\n", " this.image_mode = 'full';\n", "\n", " this.root = $('
');\n", " this._root_extra_style(this.root)\n", " this.root.attr('style', 'display: inline-block');\n", "\n", " $(parent_element).append(this.root);\n", "\n", " this._init_header(this);\n", " this._init_canvas(this);\n", " this._init_toolbar(this);\n", "\n", " var fig = this;\n", "\n", " this.waiting = false;\n", "\n", " this.ws.onopen = function () {\n", " fig.send_message(\"supports_binary\", {value: fig.supports_binary});\n", " fig.send_message(\"send_image_mode\", {});\n", " if (mpl.ratio != 1) {\n", " fig.send_message(\"set_dpi_ratio\", {'dpi_ratio': mpl.ratio});\n", " }\n", " fig.send_message(\"refresh\", {});\n", " }\n", "\n", " this.imageObj.onload = function() {\n", " if (fig.image_mode == 'full') {\n", " // Full images could contain transparency (where diff images\n", " // almost always do), so we need to clear the canvas so that\n", " // there is no ghosting.\n", " fig.context.clearRect(0, 0, fig.canvas.width, fig.canvas.height);\n", " }\n", " fig.context.drawImage(fig.imageObj, 0, 0);\n", " };\n", "\n", " this.imageObj.onunload = function() {\n", " fig.ws.close();\n", " }\n", "\n", " this.ws.onmessage = this._make_on_message_function(this);\n", "\n", " this.ondownload = ondownload;\n", "}\n", "\n", "mpl.figure.prototype._init_header = function() {\n", " var titlebar = $(\n", " '
');\n", " var titletext = $(\n", " '
');\n", " titlebar.append(titletext)\n", " this.root.append(titlebar);\n", " this.header = titletext[0];\n", "}\n", "\n", "\n", "\n", "mpl.figure.prototype._canvas_extra_style = function(canvas_div) {\n", "\n", "}\n", "\n", "\n", "mpl.figure.prototype._root_extra_style = function(canvas_div) {\n", "\n", "}\n", "\n", "mpl.figure.prototype._init_canvas = function() {\n", " var fig = this;\n", "\n", " var canvas_div = $('
');\n", "\n", " canvas_div.attr('style', 'position: relative; clear: both; outline: 0');\n", "\n", " function canvas_keyboard_event(event) {\n", " return fig.key_event(event, event['data']);\n", " }\n", "\n", " canvas_div.keydown('key_press', canvas_keyboard_event);\n", " canvas_div.keyup('key_release', canvas_keyboard_event);\n", " this.canvas_div = canvas_div\n", " this._canvas_extra_style(canvas_div)\n", " this.root.append(canvas_div);\n", "\n", " var canvas = $('');\n", " canvas.addClass('mpl-canvas');\n", " canvas.attr('style', \"left: 0; top: 0; z-index: 0; outline: 0\")\n", "\n", " this.canvas = canvas[0];\n", " this.context = canvas[0].getContext(\"2d\");\n", "\n", " var backingStore = this.context.backingStorePixelRatio ||\n", "\tthis.context.webkitBackingStorePixelRatio ||\n", "\tthis.context.mozBackingStorePixelRatio ||\n", "\tthis.context.msBackingStorePixelRatio ||\n", "\tthis.context.oBackingStorePixelRatio ||\n", "\tthis.context.backingStorePixelRatio || 1;\n", "\n", " mpl.ratio = (window.devicePixelRatio || 1) / backingStore;\n", "\n", " var rubberband = $('');\n", " rubberband.attr('style', \"position: absolute; left: 0; top: 0; z-index: 1;\")\n", "\n", " var pass_mouse_events = true;\n", "\n", " canvas_div.resizable({\n", " start: function(event, ui) {\n", " pass_mouse_events = false;\n", " },\n", " resize: function(event, ui) {\n", " fig.request_resize(ui.size.width, ui.size.height);\n", " },\n", " stop: function(event, ui) {\n", " pass_mouse_events = true;\n", " fig.request_resize(ui.size.width, ui.size.height);\n", " },\n", " });\n", "\n", " function mouse_event_fn(event) {\n", " if (pass_mouse_events)\n", " return fig.mouse_event(event, event['data']);\n", " }\n", "\n", " rubberband.mousedown('button_press', mouse_event_fn);\n", " rubberband.mouseup('button_release', mouse_event_fn);\n", " // Throttle sequential mouse events to 1 every 20ms.\n", " rubberband.mousemove('motion_notify', mouse_event_fn);\n", "\n", " rubberband.mouseenter('figure_enter', mouse_event_fn);\n", " rubberband.mouseleave('figure_leave', mouse_event_fn);\n", "\n", " canvas_div.on(\"wheel\", function (event) {\n", " event = event.originalEvent;\n", " event['data'] = 'scroll'\n", " if (event.deltaY < 0) {\n", " event.step = 1;\n", " } else {\n", " event.step = -1;\n", " }\n", " mouse_event_fn(event);\n", " });\n", "\n", " canvas_div.append(canvas);\n", " canvas_div.append(rubberband);\n", "\n", " this.rubberband = rubberband;\n", " this.rubberband_canvas = rubberband[0];\n", " this.rubberband_context = rubberband[0].getContext(\"2d\");\n", " this.rubberband_context.strokeStyle = \"#000000\";\n", "\n", " this._resize_canvas = function(width, height) {\n", " // Keep the size of the canvas, canvas container, and rubber band\n", " // canvas in synch.\n", " canvas_div.css('width', width)\n", " canvas_div.css('height', height)\n", "\n", " canvas.attr('width', width * mpl.ratio);\n", " canvas.attr('height', height * mpl.ratio);\n", " canvas.attr('style', 'width: ' + width + 'px; height: ' + height + 'px;');\n", "\n", " rubberband.attr('width', width);\n", " rubberband.attr('height', height);\n", " }\n", "\n", " // Set the figure to an initial 600x600px, this will subsequently be updated\n", " // upon first draw.\n", " this._resize_canvas(600, 600);\n", "\n", " // Disable right mouse context menu.\n", " $(this.rubberband_canvas).bind(\"contextmenu\",function(e){\n", " return false;\n", " });\n", "\n", " function set_focus () {\n", " canvas.focus();\n", " canvas_div.focus();\n", " }\n", "\n", " window.setTimeout(set_focus, 100);\n", "}\n", "\n", "mpl.figure.prototype._init_toolbar = function() {\n", " var fig = this;\n", "\n", " var nav_element = $('
')\n", " nav_element.attr('style', 'width: 100%');\n", " this.root.append(nav_element);\n", "\n", " // Define a callback function for later on.\n", " function toolbar_event(event) {\n", " return fig.toolbar_button_onclick(event['data']);\n", " }\n", " function toolbar_mouse_event(event) {\n", " return fig.toolbar_button_onmouseover(event['data']);\n", " }\n", "\n", " for(var toolbar_ind in mpl.toolbar_items) {\n", " var name = mpl.toolbar_items[toolbar_ind][0];\n", " var tooltip = mpl.toolbar_items[toolbar_ind][1];\n", " var image = mpl.toolbar_items[toolbar_ind][2];\n", " var method_name = mpl.toolbar_items[toolbar_ind][3];\n", "\n", " if (!name) {\n", " // put a spacer in here.\n", " continue;\n", " }\n", " var button = $('');\n", " button.click(method_name, toolbar_event);\n", " button.mouseover(tooltip, toolbar_mouse_event);\n", " nav_element.append(button);\n", " }\n", "\n", " // Add the status bar.\n", " var status_bar = $('');\n", " nav_element.append(status_bar);\n", " this.message = status_bar[0];\n", "\n", " // Add the close button to the window.\n", " var buttongrp = $('
');\n", " var button = $('');\n", " button.click(function (evt) { fig.handle_close(fig, {}); } );\n", " button.mouseover('Stop Interaction', toolbar_mouse_event);\n", " buttongrp.append(button);\n", " var titlebar = this.root.find($('.ui-dialog-titlebar'));\n", " titlebar.prepend(buttongrp);\n", "}\n", "\n", "mpl.figure.prototype._root_extra_style = function(el){\n", " var fig = this\n", " el.on(\"remove\", function(){\n", "\tfig.close_ws(fig, {});\n", " });\n", "}\n", "\n", "mpl.figure.prototype._canvas_extra_style = function(el){\n", " // this is important to make the div 'focusable\n", " el.attr('tabindex', 0)\n", " // reach out to IPython and tell the keyboard manager to turn it's self\n", " // off when our div gets focus\n", "\n", " // location in version 3\n", " if (IPython.notebook.keyboard_manager) {\n", " IPython.notebook.keyboard_manager.register_events(el);\n", " }\n", " else {\n", " // location in version 2\n", " IPython.keyboard_manager.register_events(el);\n", " }\n", "\n", "}\n", "\n", "mpl.figure.prototype._key_event_extra = function(event, name) {\n", " var manager = IPython.notebook.keyboard_manager;\n", " if (!manager)\n", " manager = IPython.keyboard_manager;\n", "\n", " // Check for shift+enter\n", " if (event.shiftKey && event.which == 13) {\n", " this.canvas_div.blur();\n", " event.shiftKey = false;\n", " // Send a \"J\" for go to next cell\n", " event.which = 74;\n", " event.keyCode = 74;\n", " manager.command_mode();\n", " manager.handle_keydown(event);\n", " }\n", "}\n", "\n", "mpl.figure.prototype.handle_save = function(fig, msg) {\n", " fig.ondownload(fig, null);\n", "}\n", "\n", "\n", "mpl.find_output_cell = function(html_output) {\n", " // Return the cell and output element which can be found *uniquely* in the notebook.\n", " // Note - this is a bit hacky, but it is done because the \"notebook_saving.Notebook\"\n", " // IPython event is triggered only after the cells have been serialised, which for\n", " // our purposes (turning an active figure into a static one), is too late.\n", " var cells = IPython.notebook.get_cells();\n", " var ncells = cells.length;\n", " for (var i=0; i= 3 moved mimebundle to data attribute of output\n", " data = data.data;\n", " }\n", " if (data['text/html'] == html_output) {\n", " return [cell, data, j];\n", " }\n", " }\n", " }\n", " }\n", "}\n", "\n", "// Register the function which deals with the matplotlib target/channel.\n", "// The kernel may be null if the page has been refreshed.\n", "if (IPython.notebook.kernel != null) {\n", " IPython.notebook.kernel.comm_manager.register_target('matplotlib', mpl.mpl_figure_comm);\n", "}\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/plain": [ "[]" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "error = stp_error(data)\n", "SNR2 = mean(data*data) / mean(error*error)\n", "print \"SNR\", 10*log10(SNR2), \"dB\"\n", "\n", "T = 20.0 / 1000 # 20 ms\n", "n = int(df * T)\n", "data_frames = audio.frames.split(data, n, pad=True)\n", "error_frames = audio.frames.split(error, n, pad=True)\n", "\n", "fig, (ax1, ax2) = subplots(2,1, sharex=True)\n", "SNRs = [10.0 * log10(mean(d*d) / mean(e*e)) for d,e in zip(data_frames, error_frames)]\n", "ax1.plot(data, \"k\", alpha=0.1, label=\"original\")\n", "ax1.plot(error, \"r\", label=\"stp residual\")\n", "ax1.set_title(\"Waveforms\")\n", "ax1.axis(\"tight\")\n", "ax1.legend()\n", "ax1.grid()\n", "ax2.plot(r_[0:len(SNRs)] * n, SNRs, \"b\")\n", "ax2.plot([0,len(data_frames)*n], [10*log10(SNR2), 10*log10(SNR2)], \"k--\")\n", "ax2.set_xlabel(\"sample index\")\n", "ax2.set_ylabel(\"SNR [dB]\")\n", "ax2.set_title(\"Signal-to-Noise Ratio\")\n", "ax2.grid()\n", "ax1.plot(r_[start:end-1],data[start:end-1], alpha=0.2,color=\"g\")\n" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", " \n", " " ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "\n", " \n", " " ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "error = error / amax(abs(error))\n", "wave.write(error, \"voice-error-stp.wav\", df=df)\n", "display(Audio(\"voice.wav\"))\n", "display(Audio(\"voice-error-stp.wav\"))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Pitch Estimation\n", "==========" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [], "source": [ "def ltp_parameters(history, frame,\n", " offset_min=1, offset_max=None,\n", " gain_min=0.0, gain_max=inf,\n", " SNR_min=1.0, SNR_max=inf,\n", " select=argmax,\n", " returns=\"offset, gain\"):\n", " p = len(history)\n", " data = r_[history, frame] # full data\n", " m = len(frame)\n", " n = len(data)\n", " nxcorrs = zeros(p+1)\n", " gains = zeros(p+1)\n", " SNRs = zeros(p+1)\n", " valids = zeros(p+1, dtype=bool)\n", " frame_norm = linalg.norm(frame)\n", " normed_frame = frame / frame_norm\n", " for i in range(p + 1):\n", " windowed_data = data[n-i-m:n-i]\n", " windowed_data_norm = linalg.norm(windowed_data)\n", " normed_windowed_data = windowed_data / windowed_data_norm\n", " nxcorr = nxcorrs[i] = dot(normed_frame, normed_windowed_data)\n", " SNR = SNRs[i] = 1.0 / sqrt(1 - nxcorr*nxcorr)\n", " #print \">\", SNR\n", " gain = gains[i] = nxcorr / windowed_data_norm * frame_norm\n", " valid = True\n", " if offset_min is not None:\n", " valid = valid and (offset_min <= i)\n", " if offset_max is not None:\n", " valid = valid and (i <= offset_max)\n", " valid = valid and (gain_min <= gain <= gain_max)\n", " valid = valid and (SNR_min <= SNR <= SNR_max)\n", " valids[i] = valid\n", "\n", " criteria = SNRs.copy() \n", " criteria[logical_not(valids)] = -inf\n", " offset = select(criteria)\n", " if not valids[offset]: # everything is invalid!\n", " raise ValueError(\"no valid set of parameters\")\n", " else:\n", " gain = gains[offset]\n", " nxcorr = nxcorrs[offset]\n", " SNR = SNRs[offset]\n", "\n", " return wish.grant(returns)" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "offset: 25 gain: 1.061276723494138\n" ] }, { "data": { "application/javascript": [ "/* Put everything inside the global mpl namespace */\n", "window.mpl = {};\n", "\n", "\n", "mpl.get_websocket_type = function() {\n", " if (typeof(WebSocket) !== 'undefined') {\n", " return WebSocket;\n", " } else if (typeof(MozWebSocket) !== 'undefined') {\n", " return MozWebSocket;\n", " } else {\n", " alert('Your browser does not have WebSocket support.' +\n", " 'Please try Chrome, Safari or Firefox ≥ 6. ' +\n", " 'Firefox 4 and 5 are also supported but you ' +\n", " 'have to enable WebSockets in about:config.');\n", " };\n", "}\n", "\n", "mpl.figure = function(figure_id, websocket, ondownload, parent_element) {\n", " this.id = figure_id;\n", "\n", " this.ws = websocket;\n", "\n", " this.supports_binary = (this.ws.binaryType != undefined);\n", "\n", " if (!this.supports_binary) {\n", " var warnings = document.getElementById(\"mpl-warnings\");\n", " if (warnings) {\n", " warnings.style.display = 'block';\n", " warnings.textContent = (\n", " \"This browser does not support binary websocket messages. \" +\n", " \"Performance may be slow.\");\n", " }\n", " }\n", "\n", " this.imageObj = new Image();\n", "\n", " this.context = undefined;\n", " this.message = undefined;\n", " this.canvas = undefined;\n", " this.rubberband_canvas = undefined;\n", " this.rubberband_context = undefined;\n", " this.format_dropdown = undefined;\n", "\n", " this.image_mode = 'full';\n", "\n", " this.root = $('
');\n", " this._root_extra_style(this.root)\n", " this.root.attr('style', 'display: inline-block');\n", "\n", " $(parent_element).append(this.root);\n", "\n", " this._init_header(this);\n", " this._init_canvas(this);\n", " this._init_toolbar(this);\n", "\n", " var fig = this;\n", "\n", " this.waiting = false;\n", "\n", " this.ws.onopen = function () {\n", " fig.send_message(\"supports_binary\", {value: fig.supports_binary});\n", " fig.send_message(\"send_image_mode\", {});\n", " if (mpl.ratio != 1) {\n", " fig.send_message(\"set_dpi_ratio\", {'dpi_ratio': mpl.ratio});\n", " }\n", " fig.send_message(\"refresh\", {});\n", " }\n", "\n", " this.imageObj.onload = function() {\n", " if (fig.image_mode == 'full') {\n", " // Full images could contain transparency (where diff images\n", " // almost always do), so we need to clear the canvas so that\n", " // there is no ghosting.\n", " fig.context.clearRect(0, 0, fig.canvas.width, fig.canvas.height);\n", " }\n", " fig.context.drawImage(fig.imageObj, 0, 0);\n", " };\n", "\n", " this.imageObj.onunload = function() {\n", " fig.ws.close();\n", " }\n", "\n", " this.ws.onmessage = this._make_on_message_function(this);\n", "\n", " this.ondownload = ondownload;\n", "}\n", "\n", "mpl.figure.prototype._init_header = function() {\n", " var titlebar = $(\n", " '
');\n", " var titletext = $(\n", " '
');\n", " titlebar.append(titletext)\n", " this.root.append(titlebar);\n", " this.header = titletext[0];\n", "}\n", "\n", "\n", "\n", "mpl.figure.prototype._canvas_extra_style = function(canvas_div) {\n", "\n", "}\n", "\n", "\n", "mpl.figure.prototype._root_extra_style = function(canvas_div) {\n", "\n", "}\n", "\n", "mpl.figure.prototype._init_canvas = function() {\n", " var fig = this;\n", "\n", " var canvas_div = $('
');\n", "\n", " canvas_div.attr('style', 'position: relative; clear: both; outline: 0');\n", "\n", " function canvas_keyboard_event(event) {\n", " return fig.key_event(event, event['data']);\n", " }\n", "\n", " canvas_div.keydown('key_press', canvas_keyboard_event);\n", " canvas_div.keyup('key_release', canvas_keyboard_event);\n", " this.canvas_div = canvas_div\n", " this._canvas_extra_style(canvas_div)\n", " this.root.append(canvas_div);\n", "\n", " var canvas = $('');\n", " canvas.addClass('mpl-canvas');\n", " canvas.attr('style', \"left: 0; top: 0; z-index: 0; outline: 0\")\n", "\n", " this.canvas = canvas[0];\n", " this.context = canvas[0].getContext(\"2d\");\n", "\n", " var backingStore = this.context.backingStorePixelRatio ||\n", "\tthis.context.webkitBackingStorePixelRatio ||\n", "\tthis.context.mozBackingStorePixelRatio ||\n", "\tthis.context.msBackingStorePixelRatio ||\n", "\tthis.context.oBackingStorePixelRatio ||\n", "\tthis.context.backingStorePixelRatio || 1;\n", "\n", " mpl.ratio = (window.devicePixelRatio || 1) / backingStore;\n", "\n", " var rubberband = $('');\n", " rubberband.attr('style', \"position: absolute; left: 0; top: 0; z-index: 1;\")\n", "\n", " var pass_mouse_events = true;\n", "\n", " canvas_div.resizable({\n", " start: function(event, ui) {\n", " pass_mouse_events = false;\n", " },\n", " resize: function(event, ui) {\n", " fig.request_resize(ui.size.width, ui.size.height);\n", " },\n", " stop: function(event, ui) {\n", " pass_mouse_events = true;\n", " fig.request_resize(ui.size.width, ui.size.height);\n", " },\n", " });\n", "\n", " function mouse_event_fn(event) {\n", " if (pass_mouse_events)\n", " return fig.mouse_event(event, event['data']);\n", " }\n", "\n", " rubberband.mousedown('button_press', mouse_event_fn);\n", " rubberband.mouseup('button_release', mouse_event_fn);\n", " // Throttle sequential mouse events to 1 every 20ms.\n", " rubberband.mousemove('motion_notify', mouse_event_fn);\n", "\n", " rubberband.mouseenter('figure_enter', mouse_event_fn);\n", " rubberband.mouseleave('figure_leave', mouse_event_fn);\n", "\n", " canvas_div.on(\"wheel\", function (event) {\n", " event = event.originalEvent;\n", " event['data'] = 'scroll'\n", " if (event.deltaY < 0) {\n", " event.step = 1;\n", " } else {\n", " event.step = -1;\n", " }\n", " mouse_event_fn(event);\n", " });\n", "\n", " canvas_div.append(canvas);\n", " canvas_div.append(rubberband);\n", "\n", " this.rubberband = rubberband;\n", " this.rubberband_canvas = rubberband[0];\n", " this.rubberband_context = rubberband[0].getContext(\"2d\");\n", " this.rubberband_context.strokeStyle = \"#000000\";\n", "\n", " this._resize_canvas = function(width, height) {\n", " // Keep the size of the canvas, canvas container, and rubber band\n", " // canvas in synch.\n", " canvas_div.css('width', width)\n", " canvas_div.css('height', height)\n", "\n", " canvas.attr('width', width * mpl.ratio);\n", " canvas.attr('height', height * mpl.ratio);\n", " canvas.attr('style', 'width: ' + width + 'px; height: ' + height + 'px;');\n", "\n", " rubberband.attr('width', width);\n", " rubberband.attr('height', height);\n", " }\n", "\n", " // Set the figure to an initial 600x600px, this will subsequently be updated\n", " // upon first draw.\n", " this._resize_canvas(600, 600);\n", "\n", " // Disable right mouse context menu.\n", " $(this.rubberband_canvas).bind(\"contextmenu\",function(e){\n", " return false;\n", " });\n", "\n", " function set_focus () {\n", " canvas.focus();\n", " canvas_div.focus();\n", " }\n", "\n", " window.setTimeout(set_focus, 100);\n", "}\n", "\n", "mpl.figure.prototype._init_toolbar = function() {\n", " var fig = this;\n", "\n", " var nav_element = $('
')\n", " nav_element.attr('style', 'width: 100%');\n", " this.root.append(nav_element);\n", "\n", " // Define a callback function for later on.\n", " function toolbar_event(event) {\n", " return fig.toolbar_button_onclick(event['data']);\n", " }\n", " function toolbar_mouse_event(event) {\n", " return fig.toolbar_button_onmouseover(event['data']);\n", " }\n", "\n", " for(var toolbar_ind in mpl.toolbar_items) {\n", " var name = mpl.toolbar_items[toolbar_ind][0];\n", " var tooltip = mpl.toolbar_items[toolbar_ind][1];\n", " var image = mpl.toolbar_items[toolbar_ind][2];\n", " var method_name = mpl.toolbar_items[toolbar_ind][3];\n", "\n", " if (!name) {\n", " // put a spacer in here.\n", " continue;\n", " }\n", " var button = $('');\n", " button.click(method_name, toolbar_event);\n", " button.mouseover(tooltip, toolbar_mouse_event);\n", " nav_element.append(button);\n", " }\n", "\n", " // Add the status bar.\n", " var status_bar = $('');\n", " nav_element.append(status_bar);\n", " this.message = status_bar[0];\n", "\n", " // Add the close button to the window.\n", " var buttongrp = $('
');\n", " var button = $('');\n", " button.click(function (evt) { fig.handle_close(fig, {}); } );\n", " button.mouseover('Stop Interaction', toolbar_mouse_event);\n", " buttongrp.append(button);\n", " var titlebar = this.root.find($('.ui-dialog-titlebar'));\n", " titlebar.prepend(buttongrp);\n", "}\n", "\n", "mpl.figure.prototype._root_extra_style = function(el){\n", " var fig = this\n", " el.on(\"remove\", function(){\n", "\tfig.close_ws(fig, {});\n", " });\n", "}\n", "\n", "mpl.figure.prototype._canvas_extra_style = function(el){\n", " // this is important to make the div 'focusable\n", " el.attr('tabindex', 0)\n", " // reach out to IPython and tell the keyboard manager to turn it's self\n", " // off when our div gets focus\n", "\n", " // location in version 3\n", " if (IPython.notebook.keyboard_manager) {\n", " IPython.notebook.keyboard_manager.register_events(el);\n", " }\n", " else {\n", " // location in version 2\n", " IPython.keyboard_manager.register_events(el);\n", " }\n", "\n", "}\n", "\n", "mpl.figure.prototype._key_event_extra = function(event, name) {\n", " var manager = IPython.notebook.keyboard_manager;\n", " if (!manager)\n", " manager = IPython.keyboard_manager;\n", "\n", " // Check for shift+enter\n", " if (event.shiftKey && event.which == 13) {\n", " this.canvas_div.blur();\n", " event.shiftKey = false;\n", " // Send a \"J\" for go to next cell\n", " event.which = 74;\n", " event.keyCode = 74;\n", " manager.command_mode();\n", " manager.handle_keydown(event);\n", " }\n", "}\n", "\n", "mpl.figure.prototype.handle_save = function(fig, msg) {\n", " fig.ondownload(fig, null);\n", "}\n", "\n", "\n", "mpl.find_output_cell = function(html_output) {\n", " // Return the cell and output element which can be found *uniquely* in the notebook.\n", " // Note - this is a bit hacky, but it is done because the \"notebook_saving.Notebook\"\n", " // IPython event is triggered only after the cells have been serialised, which for\n", " // our purposes (turning an active figure into a static one), is too late.\n", " var cells = IPython.notebook.get_cells();\n", " var ncells = cells.length;\n", " for (var i=0; i= 3 moved mimebundle to data attribute of output\n", " data = data.data;\n", " }\n", " if (data['text/html'] == html_output) {\n", " return [cell, data, j];\n", " }\n", " }\n", " }\n", " }\n", "}\n", "\n", "// Register the function which deals with the matplotlib target/channel.\n", "// The kernel may be null if the page has been refreshed.\n", "if (IPython.notebook.kernel != null) {\n", " IPython.notebook.kernel.comm_manager.register_target('matplotlib', mpl.mpl_figure_comm);\n", "}\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/plain": [ "" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "figure()\n", "m = arange(len(SNRs))\n", "plot(m[offset_min:],SNRs[offset_min:], \"r\", alpha=0.25, label=\"SNR\")\n", "n = arange(len(SNRs))\n", "plot(n[valids],SNRs[valids], \"bx\", linewidth=1.0,label=\"valid\")\n", "xlabel(\"offset\")\n", "ylabel(\"SNR (linear scale)\")\n", "axis(\"tight\")\n", "legend(loc=0)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Long-Term Prediction\n", "====================" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [], "source": [ "class LTP(Quantizer):\n", " def __init__(self, order, **options):\n", " self.fir = FIR(a=r_[1.0, zeros(order)])\n", " self.history = zeros(order)\n", " self.ar = AR(a=zeros(order))\n", " self.order = order\n", " self.options = options\n", " def encode(self, frame):\n", " a = zeros_like(self.fir.a)\n", " a[0] = 1.0\n", " try:\n", " offset, gain = ltp_parameters(self.history, frame, **self.options)\n", " a[offset] = - gain\n", " except ValueError:\n", " offset, gain = 0, 0.0\n", " self.fir.a[:] = a \n", " error = self.fir(frame)\n", " self.history = r_[self.history[len(frame):], frame]\n", " return (offset, gain), error\n", " def decode(self, data):\n", " (offset, gain), error = data\n", " a = zeros_like(self.ar.a)\n", " a[offset-1] = gain\n", " self.ar.a[:] = a\n", " return self.ar(error)" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "320\n", "40\n" ] } ], "source": [ "f_min = 50.0\n", "f_max = 400.0\n", "order_ltp = int(df/f_min)\n", "print order_ltp\n", "offset_min = int(df/f_max)\n", "print offset_min" ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [], "source": [ "def ltp_error(data, T=0.005, order=order_ltp, **options):\n", " length = len(data)\n", " n = int(T * df) # number of samples for T s at the given sampling frequency.\n", " frames = audio.frames.split(data, n, pad=True)\n", " ltp = LTP(order=order, **options)\n", " error = zeros(n*len(frames))\n", " offset = zeros_like(error)\n", " gain = zeros_like(error)\n", " for i, frame in enumerate(frames):\n", " (offset_, gain_), error_frame = ltp.encode(frame)\n", " error[i*n:(i+1)*n] = error_frame\n", " offset[i*n:(i+1)*n] = ones_like(error_frame) * offset_\n", " gain[i*n:(i+1)*n] = ones_like(error_frame) * gain_\n", " error = error[:length]\n", " offset = offset[:length]\n", " gain = gain[:length]\n", " return error, offset, gain" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [ { "data": { "application/javascript": [ "/* Put everything inside the global mpl namespace */\n", "window.mpl = {};\n", "\n", "\n", "mpl.get_websocket_type = function() {\n", " if (typeof(WebSocket) !== 'undefined') {\n", " return WebSocket;\n", " } else if (typeof(MozWebSocket) !== 'undefined') {\n", " return MozWebSocket;\n", " } else {\n", " alert('Your browser does not have WebSocket support.' +\n", " 'Please try Chrome, Safari or Firefox ≥ 6. ' +\n", " 'Firefox 4 and 5 are also supported but you ' +\n", " 'have to enable WebSockets in about:config.');\n", " };\n", "}\n", "\n", "mpl.figure = function(figure_id, websocket, ondownload, parent_element) {\n", " this.id = figure_id;\n", "\n", " this.ws = websocket;\n", "\n", " this.supports_binary = (this.ws.binaryType != undefined);\n", "\n", " if (!this.supports_binary) {\n", " var warnings = document.getElementById(\"mpl-warnings\");\n", " if (warnings) {\n", " warnings.style.display = 'block';\n", " warnings.textContent = (\n", " \"This browser does not support binary websocket messages. \" +\n", " \"Performance may be slow.\");\n", " }\n", " }\n", "\n", " this.imageObj = new Image();\n", "\n", " this.context = undefined;\n", " this.message = undefined;\n", " this.canvas = undefined;\n", " this.rubberband_canvas = undefined;\n", " this.rubberband_context = undefined;\n", " this.format_dropdown = undefined;\n", "\n", " this.image_mode = 'full';\n", "\n", " this.root = $('
');\n", " this._root_extra_style(this.root)\n", " this.root.attr('style', 'display: inline-block');\n", "\n", " $(parent_element).append(this.root);\n", "\n", " this._init_header(this);\n", " this._init_canvas(this);\n", " this._init_toolbar(this);\n", "\n", " var fig = this;\n", "\n", " this.waiting = false;\n", "\n", " this.ws.onopen = function () {\n", " fig.send_message(\"supports_binary\", {value: fig.supports_binary});\n", " fig.send_message(\"send_image_mode\", {});\n", " if (mpl.ratio != 1) {\n", " fig.send_message(\"set_dpi_ratio\", {'dpi_ratio': mpl.ratio});\n", " }\n", " fig.send_message(\"refresh\", {});\n", " }\n", "\n", " this.imageObj.onload = function() {\n", " if (fig.image_mode == 'full') {\n", " // Full images could contain transparency (where diff images\n", " // almost always do), so we need to clear the canvas so that\n", " // there is no ghosting.\n", " fig.context.clearRect(0, 0, fig.canvas.width, fig.canvas.height);\n", " }\n", " fig.context.drawImage(fig.imageObj, 0, 0);\n", " };\n", "\n", " this.imageObj.onunload = function() {\n", " fig.ws.close();\n", " }\n", "\n", " this.ws.onmessage = this._make_on_message_function(this);\n", "\n", " this.ondownload = ondownload;\n", "}\n", "\n", "mpl.figure.prototype._init_header = function() {\n", " var titlebar = $(\n", " '
');\n", " var titletext = $(\n", " '
');\n", " titlebar.append(titletext)\n", " this.root.append(titlebar);\n", " this.header = titletext[0];\n", "}\n", "\n", "\n", "\n", "mpl.figure.prototype._canvas_extra_style = function(canvas_div) {\n", "\n", "}\n", "\n", "\n", "mpl.figure.prototype._root_extra_style = function(canvas_div) {\n", "\n", "}\n", "\n", "mpl.figure.prototype._init_canvas = function() {\n", " var fig = this;\n", "\n", " var canvas_div = $('
');\n", "\n", " canvas_div.attr('style', 'position: relative; clear: both; outline: 0');\n", "\n", " function canvas_keyboard_event(event) {\n", " return fig.key_event(event, event['data']);\n", " }\n", "\n", " canvas_div.keydown('key_press', canvas_keyboard_event);\n", " canvas_div.keyup('key_release', canvas_keyboard_event);\n", " this.canvas_div = canvas_div\n", " this._canvas_extra_style(canvas_div)\n", " this.root.append(canvas_div);\n", "\n", " var canvas = $('');\n", " canvas.addClass('mpl-canvas');\n", " canvas.attr('style', \"left: 0; top: 0; z-index: 0; outline: 0\")\n", "\n", " this.canvas = canvas[0];\n", " this.context = canvas[0].getContext(\"2d\");\n", "\n", " var backingStore = this.context.backingStorePixelRatio ||\n", "\tthis.context.webkitBackingStorePixelRatio ||\n", "\tthis.context.mozBackingStorePixelRatio ||\n", "\tthis.context.msBackingStorePixelRatio ||\n", "\tthis.context.oBackingStorePixelRatio ||\n", "\tthis.context.backingStorePixelRatio || 1;\n", "\n", " mpl.ratio = (window.devicePixelRatio || 1) / backingStore;\n", "\n", " var rubberband = $('');\n", " rubberband.attr('style', \"position: absolute; left: 0; top: 0; z-index: 1;\")\n", "\n", " var pass_mouse_events = true;\n", "\n", " canvas_div.resizable({\n", " start: function(event, ui) {\n", " pass_mouse_events = false;\n", " },\n", " resize: function(event, ui) {\n", " fig.request_resize(ui.size.width, ui.size.height);\n", " },\n", " stop: function(event, ui) {\n", " pass_mouse_events = true;\n", " fig.request_resize(ui.size.width, ui.size.height);\n", " },\n", " });\n", "\n", " function mouse_event_fn(event) {\n", " if (pass_mouse_events)\n", " return fig.mouse_event(event, event['data']);\n", " }\n", "\n", " rubberband.mousedown('button_press', mouse_event_fn);\n", " rubberband.mouseup('button_release', mouse_event_fn);\n", " // Throttle sequential mouse events to 1 every 20ms.\n", " rubberband.mousemove('motion_notify', mouse_event_fn);\n", "\n", " rubberband.mouseenter('figure_enter', mouse_event_fn);\n", " rubberband.mouseleave('figure_leave', mouse_event_fn);\n", "\n", " canvas_div.on(\"wheel\", function (event) {\n", " event = event.originalEvent;\n", " event['data'] = 'scroll'\n", " if (event.deltaY < 0) {\n", " event.step = 1;\n", " } else {\n", " event.step = -1;\n", " }\n", " mouse_event_fn(event);\n", " });\n", "\n", " canvas_div.append(canvas);\n", " canvas_div.append(rubberband);\n", "\n", " this.rubberband = rubberband;\n", " this.rubberband_canvas = rubberband[0];\n", " this.rubberband_context = rubberband[0].getContext(\"2d\");\n", " this.rubberband_context.strokeStyle = \"#000000\";\n", "\n", " this._resize_canvas = function(width, height) {\n", " // Keep the size of the canvas, canvas container, and rubber band\n", " // canvas in synch.\n", " canvas_div.css('width', width)\n", " canvas_div.css('height', height)\n", "\n", " canvas.attr('width', width * mpl.ratio);\n", " canvas.attr('height', height * mpl.ratio);\n", " canvas.attr('style', 'width: ' + width + 'px; height: ' + height + 'px;');\n", "\n", " rubberband.attr('width', width);\n", " rubberband.attr('height', height);\n", " }\n", "\n", " // Set the figure to an initial 600x600px, this will subsequently be updated\n", " // upon first draw.\n", " this._resize_canvas(600, 600);\n", "\n", " // Disable right mouse context menu.\n", " $(this.rubberband_canvas).bind(\"contextmenu\",function(e){\n", " return false;\n", " });\n", "\n", " function set_focus () {\n", " canvas.focus();\n", " canvas_div.focus();\n", " }\n", "\n", " window.setTimeout(set_focus, 100);\n", "}\n", "\n", "mpl.figure.prototype._init_toolbar = function() {\n", " var fig = this;\n", "\n", " var nav_element = $('
')\n", " nav_element.attr('style', 'width: 100%');\n", " this.root.append(nav_element);\n", "\n", " // Define a callback function for later on.\n", " function toolbar_event(event) {\n", " return fig.toolbar_button_onclick(event['data']);\n", " }\n", " function toolbar_mouse_event(event) {\n", " return fig.toolbar_button_onmouseover(event['data']);\n", " }\n", "\n", " for(var toolbar_ind in mpl.toolbar_items) {\n", " var name = mpl.toolbar_items[toolbar_ind][0];\n", " var tooltip = mpl.toolbar_items[toolbar_ind][1];\n", " var image = mpl.toolbar_items[toolbar_ind][2];\n", " var method_name = mpl.toolbar_items[toolbar_ind][3];\n", "\n", " if (!name) {\n", " // put a spacer in here.\n", " continue;\n", " }\n", " var button = $('