如何显示MP3帧头的布局 [英] how to display the MP3 frame header layout

查看:56
本文介绍了如何显示MP3帧头的布局的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

该代码段应该显示MP3帧头的布局,但显示的内容是错误的!
mp3标头的布局在
中有详细介绍 http://www.mp3-tech.org/programmer/frame_header.html
这是我的代码:

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
http://www.mp3-tech.org/programmer/frame_header.html
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);
 main(void)
{
    char nom[100];
    mp3_header* mp3;
    printf("Veuillez entrer le nom et le chemin du file sans l''extension\n");
    gets(nom);
    strcat(nom,".mp3");
    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;
    mp3=(mp3_header*)malloc(sizeof(mp3_header));
    fichier= fopen(nom, "r");
        if (fichier== NULL)
        {
            printf("Erreur lecture \n");
            exit(1);
        }
    //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);
    fclose(fichier);
    return mp3;
}



所以,请任何人帮我使它正常工作吗?



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

推荐答案

怎么了?您是否已针对此代码检查了mp3规范,看是否正确?
How is it wrong ? Have you checked the mp3 spec against this code to see if it is right ?


我已使用十六进制编辑器打开了mp3文件,以检查其他字段并将其与mp3进行了比较标题布局,此外,在mp3标题布局(链接)上,应设置第一个字段的前12位.但是,对于我的代码,与使用编辑器打开文件时看到的内容不一致,因此我得出结论认为这是错误的.
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.


您提供给我们的文档(在 http://www.mp3-tech.org/programmer/frame_header.html [ ^ ]),说帧头是 32位宽,并且这些字段按 bit字段排列.

在您的代码中,您正在读取每个字段,就好像它是4个字节,然后13 * 4 = 52个字节= 416位...

尝试通过以下方式定义标题:

The documentation that you give us (at http://www.mp3-tech.org/programmer/frame_header.html[^]), 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);


这篇关于如何显示MP3帧头的布局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