【ChipIntelli 系列】从 bat 脚本看生成固件 voice.bin 过程

在工程\projects\xxx[prj_name]\firmware 目录下, 有一个 合成分区bin文件.bat
其一大作用是将音频文件生成 voice.bin ,并为后续烧录 Flash 作准备。

 

Makefile 中生成 voice.bin 的过程代码如下:

echo 请选择播报音音频格式:
@REM echo 1. adpcm (默认格式,压缩比4:1,音质一般,解码器小,解码运算量小)
@REM echo 2. pcm   (无压缩,占用flash空间较大,音质好,无解码器,无解码运算)
@REM echo 3. flac  (压缩比大约3:1,音质好,解码器小,解码运算量略大于adpcm)
@REM echo 4. mp3    (压缩比大约10:1,音质较好,解码器较大,解码运算量大)
@REM echo 5. opus   (压缩比大约16:1,音频质量一般,解码器小)
@REM set fmt=4
set fmt=4
@REM set /p fmt=default=4:

echo %fmt%

if %fmt% == 1 (
	if not exist "adpcm" (
		mkdir "adpcm"
	)
	for /d %%i in (*) do (
		echo %%i | findstr "\[.*\].*" > nul && call:to_adpcm adpcm %%i
	)
	..\%TOOLS_PATH%\group_merge.exe -i adpcm -o ..\voice
) else if %fmt% equ 4 (
	if not exist "mp3" (
		mkdir "mp3"
	)
	@REM for /d %%i in (*) do (
	@REM 	echo %%i | findstr "\[.*\].*" > nul && call:to_mp3 mp3 %%i
	@REM )
	call:to_mp3 mp3 src
	@REM ..\%TOOLS_PATH%\group_merge.exe -i mp3 -o ..\voice
	..\%TOOLS_PATH%\ci-tool-kit ID3-editor --input-dir mp3
	..\%TOOLS_PATH%\ci-tool-kit merge user-file -i mp3 -o mp3
	move mp3\mp3.bin .\voice.bin
) else if %fmt% equ 3 (
	if not exist "flac" (
		mkdir "flac"
	)
	for /d %%i in (*) do (
		echo %%i | findstr "\[.*\].*" > nul && call:to_flac flac %%i
	)
	..\%TOOLS_PATH%\group_merge.exe -i flac -o ..\voice
) else if %fmt% equ 2 (
	if not exist "pcm" (
		mkdir "pcm"
	)
	for /d %%i in (*) do (
		echo %%i | findstr "\[.*\].*" > nul && call:to_pcm pcm %%i
	)
	..\%TOOLS_PATH%\group_merge.exe -i pcm -o ..\voice
) else if %fmt% equ 5 (
	if not exist "opus" (
		mkdir "opus"
	)
	@REM for /d %%i in (*) do (
	@REM 	echo %%i | findstr "\[.*\].*" > nul && call:to_opus opus %%i
	@REM )
	@REM ..\%TOOLS_PATH%\group_merge.exe -i opus -o ..\voice
	..\%TOOLS_PATH%\ci-tool-kit merge user-file -i opus -o opus
	move opus\opus.bin .\voice.bin
) else (

	echo error: 不支持的音频格式

)

cd ..

@echo on

exit /b


:to_pcm
echo %1\%2
if not exist %1\%2 (
	mkdir %1\%2
)
for %%j in (%2\*.wav) do (
	echo %%j
	..\%TOOLS_PATH%\wav_to_adpcm -f wav -i "%%j" -o "%1\%%j"
)
goto:eof

:to_adpcm
echo %1\%2
if not exist %1\%2 (
	mkdir %1\%2
)
for %%j in (%2\*.wav) do (
	echo %%j
	..\%TOOLS_PATH%\wav_to_adpcm -i "%%j" -o "%1\%%j"
)
goto:eof


