{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1e12f3ef",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "        采样设备：\n",
      "            - 设备类型：音频输出\n",
      "            - 序号：26\n",
      "            - 名称：耳机 (HUAWEI FreeLace 活力版) [Loopback]\n",
      "            - 最大输入通道数：2\n",
      "            - 默认低输入延迟：0.003s\n",
      "            - 默认高输入延迟：0.01s\n",
      "            - 默认采样率：48000.0Hz\n",
      "            - 是否回环设备：True\n",
      "\n",
      "        音频样本块大小：2400\n",
      "        样本位宽：2\n",
      "        采样格式：8\n",
      "        音频通道数：2\n",
      "        音频采样率：48000\n",
      "        \n"
     ]
    }
   ],
   "source": [
    "import sys\n",
    "import os\n",
    "import wave\n",
    "\n",
    "current_dir = os.getcwd() \n",
    "sys.path.append(os.path.join(current_dir, '../caption-engine'))\n",
    "\n",
    "from sysaudio.win import AudioStream\n",
    "from audioprcs import resampleRawChunk, mergeChunkChannels\n",
    "\n",
    "stream = AudioStream(0)\n",
    "stream.printInfo()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "a72914f4",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Recording...\n",
      "Done\n"
     ]
    }
   ],
   "source": [
    "\"\"\"获取系统音频输出5秒，然后保存为wav文件\"\"\"\n",
    "\n",
    "with wave.open('output.wav', 'wb') as wf:\n",
    "    wf.setnchannels(stream.CHANNELS)\n",
    "    wf.setsampwidth(stream.SAMP_WIDTH)\n",
    "    wf.setframerate(stream.RATE)\n",
    "    stream.openStream()\n",
    "\n",
    "    print('Recording...')\n",
    "\n",
    "    for _ in range(0, 100):\n",
    "        chunk = stream.read_chunk()\n",
    "        if isinstance(chunk, bytes):\n",
    "            wf.writeframes(chunk)\n",
    "        else:\n",
    "            raise Exception('Error: chunk is not bytes')\n",
    "        \n",
    "    stream.closeStream()    \n",
    "    print('Done')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a6e8a098",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Recording...\n",
      "Done\n"
     ]
    }
   ],
   "source": [
    "\"\"\"获取系统音频输入，转换为单通道音频，持续5秒，然后保存为wav文件\"\"\"\n",
    "\n",
    "with wave.open('output.wav', 'wb') as wf:\n",
    "    wf.setnchannels(1)\n",
    "    wf.setsampwidth(stream.SAMP_WIDTH)\n",
    "    wf.setframerate(stream.RATE)\n",
    "    stream.openStream()\n",
    "\n",
    "    print('Recording...')\n",
    "\n",
    "    for _ in range(0, 100):\n",
    "        chunk = mergeChunkChannels(\n",
    "            stream.read_chunk(),\n",
    "            stream.CHANNELS\n",
    "        )\n",
    "        if isinstance(chunk, bytes):\n",
    "            wf.writeframes(chunk)\n",
    "        else:\n",
    "            raise Exception('Error: chunk is not bytes')\n",
    "        \n",
    "    stream.closeStream()    \n",
    "    print('Done')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "aaca1465",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Recording...\n",
      "Done\n"
     ]
    }
   ],
   "source": [
    "\"\"\"获取系统音频输入，转换为单通道音频并重采样到16000Hz，持续5秒，然后保存为wav文件\"\"\"\n",
    "\n",
    "with wave.open('output.wav', 'wb') as wf:\n",
    "    wf.setnchannels(1)\n",
    "    wf.setsampwidth(stream.SAMP_WIDTH)\n",
    "    wf.setframerate(16000)\n",
    "    stream.openStream()\n",
    "\n",
    "    print('Recording...')\n",
    "\n",
    "    for _ in range(0, 100):\n",
    "        chunk = resampleRawChunk(\n",
    "            stream.read_chunk(),\n",
    "            stream.CHANNELS,\n",
    "            stream.RATE,\n",
    "            16000,\n",
    "            mode=\"sinc_best\"\n",
    "        )\n",
    "        if isinstance(chunk, bytes):\n",
    "            wf.writeframes(chunk)\n",
    "        else:\n",
    "            raise Exception('Error: chunk is not bytes')\n",
    "        \n",
    "    stream.closeStream()    \n",
    "    print('Done')"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "mystd",
   "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.10.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
