Final code
Dependencies: C12832 PinDetect USBHost mbed wave_player
Fork of MusicPlayer_ThaoLeMinh by
ReadTime.h@3:82b68ab94e3e, 2016-12-13 (annotated)
- Committer:
- LyCosine
- Date:
- Tue Dec 13 10:00:51 2016 +0000
- Revision:
- 3:82b68ab94e3e
Yan revised
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
LyCosine | 3:82b68ab94e3e | 1 | #include<stdio.h> |
LyCosine | 3:82b68ab94e3e | 2 | #include<string.h> |
LyCosine | 3:82b68ab94e3e | 3 | #include<stdlib.h> |
LyCosine | 3:82b68ab94e3e | 4 | |
LyCosine | 3:82b68ab94e3e | 5 | char ToHex(int num) { |
LyCosine | 3:82b68ab94e3e | 6 | if (num < 0 && num > 15) { |
LyCosine | 3:82b68ab94e3e | 7 | return '!'; |
LyCosine | 3:82b68ab94e3e | 8 | } |
LyCosine | 3:82b68ab94e3e | 9 | else if (num < 10) { |
LyCosine | 3:82b68ab94e3e | 10 | return num + 48; |
LyCosine | 3:82b68ab94e3e | 11 | } |
LyCosine | 3:82b68ab94e3e | 12 | else { |
LyCosine | 3:82b68ab94e3e | 13 | switch (num) { |
LyCosine | 3:82b68ab94e3e | 14 | case 10: return 'A'; |
LyCosine | 3:82b68ab94e3e | 15 | case 11: return 'B'; |
LyCosine | 3:82b68ab94e3e | 16 | case 12: return 'C'; |
LyCosine | 3:82b68ab94e3e | 17 | case 13: return 'D'; |
LyCosine | 3:82b68ab94e3e | 18 | case 14: return 'E'; |
LyCosine | 3:82b68ab94e3e | 19 | case 15: return 'F'; |
LyCosine | 3:82b68ab94e3e | 20 | } |
LyCosine | 3:82b68ab94e3e | 21 | } |
LyCosine | 3:82b68ab94e3e | 22 | return '?'; |
LyCosine | 3:82b68ab94e3e | 23 | } |
LyCosine | 3:82b68ab94e3e | 24 | |
LyCosine | 3:82b68ab94e3e | 25 | int ToDec(char *str) { |
LyCosine | 3:82b68ab94e3e | 26 | int num = 0; |
LyCosine | 3:82b68ab94e3e | 27 | bool flag = true; |
LyCosine | 3:82b68ab94e3e | 28 | int len = strlen(str); |
LyCosine | 3:82b68ab94e3e | 29 | for (int i = 0; i < len; i++) { |
LyCosine | 3:82b68ab94e3e | 30 | if (flag && str[i] == '0') { |
LyCosine | 3:82b68ab94e3e | 31 | continue; |
LyCosine | 3:82b68ab94e3e | 32 | } |
LyCosine | 3:82b68ab94e3e | 33 | flag = false; |
LyCosine | 3:82b68ab94e3e | 34 | if (str[i] > '9') { |
LyCosine | 3:82b68ab94e3e | 35 | num = 16 * num + (str[i] - 55); |
LyCosine | 3:82b68ab94e3e | 36 | } |
LyCosine | 3:82b68ab94e3e | 37 | else { |
LyCosine | 3:82b68ab94e3e | 38 | num = 16 * num + (str[i] - '0'); |
LyCosine | 3:82b68ab94e3e | 39 | } |
LyCosine | 3:82b68ab94e3e | 40 | } |
LyCosine | 3:82b68ab94e3e | 41 | return num; |
LyCosine | 3:82b68ab94e3e | 42 | } |
LyCosine | 3:82b68ab94e3e | 43 | |
LyCosine | 3:82b68ab94e3e | 44 | double ReadTime(const char *path) { |
LyCosine | 3:82b68ab94e3e | 45 | char size[9]; |
LyCosine | 3:82b68ab94e3e | 46 | char byteRate[9]; |
LyCosine | 3:82b68ab94e3e | 47 | unsigned char ch[100]; |
LyCosine | 3:82b68ab94e3e | 48 | FILE *file; |
LyCosine | 3:82b68ab94e3e | 49 | |
LyCosine | 3:82b68ab94e3e | 50 | file = fopen(path,"rb"); |
LyCosine | 3:82b68ab94e3e | 51 | |
LyCosine | 3:82b68ab94e3e | 52 | for (int i = 0; i < 58; i++) { |
LyCosine | 3:82b68ab94e3e | 53 | ch[i] = fgetc(file); |
LyCosine | 3:82b68ab94e3e | 54 | } |
LyCosine | 3:82b68ab94e3e | 55 | |
LyCosine | 3:82b68ab94e3e | 56 | for (int i = 7, j = 0; i >= 4; i--, j += 2) { |
LyCosine | 3:82b68ab94e3e | 57 | if (ch[i] < 16) { |
LyCosine | 3:82b68ab94e3e | 58 | size[j] = '0'; |
LyCosine | 3:82b68ab94e3e | 59 | size[j + 1] = ToHex(ch[i]); |
LyCosine | 3:82b68ab94e3e | 60 | } |
LyCosine | 3:82b68ab94e3e | 61 | else { |
LyCosine | 3:82b68ab94e3e | 62 | size[j] = ToHex(ch[i] / 16); |
LyCosine | 3:82b68ab94e3e | 63 | size[j + 1] = ToHex(ch[i] % 16); |
LyCosine | 3:82b68ab94e3e | 64 | } |
LyCosine | 3:82b68ab94e3e | 65 | } |
LyCosine | 3:82b68ab94e3e | 66 | size[8] = '\0'; |
LyCosine | 3:82b68ab94e3e | 67 | |
LyCosine | 3:82b68ab94e3e | 68 | for (int i = 31, j = 0; i > 27; i--, j += 2) { |
LyCosine | 3:82b68ab94e3e | 69 | if (ch[i] < 16) { |
LyCosine | 3:82b68ab94e3e | 70 | byteRate[j] = '0'; |
LyCosine | 3:82b68ab94e3e | 71 | byteRate[j + 1] = ToHex(ch[i]); |
LyCosine | 3:82b68ab94e3e | 72 | } |
LyCosine | 3:82b68ab94e3e | 73 | else { |
LyCosine | 3:82b68ab94e3e | 74 | byteRate[j] = ToHex(ch[i] / 16); |
LyCosine | 3:82b68ab94e3e | 75 | byteRate[j + 1] = ToHex(ch[i] % 16); |
LyCosine | 3:82b68ab94e3e | 76 | } |
LyCosine | 3:82b68ab94e3e | 77 | } |
LyCosine | 3:82b68ab94e3e | 78 | byteRate[8] = '\0'; |
LyCosine | 3:82b68ab94e3e | 79 | |
LyCosine | 3:82b68ab94e3e | 80 | int n_size = ToDec(size); |
LyCosine | 3:82b68ab94e3e | 81 | int n_byteRate = ToDec(byteRate); |
LyCosine | 3:82b68ab94e3e | 82 | double time = (double)n_size / (double)n_byteRate; |
LyCosine | 3:82b68ab94e3e | 83 | |
LyCosine | 3:82b68ab94e3e | 84 | return time; |
LyCosine | 3:82b68ab94e3e | 85 | } |