#1
  1. Cast down
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Location
    Sweden
    Posts
    321
    Rep Power
    11

    Reading mp3 id3, something wrong with genre/year


    Something is wrong with the Genre/Year of this.. Genre is 1 less than it's supposed to be, and If there is a comment, it shows up with the year, ("1999COMMENT HERE"):

    Code:
    #include <stdio.h> 
    #include <stdlib.h> 
    
    typedef struct _id3tag
    {
    	char title[30]; 
    	char artist[30]; 
    	char album[30]; 
    	char year[4]; 
    	char comment[30];
    	unsigned char track; 
    	unsigned char genre; 
    } id3tag; 
    
    int ReadID3(const char* Filename, id3tag *ID3Tag); //0==error, 1==sucess 
    
    int main(int argc, char **argv)
    {
    	id3tag ID3; 
    	if(!ReadID3("C:\\g.mp3",&ID3))
    		printf("error!\n"); 
    	
    	printf("Genre %d\nYear %s\n",ID3.genre,ID3.year); 
    	return 0; 
    }
    
    
    int ReadID3(const char* Filename, id3tag *ID3Tag)
    {
    	FILE *fp=fopen(Filename,"rb"); 
    	char buffer[128]; 
    	int x; 
    
    	fseek(fp,-128,SEEK_END); 
    	fread(buffer,sizeof(char),sizeof(buffer),fp); 
    
    	if(!(buffer[0]=='T' && buffer[1] == 'A' && buffer[2] == 'G'))
    	{
    		return 0; 
    	}
    	
    	//Found id3 tag, let's fill out our id3tag struct 
    	for(x=0;x<30;x++)
    		ID3Tag->title[x] = buffer[x+3]; //take 3 off cause of TAG 
    		ID3Tag->title[30] = '\0'; 
    
    	for(x=0;x<30;x++)
    		ID3Tag->artist[x] = buffer[x+33]; //33 = TAG + title 
    		ID3Tag->artist[30] = '\0'; 
    
    	for(x=0;x<30;x++)
    		ID3Tag->album[x] = buffer[x+63]; //TAG + title + artist 
    		ID3Tag->album[30] = '\0'; 
    
    	for(x=0;x<4;x++)
    		ID3Tag->year[x] = buffer[x+93]; //TAG + title + artist + album 
    		ID3Tag->year[4] = '\0'; 
    		
    	for(x=0;x<30;x++)
    		ID3Tag->comment[x] = buffer[x+97]; //TAG + title + artist + album + year 
    		ID3Tag->comment[30] = '\0'; 
    
    	if(buffer[127] > 0 && buffer[127] < 256)
    		ID3Tag->genre = buffer[127]; //If its between 1-255, put it 
    	else 
    		ID3Tag->genre = 255; //255 means unused 
    
    	//ID3 1.1 compatibility (Track field) 
    	if(ID3Tag->comment[28] == '\0')
    		ID3Tag->track = ID3Tag->comment[29]; //If comment ends at 28.. then 29 is the track 
    	else
    		ID3Tag->track = 0; //0 mean's unknown track 
    	return 1; 
    }
    Last edited by movEAX_444; July 26th, 2003 at 11:00 PM.
  2. #2
  3. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,086
    Rep Power
    2222

    Re: Binary I/O functions in C, to read mp3 id3


    Originally posted by movEAX_444
    How would I read bytes in a file? Like ID3 tags in a mp3.
    This is what I have so far, I want to read the last 128 bytes (the first 3 of those 128 determines if it's an ID3 tag)

    Code:
    	FILE *fp=fopen("C:\\whatever.mp3","rb"); 
    	fseek(fp,128,SEEK_END); 
    	fclose(fp);
    I haven't actually played with it, but wouldn't you want to make that a negative offset from the end? E.g.:
    Code:
    	FILE *fp=fopen("C:\\whatever.mp3","rb"); 
    	fseek(fp,-128,SEEK_END); 
    	fclose(fp);
  4. #3
  5. Cast down
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Location
    Sweden
    Posts
    321
    Rep Power
    11
    SEEK_END read's backwards 128 bytes (or whatever you want it to), -128 doesn't work I get an error. Another way to do it is just to start reading from filesize - 128.
  6. #4
  7. Cast down
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Location
    Sweden
    Posts
    321
    Rep Power
    11
    Ok I think SEEK_END is with a negative, I got it to work with no error. You were right. I am getting closer to getting at least something working.


    haha it wasn't working because the mp3 didn't have an id3 tag.
    Last edited by movEAX_444; July 26th, 2003 at 08:03 PM.

IMN logo majestic logo threadwatch logo seochat tools logo