{ "cells": [ { "cell_type": "code", "execution_count": 1, "id": "choice-lender", "metadata": {}, "outputs": [], "source": [ "eng=\"one minute a voice said and the time buzzer sounded\"\n", "chn=\"可控是病毒武器最基本的要求\"" ] }, { "cell_type": "code", "execution_count": 3, "id": "ruled-kuwait", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "o\n", "n\n", "e\n", " \n", "m\n", "i\n", "n\n", "u\n", "t\n", "e\n", " \n", "a\n", " \n", "v\n", "o\n", "i\n", "c\n", "e\n", " \n", "s\n", "a\n", "i\n", "d\n", " \n", "a\n", "n\n", "d\n", " \n", "t\n", "h\n", "e\n", " \n", "t\n", "i\n", "m\n", "e\n", " \n", "b\n", "u\n", "z\n", "z\n", "e\n", "r\n", " \n", "s\n", "o\n", "u\n", "n\n", "d\n", "e\n", "d\n" ] } ], "source": [ "for char in eng:\n", " print(char)" ] }, { "cell_type": "code", "execution_count": 4, "id": "passive-petite", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "可\n", "控\n", "是\n", "病\n", "毒\n", "武\n", "器\n", "最\n", "基\n", "本\n", "的\n", "要\n", "求\n" ] } ], "source": [ "for char in chn:\n", " print(char)" ] }, { "cell_type": "code", "execution_count": 7, "id": "olympic-realtor", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "one\n", "minute\n", "a\n", "voice\n", "said\n", "and\n", "the\n", "time\n", "buzzer\n", "sounded\n" ] } ], "source": [ "for word in eng.split():\n", " print(word)" ] }, { "cell_type": "code", "execution_count": 8, "id": "induced-enhancement", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "可控是病毒武器最基本的要求\n" ] } ], "source": [ "for word in chn.split():\n", " print(word)" ] }, { "cell_type": "code", "execution_count": 9, "id": "lovely-bottle", "metadata": {}, "outputs": [ { "ename": "ModuleNotFoundError", "evalue": "No module named 'StringIO'", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mModuleNotFoundError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0;32mimport\u001b[0m \u001b[0mStringIO\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mModuleNotFoundError\u001b[0m: No module named 'StringIO'" ] } ], "source": [ "import StringIO" ] }, { "cell_type": "code", "execution_count": 10, "id": "interested-cardiff", "metadata": {}, "outputs": [], "source": [ "from io import StringIO" ] }, { "cell_type": "code", "execution_count": 11, "id": "portable-ivory", "metadata": {}, "outputs": [], "source": [ "inputs = StringIO()" ] }, { "cell_type": "code", "execution_count": 18, "id": "compatible-destination", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "64" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "inputs.write(\"nor is mister quilter's manner less interesting than his matter\" + '\\n')" ] }, { "cell_type": "code", "execution_count": 19, "id": "federal-margin", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "nor is mister quilter's manner less interesting than his matternor is mister quilter's manner less interesting than his matter\n", "\n" ] } ], "source": [ "print(inputs.getvalue())" ] }, { "cell_type": "code", "execution_count": 20, "id": "consecutive-entity", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "64" ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "inputs.write(\"nor is mister quilter's manner less interesting than his matter\" + '\\n')" ] }, { "cell_type": "code", "execution_count": 21, "id": "desirable-anxiety", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "nor is mister quilter's manner less interesting than his matternor is mister quilter's manner less interesting than his matter\n", "nor is mister quilter's manner less interesting than his matter\n", "\n" ] } ], "source": [ "print(inputs.getvalue())" ] }, { "cell_type": "code", "execution_count": 23, "id": "employed-schedule", "metadata": {}, "outputs": [], "source": [ "import tempfile" ] }, { "cell_type": "code", "execution_count": 25, "id": "unlikely-honduras", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['__class__', '__del__', '__delattr__', '__dict__', '__dir__', '__doc__', '__enter__', '__eq__', '__exit__', '__format__', '__ge__', '__getattribute__', '__getstate__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__iter__', '__le__', '__lt__', '__ne__', '__new__', '__next__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '_checkClosed', '_checkReadable', '_checkSeekable', '_checkWritable', '_dealloc_warn', '_finalizing', 'close', 'closed', 'detach', 'fileno', 'flush', 'isatty', 'mode', 'name', 'peek', 'raw', 'read', 'read1', 'readable', 'readinto', 'readinto1', 'readline', 'readlines', 'seek', 'seekable', 'tell', 'truncate', 'writable', 'write', 'writelines']\n", "57\n" ] } ], "source": [ "with tempfile.TemporaryFile() as fp:\n", " print(dir(fp))\n", " print(fp.name)" ] }, { "cell_type": "code", "execution_count": 27, "id": "needed-trail", "metadata": {}, "outputs": [], "source": [ "a = tempfile.mkstemp(suffix=None, prefix='test', dir=None, text=False)" ] }, { "cell_type": "code", "execution_count": 28, "id": "hazardous-choir", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['__add__', '__class__', '__contains__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'count', 'index']\n" ] } ], "source": [ "print(dir(a))" ] }, { "cell_type": "code", "execution_count": 29, "id": "front-sauce", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "(57, '/tmp/test27smzbzc')\n" ] } ], "source": [ "print(a)" ] }, { "cell_type": "code", "execution_count": 31, "id": "shared-wages", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n" ] } ], "source": [ "print(a.index)" ] }, { "cell_type": "code", "execution_count": 34, "id": "charged-carnival", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__enter__', '__eq__', '__exit__', '__format__', '__ge__', '__getattr__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__iter__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', '_closer', 'close', 'delete', 'file', 'name']\n", "/tmp/tmpfjn7mygy\n" ] } ], "source": [ "fp= tempfile.NamedTemporaryFile(mode='w', delete=False)\n", "print(dir(fp))\n", "print(fp.name)\n", "fp.close()" ] }, { "cell_type": "code", "execution_count": 36, "id": "religious-terror", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "/tmp/tmpfjn7mygy\n" ] } ], "source": [ "import os\n", "os.path.exists(fp.name)\n", "print(fp.name)" ] }, { "cell_type": "code", "execution_count": 37, "id": "communist-gospel", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "" ] }, "execution_count": 37, "metadata": {}, "output_type": "execute_result" } ], "source": [ "fp.write" ] }, { "cell_type": "code", "execution_count": 39, "id": "simplified-clarity", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'example'" ] }, "execution_count": 39, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s='/home/ubuntu/python/example.py'\n", "os.path.splitext(os.path.basename(s))[0]" ] }, { "cell_type": "code", "execution_count": 40, "id": "popular-genius", "metadata": {}, "outputs": [], "source": [ "from collections import Counter" ] }, { "cell_type": "code", "execution_count": 43, "id": "studied-burner", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "dict_items([('hello', 1), ('world', 1)])\n" ] } ], "source": [ "counter = Counter()\n", "counter.update([\"hello\"])\n", "counter.update([\"world\"])\n", "print(counter.items())" ] }, { "cell_type": "code", "execution_count": 44, "id": "mineral-ceremony", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "dict_items([('h', 1), ('e', 1), ('l', 3), ('o', 2), ('w', 1), ('r', 1), ('d', 1)])\n" ] } ], "source": [ "counter = Counter()\n", "counter.update(\"hello\")\n", "counter.update(\"world\")\n", "print(counter.items())" ] }, { "cell_type": "code", "execution_count": 45, "id": "nonprofit-freedom", "metadata": {}, "outputs": [], "source": [ "counter.update(list(\"hello\"))" ] }, { "cell_type": "code", "execution_count": 46, "id": "extended-methodology", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "dict_items([('h', 2), ('e', 2), ('l', 5), ('o', 3), ('w', 1), ('r', 1), ('d', 1)])\n" ] } ], "source": [ "print(counter.items())" ] }, { "cell_type": "code", "execution_count": 47, "id": "grand-benjamin", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['h', 'e', 'l', 'l', 'o']" ] }, "execution_count": 47, "metadata": {}, "output_type": "execute_result" } ], "source": [ "list(\"hello\")" ] }, { "cell_type": "code", "execution_count": 53, "id": "marine-fundamentals", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{}\n" ] } ], "source": [ "from io import StringIO\n", "a = StringIO(initial_value='{}', newline='')\n", "print(a.read())" ] }, { "cell_type": "code", "execution_count": 56, "id": "suitable-charlotte", "metadata": {}, "outputs": [ { "ename": "TypeError", "evalue": "expected str, bytes or os.PathLike object, not _io.StringIO", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0;32mwith\u001b[0m \u001b[0mio\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mopen\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0ma\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;32mas\u001b[0m \u001b[0mf\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 2\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mf\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mread\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;31mTypeError\u001b[0m: expected str, bytes or os.PathLike object, not _io.StringIO" ] } ], "source": [ "with io.open(a) as f:\n", " print(f.read())" ] }, { "cell_type": "code", "execution_count": 57, "id": "institutional-configuration", "metadata": {}, "outputs": [], "source": [ "io.open?" ] }, { "cell_type": "code", "execution_count": 58, "id": "pregnant-modem", "metadata": {}, "outputs": [], "source": [ "def get_default_args(fn):\n", " if fn is None:\n", " return {}\n", "\n", " signature = inspect.signature(fn)\n", " return {\n", " k: v.default\n", " for k, v in signature.parameters.items()\n", " if v.default is not inspect.Parameter.empty\n", " }" ] }, { "cell_type": "code", "execution_count": 59, "id": "threaded-grove", "metadata": {}, "outputs": [ { "ename": "NameError", "evalue": "name 'inspect' is not defined", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mget_default_args\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mio\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mopen\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;32m\u001b[0m in \u001b[0;36mget_default_args\u001b[0;34m(fn)\u001b[0m\n\u001b[1;32m 3\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0;34m{\u001b[0m\u001b[0;34m}\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 4\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 5\u001b[0;31m \u001b[0msignature\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0minspect\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0msignature\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mfn\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 6\u001b[0m return {\n\u001b[1;32m 7\u001b[0m \u001b[0mk\u001b[0m\u001b[0;34m:\u001b[0m \u001b[0mv\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mdefault\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;31mNameError\u001b[0m: name 'inspect' is not defined" ] } ], "source": [ "get_default_args(io.open)" ] }, { "cell_type": "code", "execution_count": 35, "id": "equal-vanilla", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Requirement already satisfied: sox in /workspace/DeepSpeech-2.x/tools/venv/lib/python3.7/site-packages (1.4.1)\n", "Requirement already satisfied: numpy>=1.9.0 in /workspace/DeepSpeech-2.x/tools/venv/lib/python3.7/site-packages (from sox) (1.20.1)\n", "Requirement already satisfied: librosa in /workspace/DeepSpeech-2.x/tools/venv/lib/python3.7/site-packages (0.8.0)\n", "Requirement already satisfied: scikit-learn!=0.19.0,>=0.14.0 in /workspace/DeepSpeech-2.x/tools/venv/lib/python3.7/site-packages (from librosa) (0.24.1)\n", "Requirement already satisfied: numba>=0.43.0 in /workspace/DeepSpeech-2.x/tools/venv/lib/python3.7/site-packages (from librosa) (0.52.0)\n", "Requirement already satisfied: pooch>=1.0 in /workspace/DeepSpeech-2.x/tools/venv/lib/python3.7/site-packages (from librosa) (1.3.0)\n", "Requirement already satisfied: scipy>=1.0.0 in /workspace/DeepSpeech-2.x/tools/venv/lib/python3.7/site-packages (from librosa) (1.2.1)\n", "Requirement already satisfied: numpy>=1.15.0 in /workspace/DeepSpeech-2.x/tools/venv/lib/python3.7/site-packages (from librosa) (1.20.1)\n", "Requirement already satisfied: decorator>=3.0.0 in /workspace/DeepSpeech-2.x/tools/venv/lib/python3.7/site-packages (from librosa) (4.4.2)\n", "Requirement already satisfied: resampy>=0.2.2 in /workspace/DeepSpeech-2.x/tools/venv/lib/python3.7/site-packages (from librosa) (0.2.2)\n", "Requirement already satisfied: audioread>=2.0.0 in /workspace/DeepSpeech-2.x/tools/venv/lib/python3.7/site-packages (from librosa) (2.1.9)\n", "Requirement already satisfied: soundfile>=0.9.0 in /workspace/DeepSpeech-2.x/tools/venv/lib/python3.7/site-packages (from librosa) (0.9.0.post1)\n", "Requirement already satisfied: joblib>=0.14 in /workspace/DeepSpeech-2.x/tools/venv/lib/python3.7/site-packages (from librosa) (1.0.1)\n", "Requirement already satisfied: setuptools in /workspace/DeepSpeech-2.x/tools/venv/lib/python3.7/site-packages (from numba>=0.43.0->librosa) (51.0.0)\n", "Requirement already satisfied: llvmlite<0.36,>=0.35.0 in /workspace/DeepSpeech-2.x/tools/venv/lib/python3.7/site-packages (from numba>=0.43.0->librosa) (0.35.0)\n", "Requirement already satisfied: appdirs in /workspace/DeepSpeech-2.x/tools/venv/lib/python3.7/site-packages (from pooch>=1.0->librosa) (1.4.4)\n", "Requirement already satisfied: packaging in /workspace/DeepSpeech-2.x/tools/venv/lib/python3.7/site-packages (from pooch>=1.0->librosa) (20.9)\n", "Requirement already satisfied: requests in /workspace/DeepSpeech-2.x/tools/venv/lib/python3.7/site-packages (from pooch>=1.0->librosa) (2.25.1)\n", "Requirement already satisfied: six>=1.3 in /workspace/DeepSpeech-2.x/tools/venv/lib/python3.7/site-packages (from resampy>=0.2.2->librosa) (1.15.0)\n", "Requirement already satisfied: threadpoolctl>=2.0.0 in /workspace/DeepSpeech-2.x/tools/venv/lib/python3.7/site-packages (from scikit-learn!=0.19.0,>=0.14.0->librosa) (2.1.0)\n", "Requirement already satisfied: cffi>=0.6 in /workspace/DeepSpeech-2.x/tools/venv/lib/python3.7/site-packages (from soundfile>=0.9.0->librosa) (1.14.4)\n", "Requirement already satisfied: pycparser in /workspace/DeepSpeech-2.x/tools/venv/lib/python3.7/site-packages (from cffi>=0.6->soundfile>=0.9.0->librosa) (2.20)\n", "Requirement already satisfied: pyparsing>=2.0.2 in /workspace/DeepSpeech-2.x/tools/venv/lib/python3.7/site-packages (from packaging->pooch>=1.0->librosa) (2.4.7)\n", "Requirement already satisfied: idna<3,>=2.5 in /workspace/DeepSpeech-2.x/tools/venv/lib/python3.7/site-packages (from requests->pooch>=1.0->librosa) (2.10)\n", "Requirement already satisfied: certifi>=2017.4.17 in /workspace/DeepSpeech-2.x/tools/venv/lib/python3.7/site-packages (from requests->pooch>=1.0->librosa) (2020.12.5)\n", "Requirement already satisfied: chardet<5,>=3.0.2 in /workspace/DeepSpeech-2.x/tools/venv/lib/python3.7/site-packages (from requests->pooch>=1.0->librosa) (4.0.0)\n", "Requirement already satisfied: urllib3<1.27,>=1.21.1 in /workspace/DeepSpeech-2.x/tools/venv/lib/python3.7/site-packages (from requests->pooch>=1.0->librosa) (1.26.3)\n" ] } ], "source": [ "!pip install sox\n", "!pip install librosa" ] }, { "cell_type": "code", "execution_count": 36, "id": "gorgeous-stanford", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n" ] } ], "source": [ "import numpy as np\n", "import sox\n", "tfm = sox.Transformer()\n", "sample_rate = 44100\n", "y = np.sin(2 * np.pi * 440.0 * np.arange(sample_rate * 1.0) / sample_rate)\n", "print(y.dtype.type)" ] }, { "cell_type": "code", "execution_count": 37, "id": "geological-actor", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[ 0. 0.06264832 0.12505052 ... -0.18696144 -0.12505052\n", " -0.06264832]\n" ] } ], "source": [ "output_array = tfm.build_array(input_array=y, sample_rate_in=sample_rate)\n", "print(output_array)" ] }, { "cell_type": "code", "execution_count": 38, "id": "miniature-ethnic", "metadata": {}, "outputs": [], "source": [ "tfm.build_array?" ] }, { "cell_type": "code", "execution_count": 40, "id": "honest-clarity", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['8svx', 'aif', 'aifc', 'aiff', 'aiffc', 'al', 'amb', 'amr-nb', 'amr-wb', 'anb', 'au', 'avr', 'awb', 'caf', 'cdda', 'cdr', 'cvs', 'cvsd', 'cvu', 'dat', 'dvms', 'f32', 'f4', 'f64', 'f8', 'fap', 'flac', 'fssd', 'gsm', 'gsrt', 'hcom', 'htk', 'ima', 'ircam', 'la', 'lpc', 'lpc10', 'lu', 'mat', 'mat4', 'mat5', 'maud', 'nist', 'ogg', 'paf', 'prc', 'pvf', 'raw', 's1', 's16', 's2', 's24', 's3', 's32', 's4', 's8', 'sb', 'sd2', 'sds', 'sf', 'sl', 'sln', 'smp', 'snd', 'sndfile', 'sndr', 'sndt', 'sou', 'sox', 'sph', 'sw', 'txw', 'u1', 'u16', 'u2', 'u24', 'u3', 'u32', 'u4', 'u8', 'ub', 'ul', 'uw', 'vms', 'voc', 'vorbis', 'vox', 'w64', 'wav', 'wavpcm', 'wv', 'wve', 'xa', 'xi']\n" ] } ], "source": [ "print(sox.core._get_valid_formats())" ] }, { "cell_type": "code", "execution_count": 54, "id": "environmental-stewart", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "float64\n", "(59471,)\n", "16000\n", "(54065,)\n", "1.0999907518727459\n" ] } ], "source": [ "import soundfile as sf\n", "wav='/workspace/DeepSpeech-2.x/examples/aishell/s1/../../..//examples/dataset/aishell/data_aishell/wav/dev/S0724/BAC009S0724W0190.wav'\n", "samples, sr = sf.read(wav)\n", "print(samples.dtype)\n", "print(samples.shape)\n", "print(sr)\n", "tfm = sox.Transformer()\n", "tfm.speed(1.1)\n", "output_array = tfm.build_array(input_array=samples, sample_rate_in=sr)\n", "output_array.dtype\n", "print(output_array.shape)\n", "print(len(samples)/len(output_array))" ] }, { "cell_type": "code", "execution_count": 42, "id": "trying-brazil", "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", " \n", " " ], "text/plain": [ "" ] }, "execution_count": 42, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import IPython.display as ipd\n", "ipd.Audio(wav) # load a local WAV file" ] }, { "cell_type": "code", "execution_count": 43, "id": "chronic-interval", "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", " \n", " " ], "text/plain": [ "" ] }, "execution_count": 43, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ipd.Audio(output_array, rate=sr) # load a NumPy array" ] }, { "cell_type": "code", "execution_count": 44, "id": "widespread-basin", "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", " \n", " " ], "text/plain": [ "" ] }, "execution_count": 44, "metadata": {}, "output_type": "execute_result" } ], "source": [ "tfm = sox.Transformer()\n", "tfm.speed(0.9)\n", "output_array = tfm.build_array(input_array=samples, sample_rate_in=sr)\n", "ipd.Audio(output_array, rate=sr) # load a NumPy array" ] }, { "cell_type": "code", "execution_count": 45, "id": "clinical-lighting", "metadata": {}, "outputs": [], "source": [ "import librosa\n", "x, sr = librosa.load(wav, sr=16000)" ] }, { "cell_type": "code", "execution_count": 46, "id": "federal-supervision", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "float32\n", "float64\n" ] } ], "source": [ "print(x.dtype)\n", "print(samples.dtype)" ] }, { "cell_type": "code", "execution_count": 30, "id": "parallel-trademark", "metadata": {}, "outputs": [], "source": [ "sf.read?" ] }, { "cell_type": "code", "execution_count": 31, "id": "extended-fishing", "metadata": {}, "outputs": [], "source": [ "librosa.load?" ] }, { "cell_type": "code", "execution_count": 47, "id": "baking-auckland", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 47, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.allclose(x, samples)" ] }, { "cell_type": "code", "execution_count": null, "id": "changed-storage", "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.0" } }, "nbformat": 4, "nbformat_minor": 5 }