0

我正在尝试读取 .au 文件的标头(前 24 个字节,分成 6 个 uint32_t)并打印出编码、采样率和通道数。最终,我将播放 .au 文件的其余部分,但现在当我到达 fread 行时,我遇到了段错误(我通过 gdb 进行了检查)。为什么会这样?我的代码如下。谢谢。

#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <arpa/inet.h>
#include <alsa/asoundlib.h>
#include <fcntl.h>

#define AUDIO_FILE_MAGIC (uint32_t)0x02E736E64

#define AUDIO_FILE_ENCODING_MULAW 1
#define AUDIO_FILE_ENCODING_LINEAR_8 2
#define AUDIO_FILE_ENCODING_LINEAR_16 3

#define STEREO 2
#define MONO 1

typedef struct {
    uint32_t magic;
    uint32_t hdr_size;
    uint32_t data_size;
    uint32_t encoding;
    uint32_t sample_rate;
    uint32_t channels;
} au_header;

int main(int argc, char **argv) {   
    // Check arguments
    if(argc != 2) {
        fprintf(stderr, "Usage: %s <filename.au>\n", argv[0]);
        exit(1);
    }
    au_header header;
    char str[32];
    FILE *f = fopen(argv[1], "rb");

    fread(str, 4, 6, f);
    fclose(f);
    uint32_t *intptr = (uint32_t *) str;

    // Remember to change from network order
    header.magic = ntohl(intptr[0]);
    header.hdr_size = ntohl(intptr[1]);
    header.data_size = ntohl(intptr[2]);
    header.encoding = ntohl(intptr[3]);
    header.sample_rate = ntohl(intptr[4]);
    header.channels = ntohl(intptr[5]);

    printf("endcoding: %d\nsample rate: %d\nchannels: %d\n", header.encoding, header.sample_rate, header.channels);
    /* TO DO:

       Implement audio file playback here

    */

}
4

0 回答 0