最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

python操作麥克風(fēng)方式

 更新時(shí)間:2026年01月17日 09:19:06   作者:AI算法網(wǎng)奇  
文章總結(jié):作者分享了麥克風(fēng)測(cè)試的個(gè)人經(jīng)驗(yàn),包括使用websockerserver.py和pyaudio_client.py進(jìn)行麥克風(fēng)讀取的方法,希望對(duì)大家有所幫助,并鼓勵(lì)大家支持腳本之家

查詢麥克風(fēng)

import sounddevice as sd

# 1. 查看所有主機(jī)API
print("=== All Host APIs ===")
for i, h in enumerate(sd.query_hostapis()):
    print(f"HostAPI {i}: {h['name']}")

print("\n=== All Devices ===")
# 2. 查看所有設(shè)備
for i, d in enumerate(sd.query_devices()):
    hostapi = sd.query_hostapis(d['hostapi'])
    device_type = []
    if d['max_input_channels'] > 0:
        device_type.append("Input")
    if d['max_output_channels'] > 0:
        device_type.append("Output")
    
    print(f"Device {i}: {d['name']}")
    print(f"  HostAPI: {hostapi['name']}")
    print(f"  Type: {', '.join(device_type)}")
    print(f"  Input channels: {d['max_input_channels']}")
    print(f"  Output channels: {d['max_output_channels']}")
    print()

# 3. 嘗試使用默認(rèn)設(shè)備
print("\n=== Testing Default Devices ===")
print(f"Default input device: {sd.default.device[0]}")
print(f"Default output device: {sd.default.device[1]}")

# 4. 直接使用默認(rèn)輸入設(shè)備
try:
    def audio_callback(indata, frames, time, status):
        if status:
            print(f"Status: {status}")
        print(f"Audio shape: {indata.shape}")
    
    
    with sd.InputStream(samplerate=16000, channels=1, dtype='int16', blocksize=320, callback=audio_callback):
        input("?? Recording with default device... Press Enter to stop\n")
except Exception as e:
    print(f"Error: {e}")

# 5. 或者,列出所有輸入設(shè)備
print("\n=== All Input Devices ===")
input_devices = []
for i, d in enumerate(sd.query_devices()):
    if d['max_input_channels'] > 0:
        hostapi = sd.query_hostapis(d['hostapi'])
        input_devices.append((i, d, hostapi['name']))
        print(f"Device {i}: {d['name']}")
        print(f"  HostAPI: {hostapi['name']}")
        print(f"  Channels: {d['max_input_channels']}")

# 6. 選擇一個(gè)可用的輸入設(shè)備
if input_devices:
    print("\n=== Try using first available input device ===")
    mic_index = input_devices[0][0]
    device_info = input_devices[0][1]
    
    channels = min(1, device_info['max_input_channels'])  # 使用單聲道更通用
    
    try:
        with sd.InputStream(device=mic_index, samplerate=16000, channels=channels, dtype='int16', blocksize=320, callback=lambda indata, frames, time, status: print(f"Audio shape: {indata.shape}")):
            input(f"?? Recording with {device_info['name']}... Press Enter to stop\n")
    except Exception as e:
        print(f"Error with device {mic_index}: {e}")
else:
    print("No input devices found at all!")

測(cè)試麥克風(fēng)

import sounddevice as sd
import numpy as np

# 直接使用設(shè)備10
mic_index = 10

print("Testing microphone...")

def callback(indata, frames, time, status):
    volume = np.linalg.norm(indata) * 10
    print(f"Microphone level: {volume:.2f}", end='\r')

# 嘗試不同的參數(shù)組合
settings_to_try = [
    {'samplerate': 16000, 'channels': 1, 'dtype': 'int16'},
    {'samplerate': 44100, 'channels': 1, 'dtype': 'float32'},
    {'samplerate': 48000, 'channels': 1, 'dtype': 'int16'},
]

