1+ package com .xwintop .xJavaFxTool .services .littleTools ;
2+
3+ import com .xwintop .xJavaFxTool .controller .littleTools .Mp3ConvertToolController ;
4+ import com .xwintop .xcore .util .FileUtil ;
5+ import lombok .Getter ;
6+ import lombok .Setter ;
7+ import lombok .extern .slf4j .Slf4j ;
8+ import org .apache .commons .io .FileUtils ;
9+ import org .apache .commons .lang3 .StringUtils ;
10+
11+ import java .io .File ;
12+ import java .util .HashMap ;
13+ import java .util .Map ;
14+
15+ /**
16+ * @ClassName: Mp3ConvertToolService
17+ * @Description: mp3格式转换工具
18+ * @author: xufeng
19+ * @date: 2019/8/8 0008 20:41
20+ */
21+
22+ @ Getter
23+ @ Setter
24+ @ Slf4j
25+ public class Mp3ConvertToolService {
26+ private Mp3ConvertToolController mp3ConvertToolController ;
27+
28+ public Mp3ConvertToolService (Mp3ConvertToolController mp3ConvertToolController ) {
29+ this .mp3ConvertToolController = mp3ConvertToolController ;
30+ }
31+
32+ public void addTableData (File file ) {
33+ Map <String , String > rowValue = new HashMap <>();
34+ rowValue .put ("fileName" , file .getName ());
35+ rowValue .put ("absolutePath" , file .getAbsolutePath ());
36+ rowValue .put ("fileSize" , FileUtil .formetFileSize (file .length ()));
37+ rowValue .put ("convertStatus" , "待转换" );
38+ mp3ConvertToolController .getTableData ().add (rowValue );
39+ }
40+
41+ public void convertAction () {
42+ for (Map <String , String > tableDatum : mp3ConvertToolController .getTableData ()) {
43+ String absolutePath = tableDatum .get ("absolutePath" );
44+ if (StringUtils .endsWithIgnoreCase (absolutePath , ".qmcflac" )) {
45+ if (convertQmc (absolutePath )) {
46+ tableDatum .put ("convertStatus" , "转换成功" );
47+ } else {
48+ tableDatum .put ("convertStatus" , "转换失败" );
49+ }
50+ }
51+ }
52+ mp3ConvertToolController .getTableViewMain ().refresh ();
53+ }
54+
55+ private boolean convertQmc (String absolutePath ) {
56+ try {
57+ byte [] buffer = FileUtils .readFileToByteArray (new File (absolutePath ));
58+ QmcDecode dc = new QmcDecode ();
59+ for (int i = 0 ; i < buffer .length ; ++i ) {
60+ buffer [i ] = (byte ) (dc .NextMask () ^ buffer [i ]);
61+ }
62+ String file = StringUtils .removeEndIgnoreCase (absolutePath , ".qmcflac" );
63+ if (StringUtils .isEmpty (mp3ConvertToolController .getOutputFolderTextField ().getText ())) {
64+ FileUtils .writeByteArrayToFile (new File (file + ".mp3" ), buffer );
65+ } else {
66+ FileUtils .writeByteArrayToFile (new File (mp3ConvertToolController .getOutputFolderTextField ().getText (), new File (file + ".mp3" ).getName ()), buffer );
67+ }
68+ return true ;
69+ } catch (Exception e ) {
70+ log .error ("转换异常" , e );
71+ return false ;
72+ }
73+ }
74+ }
75+
76+ class QmcDecode {
77+ private int x = -1 ;
78+ private int y = 8 ;
79+ private int dx = 1 ;
80+ private int index = -1 ;
81+ private int [][] seedMap = {
82+ {0x4a , 0xd6 , 0xca , 0x90 , 0x67 , 0xf7 , 0x52 },
83+ {0x5e , 0x95 , 0x23 , 0x9f , 0x13 , 0x11 , 0x7e },
84+ {0x47 , 0x74 , 0x3d , 0x90 , 0xaa , 0x3f , 0x51 },
85+ {0xc6 , 0x09 , 0xd5 , 0x9f , 0xfa , 0x66 , 0xf9 },
86+ {0xf3 , 0xd6 , 0xa1 , 0x90 , 0xa0 , 0xf7 , 0xf0 },
87+ {0x1d , 0x95 , 0xde , 0x9f , 0x84 , 0x11 , 0xf4 },
88+ {0x0e , 0x74 , 0xbb , 0x90 , 0xbc , 0x3f , 0x92 },
89+ {0x00 , 0x09 , 0x5b , 0x9f , 0x62 , 0x66 , 0xa1 }
90+ };
91+
92+ public int NextMask () {
93+ int ret ;
94+ index ++;
95+ if (x < 0 ) {
96+ dx = 1 ;
97+ y = ((8 - y ) % 8 );
98+ ret = ((8 - y ) % 8 );
99+ ret = 0xc3 ;
100+ } else if (x > 6 ) {
101+ dx = -1 ;
102+ y = 7 - y ;
103+ ret = 0xd8 ;
104+ } else {
105+ ret = seedMap [y ][x ];
106+ }
107+
108+ x += dx ;
109+ if (index == 0x8000 || (index > 0x8000 && (index + 1 ) % 0x8000 == 0 )) {
110+ return NextMask ();
111+ }
112+ return ret ;
113+ }
114+ }
0 commit comments