如何找到.wav文件的数据(样本)的大小



我必须规范化.wav音频文件。从文件(ChunkID, ChunkSize, Format, fmt等)中成功获取元数据(前44字节),因此我可以找出有多少通道(NumChannels)或BitPerSaple等。

现在我必须将所有样本的数据复制到动态分配的数组,但我不知道如何获得文件的大小(用于malloc()函数)。

下面是代码(如果有帮助的话):
#include <stdio.h>
#include <stdlib.h>
#define hdr_SIZE 44
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()
{
    char nameIn[255], nameOut[255];
    printf("Enter the names of input and output files including file extension:n");
    scanf ("%s", nameIn);
    //printf("%sn", nameIn);
    scanf ("%s", nameOut);
    //printf("%sn", nameOut);

    FILE *input = fopen( nameIn, "rb");
    FILE *output = fopen( nameOut, "wb");
    header hdr;
    if(input == NULL)
    {
        printf("Unable to open wave file (input)n");
        exit(EXIT_FAILURE);
    }
    fread(&hdr, sizeof(char), hdr_SIZE, input);
    /* Displaying file's metadata (chunks). */
    printf("n*********************************n");
    printf("WAVE file's metadata:nn");
    printf("%4.4sn",  hdr.ChunkID );
    printf("%dn",     hdr.ChunkSize );
    printf("%4.4sn",  hdr.Format );
    printf("%4.4sn",  hdr.S1.SubChunk1ID );
    printf("%dn",     hdr.S1.SubChunk1Size );
    printf("%dn",     hdr.S1.AudioFormat );
    printf("%dn",     hdr.S1.NumChannels );
    printf("%dn",     hdr.S1.SampleRate );
    printf("%dn",     hdr.S1.ByteRate );
    printf("%dn",     hdr.S1.BlockAlign );
    printf("%dn",     hdr.S1.BitsPerSample );
    printf("%4.4sn",  hdr.S2.Subchunk2ID );
    printf("%dn",     hdr.S2.Subchunk2Size );
    printf("n*********************************n");

    /* Dead end... =( */

    fclose(input);
    fclose(output);
    return 0;
}

操作系统Windows 7;代码:IDE:块。


更新(解决方案):事实证明,我已经有了样本的大小值(Subchunk2Size)。所以在我的例子中,我只需要使用hdr.S2.Subchunk2Sizemalloc()函数

stat -less实现查找文件大小,

long get_file_size( char *filename )
{
  FILE *fp;
  long n;
  /* Open file */
  fp = fopen(filename, "rb");
  /* Ignoring error checks */
  /* Find end of file */
  fseek(fp, 0L, SEEK_END);
  /* Get current position */
  n = ftell(fp);
  /* Close file */
  fclose(fp);
  /* Return the file size*/
  return n;
}

"如何获取文件大小":use stat(2):

#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
long get_file_size(int fd)
{
  struct stat st;
  if (fstat(fd, &st) == -1)
    return -1; /* error.. */
  return (long) st.st_size;
}
int main() 
{
  /* ... */
  long data_size;
  data_size = get_file_size(fileno(input)) - hdr_SIZE;
  /* ... */
}

相关内容

  • 没有找到相关文章

最新更新