Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

Read mp3 tags id3_v2 C no output

$
0
0
Hello everyone,

Well I just finished my code based on some guidance that I found on internet. Unfortunately the output is not the one that I was expecting. Initially I had some warnings about the pointers that I am using, I thought I fixed them by using (char*) as suggests Ubuntu man page.

Maybe this is the problem but because I am a beginner in programming, I can not find the problem. Any one can help me? I have attached the code and the file that I am trying to read.

Is there any way to go step by step the code through any program and see mistakes?
I tried to upload the file but it gives me a continuous error.

Thanks advance for your time and for your help.

Best Regards,
Thanos
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <unistd.h>

typedef struct{
   unsigned char header_id[3]; /* We indicate that this mp3 file is ID3 format 3 bytes */
   uint8_t major_version; /* We set the size and type of unsigned integer of length 8 bits to the Major Version, unsigned int minimum value is 0 */
   uint8_t revision_number; /* We set the size and type of unsigned integer of length 8 bits to the Revision Number, unsigned int minimum value is 0 */
   unsigned char flags; /* We indicate two flag bytes, we use unsigned characters so they can take values 0 - 255 and not -128 up to 127*/
   uint32_t size; /* We the size is indicated by 4 bytes = 32 bits */
}mp3_Header;

typedef struct{
	uint32_t extended_size;
	unsigned char number_flags;
	unsigned char extended_flags;	
}mp3_Extended_Header;

