فهرست منبع

add:audio c转lua

alienwalker 4 سال پیش
والد
کامیت
10d01523ad

+ 1 - 0
application/include/app_interface.h

@@ -42,6 +42,7 @@
 #include "core_dcmi.h"
 #include "core_rng.h"
 #include "core_task.h"
+#include "audio_ll_drv.h"
 #include "core_soft_keyboard.h"
 #include "lfs.h"
 #include "usb_driver.h"

+ 78 - 0
application/src/luat_audio_air105.c

@@ -0,0 +1,78 @@
+/*
+ * Copyright (c) 2022 OpenLuat & AirM2M
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy of
+ * this software and associated documentation files (the "Software"), to deal in
+ * the Software without restriction, including without limitation the rights to
+ * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
+ * the Software, and to permit persons to whom the Software is furnished to do so,
+ * subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in all
+ * copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
+ * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
+ * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
+ * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ */
+
+
+#include "luat_base.h"
+#include "luat_msgbus.h"
+#include "luat_fs.h"
+#include <stdlib.h>
+#include "luat_multimedia.h"
+#include "app_interface.h"
+
+static Audio_StreamStruct prvAudioStream;
+int32_t luat_audio_app_cb(void *pData, void *pParam)
+{
+    rtos_msg_t msg = {0};
+	msg.handler = l_multimedia_raw_handler;
+	msg.arg1 = (pParam == INVALID_HANDLE_VALUE)?MULTIMEDIA_CB_AUDIO_DONE:MULTIMEDIA_CB_AUDIO_NEED_DATA;
+	msg.arg2 = prvAudioStream.pParam;
+	luat_msgbus_put(&msg, 1);
+
+}
+
+int luat_audio_start_raw(uint8_t multimedia_id, uint8_t audio_format, uint8_t num_channels, uint32_t sample_rate, uint8_t bits_per_sample, uint8_t is_signed)
+{
+	prvAudioStream.pParam = multimedia_id;
+	prvAudioStream.CB = luat_audio_app_cb;
+	prvAudioStream.BitDepth = bits_per_sample;
+	prvAudioStream.BusType = AUSTREAM_BUS_DAC;
+	prvAudioStream.BusID = 0;
+	prvAudioStream.Format = audio_format;
+	prvAudioStream.ChannelCount = num_channels;
+	prvAudioStream.SampleRate = sample_rate;
+	prvAudioStream.IsDataSigned = is_signed;
+	return Audio_StartRaw(&prvAudioStream);
+}
+
+int luat_audio_write_raw(uint8_t multimedia_id, uint8_t *data, uint32_t len)
+{
+	return Audio_WriteRaw(&prvAudioStream, data, len);
+}
+
+
+int luat_audio_stop_raw(uint8_t multimedia_id)
+{
+	Audio_Stop(&prvAudioStream);
+	return ERROR_NONE;
+}
+
+int luat_audio_pause_raw(uint8_t multimedia_id, uint8_t is_pause)
+{
+	if (is_pause)
+	{
+		Audio_Pause(&prvAudioStream);
+	}
+	else
+	{
+		Audio_Resume(&prvAudioStream);
+	}
+	return ERROR_NONE;
+}

+ 1 - 0
application/src/luat_base_air105.c

@@ -200,6 +200,7 @@ static const luaL_Reg loadedlibs[] = {
   {"softkb", luaopen_softkb}, 
 #endif
   {"usbapp", luaopen_usbapp},
+  {"audio", luaopen_multimedia_audio},
   {NULL, NULL}
 };
 

+ 1 - 1
bsp/audio/include/audio_ll_drv.h

@@ -25,7 +25,7 @@ typedef struct
 	llist_head DataHead;
 	uint32_t SampleRate;
 	uint8_t BitDepth;
-	uint8_t ChannelCount;	//声道,目前只有1
+	uint8_t ChannelCount;	//声道,目前只有1或者2
 	auStreamFormat_t Format;
 	auStreamBusType_t BusType;	//音频总线类型,DAC, IIS之类的
 	uint8_t BusID;		//音频总线ID

+ 23 - 8
bsp/audio/src/audio_ll_drv.c

