MP3 ID3V1(C)
22 Feb 2012背景音乐:空のように 海のように
最近刚淘到的一首歌,很喜欢,希望你也一样。
(现在是晚上11:30,窗外在下小雨,这意境,神怡。)
由于最近需要用到C,复习一下,发现当初晦涩的C语言如今变得是那么灵秀,像个姑娘。
复习一下指针,我用MP3的结构来动手,没办法,喜音乐。
运行:
MP3 ID3V1结构如下:
02
03 Dim ID3v1TAG As String ‘TAG三个字母,ID3V1的标识
04
05 Dim Title As String ‘存储标题信息,30个字节
06
07 Dim Artist As String ‘存储歌手信息,30个字节
08
09 Dim Album As String ‘存储专辑信息,30个字节
10
11 Dim Year As String ‘存储年代信息,4个字节
12
13 Dim Comments As String ‘存储备注信息,28个字节(有时为30字节)
14
15 Dim Genre As String ‘存储音乐风格信息,保留位,1个字节
16
17 Dim Reserved As String ‘保留位,1个字节(有时没有)
18
19 Dim Track As String ‘音轨(曲号)保留位,1个字节(有时没有)
20
21 End Structure
fread
功 能: 从一个流中读数据
函数原型: size_t fread(void*buffer,size_tsize,size_tcount,FILE*stream);
参 数:
1.用于接收数据的地址(指针)(buffer)
2.单个元素的大小(size) :单位是字节而不是位,例如读取一个int型数据就是4个字节
3.元素个数(count)
4.提供数据的文件指针(stream)
返回值:读取的元素的个数
memcmp
原型:int memcmp(const void *buf1, const void *buf2, unsigned int count);
用法:#include
功能:比较内存区域buf1和buf2的前count个字节。
说明:
当buf1<buf2时,返回值<0
当buf1=buf2时,返回值=0
当buf1>buf2时,返回值>0
源代码:
02 #include
03 #include
04 char *genre[148]=
05 {
06 “Blues”,“Classic Rock”,“Country”,“Dance”,“Disco”,“Funk”,“Grunge”,“Hip-Hop”,
07 “Jazz”,“Metal”,“New Age”,“Oldies”,“Other”,“Pop”,“R&B”,“Rap”,“Reggae”,“Rock”,
08 “Techno”,“Industrial”,“Alternative”,“Ska”,“Death Metal”,“Pranks”,“Soundtrack”,
09 “Euro-Techno”,“Ambient”,“Trip Hop”,“Vocal”,“Jazz Funk”,“Fusion”,“Trance”,
10 “Classical”,“Instrumental”,“Acid”,“House”,“Game”,“Sound Clip”,“Gospel”,“Noise”,
11 “Alternative Rock”,“Bass”,“Soul”,“Punk”,“Space”,“Meditative”,“Instrumental Pop”,
12 “Instrumental Rock”,“Ethnic”,“Gothic”,“Darkwave”,“Techno-Industrial”,“Electronic”,
13 “Pop Folk”,“Eurodance”,“Dream”,“Southern Rock”,“Comedy”,“Cult”,“Gangsta”,“Top 40″,
14 “Christian Rap”,“Pop Funk”,“Jungle”,“Native American”,“Cabaret”,“New Wave”,
15 “Psychadelic”,“Rave”,“ShowTunes”,“Trailer”,“Lo-Fi”,“Tribal”,“Acid Punk”,“Acid Jazz”,
16 “Polka”,“Retro”,“Musical”,“Rock & Roll”,“Hard Rock”,“Folk”,“Folk Rock”,
17 “National Folk”,“Swing”,“Fast Fusion”,“Bebob”,“Latin”,“Revival”,“Celtic”,
18 “Bluegrass”,“Avantgarde”,“Gothic Rock”,“Progressive Rock”,“Psychedelic Rock”,
19 “Symphonic Rock”,“Slow Rock”,“Big Band”,“Chorus”,“Easy Listening”,“Acoustic”,
20 “Humour”,“Speech”,“Chanson”,“Opera”,“Chamber Music”,“Sonata”,“Symphony”,“Booty Bass”,
21 “Primus”,“Porn Groove”,“Satire”,“Slow Jam”,“Club”,“Tango”,“Samba”,“Folklore”,“Ballad”,
22 “Power Ballad”,“Rhytmic Soul”,“Freestyle”,“Duet”,“Punk Rock”,“Drum Solo”,“A Capella”,
23 “Euro House”,“Dance Hall”,“Goa”,“Drum & Bass”,“Club House”,“Hardcore”,“Terror”,
24 “Indie”,“BritPop”,“Negerpunk”,“Polsk Punk”,“Beat”,“Christian Gangsta Rap”,
25 “Heavy Metal”,“Black Metal”,“Crossover”,“Contemporary Christian”,“Christian Rock”,
26 “Merengue”,“Salsa”,“Trash Metal”,“Anime”,“JPop”,“SynthPop”
27 };
28 void ID3V1(char path[])
29 {
30 FILE *fl;
31 fl=fopen(path,“r”);
32 if(!fl)
33 {
34 printf(“Read failed.\n“);
35 exit();
36 }
37 fseek(fl,-128,2);
38 char *str;
39 char z[3];
40 fread(&z,1,3,fl);
41 if(memcmp(&z,“TAG”,3))
42 {
43 printf(“Read faild.\n“);
44 exit();
45 }
46 else
47 {
48 printf(“\n“);
49 str=(char *)malloc(30*sizeof(char));
50 fread(str,1,30,fl);
51 printf(“TITLE: %s\n“,str);
52 fread(str,1,30,fl);
53 printf(“ARTIST: %s\n“,str);
54 fread(str,1,30,fl);
55 printf(“ALBUM: %s\n“,str);
56 fread(str,1,4,fl);
57 if(str[]!=)
58 {
59 printf(“YEAR: %c%c%c%c\n“,str[],str[1],str[2],str[3]);
60 }
61 else
62 {
63 printf(“YEAR:\n“);
64 }
65 fread(str,1,30,fl);
66 //printf(“COMMENTS: %s\n”,str);
67 if((str[28]==)&&str[29]!=)
68 {
69 printf(“TRACK: %d\n“,str[29]);
70 }
71 else
72 {
73 printf(“TRACK\n“);
74 }
75 printf(“COMMENTS: %s\n“,str);
76 fread(str,1,1,fl);
77 int g=(int)*str;
78 printf(“GENRE:%s\n“,genre[g]);
79 printf(“\n“);
80 free(str);
81 }
82 fclose(fl);
83
84 }
85
86 void main()
87 {
88 printf(“/////////////////////////////////////////////////\n“);
89 printf(“ LAZYNIGHT MP3 ID3V1 INFO\n“);
90 printf(“/////////////////////////////////////////////////\n“);
91 printf(“Please input the path and filename of your mp3 files.\n“);
92 char path[100];
93 scanf(“%s”,&path);
94 ID3V1(path);
95 }
</div>
转载请注明:[于哲的博客][1] » [MP3 ID3V1(C)][2]
[1]: http://lazynight.me
[2]: http://lazynight.me/1689.html