简体   繁体   中英

How to give mic input as stdin to C program

I currently have written a code which takes in a raw file as input and does some audio processing and writes it to another different raw file.

The way I am currently inputting is

.\my_code_binary < input.raw > output.raw

as you can see, I am making the input.raw as stdin and output.raw as stdout for the execution my program.

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

Now I want to make my program run real-time, as in, take my mic input as stdin and mic output as stdout. Any resources or code snippets will help me out, I am a beginner at audio processing in C.

EDIT: I am using a Raspberry Pi 4

To avoid the shell redirection you could try something like this:

#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);
}

However, this is not real-time as it requires, that the file myinfile already exists. As Linux handles all devices as files, You could try to use the device file associated with your microphone as myinfile , eg /dev/mymicrophone .

You may also consider using the more basic Low-Level-I/O functions, that use filedescriptors instead of the struct FILE .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM