杨佳铮 10 месяцев назад
Родитель
Сommit
ec8dddfaf4

+ 7 - 2
components/multimedia/luat_lib_multimedia_audio.c

@@ -304,7 +304,7 @@ static void record_stop(uint8_t *data, uint32_t len){
 
 /**
 录音
-@api audio.record(id, record_type, record_time, amr_quailty, path, record_callback_time, buff0, buff1)
+@api audio.record(id, record_type, record_time, amr_quailty, path, record_callback_time, buff0, buff1,channelCount)
 @int id             多媒体播放通道号
 @int record_type    录音音频格式,支持 audio.AMR audio.PCM (部分平台支持audio.AMR_WB),或者直接输入采样率
 @int record_time    录制时长 单位秒,可选,默认0即表示一直录制
@@ -313,6 +313,7 @@ static void record_stop(uint8_t *data, uint32_t len){
 @int record_callback_time	不指定录音文件路径时,单次录音回调时长,单位是100ms。默认1,既100ms
 @zbuff				录音原始PCM数据缓存0,不填写录音文件路径才会用到
 @zbuff				录音原始PCM数据缓存1,不填写录音文件路径才会用到
+@channelCount		1单声道录音 2立体声录音 默认单声道
 @return boolean     成功返回true,否则返回false
 @usage
 err,info = audio.record(id, type, record_time, quailty, path)
@@ -344,6 +345,7 @@ static int l_audio_record(lua_State *L){
     }
 
     record_buffer_len = luaL_optinteger(L, 6, 1);
+	g_s_record.channelCnt = luaL_optinteger(L, 9, LUAT_RECORD_MONO);
     if (g_s_record.type==LUAT_MULTIMEDIA_DATA_TYPE_AMR_NB||g_s_record.type==LUAT_MULTIMEDIA_DATA_TYPE_AMR_WB){
 #ifdef LUAT_SUPPORT_AMR
     if (g_s_record.type==LUAT_MULTIMEDIA_DATA_TYPE_AMR_NB){
@@ -858,7 +860,10 @@ static const rotable_Reg_t reg_audio[] =
 	{ "VOLTAGE_1800", 		ROREG_INT(LUAT_AUDIO_VOLTAGE_1800)},
     //@const VOLTAGE_3300 number 可配置的codec工作电压,3.3V
 	{ "VOLTAGE_3300", 		ROREG_INT(LUAT_AUDIO_VOLTAGE_3300)},
-
+    //@const RECORD_MONO number 录音使用单声道
+	{ "RECORD_MONO", 		ROREG_INT(LUAT_RECORD_MONO)},
+    //@const RECORD_STEREO number 录音使用立体声
+	{ "RECORD_STEREO", 		ROREG_INT(LUAT_RECORD_STEREO)},
 	{ NULL,            ROREG_INT(0)}
 };
 

+ 96 - 0
demo/audio_bk/main.lua

@@ -0,0 +1,96 @@
+PROJECT = "adcdemo"
+VERSION = "1.0.0"
+SSID = "goat"
+WIFIPASS = "Linxumeng1234"
+AccessKeyID = "LTAI5tNJonTcotn8XYFqD5eq"
+AccessKeySecret = "MtvSCvWf1jnpZljeLILMkRRR7tZKTF"
+sys = require("sys")
+sysplus = require("sysplus")
+libnet = require "libnet"
+log.setLevel(1)
+log.info("main", PROJECT, VERSION)
+
+pendingData = nil
+function ConnectWifi()
+	-- 联网 对表
+	wlan.init()
+	wlan.connect(SSID, WIFIPASS)
+	log.info("wlan", "wait for IP_READY")
+	sys.waitUntil("IP_READY", 30000)
+	if wlan.ready() then
+		log.info("wlan", "ready !!")
+	end
+end
+ping = zbuff.create(42)
+pong = zbuff.create(42)
+function audioCallback(audio_id, msg,data)
+    if msg == audio.RECORD_DATA then
+		if(data==1)then
+			pendingData = pong:toStr()
+		else
+			pendingData = ping:toStr()
+		end
+	end
+end
+-- 联网
+
+audio.setBus(0, audio.BUS_DAC)
+audio.vol(0,70);
+audio.micVol(0,100);
+audio.start(0, audio.PCM,1, 8000, 16) -- 通道0,PCM格式,单声道,采样率16000Hz,16位深度
+audio.on(0, audioCallback)
+audio.record(0, audio.PCM, 0, 0, nil,1,ping,pong,audio.RECORD_STEREO)
+
+function main()
+	ConnectWifi()
+	local netc = socket.create(nil, "main")
+	socket.config(netc, 8899, true, false)
+	local result = libnet.connect( "main", 15000, netc, "192.168.32.200", 8899)
+	
+	if result then
+		log.info("socket", "服务器连上了")
+	else
+		log.info("socket", "服务器没连上了!!!")
+	end
+	libnet.tx( "main", 0, netc, "helloworld")
+	local rolling=0
+	local buff = zbuff.create(1024)
+	while true do
+		sys.wait(10)
+		local ok, param = socket.rx(netc, buff)
+		if ok and buff ~= nil and buff:used() > 0 then
+			data=buff:toStr()
+			log.info("rx_buff", #data)
+			while true do
+				local resu = audio.write(0,data) 
+				if resu then 
+					break
+				end
+			end
+			libnet.tx( "main", 0, netc, "OK")
+		end
+		if buff ~= nil then
+			buff:del()
+		end
+		if pendingData then
+			local max_size = 1000 -- 每段的最大字节数 udp MTU(一包最多)约1400
+			local total_length = #pendingData
+			local offset = 0
+			while offset < total_length do
+				local send_length = math.min(max_size, total_length - offset)
+				local segment = pendingData:sub(offset + 1, offset + send_length)
+				libnet.tx("main", 0, netc, string.pack("<I", rolling) .. segment)
+				rolling = rolling +1
+				offset = offset + send_length
+			end
+			pendingData = nil
+		end
+	end
+end
+
+local function netCB(msg)
+	--log.info("未处理消息", msg[1], msg[2], msg[3], msg[4])
+end
+local txqueue = {}
+sysplus.taskInitEx(	main, "main", netCB, "main", txqueue, "main")
+sys.run()

+ 31 - 0
demo/audio_bk/发数据.py

@@ -0,0 +1,31 @@
+import socket
+import time,fpstimer
+import numpy as np
+from scipy.io.wavfile import write
+# 配置 UDP 端口和绑定地址
+UDP_IP = "0.0.0.0"  # 监听所有可用的网络接口
+UDP_PORT = 8899     # 监听的端口
+sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM,socket.IPPROTO_UDP)
+sock.bind((UDP_IP, UDP_PORT))
+# _,target = sock.recvfrom(1500)
+target = ('192.168.32.102', 8899)
+print("已经连接",target)
+
+with open("1.bin","rb") as f:
+    d=f.read()
+    data2=np.frombuffer(d,dtype=np.int16)
+    
+data2 = data2.astype(np.float32) * 32000.0 / np.max(np.abs(data2))
+data2 = data2.astype(np.int16)[::2]
+
+with open("2.bin","wb") as f:
+    f.write(data2.tobytes())
+with open("2.bin","rb") as f:
+    while True:
+        d=f.read(1024)
+        
+        if len(d) == 0:break
+        n=sock.sendto(d,target)
+        while sock.recvfrom(1500)[0] != b'OK':pass
+sock.close()
+

+ 35 - 0
demo/audio_bk/接数据.py

@@ -0,0 +1,35 @@
+import socket
+import time
+import numpy as np
+from scipy.io.wavfile import write
+# 配置 UDP 端口和绑定地址
+UDP_IP = "0.0.0.0"  # 监听所有可用的网络接口
+UDP_PORT = 8899     # 监听的端口
+sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
+sock.bind((UDP_IP, UDP_PORT))
+print("已经连接")
+data2 = []
+lastroll = None
+try:
+    with open("1.bin","wb") as f:
+        while True:
+            data,_ = sock.recvfrom(1500)  # 每次接收最大 1024 字节
+            f.write(data[4:])
+            roll = int.from_bytes(data[:4],'little')
+            if lastroll is not None and roll != lastroll +1:
+                print(roll,lastroll,len(data))
+            else:
+                print(len(data))
+            lastroll=roll
+            
+except KeyboardInterrupt:
+    pass
+sock.close()
+with open("1.bin","rb") as f:
+    d=f.read()
+    data2=np.frombuffer(d,dtype=np.int16).reshape([-1,2])
+
+data2 = data2.astype(np.float32) * 32000.0 / np.max(np.abs(data2))
+data2 = data2.astype(np.int16)
+write("output.wav", 8000, data2)
+

+ 5 - 0
luat/include/luat_audio.h

@@ -71,6 +71,10 @@ typedef enum{
     LUAT_AUDIO_VOLTAGE_3300 = 0,         	 /* 工作在3.3V */
 	LUAT_AUDIO_VOLTAGE_1800,       			 /* 工作在1.8V */
 }luat_audio_voltage_t;
+typedef enum{
+    LUAT_RECORD_MONO = 1,         	 /* 工作在3.3V */
+	LUAT_RECORD_STEREO = 2,       			 /* 工作在1.8V */
+}luat_record_channel_t;
 
 typedef enum{
 	LUAT_AUDIO_BUS_DAC=0,
@@ -97,6 +101,7 @@ typedef struct{
     uint8_t multimedia_id;
 	uint8_t quailty;
 	uint8_t is_run;
+    luat_record_channel_t channelCnt;
 }luat_record_ctrl_t;
 
 #endif