allow user to select block size

Patch provided by 'airwave', Thanks!

Signed-off-by: Steve Markgraf <steve@steve-m.de>
This commit is contained in:
Steve Markgraf 2012-04-09 14:46:22 +02:00
parent 6eeceba397
commit 7e6d2f55ee

View File

@ -26,7 +26,11 @@
#include "rtl-sdr.h" #include "rtl-sdr.h"
#define READLEN (16 * 16384) #define DEFAULT_SAMPLE_RATE 2048000
#define DEFAULT_ASYNC_BUF_NUMBER 32
#define DEFAULT_BUF_LENGTH (16 * 16384)
#define MINIMAL_BUF_LENGTH 32
#define MAXIMAL_BUF_LENGTH (256 * 16384)
static int do_exit = 0; static int do_exit = 0;
static rtlsdr_dev_t *dev = NULL; static rtlsdr_dev_t *dev = NULL;
@ -35,10 +39,11 @@ void usage(void)
{ {
fprintf(stderr, fprintf(stderr,
"rtl-sdr, an I/Q recorder for RTL2832 based DVB-T receivers\n\n" "rtl-sdr, an I/Q recorder for RTL2832 based DVB-T receivers\n\n"
"Usage:\t -f frequency to tune to [Hz]\n" "Usage:\t -f frequency_to_tune_to [Hz]\n"
"\t[-s samplerate (default: 2048000 Hz)]\n" "\t[-s samplerate (default: 2048000 Hz)]\n"
"\t[-d device index (default: 0)]\n" "\t[-d device_index (default: 0)]\n"
"\t[-g tuner gain (default: 0 dB)]\n" "\t[-g tuner_gain (default: 0 dB)]\n"
"\t[-b output_block_size (default: 16 * 16384)]\n"
"\toutput filename\n"); "\toutput filename\n");
exit(1); exit(1);
} }
@ -58,16 +63,19 @@ static void rtlsdr_callback(unsigned char *buf, uint32_t len, void *ctx)
int main(int argc, char **argv) int main(int argc, char **argv)
{ {
struct sigaction sigact; struct sigaction sigact;
int r, opt;
char *filename = NULL; char *filename = NULL;
uint32_t frequency = 0, samp_rate = 2048000;
uint8_t buffer[READLEN];
int n_read; int n_read;
FILE *file; int r, opt;
uint32_t dev_index = 0;
int i, gain = 0; int i, gain = 0;
FILE *file;
uint8_t *buffer;
uint32_t dev_index = 0;
uint32_t frequency = 0;
uint32_t samp_rate = DEFAULT_SAMPLE_RATE;
uint32_t out_block_size = DEFAULT_BUF_LENGTH;
while ((opt = getopt(argc, argv, "d:f:g:s:")) != -1) {
while ((opt = getopt(argc, argv, "d:f:g:s:b:")) != -1) {
switch (opt) { switch (opt) {
case 'd': case 'd':
dev_index = atoi(optarg); dev_index = atoi(optarg);
@ -81,6 +89,9 @@ int main(int argc, char **argv)
case 's': case 's':
samp_rate = (int)atof(optarg); samp_rate = (int)atof(optarg);
break; break;
case 'b':
out_block_size = (uint32_t)atof(optarg);
break;
default: default:
usage(); usage();
break; break;
@ -92,6 +103,19 @@ int main(int argc, char **argv)
} else { } else {
filename = argv[optind]; filename = argv[optind];
} }
if(out_block_size < MINIMAL_BUF_LENGTH ||
out_block_size > MAXIMAL_BUF_LENGTH ){
fprintf(stderr,
"Output block size wrong value, falling back to default\n");
fprintf(stderr,
"Minimal length: %u\n", MINIMAL_BUF_LENGTH);
fprintf(stderr,
"Maximal length: %u\n", MAXIMAL_BUF_LENGTH);
out_block_size = DEFAULT_BUF_LENGTH;
}
buffer = malloc(out_block_size * sizeof(uint8_t));
int device_count = rtlsdr_get_device_count(); int device_count = rtlsdr_get_device_count();
if (!device_count) { if (!device_count) {
@ -155,19 +179,20 @@ int main(int argc, char **argv)
fprintf(stderr, "Reading samples...\n"); fprintf(stderr, "Reading samples...\n");
#if 0 #if 0
while (!do_exit) { while (!do_exit) {
r = rtlsdr_read_sync(dev, buffer, READLEN, &n_read); r = rtlsdr_read_sync(dev, buffer, out_block_size, &n_read);
if (r < 0) if (r < 0)
fprintf(stderr, "WARNING: sync read failed.\n"); fprintf(stderr, "WARNING: sync read failed.\n");
fwrite(buffer, n_read, 1, file); fwrite(buffer, n_read, 1, file);
if (n_read < READLEN) { if (n_read < out_block_size) {
fprintf(stderr, "Short read, samples lost, exiting!\n"); fprintf(stderr, "Short read, samples lost, exiting!\n");
break; break;
} }
} }
#else #else
rtlsdr_read_async(dev, rtlsdr_callback, (void *)file, 0, 0); rtlsdr_read_async(dev, rtlsdr_callback, (void *)file,
DEFAULT_ASYNC_BUF_NUMBER, out_block_size);
#endif #endif
if (do_exit) if (do_exit)
fprintf(stderr, "\nUser cancel, exiting...\n"); fprintf(stderr, "\nUser cancel, exiting...\n");
@ -175,8 +200,8 @@ int main(int argc, char **argv)
fprintf(stderr, "\nSystem cancel, exiting...\n"); fprintf(stderr, "\nSystem cancel, exiting...\n");
fclose(file); fclose(file);
rtlsdr_close(dev); rtlsdr_close(dev);
free (buffer);
out: out:
return r >= 0 ? r : -r; return r >= 0 ? r : -r;
} }