for i, settings in enumerate(settings_to_try):
    print(f"\nTry {i+1}: {settings}")
    try:
        with sd.InputStream(
            device=mic_index,
            callback=callback,
            **settings
        ):
            input(f"Settings {i+1} working! Press Enter to stop...\n")
            break
    except Exception as e:
        print(f"Failed: {e}")

讀取麥克風(fēng)

import sounddevice as sd
import numpy as np
from scipy import signal

mic_index = 10
target_samplerate = 16000  # 目標(biāo)采樣率
original_samplerate = 44100  # 設(shè)備支持的采樣率

print(f"Recording at {original_samplerate}Hz, resampling to {target_samplerate}Hz")


def callback(indata, frames, time, status):
    """接收44100Hz音頻,重采樣到16000Hz"""
    if status:
        print(status)
    
    # 如果是立體聲,轉(zhuǎn)換為單聲道
    if indata.shape[1] > 1:
        audio = np.mean(indata, axis=1)
    else:
        audio = indata.flatten()
    
    # 重采樣到16000Hz
    num_samples = int(len(audio) * target_samplerate / original_samplerate)
    resampled = signal.resample(audio, num_samples)
    
    print(f"Original: {len(audio)} samples, Resampled: {len(resampled)} samples", end='\r')


with sd.InputStream(device=mic_index, samplerate=original_samplerate, channels=1, dtype='float32', callback=callback):
    input("Recording and resampling... Press Enter to stop\n")

web socker server.py

import asyncio
import websockets
import json
import numpy as np
from datetime import datetime
import time

async def handle_audio_client(websocket, path):
    """處理音頻客戶端連接"""
    client_id = id(websocket)
    client_ip = websocket.remote_address[0]
    
    print(f"\n? Client {client_id} connected from {client_ip}")
    
    try:
        # 1. 接收音頻格式信息
        try:
            format_msg = await asyncio.wait_for(websocket.recv(), timeout=5.0)
            if isinstance(format_msg, str):
                format_data = json.loads(format_msg)
                print(f"?? Audio format: {format_data}")
                
                # 發(fā)送確認(rèn)
                await websocket.send(json.dumps({'status': 'ready', 'message': 'Start sending audio!'}))
        except asyncio.TimeoutError:
            print("?? No format received, assuming default settings")
        
        # 2. 實(shí)時(shí)接收音頻數(shù)據(jù)
        print("?? Listening for audio data...")
        print("-" * 60)
        
        packet_count = 0
        total_bytes = 0
        start_time = time.time()
        last_print_time = time.time()
        
        try:
            while True:
                try:
                    # 接收數(shù)據(jù)(設(shè)置超時(shí))
                    message = await asyncio.wait_for(websocket.recv(), timeout=2.0)
                    packet_count += 1
                    
                    if isinstance(message, bytes):
                        # 音頻數(shù)據(jù)
                        data_size = len(message)
                        total_bytes += data_size
                        
                        # 解析音頻數(shù)據(jù)
                        try:
                            audio_data = np.frombuffer(message, dtype=np.float32)
                            
                            # 計(jì)算實(shí)時(shí)統(tǒng)計(jì)
                            if len(audio_data) > 0:
                                # 計(jì)算音量
                                rms = np.sqrt(np.mean(audio_data ** 2))
                                max_val = np.max(np.abs(audio_data))
                                
                                # 轉(zhuǎn)換為分貝
                                if rms > 0:
                                    db = 20 * np.log10(rms)
                                else:
                                    db = -100
                                
                                # 創(chuàng)建音量可視化
                                bars = max(0, min(int((db + 60) / 3), 20))
                                volume_bar = "█" * bars + "?" * (20 - bars)
                                
                                # 每秒更新顯示
                                current_time = time.time()
                                if current_time - last_print_time >= 0.1:  # 每0.1秒更新一次
                                    elapsed = current_time - start_time
                                    data_rate = total_bytes / elapsed / 1024  # KB/s
                                    
                                    print(f"\r?? Packets: {packet_count:4d} | "
                                          f"Rate: {data_rate:5.1f} KB/s | "
                                          f"RMS: {rms:6.4f} | "
                                          f"dB: {db:6.1f} | "
                                          f"Volume: [{volume_bar}]", end="", flush=True)
                                    
                                    last_print_time = current_time
                        
                        except Exception as e:
                            print(f"\n?? Audio processing error: {e}")
                    
                    elif isinstance(message, str):
                        print(f"\n?? Message: {message}")
                
                except asyncio.TimeoutError:
                    # 超時(shí),檢查連接是否還活著
                    try:
                        await websocket.ping()
                        continue
                    except:
                        break
        
        except websockets.exceptions.ConnectionClosed:
            print(f"\n?? Client disconnected normally")
    
    except Exception as e:
        print(f"\n? Error: {e}")
    
    finally:
        # 連接結(jié)束,顯示統(tǒng)計(jì)
        elapsed = time.time() - start_time
        if elapsed > 0:
            print(f"\n" + "=" * 60)
            print(f"?? Connection Statistics:")
            print(f"   Client ID: {client_id}")
            print(f"   Duration: {elapsed:.1f} seconds")
            print(f"   Packets received: {packet_count}")
            print(f"   Total data: {total_bytes / 1024:.1f} KB")
            print(f"   Average rate: {total_bytes / elapsed / 1024:.1f} KB/s")
            print(f"   Packets/sec: {packet_count / elapsed:.1f}")
            print("=" * 60)


