Record audio to an output file.Supports specifying device and backend to use.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <errno.h>
#include <unistd.h>
struct RecordContext {
struct SoundIoRingBuffer *ring_buffer;
};
SoundIoFormatFloat32NE,
SoundIoFormatFloat32FE,
SoundIoFormatS32NE,
SoundIoFormatS32FE,
SoundIoFormatS24NE,
SoundIoFormatS24FE,
SoundIoFormatS16NE,
SoundIoFormatS16FE,
SoundIoFormatFloat64NE,
SoundIoFormatFloat64FE,
SoundIoFormatU32NE,
SoundIoFormatU32FE,
SoundIoFormatU24NE,
SoundIoFormatU24FE,
SoundIoFormatU16NE,
SoundIoFormatU16FE,
};
static int prioritized_sample_rates[] = {
48000,
44100,
96000,
24000,
0,
};
static int min_int(int a, int b) {
return (a < b) ? a : b;
}
static void read_callback(
struct SoundIoInStream *instream,
int frame_count_min,
int frame_count_max) {
struct RecordContext *rc = instream->
userdata;
int err;
if (free_count < frame_count_min) {
fprintf(stderr, "ring buffer overflow\n");
exit(1);
}
int write_frames = min_int(free_count, frame_count_max);
int frames_left = write_frames;
for (;;) {
int frame_count = frames_left;
exit(1);
}
if (!frame_count)
break;
if (!areas) {
} else {
for (int frame = 0; frame < frame_count; frame += 1) {
}
}
}
exit(1);
}
frames_left -= frame_count;
if (frames_left <= 0)
break;
}
}
static int count = 0;
fprintf(stderr, "overflow %d\n", ++count);
}
static int usage(char *exe) {
fprintf(stderr, "Usage: %s [options] outfile.wav\n"
"Options:\n"
" [--backend dummy|alsa|pulseaudio|jack|coreaudio|wasapi]\n"
" [--device id]\n"
" [--raw]\n"
, exe);
return 1;
}
int main(int argc, char **argv) {
char *exe = argv[0];
char *device_id = NULL;
bool is_raw = false;
char *outfile = NULL;
for (int i = 1; i < argc; i += 1) {
char *arg = argv[i];
if (arg[0] == '-' && arg[1] == '-') {
if (strcmp(arg, "--raw") == 0) {
is_raw = true;
} else if (++i >= argc) {
return usage(exe);
} else if (strcmp(arg, "--backend") == 0) {
if (strcmp("dummy", argv[i]) == 0) {
} else if (strcmp("alsa", argv[i]) == 0) {
} else if (strcmp("pulseaudio", argv[i]) == 0) {
} else if (strcmp("jack", argv[i]) == 0) {
} else if (strcmp("coreaudio", argv[i]) == 0) {
} else if (strcmp("wasapi", argv[i]) == 0) {
} else {
fprintf(stderr, "Invalid backend: %s\n", argv[i]);
return 1;
}
} else if (strcmp(arg, "--device") == 0) {
device_id = argv[i];
} else {
return usage(exe);
}
} else if (!outfile) {
outfile = argv[i];
} else {
return usage(exe);
}
}
if (!outfile)
return usage(exe);
struct RecordContext rc;
if (!soundio) {
fprintf(stderr, "out of memory\n");
return 1;
}
if (err) {
return 1;
}
if (device_id) {
if (device->
is_raw == is_raw && strcmp(device->
id, device_id) == 0) {
selected_device = device;
break;
}
}
if (!selected_device) {
fprintf(stderr, "Invalid device id: %s\n", device_id);
return 1;
}
} else {
}
fprintf(stderr,
"Device: %s\n", selected_device->
name);
int sample_rate = 0;
int *sample_rate_ptr;
for (sample_rate_ptr = prioritized_sample_rates; *sample_rate_ptr; sample_rate_ptr += 1) {
sample_rate = *sample_rate_ptr;
break;
}
}
if (!sample_rate)
fmt = *fmt_ptr;
break;
}
}
FILE *out_f = fopen(outfile, "wb");
if (!out_f) {
fprintf(stderr, "unable to open %s: %s\n", outfile, strerror(errno));
return 1;
}
if (!instream) {
fprintf(stderr, "out of memory\n");
return 1;
}
return 1;
}
fprintf(stderr, "%s %dHz %s interleaved\n",
const int ring_buffer_duration_seconds = 30;
if (!rc.ring_buffer) {
fprintf(stderr, "out of memory\n");
return 1;
}
return 1;
}
for (;;) {
sleep(1);
size_t amt = fwrite(read_buf, 1, fill_bytes, out_f);
if ((int)amt != fill_bytes) {
fprintf(stderr, "write error: %s\n", strerror(errno));
return 1;
}
}
return 0;
}