实现互联网广播:尝试在c中打开'.au'文件

时间:2014-11-09 17:24:25

标签: c multithreading audio file-extension

我正在尝试实施一个网络电台'这需要服务器继续连续播放音频文件,并且每当客户端请求到达时,服务器创建一个将音频文件传递给用户的线程。服务器在启动时会生成一个线程,该线程继续从音频文件写入全局缓冲区空间,并且用户对应的缓冲区继续从全局缓冲区空间读取并继续以“块”发送音频文件。 ;给用户。并且用户尝试通过' / dev / audio'的帮助播放音频文件。 (我正在使用ubuntu)。

问题在于,当我尝试打开一个' .au'文件使用'打开'在服务器上运行,分配文件描述符0。所以,我无法从音频文件中提取内容并将其发送到客户端(而不是我在终端上输入的内容被发送到客户端)。

是否有某种开放式的方式' .au' '打开'的文件功能在C ??如果没有,那么哪些文件扩展名会打开'功能支持和我该怎么做,即我应该使用什么类型的文件扩展名来打开'打开'功能打开文件并发送给用户?

1 个答案:

答案 0 :(得分:1)

确保您使用二进制的开放文件模式 - IE。 given_mode =“rb”其中r表示读取,b表示二进制 - 下面的代码读取二进制文件并输出前几个字节 - 享受

//  gcc -o read_binary_au_file  read_binary_au_file.c

#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>

int main() {

    const char * given_mode = "rb";
    const char * given_filename = "/home/scott/Documents/data/audio/sample.au";

    FILE * fh;
    fh = fopen(given_filename, given_mode);

    if (fh == NULL) {
        fprintf(stderr, "ERROR - failed to open file %s in mode %s\n",
                given_filename, given_mode);
        return (-2);
    }

    // --- identify file size --- //

    off_t file_size;
    struct stat stat_struct; // data structure to be returned from call to fstat on file descriptor

    int fd = fileno(fh); // get file descriptor from file handle

    if ((fstat(fd, & stat_struct) != 0) || (! S_ISREG(stat_struct.st_mode))) {

        fprintf(stderr, "ERROR - failed to stat file");
        return (-3);
    }


    file_size = stat_struct.st_size;

    printf("file_size %lu\n", file_size);

    // --- now read data from the binary file --- //

    unsigned long MAX_READ_CHARS = 4096;

    unsigned long desired_num_bytes_to_read = (file_size > MAX_READ_CHARS) ? MAX_READ_CHARS : file_size;

    char * mem_buffer = malloc(sizeof(char) * desired_num_bytes_to_read);

    unsigned long num_bytes_actually_io = 0;

    // read initial bytes into memory buffer

    num_bytes_actually_io = fread(mem_buffer, sizeof(char), desired_num_bytes_to_read, fh);

    if (fclose(fh)) {

        fprintf(stderr, "ERROR - failed to close file\n");
        return (-4);
    }

    if (num_bytes_actually_io != desired_num_bytes_to_read) {

        fprintf(stderr, "ERROR - failed to read desired_num_bytes_to_read %lu ... instead num_bytes_actually_io %lu\n",
                    desired_num_bytes_to_read, num_bytes_actually_io);

        return (-1);
    }

    printf("num_bytes_actually_io %lu\n", num_bytes_actually_io);

    // ... now do something with mem_buffer which contains data read from binary file

    int num_bytes_to_show = 100;

    num_bytes_to_show = (num_bytes_actually_io > num_bytes_to_show) ? num_bytes_to_show : num_bytes_actually_io;

    printf("\nhere are the first %d characters from given binary file\n", num_bytes_to_show);

    printf("-->");
    int i = 0;
    for (; i < num_bytes_to_show; i++) {

        // printf("%c", mem_buffer[i]); // as chars
        printf("%x", mem_buffer[i]);  // as hex
    }
    printf("<--");

    printf("\n");

    free(mem_buffer);

    printf("\nprocessing complete\n");

    return 0;
}