1

mp3 ファイルのヘッダーからデータを抽出しようとする簡単なコードを書いています。

目的は、ヘッダーからビットレートやその他の重要な情報などの情報を抽出して、必要な引数を使用してファイルを mp3decoder に適切にストリーミングできるようにすることです。

これは mp3header 情報を示すウィキペディアの画像です: http://upload.wikimedia.org/wikipedia/commons/0/01/Mp3filestructure.svg

私の質問は、これを正しく攻撃していますか? 受信したデータを印刷しても意味がありません。ランダムな文字が大量に表示されるだけです。バイナリを解読して重要な情報を特定できるように、バイナリを取得する必要があります。

これが私のベースラインコードです:

// mp3 Header File IO.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "stdio.h"
#include "string.h"
#include "stdlib.h"

// Main function
int main (void)
{
    // Declare variables
    FILE *mp3file;
    char *mp3syncword; // we will need to allocate memory to this!!
    char requestedFile[255] = "";
    unsigned long fileLength;

    // Counters
    int i;

    // Memory allocation with malloc
    mp3syncword=(char *)malloc(2000);

    // Let's get the name of the requested file (hard-coded for now)
    strcpy(requestedFile,"testmp3.mp3");

    // Open the file with mode read, binary
    mp3file = fopen(requestedFile, "rb"); 
    if (!mp3file){
         // If we can't find the file, notify the user of the problem
         printf("Not found!");
    }

    // Let's get some header data from the file
    fseek(mp3file,1,SEEK_SET);
    fread(mp3syncword,32,1,mp3file);

    // For debug purposes, lets print the received data
     for(i = 0; i < 32; ++i)
        printf("%c", ((char *)mp3syncword)[i]);
    enter code here
    return 0;
}

助けていただければ幸いです。

4

3 に答える 3

1

%cフォーマット指定子として使用してバイトを出力しています。バイト値を表示するには、符号なしの数値書式指定子 ( %u10 進数または%x16進数など)を使用する必要があります。%X

unsigned charWindows ではデフォルトで署名されているため、バイト配列も宣言する必要があります。

出力をより明確にするために、各バイト値の後にスペース (またはその他のセパレーター) を出力することもできます。

標準printfでは、バイナリ表現型指定子は提供されていません。一部の実装にはこれがありますが、Visual Studio で提供されるバージョンにはありません。これを出力するには、数値に対してビット操作を実行して個々のビットを抽出し、各バイトを順番に出力する必要があります。例えば:

unsigned char byte = // Read from file
unsigned char mask = 1; // Bit mask
unsigned char bits[8];

// Extract the bits
for (int i = 0; i < 8; i++) {
    // Mask each bit in the byte and store it
    bits[i] = (byte & (mask << i)) >> i;
}

// The bits array now contains eight 1 or 0 values
// bits[0] contains the least significant bit
// bits[7] contains the most significant bit
于 2009-11-05T10:03:24.180 に答える
1

printf()C には、バイナリで出力する指定子がありません。ほとんどの人は代わりに 16 進数で出力します。これにより、(通常) 一度に 8 ビットが得られます。

printf("the first eight bits are %02x\n", (unsigned char) mp3syncword[0]);

個々のビットの値を把握するには、これを手動で解釈する必要があります。引数のキャストunsigned charは、それが否定的である場合に驚きを避けるためです。

&ビットをテストするには、演算子をビットごとの左シフト演算子と一緒に使用できます<<

if(mp3syncword[2] & (1 << 2))
{
  /* The third bit from the right of the third byte was set. */
}

ビットに「大きな」(7 よりも大きい) インデックスを使用できるようにしたい場合、つまりデータを 32 ビット ワードとして扱いたい場合は、たとえば an に読み込みunsigned int、それを検査するとよいでしょう。ただし、この読み取りを行うときは、エンディアンに注意してください。

于 2009-11-05T10:40:15.420 に答える