{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Exception in thread Thread-10:\n",
      "Traceback (most recent call last):\n",
      "  File \"c:\\python35\\lib\\threading.py\", line 914, in _bootstrap_inner\n",
      "    self.run()\n",
      "  File \"c:\\python35\\lib\\threading.py\", line 862, in run\n",
      "    self._target(*self._args, **self._kwargs)\n",
      "  File \"c:\\python35\\lib\\site-packages\\speech_recognition\\__init__.py\", line 623, in threaded_listen\n",
      "    if running[0]: callback(self, audio)\n",
      "  File \"<ipython-input-6-1a944c242ba9>\", line 11, in callback\n",
      "    r.send(texto.encode())\n",
      "AttributeError: 'Recognizer' object has no attribute 'send'\n",
      "\n"
     ]
    }
   ],
   "source": [
    "import speech_recognition as sr\n",
    " \n",
    "def callback(recognizer, audio):\n",
    "    # received audio data, now we'll recognize it using Google Speech Recognition\n",
    "    try:\n",
    "        # for testing purposes, we're just using the default API key\n",
    "        # to use another API key, use `r.recognize_google(audio, key=\"GOOGLE_SPEECH_RECOGNITION_API_KEY\")`\n",
    "        # instead of `r.recognize_google(audio)`\n",
    "        texto = recognizer.recognize_google(audio, language = \"es-ES\")\n",
    "        #print(\"Has dicho, \" + texto)\n",
    "        r.send(texto.encode())\n",
    "    except sr.UnknownValueError:\n",
    "        #print('No entiendo.')\n",
    "        r.send(b\"No entiendo.\")\n",
    "    except sr.RequestError as e:\n",
    "        print(\"Could not request results from Google Speech Recognition service; {0}\".format(e))\n",
    "        #s.send(\"No se reciben datos del servicio de reconocimiento de voz.\")  \n",
    "#s.close() \n",
    "r = sr.Recognizer()\n",
    "m = sr.Microphone()\n",
    "with m as source:\n",
    "    r.adjust_for_ambient_noise(source) # we only need to calibrate once, before we start listening\n",
    " \n",
    "# start listening in the background (note that we don't have to do this inside a `with` statement)\n",
    "stop_listening = r.listen_in_background(m, callback)\n",
    "# `stop_listening` is now a function that, when called, stops background listening"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "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.5.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
