You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
ORPA-pyOpenRPA/Tools/Jupyter-notebooks/Audio.ipynb

473 lines
12 KiB

{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"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": 1,
"metadata": {},
"outputs": [
{
"ename": "AttributeError",
"evalue": "'Recorder' object has no attribute 'test'",
"output_type": "error",
"traceback": [
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[1;31mAttributeError\u001b[0m Traceback (most recent call last)",
"\u001b[1;32m<ipython-input-1-ef80e2b488aa>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m 1\u001b[0m \u001b[1;32mfrom\u001b[0m \u001b[0mpyOpenRPA\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mRobot\u001b[0m \u001b[1;32mimport\u001b[0m \u001b[0mAudio\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 2\u001b[0m \u001b[0mlRec\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mAudio\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mRecorder\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0minDeviceInt\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;36m10\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 3\u001b[1;33m \u001b[0mlRec\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mtest\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[1;31mAttributeError\u001b[0m: 'Recorder' object has no attribute 'test'"
]
}
],
"source": [
"from pyOpenRPA.Robot import Audio\n",
"lRec = Audio.Recorder(inDeviceInt=10)\n",
"lRec.test()"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"from pyOpenRPA.Robot import Audio\n",
"lRec = Audio.Recorder(inDeviceInt=10)\n",
"lRec.CaptureStart()"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"lRec.CaptureStop()"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Available devices:\n",
"\n",
"0\n",
"0: \t Ïåðåíàçíà÷åíèå çâóêîâûõ óñòð. - Input \n",
" \t MME \n",
"\n",
"0\n",
"1: \t Ìèêðîôîí (Realtek High Definiti \n",
" \t MME \n",
"\n",
"0\n",
"2: \t Ìèêðîôîí (EpocCam Camera Audio) \n",
" \t MME \n",
"\n",
"0\n",
"3: \t Ìèêðîôîí (USB PnP Sound Device) \n",
" \t MME \n",
"\n",
"0\n",
"4: \t Ïåðåíàçíà÷åíèå çâóêîâûõ óñòð. - Output \n",
" \t MME \n",
"\n",
"0\n",
"5: \t Äèíàìèêè (USB PnP Sound Device) \n",
" \t MME \n",
"\n",
"0\n",
"6: \t 2 - NEC LCD1703M (AMD High Defi \n",
" \t MME \n",
"\n",
"0\n",
"7: \t Äèíàìèêè (Realtek High Definiti \n",
" \t MME \n",
"\n",
"1\n",
"8: \t 2 - NEC LCD1703M (AMD High Definition Audio Device) \n",
" \t Windows WASAPI \n",
"\n",
"1\n",
"9: \t Динамики (USB PnP Sound Device) \n",
" \t Windows WASAPI \n",
"\n",
"1\n",
"10: \t Динамики (Realtek High Definition Audio) \n",
" \t Windows WASAPI \n",
"\n",
"1\n",
"11: \t Микрофон (EpocCam Camera Audio) \n",
" \t Windows WASAPI \n",
"\n",
"1\n",
"12: \t Микрофон (Realtek High Definition Audio) \n",
" \t Windows WASAPI \n",
"\n",
"1\n",
"13: \t Микрофон (USB PnP Sound Device) \n",
" \t Windows WASAPI \n",
"\n",
"2\n",
"14: \t Стерео микшер (Realtek HD Audio Stereo input) \n",
" \t Windows WDM-KS \n",
"\n",
"2\n",
"15: \t Микрофон (Realtek HD Audio Mic input) \n",
" \t Windows WDM-KS \n",
"\n",
"2\n",
"16: \t Speakers (Realtek HD Audio output) \n",
" \t Windows WDM-KS \n",
"\n",
"2\n",
"17: \t Лин. вход (Realtek HD Audio Line input) \n",
" \t Windows WDM-KS \n",
"\n",
"2\n",
"18: \t Output (AMD HD Audio HDMI out #1) \n",
" \t Windows WDM-KS \n",
"\n",
"2\n",
"19: \t Динамики (USB PnP Sound Device) \n",
" \t Windows WDM-KS \n",
"\n",
"2\n",
"20: \t Микрофон (USB PnP Sound Device) \n",
" \t Windows WDM-KS \n",
"\n",
"2\n",
"21: \t MIDI (EpocCam Audio) \n",
" \t Windows WDM-KS \n",
"\n",
"2\n",
"22: \t Output (EpocCam Audio) \n",
" \t Windows WDM-KS \n",
"\n"
]
}
],
"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": 7,
"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": 1,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Starting...\n",
"End.\n",
"2\n",
"2\n",
"48000\n",
"382976\n"
]
},
{
"data": {
"text/plain": [
"<_io.BufferedRandom name='out.mp3'>"
]
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"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": 10,
"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": 12,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0.647887323943662"
]
},
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"similarity_no_case(\"Ìèêðîôîí (Realtek High Definiti\",\"Динамики (Realtek High Definition Audio)\")"
]
},
{
"cell_type": "code",
"execution_count": 23,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'index': 1,\n",
" 'structVersion': 2,\n",
" 'name': 'Ìèêðîôîí (Realtek High Definiti',\n",
" 'hostApi': 0,\n",
" 'maxInputChannels': 2,\n",
" 'maxOutputChannels': 0,\n",
" 'defaultLowInputLatency': 0.09,\n",
" 'defaultLowOutputLatency': 0.09,\n",
" 'defaultHighInputLatency': 0.18,\n",
" 'defaultHighOutputLatency': 0.18,\n",
" 'defaultSampleRate': 44100.0}"
]
},
"execution_count": 23,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"p.get_default_input_device_info()"
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {},
"outputs": [],
"source": [
"import pyaudio\n",
"p = pyaudio.PyAudio()"
]
},
{
"cell_type": "code",
"execution_count": 22,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"<pyaudio.PyAudio at 0x1a6b5ab2438>"
]
},
"execution_count": 22,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"p"
]
},
{
"cell_type": "code",
"execution_count": 26,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"False"
]
},
"execution_count": 26,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"not (False ^ True)"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"1"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"from pyOpenRPA.Robot import Audio\n",
"Audio.DeviceMicrophoneIndex()"
]
},
{
"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
}