:to_mp3
@REM echo %1\%2
@REM if not exist %1\%2 (
@REM 	mkdir %1\%2
@REM )
for %%j in (%2\*) do (
	set ttt=%%j
	set output_name=%1\!ttt:~4,-4!.mp3
	echo !output_name!
    set num=!ttt:~5,5!
    if !num! == 65535 (
        echo !num!
        set cfg_in=%%j
        set cfg_out=%1\!ttt:~4,-4!.txt
    )
	@REM ..\!TOOLS_PATH!\wav_to_mp3_flac.exe -f mp3 -i "!ttt!" -o "!output_name!"
	@REM ..\!TOOLS_PATH!\lame --silent --cbr -b24 -t --resample 16000 --out-file "!output_name!" "!ttt!"  
	..\!TOOLS_PATH!\lame --silent --cbr -b16 -t --resample 16000 "!ttt!" "!output_name!"
	@REM ..\!TOOLS_PATH!\ci-tool-kit ID3-editor -i !output_name!
)
if exist !cfg_in! ( 
    copy "!cfg_in!" "!cfg_out!"
)
goto:eof


:to_flac
echo %1\%2
if not exist %1\%2 (
	mkdir %1\%2
)
for %%j in (%2\*.wav) do (
	set ttt=%%j
	set output_name=%1\!ttt:~0,-4!.flac
	echo !output_name!
	..\!TOOLS_PATH!\wav_to_mp3_flac.exe -f flac -r 8000 -i "!ttt!" -o "!output_name!"
)
goto:eof

这段批处理脚本的核心逻辑是:将WAV源文件转换为MP3格式,并进行ID3标签编辑和合并处理

具体流程如下:

主要逻辑流程

1. 选择音频格式

text
set fmt=4  // 固定选择MP3格式(注释显示用户可选择1-5,但代码固定为4)

2. MP3处理分支 (if %fmt% equ 4)

text
创建mp3目录
调用:to_mp3子程序处理音频
执行ID3标签编辑
合并为二进制文件
重命名为voice.bin

3. :to_mp3子程序详细逻辑

A. 文件遍历与转换

batch
for %%j in (%2\*) do (  // 遍历源文件
    set ttt=%%j
    set output_name=%1\!ttt:~4,-4!.mp3  // 生成输出文件名(截取第4字符后)
    
    // 使用lame编码器转换
    ..\!TOOLS_PATH!\lame --silent --cbr -b16 -t --resample 16000 "!ttt!" "!output_name!"
)

转换参数说明:

  • --silent:静默模式

  • --cbr -b16:16kbps恒定比特率

  • --resample 16000:重采样到16kHz

  • -t:删除非必要信息

B. 特殊文件处理

batch
set num=!ttt:~5,5!  // 提取文件名第5-9字符
if !num! == 65535 (  // 如果数字为65535
    set cfg_in=%%j    // 标记为配置文件
    set cfg_out=%1\!ttt:~4,-4!.txt  // 生成对应的txt输出路径
)

C. 配置文件复制

batch
if exist !cfg_in! ( 
    copy "!cfg_in!" "!cfg_out!"  // 复制配置文件
)

4. 后续处理步骤

A. ID3标签编辑

batch
..\%TOOLS_PATH%\ci-tool-kit ID3-editor --input-dir mp3
  • 为所有MP3文件添加/编辑ID3标签

B. 文件合并

batch
..\%TOOLS_PATH%\ci-tool-kit merge user-file -i mp3 -o mp3
  • 将MP3文件合并为单个二进制文件

C. 重命名输出

batch
move mp3\mp3.bin .\voice.bin
  • 将生成的mp3.bin重命名为voice.bin

关键特点

  1. 固定格式选择:虽然代码显示可选择多种格式,但实际固定为MP3(set fmt=4

  2. 低比特率转换:16kbps CBR,适合嵌入式系统

  3. 采样率降低:重采样到16kHz,减少文件大小

  4. 特殊文件处理:识别并处理编号为65535的配置文件

  5. ID3标签支持:为音频文件添加元数据

 

posted @ 2026-01-30 11:01  FBshark  阅读(3)  评论(0)    收藏  举报