From: Richard Fitzgerald <rf@opensource.wolfsonmicro.com>
To: vinod.koul@intel.com
Cc: alsa-devel@alsa-project.org, patches@opensource.wolfsonmicro.com
Subject: [TINYCOMPRESS][PATCH] crec: support streaming output to stdout
Date: Thu, 19 Feb 2015 14:55:40 +0000 [thread overview]
Message-ID: <1424357740-6561-1-git-send-email-rf@opensource.wolfsonmicro.com> (raw)
From: Ammar Zahid Ali Syed <aali@opensource.wolfsonmicro.com>
It's useful for testing to be able to stream the captured
audio to stdout instead of having to go via a file.
Signed-off-by: Richard Fitzgerald <rf@opensource.wolfsonmicro.com>
Signed-off-by: Ammar Ali <aali@opensource.wolfsonmicro.com>
---
crec.c | 73 ++++++++++++++++++++++++++++++++++++++++-----------------------
1 files changed, 46 insertions(+), 27 deletions(-)
diff --git a/crec.c b/crec.c
index e3f189f..258fade 100644
--- a/crec.c
+++ b/crec.c
@@ -77,6 +77,8 @@
static int verbose;
static int file;
+static FILE *finfo;
+static bool streamed;
static const unsigned int DEFAULT_CHANNELS = 1;
static const unsigned int DEFAULT_RATE = 44100;
@@ -151,7 +153,7 @@ static void size_wave_header(struct wave_header *header, uint32_t size)
static void usage(void)
{
- fprintf(stderr, "usage: crec [OPTIONS] filename\n"
+ fprintf(stderr, "usage: crec [OPTIONS] [filename]\n"
"-c\tcard number\n"
"-d\tdevice node\n"
"-b\tbuffer size\n"
@@ -162,6 +164,8 @@ static void usage(void)
"-C\tSpecify the number of channels (default %u)\n"
"-R\tSpecify the sample rate (default %u)\n"
"-F\tSpecify the format: S16_LE, S32_LE (default S16_LE)\n\n"
+ "If filename is not given the output is\n"
+ "written to stdout\n\n"
"Example:\n"
"\tcrec -c 1 -d 2 test.wav\n"
"\tcrec -f 5 test.wav\n",
@@ -180,7 +184,7 @@ static int print_time(struct compress *compress)
fprintf(stderr, "ERR: %s\n", compress_get_error(compress));
return -1;
} else {
- printf("DSP recorded %jd.%jd\n",
+ fprintf(finfo, "DSP recorded %jd.%jd\n",
(intmax_t)tstamp.tv_sec, (intmax_t)tstamp.tv_nsec*1000);
}
return 0;
@@ -195,6 +199,10 @@ static int finish_record()
if (!file)
return -ENOENT;
+ /* can't rewind if streaming to stdout */
+ if (streamed)
+ return 0;
+
/* Get amount of data written to file */
ret = lseek(file, 0, SEEK_END);
if (ret < 0)
@@ -256,22 +264,27 @@ void capture_samples(char *name, unsigned int card, unsigned int device,
length = length * rate * (samplebits / 8) * channels;
if (verbose)
- printf("%s: entry, reading %u bytes\n", __func__, length);
-
- file = open(name, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
- if (file == -1) {
- fprintf(stderr, "Unable to open file '%s'\n", name);
- exit(EXIT_FAILURE);
- }
+ fprintf(finfo, "%s: entry, reading %u bytes\n", __func__, length);
+ if (!name) {
+ file = STDOUT_FILENO;
+ } else {
+ file = open(name, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
+ if (file == -1) {
+ fprintf(stderr, "Unable to open file '%s'\n", name);
+ exit(EXIT_FAILURE);
+ }
+ }
/* Write a header, will update with size once record is complete */
- init_wave_header(&header, channels, rate, samplebits);
- written = write(file, &header, sizeof(header));
- if (written != sizeof(header)) {
+ if (!streamed) {
+ init_wave_header(&header, channels, rate, samplebits);
+ written = write(file, &header, sizeof(header));
+ if (written != sizeof(header)) {
fprintf(stderr, "Error writing output file header: %s\n",
strerror(errno));
goto file_exit;
- }
+ }
+ }
memset(&codec, 0, sizeof(codec));
memset(&config, 0, sizeof(config));
@@ -299,7 +312,7 @@ void capture_samples(char *name, unsigned int card, unsigned int device,
};
if (verbose)
- printf("%s: Opened compress device\n", __func__);
+ fprintf(finfo, "%s: Opened compress device\n", __func__);
size = config.fragment_size;
buffer = malloc(size * config.fragments);
@@ -308,15 +321,15 @@ void capture_samples(char *name, unsigned int card, unsigned int device,
goto comp_exit;
}
- printf("Recording file %s On Card %u device %u, with buffer of %lu bytes\n",
+ fprintf(finfo, "Recording file %s On Card %u device %u, with buffer of %lu bytes\n",
name, card, device, buffer_size);
- printf("Format %u Channels %u, %u Hz\n",
- codec.id, codec.ch_out, rate);
+ fprintf(finfo, "Codec %u Format %u Channels %u, %u Hz\n",
+ codec.id, codec.format, codec.ch_out, rate);
compress_start(compress);
if (verbose)
- printf("%s: Capturing audio NOW!!!\n", __func__);
+ fprintf(finfo, "%s: Capturing audio NOW!!!\n", __func__);
do {
if (length && size > length - total_read)
@@ -344,7 +357,7 @@ void capture_samples(char *name, unsigned int card, unsigned int device,
}
if (verbose) {
print_time(compress);
- printf("%s: read %d\n", __func__, read);
+ fprintf(finfo, "%s: read %d\n", __func__, read);
}
}
} while (!length || total_read < length);
@@ -362,7 +375,7 @@ void capture_samples(char *name, unsigned int card, unsigned int device,
}
if (verbose)
- printf("%s: exit success\n", __func__);
+ fprintf(finfo, "%s: exit success\n", __func__);
free(buffer);
close(file);
@@ -379,7 +392,7 @@ file_exit:
close(file);
if (verbose)
- printf("%s: exit failure\n", __func__);
+ fprintf(finfo, "%s: exit failure\n", __func__);
exit(EXIT_FAILURE);
}
@@ -408,7 +421,7 @@ int main(int argc, char **argv)
exit(EXIT_FAILURE);
}
- if (argc < 2)
+ if (argc < 1)
usage();
verbose = 0;
@@ -456,15 +469,21 @@ int main(int argc, char **argv)
exit(EXIT_FAILURE);
}
}
- if (optind >= argc)
- usage();
-
- file = argv[optind];
+ if (optind >= argc) {
+ file = NULL;
+ finfo = fopen("/dev/null", "w");
+ streamed = true;
+ } else {
+ file = argv[optind];
+ finfo = stdout;
+ streamed = false;
+ }
capture_samples(file, card, device, buffer_size, frag, length,
rate, channels, format);
- printf("Finish capturing... Close Normally\n");
+ fprintf(finfo, "Finish capturing... Close Normally\n");
+
exit(EXIT_SUCCESS);
}
--
1.7.2.5
next reply other threads:[~2015-02-19 14:56 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2015-02-19 14:55 Richard Fitzgerald [this message]
2015-02-24 16:36 ` [TINYCOMPRESS][PATCH] crec: support streaming output to stdout Vinod Koul
2015-02-25 15:30 ` Richard Fitzgerald
2015-03-04 12:57 ` Vinod Koul
2015-03-04 16:27 ` Richard Fitzgerald
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1424357740-6561-1-git-send-email-rf@opensource.wolfsonmicro.com \
--to=rf@opensource.wolfsonmicro.com \
--cc=alsa-devel@alsa-project.org \
--cc=patches@opensource.wolfsonmicro.com \
--cc=vinod.koul@intel.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.