1

入力ファイルは、in.wav. チャンクを読み取って (成功)、サンプルを読み取ってオーディオ ファイルを正規化する必要があります...

問題は、.wav ファイルのサンプルの値maxと値を調べようとしてクラッシュすることです。min配列内の最小値と最大値を見つけるだけですが、クラッシュします...何が問題なのか教えてください。

コードは次のとおりです。

#include <stdio.h>
#include <stdlib.h>
#include "main.h"
#define hdr_SIZE 64

typedef struct FMT
{
    char        SubChunk1ID[4];
    int         SubChunk1Size;
    short int   AudioFormat;
    short int   NumChannels;
    int         SampleRate;
    int         ByteRate;
    short int   BlockAlign;
    short int   BitsPerSample;

} fmt;

typedef struct DATA
{
    char        Subchunk2ID[4];
    int         Subchunk2Size;
    int         Data[441000]; 
} data;

typedef struct HEADER
{
    char        ChunkID[4];
    int         ChunkSize;
    char        Format[4];
    fmt         S1;
    data        S2;
} header;



int main()
{
    FILE *input = fopen( "in.wav", "rb");   /// nameIn

    if(input == NULL)
    {
        printf("Unable to open wave file (input)\n");
        exit(EXIT_FAILURE);
    }

    FILE *output = fopen( "out.wav", "wb"); /// nameOut
    header hdr;


    fread(&hdr, sizeof(char), hdr_SIZE, input); 
    /* NOTE: Chunks has been copied successfully. */


    /*###############################*/                                  
    /*##### UPDATE (char *ptr;) #####*/
    /*###############################*/
    char *ptr;  // 'int' was written here instead of 'char'. That's was a stupid mistake...
    long n = hdr.S2.Subchunk2Size;


    /// COPYING SAMPLES...
    ptr = malloc(sizeof(hdr.S2.Subchunk2Size));
    while ( n-- != 0 )
    {
        fread(&ptr, 1, 1, input);  // Continues reading after the least 'stop' place. 
    }                              // I was being told here (on "stack") that it is so...


    n = hdr.S2.Subchunk2Size; // Resetting 'n'.
    int min = ptr[0], max = ptr[0], i;

    /* THE PROBLEM IS HERE: */
    for ( i = 0; i < n; i++ )
        {
            if ( ptr[i] < min )     // If the next elements is less than previous, swap them.
                min = ptr[i];
            if ( ptr[i] > max ) // If the next elements is bigger than previous, swap them.
                max = ptr[i];
    }

    printf("> > >%d__%d\n", min, max);    // Displaying of 'min' and 'max'.

    fclose(input);
    fclose(output);

    return 0;
}

アップデート:

ユーレカ!これはすべて、サンプルあたり 8 ビットのためです。char 型と同様に (サンプルを使用して) それらを操作する必要があります。(コード内の「### UPDATE ###」コメントを参照してください)

4

3 に答える 3

1

あなたはあなたのmalloc()edptrを次のように破損しています:

fread(&ptr, 1, 1, input); /* overwrite the content of ptr */

そのため、使用しようとするとプログラムがクラッシュしますptr。使用する:

fread(ptr, 1, 1, input);

またはより良い: while ループを使用せず、次を使用します。

  fread(ptr, 1, n, inout);
于 2013-05-09T18:48:13.483 に答える
1

このコード:

    /// COPYING SAMPLES...
    ptr = malloc(sizeof(hdr.S2.Subchunk2Size));
    while ( n-- != 0 )
    {
        fread(&ptr, 1, 1, input);  // Continues reading after the least 'stop' place. 
    }                              // I was being told here (on "stack") that it is so...

ptr変数 ntimesの最初のバイトを上書きします。これはptrの値を壊します。代わりに割り当てられたバッファに読み込むように修正したとしても (を削除して&)、割り当てられたメモリの最初のバイトのみを書き換えます。

おそらく意図したのは次のとおりです。

    fread(ptr, 1, n, input);

whileループは必要ないことに注意してください。しかし、それはあなたの本音に関する私の推測です。

于 2013-05-09T18:46:58.133 に答える