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

推荐订阅源

Engineering at Meta
Engineering at Meta
博客园_首页
H
Help Net Security
WordPress大学
WordPress大学
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
罗磊的独立博客
博客园 - 三生石上(FineUI控件)
B
Blog
I
InfoQ
SecWiki News
SecWiki News
T
Tailwind CSS Blog
Spread Privacy
Spread Privacy
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
V
Vulnerabilities – Threatpost
N
Netflix TechBlog - Medium
P
Palo Alto Networks Blog
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
Vercel News
Vercel News
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
K
Kaspersky official blog
M
MIT News - Artificial intelligence
S
Schneier on Security
T
Threat Research - Cisco Blogs
F
Fortinet All Blogs
Cyberwarzone
Cyberwarzone
Scott Helme
Scott Helme
aimingoo的专栏
aimingoo的专栏
Martin Fowler
Martin Fowler
MyScale Blog
MyScale Blog
The Cloudflare Blog
Recent Announcements
Recent Announcements
Security Latest
Security Latest
G
GRAHAM CLULEY
IT之家
IT之家
Y
Y Combinator Blog
The Last Watchdog
The Last Watchdog
腾讯CDC
Google DeepMind News
Google DeepMind News
V
V2EX
S
Securelist
TaoSecurity Blog
TaoSecurity Blog
B
Blog RSS Feed
S
SegmentFault 最新的问题
博客园 - 叶小钗
P
Proofpoint News Feed
云风的 BLOG
云风的 BLOG
Project Zero
Project Zero
G
Google Developers Blog
Google DeepMind News
Google DeepMind News
F
Full Disclosure

博客园 - 白桦的天空

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())