{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import sys\n",
    "import os\n",
    "#sys.path.insert(0, os.path.abspath(\"..\\\\..\\\\Sources\")) # FOR WINDOWS\n",
    "sys.path.insert(0, os.path.abspath(\"../../Sources\")) # FOR LINUX\n",
    "import time"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from pyOpenRPA.Robot import Audio\n",
    "print(Audio.DeviceSystemSoundIndex())\n",
    "Audio.DeviceListGet()[9]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from pyOpenRPA.Robot import Audio\n",
    "lRec = Audio.Recorder(inDeviceInt = Audio.DeviceMicrophoneIndex())\n",
    "lRec.CaptureStart(inFileNameStr = \"out2\", inFileFormatStr = \"mp3\", inDurationSecFloat = None, inChunkSecFloat = 15.0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from pyOpenRPA.Robot import Audio\n",
    "lRec = Audio.Recorder()\n",
    "lRec.CaptureStart(inFileNameStr = \"out\", inFileFormatStr = \"mp3\", inDurationSecFloat = None, inChunkSecFloat = 5.0, inCallbackChunkDef=test)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "lRec.hi"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "lRec.CaptureStop()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def test(lRec, lFilenameStr):\n",
    "    lRec.hi=[lFilenameStr]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "lRec.StatusGet()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print (textcolors.blue + \"Available devices:\\n\" + textcolors.end)\n",
    "for i in range(0, p.get_device_count()):\n",
    "    info = p.get_device_info_by_index(i)\n",
    "    print(info[\"hostApi\"])\n",
    "    print (textcolors.green + str(info[\"index\"]) + textcolors.end + \": \\t %s \\n \\t %s \\n\" % (info[\"name\"], p.get_host_api_info_by_index(info[\"hostApi\"])[\"name\"]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "class textcolors:\n",
    "    if not os.name == 'nt':\n",
    "        blue = '\\033[94m'\n",
    "        green = '\\033[92m'\n",
    "        warning = '\\033[93m'\n",
    "        fail = '\\033[91m'\n",
    "        end = '\\033[0m'\n",
    "    else:\n",
    "        blue = ''\n",
    "        green = ''\n",
    "        warning = ''\n",
    "        fail = ''\n",
    "        end = ''"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pyaudio\n",
    "defaultframes = 512\n",
    "recorded_frames = []\n",
    "device_info = {}\n",
    "useloopback = True\n",
    "#Use module\n",
    "p = pyaudio.PyAudio()\n",
    "\n",
    "recordtime = 2\n",
    "device_id = 10\n",
    "device_info = p.get_device_info_by_index(device_id)\n",
    "#Open stream\n",
    "channelcount = device_info[\"maxInputChannels\"] if (device_info[\"maxOutputChannels\"] < device_info[\"maxInputChannels\"]) else device_info[\"maxOutputChannels\"]\n",
    "stream = p.open(format = pyaudio.paInt16,\n",
    "                channels = channelcount,\n",
    "                rate = int(device_info[\"defaultSampleRate\"]),\n",
    "                input = True,\n",
    "                frames_per_buffer = defaultframes,\n",
    "                input_device_index = device_info[\"index\"],\n",
    "                as_loopback = useloopback)\n",
    "\n",
    "#Start Recording\n",
    "print (\"Starting...\")\n",
    "\n",
    "for i in range(0, int(int(device_info[\"defaultSampleRate\"]) / defaultframes * recordtime)):\n",
    "    recorded_frames.append(stream.read(defaultframes))\n",
    "\n",
    "print (\"End.\")\n",
    "#Stop Recording\n",
    "\n",
    "stream.stop_stream()\n",
    "stream.close()\n",
    "\n",
    "#Close module\n",
    "p.terminate()\n",
    "\n",
    "filename = \"out.wav\"\n",
    "from pydub import AudioSegment\n",
    "# Advanced usage, if you have raw audio data:\n",
    "sound = AudioSegment(\n",
    "    # raw audio data (bytes)\n",
    "    data=b''.join(recorded_frames),\n",
    "    # 2 byte (16 bit) samples\n",
    "    sample_width=p.get_sample_size(pyaudio.paInt16),\n",
    "    # 44.1 kHz frame rate\n",
    "    frame_rate=int(device_info[\"defaultSampleRate\"]),\n",
    "    # stereo\n",
    "    channels=channelcount\n",
    ")\n",
    "print(channelcount)\n",
    "print(p.get_sample_size(pyaudio.paInt16))\n",
    "print(int(device_info[\"defaultSampleRate\"]))\n",
    "print(len(b''.join(recorded_frames)))\n",
    "sound.export(\"out.mp3\", format=\"mp3\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import difflib\n",
    "def similarity_no_case(s1, s2):\n",
    "  normalized1 = s1.lower()\n",
    "  normalized2 = s2.lower()\n",
    "  matcher = difflib.SequenceMatcher(None, normalized1, normalized2)\n",
    "  return matcher.ratio()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "similarity_no_case(\"Ìèêðîôîí (Realtek High Definiti\",\"Динамики (Realtek High Definition Audio)\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "p.get_default_input_device_info()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pyaudio\n",
    "p = pyaudio.PyAudio()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "p"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "not (False ^ True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from pyOpenRPA.Robot import Audio\n",
    "Audio.DeviceMicrophoneIndex()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "os.path.abspath(\"\",\"\")"
   ]
  },
  {
   "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.7.2"
  },
  "varInspector": {
   "cols": {
    "lenName": 16,
    "lenType": 16,
    "lenVar": 40
   },
   "kernels_config": {
    "python": {
     "delete_cmd_postfix": "",
     "delete_cmd_prefix": "del ",
     "library": "var_list.py",
     "varRefreshCmd": "print(var_dic_list())"
    },
    "r": {
     "delete_cmd_postfix": ") ",
     "delete_cmd_prefix": "rm(",
     "library": "var_list.r",
     "varRefreshCmd": "cat(var_dic_list()) "
    }
   },
   "types_to_exclude": [
    "module",
    "function",
    "builtin_function_or_method",
    "instance",
    "_Feature"
   ],
   "window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}