1+ using Microsoft . AspNetCore . Mvc ;
2+ using Sitreamai ;
3+
4+ namespace MaiChartManager . Controllers . Tools ;
5+
6+ [ ApiController ]
7+ [ Route ( "MaiChartManagerServlet/[action]Api" ) ]
8+ public class AudioConvertToolController : ControllerBase
9+ {
10+ [ HttpPost ]
11+ public IActionResult AudioConvertTool ( )
12+ {
13+ if ( AppMain . BrowserWin is null ) return BadRequest ( "浏览器窗口未初始化" ) ;
14+
15+ var dialog = new OpenFileDialog ( )
16+ {
17+ Title = "请选择要转换的音频文件" ,
18+ Filter = "音频文件|*.wav;*.mp3;*.aac;*.ogg;*.flac;*.m4a;*.wma;*.ape;*.acb;*.awb;*.mp4" ,
19+ } ;
20+
21+ if ( AppMain . BrowserWin . Invoke ( ( ) => dialog . ShowDialog ( AppMain . BrowserWin ) ) != DialogResult . OK )
22+ return BadRequest ( "未选择文件" ) ;
23+
24+ var inputFile = dialog . FileName ;
25+ var extension = Path . GetExtension ( inputFile ) . ToLowerInvariant ( ) ;
26+ var directory = Path . GetDirectoryName ( inputFile ) ;
27+ var fileNameWithoutExt = Path . GetFileNameWithoutExtension ( inputFile ) ;
28+
29+ try
30+ {
31+ // 检查是否是 ACB 或 AWB 文件 - 转换为 MP3
32+ if ( extension == ".acb" || extension == ".awb" )
33+ {
34+ return ConvertAcbAwbToMp3 ( inputFile , directory , fileNameWithoutExt , extension ) ;
35+ }
36+
37+ // 其他格式转换为 ACB/AWB
38+ return ConvertToAcbAwb ( inputFile , directory , fileNameWithoutExt , extension ) ;
39+ }
40+ catch ( Exception ex )
41+ {
42+ return BadRequest ( $ "转换失败: { ex . Message } ") ;
43+ }
44+ }
45+
46+ /// <summary>
47+ /// 将 ACB/AWB 转换为 MP3
48+ /// </summary>
49+ private IActionResult ConvertAcbAwbToMp3 ( string inputFile , string directory , string fileNameWithoutExt , string extension )
50+ {
51+ string acbPath ;
52+ string awbPath ;
53+
54+ // 根据输入文件类型确定 ACB 和 AWB 路径
55+ if ( extension == ".acb" )
56+ {
57+ acbPath = inputFile ;
58+ awbPath = Path . Combine ( directory , fileNameWithoutExt + ".awb" ) ;
59+ }
60+ else // .awb
61+ {
62+ awbPath = inputFile ;
63+ acbPath = Path . Combine ( directory , fileNameWithoutExt + ".acb" ) ;
64+ }
65+
66+ // 检查配对文件是否存在
67+ if ( ! System . IO . File . Exists ( acbPath ) )
68+ {
69+ return BadRequest ( $ "找不到配对的 ACB 文件: { acbPath } ") ;
70+ }
71+
72+ if ( ! System . IO . File . Exists ( awbPath ) )
73+ {
74+ return BadRequest ( $ "找不到配对的 AWB 文件: { awbPath } ") ;
75+ }
76+
77+ // 转换 ACB 到 WAV
78+ byte [ ] wavData = Audio . AcbToWav ( acbPath ) ;
79+
80+ // 生成 MP3 输出路径
81+ string mp3Path = Path . Combine ( directory , fileNameWithoutExt + ".mp3" ) ;
82+
83+ // 将 WAV 数据转换为 MP3
84+ Audio . ConvertWavBytesToMp3 ( wavData , mp3Path ) ;
85+
86+ return Ok ( new { message = "转换完成!" , outputPath = mp3Path } ) ;
87+ }
88+
89+ /// <summary>
90+ /// 将音频文件转换为 ACB/AWB
91+ /// </summary>
92+ private IActionResult ConvertToAcbAwb ( string inputFile , string directory , string fileNameWithoutExt , string extension )
93+ {
94+ string tempAudioFile = null ;
95+
96+ try
97+ {
98+ string actualInputFile = inputFile ;
99+
100+ // 如果是 MP4 文件,先提取音轨
101+ if ( extension == ".mp4" )
102+ {
103+ tempAudioFile = Path . Combine ( Path . GetTempPath ( ) , Guid . NewGuid ( ) . ToString ( ) + ".wav" ) ;
104+ Audio . ExtractAudioFromMp4 ( inputFile , tempAudioFile ) ;
105+ actualInputFile = tempAudioFile ;
106+ }
107+
108+ // 生成输出路径
109+ string acbPath = Path . Combine ( directory , fileNameWithoutExt + ".acb" ) ;
110+ string awbPath = Path . Combine ( directory , fileNameWithoutExt + ".awb" ) ;
111+
112+ // 执行转换
113+ Audio . ConvertToMai ( actualInputFile , acbPath ) ;
114+
115+ return Ok ( new { message = "转换完成!" , acbPath = acbPath , awbPath = awbPath } ) ;
116+ }
117+ finally
118+ {
119+ // 删除临时音频文件
120+ if ( tempAudioFile != null && System . IO . File . Exists ( tempAudioFile ) )
121+ {
122+ try
123+ {
124+ System . IO . File . Delete ( tempAudioFile ) ;
125+ }
126+ catch
127+ {
128+ // 忽略删除错误
129+ }
130+ }
131+ }
132+ }
133+ }
0 commit comments