Просмотр исходного кода

fix:mp3解码没有根据frame大小来调整读取文件数据大小

alienwalker 4 лет назад
Родитель
Сommit
0240ced859
2 измененных файлов с 41 добавлено и 28 удалено
  1. 13 3
      components/multimedia/luat_lib_multimedia.c
  2. 28 25
      demo/multimedia/Air105/music_demo.lua

+ 13 - 3
components/multimedia/luat_lib_multimedia.c

@@ -264,6 +264,7 @@ local result = codec.get_audio_data(coder, "xxx", zbuff)
 static int l_codec_get_audio_data(lua_State *L) {
 	luat_multimedia_codec_t *coder = (luat_multimedia_codec_t *)lua_touserdata(L, 1);
 	uint32_t pos = 0;
+	uint32_t frame_len = 0;
 	int result;
 	luat_zbuff_t *in_buff = ((luat_zbuff_t *)luaL_checkudata(L, 2, LUAT_ZBUFF_TYPE));
 	luat_zbuff_t *out_buff = ((luat_zbuff_t *)luaL_checkudata(L, 3, LUAT_ZBUFF_TYPE));
@@ -279,12 +280,20 @@ static int l_codec_get_audio_data(lua_State *L) {
 				result = mp3dec_decode_frame(coder->mp3_decoder, in_buff->addr + pos, in_buff->used - pos, out_buff->addr + out_buff->used, &info);
 //				LLOGD("result %u,%u,%u", result, info.frame_bytes, pos);
 				out_buff->used += (result * info.channels * 2);
-				pos += info.frame_bytes;
+				if (result)
+				{
+					pos += info.frame_bytes;
+					frame_len = (frame_len > info.frame_bytes)?frame_len:(info.frame_bytes + 16);
+				}
+//				else
+//				{
+//					LLOGD("!!!");
+//				}
 				if ((out_buff->len - out_buff->used) < MINIMP3_MAX_SAMPLES_PER_FRAME)
 				{
 					__zbuff_resize(out_buff, out_buff->len * 2);
 				}
-			} while ((result > 0) && ((in_buff->used - pos) >= info.frame_bytes));
+			} while ((result > 0) && ((in_buff->used - pos) >= frame_len));
 //			LLOGD("result %u,%u,%u", result, in_buff->used - pos, info.frame_bytes);
 			if (pos >= in_buff->used)
 			{
@@ -302,7 +311,8 @@ static int l_codec_get_audio_data(lua_State *L) {
 
     }
 	lua_pushboolean(L, result);
-	return 1;
+	lua_pushinteger(L, frame_len);
+	return 2;
 }
 
 /**

+ 28 - 25
demo/multimedia/Air105/music_demo.lua

@@ -21,9 +21,10 @@ end)
 -- music文件夾,可以換成你自己的目录,不用加/sd/,demo里固定用TF卡目录,如果要本地目录,自行修改
 local music_dir = "/music/"
 
-function music_demo_start()
-    -- sys.wait(1000) -- 启动延时
+sys.taskInit(function()
+    sys.wait(100) -- 启动延时
     local tag_len = 0
+    local frame_len = 1045
     local spiId = 0
     local result = spi.setup(
         spiId,--串口id
@@ -36,7 +37,8 @@ function music_demo_start()
     local TF_CS = pin.PB13
     gpio.setup(TF_CS, 1)
     -- fatfs.debug(1) -- 若挂载失败,可以尝试打开调试信息,查找原因
-    fatfs.mount("SD", 0, TF_CS, 12000000)
+    fatfs.mount("SD", 0, TF_CS, 24000000)
+    -- fatfs.mount("SD", 0, TF_CS, 24000000)
     local data, err = fatfs.getfree("SD")
     -- usbapp.udisk_attach_sdhc(0)
     -- usbapp.start(0)
@@ -48,12 +50,14 @@ function music_demo_start()
         log.info("fatfs", "getfree", json.encode(data))
         local dir_nums, dir_info = fatfs.lsdir(music_dir)
         for k,v in pairs(dir_info) do
-            log.info(v.size, k)
+            log.info("find",k)
             play_list[v.size] = "/sd".. music_dir .. k
         end
         while true do
             for k,v in pairs(play_list) do
-                log.info(v)
+                log.info('play',v)
+                log.info(rtos.meminfo("sys"))
+                log.info(rtos.meminfo("lua"))
                 if v:find(".mp3") or v:find(".MP3") then
                     f = io.open(v, "rb")               
                     if f then
@@ -74,35 +78,29 @@ function music_demo_start()
                             log.info("mp3 info", NumChannels, SampleRate, BitsPerSample)
                             buff:resize(65536)
                             in_buff:copy(nil, data)
-                            result = codec.get_audio_data(codecr, in_buff, buff)
-                            while buff:used() == 0 do
-                                log.info("need more data to decode", in_buff:used())
-                                data = f:read(2048)
-                                in_buff:copy(nil, data)
-                                result = codec.get_audio_data(codecr, in_buff, buff)
-                            end
+                            result, frame_len = codec.get_audio_data(codecr, in_buff, buff)
+                            log.debug("frame_len",frame_len)
+                            in_buff:resize(frame_len * 8 + 512)
                             log.debug("start", audio.start(0, AudioFormat, NumChannels, SampleRate, BitsPerSample, is_signed))
                             audio.write(0, buff)
                             --local tick1,_ = mcu.tick64()
-                            data = f:read(2048)
+                            data = f:read(frame_len * 4)
                             in_buff:copy(nil, data) 
-                            result = codec.get_audio_data(codecr, in_buff, buff)
+                            result,_ = codec.get_audio_data(codecr, in_buff, buff)
                             --local tick2,_ = mcu.tick64()
                             --log.debug(mcu.dtick64(tick2, tick1)/_)
                             audio.write(0, buff)
-                            data = f:read(2048)
-                            while data and #data > 0 and not decode_error do
+                            data = f:read(frame_len * 4)
+                            while result and data and #data > 0 do
                                 sys.waitUntil("moredata", 2000)
                                 in_buff:copy(nil, data) 
-                                result = codec.get_audio_data(codecr, in_buff, buff)
-                                while buff:used() == 0 and data and #data > 0 do
-                                    log.info("need more data to decode", in_buff:used())
-                                    data = f:read(2048)
-                                    in_buff:copy(nil, data)
-                                    result = codec.get_audio_data(codecr, in_buff, buff)
+                                result,_= codec.get_audio_data(codecr, in_buff, buff)
+                                if result then
+                                    audio.write(0, buff)
+                                    data = f:read(frame_len * 4)
+                                else
+                                    log.info("解码结束")
                                 end
-                                audio.write(0, buff)
-                                data = f:read(2048)
                             end
                             sys.waitUntil("playover", 2000)           
                             
@@ -156,9 +154,14 @@ function music_demo_start()
                         f:close()
                     end
                 end
+                log.info(rtos.meminfo("sys"))
+                log.info(rtos.meminfo("lua"))
             end
         end
+
+
+
     else
         log.info("fatfs", "err", err)
     end
-end
+end)