typedef struct{
	unsigned char frame_id[4]; 
	uint32_t frame_size;
	unsigned char frame_flags[2];	
}mp3_Frame;

 int main (int argc, char *argv[])
   {
	   char a[] = "silence.mp3";
	   if ( argc != 1 )  /* Input from user argc should be 1 for correct
						  *	execution. The argument count variable
						  * stores the number of arguments plus one. */ 
		{
			 /*We print argv[0], if the program that user has chosen for
			  * execution it is not correct as input if has more inputs*/ 
			printf( "Please choose one file: %s  <silence.mp3> \n", argv[0] );
		}
		else 
		{
			mp3_Header first;
			mp3_Extended_Header second;
			mp3_Frame third;
			
			unsigned char header_id[4], memory[4], new_flagg[2], characters;
			int i,b=1;
			unsigned long length_of_data = 0;
						
			FILE *file; /* A file pointer is a variable of type FILE, 
						 * we declare a file pointer.*/
						 
			file = fopen("silence.mp3", "rb");  /* Open the file
												 * silence.mp3 in
												 * "reading" mode "r". */
												 
			if (file == NULL) {
					/* If the file could not open for a variety of reasons
					*  the program should inform us by print the document
					*  and exit.*/ 
					printf( "I couldn't open: %s for reading.\n", a );
					exit(0);
				}
			
			else 
			{
				
				/*Set the pointer at the possition at the beggining of the file, 
				 * if the procedure is correct it will return 0 if not it will return -1. */
				if (fseek(file, 0 , SEEK_SET) == -1)
					{
						fprintf(stderr, "Not able to fseek");
						return EXIT_FAILURE;
					}

				/* The  function  fread() will read 4 bytes of data, each of them in size
				 * of 1 byte long and they will be stored in memory (givven name) . */
				if ( (size_t) fread( memory , (size_t) 4 , (size_t) 1 , file) !=1)
				
					{
						printf("Could not read the file\n");
						exit (0);
					}
				
				/* I will copy the string from location (memory) to the destination tag_id from first typedef stuct */
				strncpy( (char *) first.header_id , (char *) memory , (size_t) 4);
								
				if ( (size_t) fread( &first.major_version , (size_t) 1 , (size_t) 1 , file) != 1) 

					{
						printf("Could not read the major_version\n");
						exit (0);
					}
				
				if ( (size_t) fread( &first.revision_number , (size_t) 1, (size_t) 1 , file) != 1) 

					{
						printf("Could not read the revision_number\n");
						exit (0);
					}
				
				/* Read the total size of the header */				
				if ( (size_t) fread(memory , (size_t) sizeof(first.size) , (size_t) 1 , file) != 1) 

					{
						printf("Could not read memory\n");
						exit (0);
					}
				
					/* We break down the packet in bytes and allocate the bytes in memory in a sequence. 
					* After the first sequence is stored for the rest we have to shift them by one possition.
					* Because the first bit determines the sign on the character/ intiger and we have set it to possitive. 
					* We shift the possition every 7 bits because 1 byte has 8 bits - 1 the sign ( + , - ). */
					first.size = (memory[3] & 0xFF) |
								((memory[2] & 0xFF) << 7 ) |
								((memory[1] & 0xFF) << 14 ) |
								((memory[0] & 0xFF) << 21 );
				
					/* Store temporarilly the length of the data */
					length_of_data = first.size;
				
					/* Print the data that we have collect so far */
					fprintf( stdout , "%s version %d . %d\t Size %d\n", first.header_id , first. major_version , first.revision_number , first.size);
				
				/* At this point we want to make sure that the whole header has been executed correctly */
				if (( first.flags & (01000000) ) == 01000000 )
					
					{	
						/* Extended Header 6 Bytes */
					if ( (size_t) fread( memory , (size_t) sizeof(second.extended_size) , 1 , file ) != 1);
						
						{
							printf("Could not read Memory\n");
							exit (0);
						}
						
						/* Break down the packet in pieces and extrac the data */
						second.extended_size = (memory[3] & 0xFF) |
											  ((memory[2] & 0xFF) << 7 ) |
											  ((memory[1] & 0xFF) << 14 ) |
											  ((memory[0] & 0xFF) << 21 );
					if ( (size_t) fread( &second.number_flags , 1 , 1 , file ) !=1);
					
						{
							printf("Could not read Number of Flags\n");
							exit (0);
						}
						
					if ( (size_t) fread( &second.extended_flags , 1 , 1 , file ) !=1);
					
						{
							printf("Could not read Extended Flags\n");
							exit (0);
						}
						
						fprintf(stdout,"Extended header size: %d\n", second.extended_size);
						/* From the stored value we substract the Extended Header */
						length_of_data = length_of_data - second.extended_size;
						/* Reposition the seek pointer after the Extended Header */
						fseek ( file , second.extended_size + 10 , SEEK_SET );
					}
					
				else
					{
						/* If process has been executed correclty proceed and place reader after Extended Header (10 bytes) */
						fseek ( file , 10 , SEEK_SET );
					}
						
				while(b!=0) /* Read until the end of file */
						{
							if ( (size_t) fread( header_id , (size_t) 4 , 1 , file ) !=1);
							
							{
								printf("Could not read Header_id_1 \n");
								exit (0);
							}
							
							strncpy( (char *) third.frame_id , (char *) header_id , (size_t) 4 );
							
							if ( (size_t) fread( memory , (size_t) sizeof(third.frame_size) , 1 , file ) !=1);
							
							{
								printf("Could not read Memory\n");
								exit (0);
							}
							
							/* Extract data and store them */
							third.frame_size = (memory[3] & 0xFF) |
											   ((memory[2] & 0xFF) << 7 ) |
											   ((memory[1] & 0xFF) << 14 ) |
											   ((memory[0] & 0xFF) << 21 );
							if ( (size_t) fread( new_flagg , (size_t) 2 , 1 , file ) !=1);
							
							{
								printf("Could not read Memory\n");
								exit (0);
							}
							
							strncpy( (char *) third.frame_flags , (char *) new_flagg , (size_t) 2 );
							/* Print inforamtion of Frames */
							printf("Third Part Frame id : %s Frame Size : %d Flags : ", third.frame_id , third.frame_size );	  
							
							/* Compare the strings */
							if ( strncmp( (char *) third.frame_id , "TPE1" , (size_t) 4 ) == 0 )  
								{
									for ( i = 1; i <= third.frame_size; i++ )
										{
											characters = fgetc(file);
											printf( "Number of characters: %c" , characters );
										}
								printf("\n");
								}
								
							else if ( strncmp( (char *) third.frame_id , "TALB" , (size_t) 4 ) == 0 )
								{
									for ( i=1 ; i <= third.frame_size; i++ )
										{
											characters = fgetc(file);
											printf( "Number of characters: %c" , characters );
										}
								printf("\n");
								}
								
							else if ( strncmp ( (char *) third.frame_id , "TYER" , (size_t) 4 ) == 0 )
								{
									for ( i = 1; i <= third.frame_size; i++ )
										{
											characters = fgetc(file);
											printf( "Number of characters: %c" , characters );
										}
								printf("\n");
								}
								
							else if ( strncmp ( (char *) third.frame_id , "TCON" , (size_t) 4 ) == 0 )
								{
									for ( i = 1; i <= third.frame_size; i++ )
										{
											characters = fgetc(file);
											printf( "Number of characters: %c" , characters );
										}
								printf("\n");
								}
							
							else if ( strncmp ( (char *) third.frame_id , "TRCK" , (size_t) 4 ) == 0 )
								{
									for ( i = 1; i <= third.frame_size; i++ )
										{
											characters = fgetc(file);
											printf( "Number of characters: %c" , characters );
										}
								printf("\n");
								}
								
							else if ( strncmp ( (char *) third.frame_id , "TIT2" , (size_t) 4 ) == 0 )
								{
									for ( i = 1; i <= third.frame_size; i++ )
										{
											characters = fgetc(file);
											printf( "Number of characters: %c" , characters );
										}
								printf("\n");
								}
							
							else
								{
									printf("\n"); b=0;
								}
						}
					
					printf("Finished Reading, Clossing silence.mp3 file! Goodbye!\n");

			fclose(file);
			return 0;

			}
		}
	}


Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>