fs.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. #include "include.h"
  2. #if FUNC_MUSIC_EN
  3. extern u8 fname_buf[100];
  4. //#define MUSIC_EXT_NUM 6 //一共搜索3个扩展名
  5. //const char tbl_music_ext[MUSIC_EXT_NUM][4] = {"mp3", "wav", "wma", "ape", "fla", "sbc"};
  6. //转换为小写字符
  7. AT(.text.fs.scan)
  8. unsigned char char_tolower(unsigned char c)
  9. {
  10. if (c >= 'A' && c <= 'Z') {
  11. c -= 'A'-'a';
  12. }
  13. return c;
  14. }
  15. //将字符串转换为小写
  16. AT(.text.fs.scan)
  17. void str_tolower(char *str, uint len)
  18. {
  19. uint i;
  20. for (i = 0; i < len; i++) {
  21. str[i] = char_tolower(str[i]);
  22. }
  23. }
  24. AT(.text.fs.scan)
  25. static bool file_is_music(char *ext_name)
  26. {
  27. // u8 i;
  28. // for (i=0; i != MUSIC_EXT_NUM; i++) {
  29. // if (0 == memcmp(ext_name, (char *)(tbl_music_ext[i]), 3)) {
  30. // return true;
  31. // }
  32. // }
  33. // return false;
  34. if (0 == memcmp(ext_name, "mp3", 3)) {
  35. return true;
  36. }
  37. #if MUSIC_WAV_SUPPORT
  38. if (0 == memcmp(ext_name, "wav", 3)) {
  39. return true;
  40. }
  41. #endif // MUSIC_WAV_SUPPORT
  42. #if MUSIC_WMA_SUPPORT
  43. if (0 == memcmp(ext_name, "wma", 3)) {
  44. return true;
  45. }
  46. #endif // MUSIC_WMA_SUPPORT
  47. #if MUSIC_APE_SUPPORT
  48. if (0 == memcmp(ext_name, "ape", 3)) {
  49. return true;
  50. }
  51. #endif // MUSIC_APE_SUPPORT
  52. #if MUSIC_FLAC_SUPPORT
  53. if (0 == memcmp(ext_name, "fla", 3)) {
  54. return true;
  55. }
  56. #endif // MUSIC_FLAC_SUPPORT
  57. #if MUSIC_M4A_SUPPORT
  58. if (0 == memcmp(ext_name, "m4a", 3)) {
  59. return true;
  60. }
  61. #endif // MUSIC_M4A_SUPPORT
  62. #if MUSIC_SBC_SUPPORT
  63. if (0 == memcmp(ext_name, "sbc", 3)) {
  64. return true;
  65. }
  66. #endif // MUSIC_SBC_SUPPORT
  67. #if MUSIC_AAC_SUPPORT
  68. if (0 == memcmp(ext_name, "aac", 3)) {
  69. return true;
  70. }
  71. #endif // MUSIC_AAC_SUPPORT
  72. return false;
  73. }
  74. ///返回值: 0->不匹配的文件类型, 1->匹配的文件类型
  75. AT(.text.fs.scan)
  76. u8 music_file_filter(void)
  77. {
  78. char extension[3];
  79. fs_get_fname_extension(extension);
  80. str_tolower(extension, 3);
  81. if (file_is_music(extension)) {
  82. #if REC_FAST_PLAY
  83. bsp_update_final_rec_file_num();
  84. #endif // REC_FAST_PLAY
  85. return 1;
  86. }
  87. return 0;
  88. }
  89. ///返回值: 0->不匹配的目录类型, 1->匹配的目录类型
  90. AT(.text.fs.scan)
  91. u8 music_dir_filter(void)
  92. {
  93. #if REC_FAST_PLAY
  94. char sfn[13];
  95. fs_get_short_fname(sfn, 1);
  96. //录音目录
  97. if (is_record_dir(sfn)) {
  98. if (f_msc.rec_scan & BIT(0)) {
  99. f_msc.rec_scan |= BIT(1);
  100. }
  101. } else {
  102. f_msc.rec_scan &= ~BIT(1);
  103. }
  104. #endif // REC_FAST_PLAY
  105. return 1;
  106. }
  107. #if MUSIC_REC_FILE_FILTER
  108. ///只播放录音文件歌曲
  109. u8 music_only_record_dir_filter(void)
  110. {
  111. char sfn[13];
  112. fs_get_short_fname(sfn, 1);
  113. //录音目录
  114. if (is_record_dir(sfn)) {
  115. #if REC_FAST_PLAY
  116. if (f_msc.rec_scan & BIT(0)) {
  117. f_msc.rec_scan |= BIT(1);
  118. }
  119. #endif // REC_FAST_PLAY
  120. return 1;
  121. }
  122. return 0;
  123. }
  124. ///只播放录音文件歌曲
  125. u8 music_only_record_file_filter(void)
  126. {
  127. char extension[3];
  128. fs_get_fname_extension(extension);
  129. str_tolower(extension, 3);
  130. if (file_is_music(extension) && fs_get_dir_depth()) { //去掉根目录正常歌曲
  131. #if REC_FAST_PLAY
  132. bsp_update_final_rec_file_num();
  133. #endif // REC_FAST_PLAY
  134. return 1;
  135. }
  136. return 0;
  137. }
  138. ///去掉录音文件
  139. u8 music_rm_record_dir_filter(void)
  140. {
  141. char sfn[13];
  142. fs_get_short_fname(sfn, 1);
  143. //录音目录
  144. if (is_record_dir(sfn)) {
  145. return 0;
  146. }
  147. return 1;
  148. }
  149. #endif // MUSIC_REC_FILE_FILTER
  150. #if REC_FAST_PLAY
  151. void bsp_update_final_rec_file_num(void)
  152. {
  153. if (fs_get_dir_depth() && (f_msc.rec_scan & BIT(1))) {
  154. if (fs_get_ftime() > sys_cb.ftime) {
  155. sys_cb.ftime = fs_get_ftime();
  156. sys_cb.rec_num = fs_get_file_count();
  157. }
  158. }
  159. }
  160. #endif // REC_FAST_PLAY
  161. AT(.text.stream)
  162. int stream_read(void *buf, unsigned int size)
  163. {
  164. UINT len;
  165. u8 res = fs_read(buf, size, &len);
  166. if (res == FR_OK) {
  167. #if MUSIC_ENCRYPT_EN
  168. if (f_msc.encrypt) {
  169. music_stream_decrypt(buf, len);
  170. }
  171. #endif
  172. return len;
  173. } else {
  174. return -1;
  175. }
  176. }
  177. AT(.text.stream)
  178. bool stream_seek(unsigned int ofs, int whence)
  179. {
  180. #if MUSIC_ENCRYPT_EN
  181. if (f_msc.encrypt) {
  182. if (whence == SEEK_SET) {
  183. ofs += 1;
  184. }
  185. }
  186. #endif
  187. u8 res = fs_lseek(ofs, whence);
  188. if (res == FR_OK) {
  189. return true;
  190. }
  191. return false;
  192. }
  193. #endif // FUNC_MUSIC_EN