Click here to Skip to main content
15,879,535 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
This chunk of code is supposed to display the layout of an MP3 frame header, but what is diplayed is wrong!
The layout of an mp3 header is detailled in
and here are my code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>

typedef struct{
                long Sync;
                int id;
                int layer;
                int Protection_bit;
                int bitrate;
                int frequency;
                int padding_byte;
                int private_bit;
                int mode;
                int mode_extension;
                int copyright_bit;
                int home;
                int emphasis;
            } mp3_header;

mp3_header* lire(char *nom);
    char nom[100];
    mp3_header* mp3;
    printf("Veuillez entrer le nom et le chemin du file sans l'extension\n");
    mp3 = lire(nom);
    printf(" le Sync est: %d \n ",&mp3->Sync);
    printf(" le id est: %d \n",&mp3->id);
    printf(" le layer est: %d \n",&mp3->layer);
    printf(" le Protection_bit est: %d \n",&mp3->Protection_bit);
    printf(" le bitrate est: %d \n",&mp3->bitrate);
    printf(" le frequecy est: %d \n",&mp3->frequency);
    printf(" le padding_byte est: %d \n",&mp3->padding_byte);
    printf(" le private_bit est: %d \n",&mp3->private_bit);
    printf(" le mode: %d \n",&mp3->mode);
    printf(" le mode_extension est: %d \n",&mp3->mode_extension);
    printf(" le copyright_bit est: %d \n",&mp3->copyright_bit);
    printf(" le home est: %d \n",&mp3->home);
    printf(" le emphasis est: %d \n",&mp3->emphasis);
mp3_header* lire (char *nom)
    mp3_header *mp3;
    FILE *fichier;
    fichier= fopen(nom, "r");
        if (fichier== NULL)
            printf("Erreur lecture \n");
    //fread(&mp3,sizeof( mp3_header),1,fichier);
    fread (&mp3->Sync,sizeof(long),1,fichier);
    fread (&mp3->id,sizeof(int),1,fichier);
    fread (&mp3->layer,sizeof(int),1,fichier);
    fread (&mp3->Protection_bit,sizeof(int),1,fichier);
    fread (&mp3->bitrate,sizeof(int),1,fichier);
    fread (&mp3->frequency,sizeof(int),1,fichier);
    fread (&mp3->padding_byte,sizeof(int),1,fichier);
    fread (&mp3->private_bit,sizeof(int),1,fichier);
    fread (&mp3->mode,sizeof(int),1,fichier);
    fread (&mp3->mode_extension,sizeof(int),1,fichier);
    fread (&mp3->copyright_bit,sizeof(int),1,fichier);
    fread (&mp3->home,sizeof(int),1,fichier);
    fread (&mp3->emphasis,sizeof(int),1,fichier);
    return mp3;

So please, can anyone help me to make it work??

How is it wrong ? Have you checked the mp3 spec against this code to see if it is right ?
Share this answer
i've opened the mp3 file with an hexadecimal editor to check the different field and compared them with the mp3 header layout , moreover on the mp3 header layout (link), the 12 first bits of the first field shoud be set. However with my code,there are no concordance with what i saw while openning the file with the editor so i've concluded that it is wrong.
Share this answer
Richard MacCutchan 25-Aug-10 7:00am    
This is the same issue as your question in the C++ forum about OGG files, and the same suggestions should help you.
The documentation that you give us (at[^]), say that the frame header is 32 bit wide and the fields are arranged as bit fields.

In your code you are reading each field as if it is 4 bytes, then 13 * 4 = 52 bytes = 416 bit...

Try defining your header this way:

typedef struct {
   unsigned emphasis : 2;
   unsigned home : 1;
   unsigned copyright_bit : 1;
   unsigned mode_extension : 2;
   unsigned mode : 2;
   unsigned private_bit : 1;
   unsigned padding_byte : 1;
   unsigned frequency : 2;
   unsigned bitrate : 4;
   unsigned Protection_bit : 1;
   unsigned layer : 2;
   unsigned id : 2;
   unsigned Sync : 11;
} mp3_header;

And then read it in a single shot:

fread(&mp3, sizeof(mp3_header), 1, fichier);
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900