@@ -117,20 +117,35 @@ static int32_t prvAudio_RunDAC(Audio_StreamStruct *pStream)
 
 static int32_t prvAudio_WriteDACRaw(Audio_StreamStruct *pStream, uint8_t *pByteData, uint32_t ByteLen)
 {
-	uint32_t i;
+	uint32_t i, VaildLen;
 	uint32_t DiffBit;
+	uint16_t *wTemp;
 	Audio_DataBlockStruct *Block = zalloc(sizeof(Audio_DataBlockStruct));
+	VaildLen = ByteLen >> (pStream->ChannelCount >> 1);
+	DBG("%u,%u", ByteLen, VaildLen);
 	if (pStream->BitDepth > 8)
 	{
-		Block->uPV.pu8 = malloc(ByteLen);
+		Block->uPV.pu8 = malloc(VaildLen);
 		if (!Block->uPV.pu8)
 		{
 			free(Block);
 			DBG("no mem!");
 			return -ERROR_NO_MEMORY;
 		}
-		memcpy(Block->uPV.pu8, pByteData, ByteLen);
-		Block->Len = ByteLen >> 1;
+		Block->Len = VaildLen >> 1;
+		if (2 == pStream->ChannelCount)
+		{
+			wTemp = pByteData;
+			for(i = 0; i < Block->Len; i++)
+			{
+				Block->uPV.pu16[i] = wTemp[i * 2];
+			}
+		}
+		else
+		{
+			memcpy(Block->uPV.pu8, pByteData, VaildLen);
+		}
+
 		if (pStream->IsDataSigned)
 		{
 			for(i = 0; i < Block->Len; i++)
@@ -158,20 +173,20 @@ static int32_t prvAudio_WriteDACRaw(Audio_StreamStruct *pStream, uint8_t *pByteD
 	}
 	else
 	{
-		Block->uPV.pu8 = malloc(ByteLen * 2);
+		Block->uPV.pu8 = malloc(VaildLen * 2);
 		if (!Block->uPV.pu8)
 		{
 			free(Block);
 			DBG("no mem!");
 			return -ERROR_NO_MEMORY;
 		}
-		Block->Len = ByteLen;
+		Block->Len = VaildLen;
 		DiffBit = prvAudio.DACBit - 8;
 		if (pStream->IsDataSigned)
 		{
 			for(i = 0; i < Block->Len; i++)
 			{
-				Block->uPV.pu16[i] = ((pByteData[i] + 0x80) & 0x00ff);
+				Block->uPV.pu16[i] = ((pByteData[i * pStream->ChannelCount] + 0x80) & 0x00ff);
 				Block->uPV.pu16[i] <<= DiffBit;
 			}
 		}
@@ -179,7 +194,7 @@ static int32_t prvAudio_WriteDACRaw(Audio_StreamStruct *pStream, uint8_t *pByteD
 		{
 			for(i = 0; i < Block->Len; i++)
 			{
-				Block->uPV.pu16[i] = pByteData[i];
+				Block->uPV.pu16[i] = pByteData[i * pStream->ChannelCount];
 				Block->uPV.pu16[i] <<= DiffBit;
 			}
 		}

+ 5 - 0
xmake.lua

@@ -145,6 +145,7 @@ target("lvgl")
     --add_includedirs("bsp/common",{public = true})
 	add_includedirs("bsp/common/include",{public = true})
     add_includedirs("bsp/cmsis/include",{public = true})
+    add_includedirs("bsp/audio/include",{public = true})
     add_includedirs(luatos.."luat/packages/lfs")
     add_includedirs(luatos.."lua/include",{public = true})
     add_includedirs(luatos.."luat/include",{public = true})
@@ -316,8 +317,12 @@ if with_luatos then
 
     add_files(luatos.."components/camera/*.c")
     add_includedirs(luatos.."components/camera")
+
     add_files(luatos.."components/soft_keyboard/*.c")
     add_includedirs(luatos.."components/soft_keyboard")
+
+    add_files(luatos.."components/multimedia/*.c")
+    add_includedirs(luatos.."components/multimedia")
 else
 
     add_files("Third_Party/vsprintf/*.c",{public = true})