如何将麦克风输入作为stdin提供给C程序



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

我目前输入的方式是

.my_code_binary < input.raw > output.raw

正如您所看到的,我将input.raw作为stdin,将output.raw用作stdout来执行我的程序。

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

现在我想让我的程序实时运行,就像in一样,把我的麦克风输入作为stdin,把麦克风输出作为stdout。任何资源或代码片段都会帮助我,我是C.音频处理的初学者

编辑:我正在使用树莓派4

为了避免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

最新更新