_       _       
  (_) ___ | |_ ____
  | |/ _ \| __|_  /
  | | (_) | |_ / / 
 _/ |\___/ \__/___|
|__/               

Piping audio into a C program with pacat

//
// soundz.c - Written by Ted Burke - last updated 22-July-2022
//
// Part of output of "pactl list sources short":
//   2 alsa_input.usb-Blue_Microphones_Yeti_Stereo_Microphone_797_2019_03_25_87907-00.analog-stereo
//     module-alsa-card.c   s16le 2ch 44100Hz   SUSPENDED
//
// gcc -o soundz soundz.c
// ./soundz.c
//
// octave
// x = csvread ("temp.csv");
// plot(x)
//
//

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

int main()
{
    const int N = 128; 
    int i, m, n;
    int16_t buf[N];
    char bar[11];
    sprintf(bar, "..........");

    printf("Soundz\n");

    FILE *pin, *fout;

    fout = fopen("temp.csv", "w");
    pin = popen("parec", "r");
    if (!pin)
    {
        fprintf(stderr, "Error opening input pipe\n");
        return 1;
    }

    for (int n=0 ; n<1000 ; ++n)
    {
        fread(buf, 2, N, pin);
        int sum = 0;
        for (m=0 ; m<N ; ++m)
        {
            sum += abs(buf[m]);
            fprintf(fout, "%d\n", buf[m]);
        }
        sum /= (N*3277);
        for (i=0 ; i<10 ; ++i) bar[i] = sum > i ? '*' : '_';
        fprintf(stderr, "\n%s", bar);
        fflush(stderr);
    }

    pclose(pin);
    fclose(fout);

    return 0;
}