0

我目前已经编写了一个代码,它将原始文件作为输入并进行一些音频处理并将其写入另一个不同的原始文件。

我目前输入的方式是

.\my_code_binary < input.raw > output.raw

如您所见,我将 input.raw 作为标准输入,将 output.raw 作为标准输出,以执行我的程序。

fread(tmp, sizeof(short), channels * size_of_frame, stdin); // the way I am using the input.raw
fwrite(tmp, sizeof(short), channels * FRAME_SIZE, stdout); // the way I am using the output.raw

现在我想让我的程序实时运行,例如,将我的麦克风输入作为标准输入,将麦克风输出作为标准输出。任何资源或代码片段都会帮助我,我是 C 音频处理的初学者。

编辑:我使用的是树莓派 4

4

1 回答 1

0

为避免 shell 重定向,您可以尝试以下操作:

#include <stdio.h>
#include <stdlib.h>

int main() {

    FILE *infile, *outfile;
    int c;

    infile = fopen("myinfile", "r");
    outfile = fopen("myoutfile", "w");

    while((c = getc(infile)) != EOF) {
        c = c * 2;  // do something
        putc(c,outfile);
    }

    fclose(infile);
    fclose(outfile);
}

但是,这不是实时的,因为它需要文件myinfile已经存在。由于 Linux 将所有设备都作为文件处理,您可以尝试将与麦克风关联的设备文件myinfile用作/dev/mymicrophone.

您也可以考虑使用更基本的 Low-Level-I/O 函数,这些函数使用文件描述符而不是struct FILE.

于 2021-04-07T07:32:41.363 回答