惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Hugging Face - Blog
Hugging Face - Blog
Google DeepMind News
Google DeepMind News
云风的 BLOG
云风的 BLOG
WordPress大学
WordPress大学
Vercel News
Vercel News
Apple Machine Learning Research
Apple Machine Learning Research
T
Tailwind CSS Blog
I
InfoQ
小众软件
小众软件
Recent Announcements
Recent Announcements
博客园 - 【当耐特】
The GitHub Blog
The GitHub Blog
大猫的无限游戏
大猫的无限游戏
美团技术团队
T
The Blog of Author Tim Ferriss
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
酷 壳 – CoolShell
酷 壳 – CoolShell
MongoDB | Blog
MongoDB | Blog
V
V2EX
J
Java Code Geeks
有赞技术团队
有赞技术团队
博客园 - 聂微东
B
Blog RSS Feed
博客园 - 司徒正美

博客园 - 白桦的天空

激活windows最快的方法 python+uwsgi警告 树莓派设置为竖屏显示 树莓派无桌面系统(RaspberryPI Lite)启动自动打开Chromium-Browser的具体方法 antui-alipay风格的移动网页设计 移动网页字体大小随着页面自动切换 如何使用 opencv-python 提取视频每一帧的图片? 自定义树莓派开机启动画面-plymouth版本 树莓派py文件自动运行 树莓派VLC获取实时视频流 一个标准的pygame例子 pygame镜像输出 python中使用pylet实现异屏输出 选择金额充值的选中css样式 Debian 无桌面+QT运行环境 手机外部浏览器跳转支付宝内置浏览器例子 前端动态获取后台处理进度显示在进度条上 Ventoy 多合一启动盘制作工具神器 - 将多个系统 Win/PE/Linux 镜像装在1个U盘里 pygame和pywebview配合做界面 通过websocket接口获取数据实时更新数据表
python监听麦克风并通过websocket输出音频数据峰值目的是绘制...
白桦的天空 · 2023-03-02 · via 博客园 - 白桦的天空

服务端代码:

#!/bin/env python
# -*- coding: utf-8 -*-

import asyncio
import websockets
import time
import threading
import signal
import sys
import pyaudio
import numpy as np
from multiprocessing import Process, Value
import multiprocessing
import ctypes

is_exit = False

def record(voice_rate):
    global is_exit    
    p = pyaudio.PyAudio()
    stream = p.open(format=pyaudio.paInt16,
                    channels=1,
                    rate=16000,
                    input=True,
                    frames_per_buffer=1024)

    while not is_exit:
        data = stream.read(1024)
        audio_data = np.fromstring(data, dtype=np.short)
        max_dB = np.max(audio_data)
        voice_rate.value = max_dB
        time.sleep(0.1)


def sig_handler(signum, frame):
    global is_exit
    is_exit = True
    sys.exit(0)


async def sendData(websocket, path):
    # rev = await websocket.recv()
    # print(f"client: {rev}")    
    await websocket.send(str(voice_rate.value))

if __name__ == "__main__":
    voice_rate = multiprocessing.Manager().Value(ctypes.c_int, 0)
    signal.signal(signal.SIGINT, sig_handler)
    # t = threading.Thread(target=add_variable, args=())
    # t.setDaemon(True)
    # t.start()

    p = Process(target=record, args=(voice_rate,))
    p.start()

    start_server = websockets.serve(sendData, '127.0.0.1', 8765)
    asyncio.get_event_loop().run_until_complete(start_server)
    asyncio.get_event_loop().run_forever()

  客户端代码:

import asyncio
import websockets
import time


async def getData():
    uri = "ws://127.0.0.1:8765"
    async with websockets.connect(uri) as websocket:
        await websocket.send("rate")
        ret = await websocket.recv()
        print(f"server: {ret}")
        time.sleep(0.1)
        await getData()

asyncio.get_event_loop().run_until_complete(getData())