aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRichard Fitzgerald <rf@opensource.wolfsonmicro.com>2013-08-12 17:30:22 +0100
committerRichard Fitzgerald <rf@opensource.wolfsonmicro.com>2013-08-13 17:21:15 +0100
commit5741cc04a4b20fd0997c8de6808c3a2557291d0d (patch)
tree3ca6ab9c830902d7f32f4f9a6005e46e7b1adb85
parent782bfda5e796cb46d0e7be0dc882ff686d5ad2a2 (diff)
tinycap: support streaming captured PCM to stdout
It is sometimes useful for testing to be able to stream the captured PCM to another tool instead of being forced to save it to a WAV file. Using '--' instead of the target filename will send the raw PCM to stdout (without any header). Signed-off-by: Richard Fitzgerald <rf@opensource.wolfsonmicro.com>
-rw-r--r--tinycap.c43
1 files changed, 30 insertions, 13 deletions
diff --git a/tinycap.c b/tinycap.c
index 7429750..b9ae251 100644
--- a/tinycap.c
+++ b/tinycap.c
@@ -57,6 +57,7 @@ struct wav_header {
};
int capturing = 1;
+int prinfo = 1;
unsigned int capture_sample(FILE *file, unsigned int card, unsigned int device,
unsigned int channels, unsigned int rate,
@@ -81,17 +82,25 @@ int main(int argc, char **argv)
unsigned int period_size = 1024;
unsigned int period_count = 4;
enum pcm_format format;
+ int no_header = 0;
if (argc < 2) {
- fprintf(stderr, "Usage: %s file.wav [-D card] [-d device] [-c channels] "
- "[-r rate] [-b bits] [-p period_size] [-n n_periods]\n", argv[0]);
+ fprintf(stderr, "Usage: %s {file.wav | --} [-D card] [-d device] [-c channels] "
+ "[-r rate] [-b bits] [-p period_size] [-n n_periods]\n\n"
+ "Use -- for filename to send raw PCM to stdout\n", argv[0]);
return 1;
}
- file = fopen(argv[1], "wb");
- if (!file) {
- fprintf(stderr, "Unable to create file '%s'\n", argv[1]);
- return 1;
+ if (strcmp(argv[1],"--") == 0) {
+ file = stdout;
+ prinfo = 0;
+ no_header = 1;
+ } else {
+ file = fopen(argv[1], "wb");
+ if (!file) {
+ fprintf(stderr, "Unable to create file '%s'\n", argv[1]);
+ return 1;
+ }
}
/* parse command line arguments */
@@ -160,20 +169,26 @@ int main(int argc, char **argv)
header.data_id = ID_DATA;
/* leave enough room for header */
- fseek(file, sizeof(struct wav_header), SEEK_SET);
+ if (!no_header) {
+ fseek(file, sizeof(struct wav_header), SEEK_SET);
+ }
/* install signal handler and begin capturing */
signal(SIGINT, sigint_handler);
frames = capture_sample(file, card, device, header.num_channels,
header.sample_rate, format,
period_size, period_count);
- printf("Captured %d frames\n", frames);
+ if (prinfo) {
+ printf("Captured %d frames\n", frames);
+ }
/* write header now all information is known */
- header.data_sz = frames * header.block_align;
- header.riff_sz = header.data_sz + sizeof(header) - 8;
- fseek(file, 0, SEEK_SET);
- fwrite(&header, sizeof(struct wav_header), 1, file);
+ if (!no_header) {
+ header.data_sz = frames * header.block_align;
+ header.riff_sz = header.data_sz + sizeof(header) - 8;
+ fseek(file, 0, SEEK_SET);
+ fwrite(&header, sizeof(struct wav_header), 1, file);
+ }
fclose(file);
@@ -216,8 +231,10 @@ unsigned int capture_sample(FILE *file, unsigned int card, unsigned int device,
return 0;
}
- printf("Capturing sample: %u ch, %u hz, %u bit\n", channels, rate,
+ if (prinfo) {
+ printf("Capturing sample: %u ch, %u hz, %u bit\n", channels, rate,
pcm_format_to_bits(format));
+ }
while (capturing && !pcm_read(pcm, buffer, size)) {
if (fwrite(buffer, 1, size, file) != size) {