async def main():
    server = await websockets.serve(handle_audio_client, "0.0.0.0", 8765, ping_interval=10, ping_timeout=20, max_size=10 * 1024 * 1024)
    
    print(f"? Server running on ws://0.0.0.0:8765")
    print(f"?? Ready to receive audio streams")
    print(f"?? Press Ctrl+C to stop\n")
    
    await server.wait_closed()


if __name__ == "__main__":
    try:
        asyncio.run(main())
    except KeyboardInterrupt:
        print("\n?? Server stopped")

audio_client.py

import asyncio
import websockets
import sounddevice as sd
import numpy as np
import json
import threading
import queue
import time

print("?? WebSocket Audio Streaming Client")
print("=" * 60)


class WorkingClient:
    def __init__(self):
        self.server_url = "ws://localhost:8765"
        self.device_index = 1
        self.sample_rate = 44100
        self.channels = 1
        self.dtype = 'float32'
        self.blocksize = 1024
        
        # 音頻隊(duì)列
        self.audio_queue = queue.Queue(maxsize=50)
        
        # 控制標(biāo)志
        self.running = True
        self.is_recording = False
    
    def start_audio_capture(self):
        """啟動(dòng)音頻采集 - 在單獨(dú)的線程中運(yùn)行"""
        print(f"?? Opening microphone (device {self.device_index})...")
        
        def callback(indata, frames, time_info, status):
            if status:
                print(f"?? Audio status: {status}")
            
            # 檢查音頻是否有效
            if np.any(indata):
                # 計(jì)算音量
                volume = np.linalg.norm(indata)
                
                
                # 只將有聲音的數(shù)據(jù)放入隊(duì)列
                if volume > 0.52:  # 音量閾值
                    try:
                        # 復(fù)制數(shù)據(jù)并放入隊(duì)列
                        audio_copy = indata.copy()
                        self.audio_queue.put(audio_copy, timeout=0.01)
                        
                        # 顯示音量(每20個(gè)包顯示一次)
                        if hasattr(callback, 'counter'):
                            callback.counter += 1
                        else:
                            callback.counter = 0
                        
                        if callback.counter % 20 == 0:
                            print(f"\r?? Mic level: {volume:.4f} | Queue: {self.audio_queue.qsize()}", end="")
                    
                    except queue.Full:
                        # 隊(duì)列滿了,清空并重新開始
                        try:
                            self.audio_queue.get_nowait()
                        except:
                            pass
        
        try:
            # 創(chuàng)建音頻流
            self.stream = sd.InputStream(device=self.device_index, samplerate=self.sample_rate, channels=self.channels, dtype=self.dtype, blocksize=self.blocksize, callback=callback)
            
            self.stream.start()
            self.is_recording = True
            print("? Microphone is ready! Speak now...")
            return True
        
        except Exception as e:
            print(f"? Failed to open microphone: {e}")
            return False
    
    async def connect_and_stream(self):
        """連接服務(wù)器并發(fā)送音頻"""
        print(f"\n?? Connecting to server...")
        
        try:
            # 連接WebSocket
            async with websockets.connect(self.server_url, ping_interval=10, ping_timeout=20) as websocket:
                
                print("? Connected to server!")
                
                # 發(fā)送音頻格式
                await websocket.send(json.dumps({'type': 'audio_format', 'samplerate': self.sample_rate, 'channels': self.channels, 'dtype': self.dtype, 'blocksize': self.blocksize}))
                
                # 等待服務(wù)器響應(yīng)
                response = await websocket.recv()
                print(f"?? Server: {response}")
                
                # 開始流式傳輸
                print("\n" + "=" * 50)
                print("?? Streaming audio to server...")
                print("?? Speak into your microphone!")
                print("?? Press Ctrl+C to stop")
                print("=" * 50 + "\n")
                
                packet_count = 0
                last_display_time = time.time()
                
                # 主發(fā)送循環(huán)
                while self.running:
                    try:
                        # 從隊(duì)列獲取音頻數(shù)據(jù)(非阻塞)
                        if not self.audio_queue.empty():
                            audio_data = self.audio_queue.get_nowait()
                            
                            # 發(fā)送音頻數(shù)據(jù)
                            await websocket.send(audio_data.tobytes())
                            packet_count += 1
                            
                            # 每秒顯示一次統(tǒng)計(jì)
                            current_time = time.time()
                            if current_time - last_display_time >= 1.0:
                                queue_size = self.audio_queue.qsize()
                                print(f"\r?? Packets: {packet_count:4d} | "
                                      f"Queue: {queue_size:2d} | "
                                      f"Sample: {audio_data[0, 0]:7.4f}...", end="")
                                last_display_time = current_time
                        
                        else:
                            # 隊(duì)列為空,短暫等待
                            await asyncio.sleep(0.01)
                            
                            # 偶爾發(fā)送ping保持連接
                            if packet_count > 0 and packet_count % 100 == 0:
                                await websocket.ping()
                    
                    except queue.Empty:
                        # 隊(duì)列空,短暫等待
                        await asyncio.sleep(0.01)
                    
                    except Exception as e:
                        print(f"\n?? Streaming error: {e}")
                        break
                
                print(f"\n?? Stopped. Total packets sent: {packet_count}")
        
        except Exception as e:
            print(f"? Connection error: {e}")
    
    async def run(self):
        """運(yùn)行客戶端"""
        try:
            # 啟動(dòng)音頻采集
            if not self.start_audio_capture():
                return
            
            # 連接并流式傳輸
            await self.connect_and_stream()
        
        except KeyboardInterrupt:
            print("\n?? Stopped by user")
        except Exception as e:
            print(f"\n? Error: {e}")
        finally:
            # 清理
            self.running = False
            if hasattr(self, 'stream'):
                self.stream.stop()
                self.stream.close()
                print("?? Microphone closed")


# 運(yùn)行客戶端
async def main():
    client = WorkingClient()
    await client.run()


if __name__ == "__main__":
    asyncio.run(main())

總結(jié)

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論

柳州市| 乌鲁木齐市| 永胜县| 博野县| 井研县| 什邡市| 长海县| 通海县| 西乌| 龙江县| 云安县| 于田县| 林州市| 高陵县| 洛川县| 成武县| 青岛市| 西丰县| 乾安县| 滨海县| 赣榆县| 株洲市| 宣恩县| 彭泽县| 永仁县| 建水县| 阳山县| 山东省| 砀山县| 同江市| 青神县| 泾川县| 科技| 澎湖县| 清河县| 额敏县| 留坝县| 远安县| 宁津县| 珲春市| 青河县|