兄弟们有福了,音频内容网上非常生僻好难整通。用硬件的思维很简单,就是单位时间的电平数据集。道理我都懂单片机上很好写。网上你要用正确的api调用麦克、喇叭才能录音出声。我来打个样吧。不然不整几天就怪了。
上代码,python音频服务端
#复制粘贴即可,细节自己百度,主要是跑通程序
import base64
import asyncio
import websockets
import cv2
import math
import np
import pyaudio
import wave
import time
import threading
import matplotlib.pyplot as plt
chunk = 4096# Record in chunks of 1024 samples
sample_format = pyaudio.paInt16 # 16 bits per sample
channels = 2 #两声道
fs = 44100 # Record at 44100 samples per second
p = pyaudio.PyAudio() # 创建音频对象
#image = cv2.imread("1.png")#image :是返回提取到的图片的值
#麦克风
micstream = p.open(format=sample_format,#音频输入流创建
channels=channels,
rate=fs,
frames_per_buffer=chunk,
input=True)
#喇叭
stream2 = p.open(format=sample_format,#音频输出流创建
channels=channels,
rate=fs,
frames_per_buffer=chunk,
output=True)
async def echo(websocket, path):
print(websockets.serve)
async for message in websocket:
stream2.write(message)#播放
#m=micstream.read(chunk)#录音
y=[]
for i in range(15):
m=micstream.read(chunk)
y.append(m)
if len(message )>1:await websocket.send(y)#发送录音
async def main():
# start a websocket server
async with websockets.serve(echo, "192.168.1.102", 8765):
await asyncio.Future() # run forever
asyncio.run(main())
html网页端,上代码
在消息栏里随便输入点数据,点击发送触发事件(播放电脑的麦克风音频)
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Web Audio API 测试</title>
</head>
<body>
<table>
<tr>
<td>参数:</td>
<td><input style="width: 390px;" type="text" name="type" id="type"></td>
<td>
<button onclick="onSendMessage()">发送消息</button>
</td>
<td>
<button onclick="onCloseMessage()">断开连接</button>
</td>
</tr>
<audio id="video"></audio>
</table>
</body>
<script>
/***
navigator.mediaDevices.getUserMedia({video: 1, audio: 1})
.then(function(stream) {
})***/
const socket = new WebSocket('ws://192.168.1.102:8765');
//播放声音
// 连接成功
socket.addEventListener('open', function (event) {
//socket.send('123')
});
const audioCtx = new AudioContext();
//websocket接收到数据
socket.onmessage = function (event) {
console.log(event.data);
var reader = new FileReader(); //文件阅读器
reader.readAsArrayBuffer(event.data); //读取成ArrayBuffer对象
reader.onload = function() { //读取完毕
//解码
var a= new Int16Array(this.result);//Int32Array
//console.log(a)
// 创建音频缓存对象
const noiseBuffer = new AudioBuffer({
length: a.length/2,//长度
sampleRate: 44100,//采样率
numberOfChannels:2,//通道数
});
// 创建一个空缓冲区
const data = noiseBuffer.getChannelData(0);//获取轨道数据,这里是左声道
const data2 = noiseBuffer.getChannelData(1);//获取轨道数据,这里是右声道
//填充data
var c=[]
for (let i = 0; i < a.length; i=i+2) {
data[i-Math.floor(i*0.5)] = a[i]/30000;
data2[i-Math.floor(i*0.5)]=a[i+1]/30000;//data[i];
// c.push(a[i]/9184)
}
console.log(noiseBuffer.getChannelData(0))
console.log(noiseBuffer.getChannelData(1))
// 创建音频流对象
const noise = new AudioBufferSourceNode(audioCtx, {
buffer: noiseBuffer
});
noise.connect(audioCtx.destination);
noise.start();
socket.send('111');
}
}
//发送消息
function onSendMessage() {
var mess = document.getElementById("type").value;
socket.send(mess);
}
//这里只是播放电脑的麦克风,为什么不把网页端传麦克风到python写在一起,是为了增加难度哈哈,自己慢慢处理,自己融合吧,嘿嘿
</script>
</html>
此节完。下节,网页端麦克音频传到python端并播放。