Skip to content

Commit 6d5cd16

Browse files
committed
rtl_fm: wav header
1 parent 588b673 commit 6d5cd16

File tree

1 file changed

+44
-1
lines changed

1 file changed

+44
-1
lines changed

src/rtl_fm.c

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,7 @@ struct output_state
165165
int16_t result[MAXIMUM_BUF_LENGTH];
166166
int result_len;
167167
int rate;
168+
int wav_format;
168169
pthread_rwlock_t rw;
169170
pthread_cond_t ready;
170171
pthread_mutex_t ready_m;
@@ -217,6 +218,7 @@ void usage(void)
217218
"\t direct: enable direct sampling\n"
218219
"\t no-mod: enable no-mod direct sampling\n"
219220
"\t offset: enable offset tuning\n"
221+
"\t wav: generate WAV header\n"
220222
"\tfilename ('-' means stdout)\n"
221223
"\t omitting the filename also uses stdout\n\n"
222224
"Experimental options:\n"
@@ -237,6 +239,7 @@ void usage(void)
237239
"\trtl_fm ... | play -t raw -r 24k -es -b 16 -c 1 -V1 -\n"
238240
"\t | aplay -r 24k -f S16_LE -t raw -c 1\n"
239241
"\t -M wbfm | play -r 32k ... \n"
242+
"\t -E wav | play -t wav - \n"
240243
"\t -s 22050 | multimon -t raw /dev/stdin\n\n");
241244
exit(1);
242245
}
@@ -1058,7 +1061,7 @@ void demod_cleanup(struct demod_state *s)
10581061

10591062
void output_init(struct output_state *s)
10601063
{
1061-
s->rate = DEFAULT_SAMPLE_RATE;
1064+
//s->rate = DEFAULT_SAMPLE_RATE;
10621065
pthread_rwlock_init(&s->rw, NULL);
10631066
pthread_cond_init(&s->ready, NULL);
10641067
pthread_mutex_init(&s->ready_m, NULL);
@@ -1124,6 +1127,40 @@ int agc_init(struct demod_state *s)
11241127
return 0;
11251128
}
11261129

1130+
int generate_header(struct demod_state *d, struct output_state *o)
1131+
{
1132+
int i, s_rate, b_rate;
1133+
char *channels = "\1\0";
1134+
char *align = "\2\0";
1135+
uint8_t samp_rate[4] = {0, 0, 0, 0};
1136+
uint8_t byte_rate[4] = {0, 0, 0, 0};
1137+
s_rate = o->rate;
1138+
b_rate = o->rate * 2;
1139+
if (d->mode_demod == &raw_demod) {
1140+
channels = "\2\0";
1141+
align = "\4\0";
1142+
b_rate *= 2;
1143+
}
1144+
for (i=0; i<4; i++) {
1145+
samp_rate[i] = (uint8_t)((s_rate >> (8*i)) & 0xFF);
1146+
byte_rate[i] = (uint8_t)((b_rate >> (8*i)) & 0xFF);
1147+
}
1148+
fwrite("RIFF", 1, 4, o->file);
1149+
fwrite("\xFF\xFF\xFF\xFF", 1, 4, o->file); /* size */
1150+
fwrite("WAVE", 1, 4, o->file);
1151+
fwrite("fmt ", 1, 4, o->file);
1152+
fwrite("\x10\0\0\0", 1, 4, o->file); /* size */
1153+
fwrite("\1\0", 1, 2, o->file); /* pcm */
1154+
fwrite(channels, 1, 2, o->file);
1155+
fwrite(samp_rate, 1, 4, o->file);
1156+
fwrite(byte_rate, 1, 4, o->file);
1157+
fwrite(align, 1, 2, o->file);
1158+
fwrite("\x10\0", 1, 2, o->file); /* bits per channel */
1159+
fwrite("data", 1, 4, o->file);
1160+
fwrite("\xFF\xFF\xFF\xFF", 1, 4, o->file); /* size */
1161+
return 0;
1162+
}
1163+
11271164
int main(int argc, char **argv)
11281165
{
11291166
#ifndef _WIN32
@@ -1201,6 +1238,8 @@ int main(int argc, char **argv)
12011238
dongle.direct_sampling = 3;}
12021239
if (strcmp("offset", optarg) == 0) {
12031240
dongle.offset_tuning = 1;}
1241+
if (strcmp("wav", optarg) == 0) {
1242+
output.wav_format = 1;}
12041243
break;
12051244
case 'F':
12061245
demod.downsample_passes = 1; /* truthy placeholder */
@@ -1320,6 +1359,10 @@ int main(int argc, char **argv)
13201359
}
13211360
}
13221361

1362+
if (output.wav_format) {
1363+
generate_header(&demod, &output);
1364+
}
1365+
13231366
//r = rtlsdr_set_testmode(dongle.dev, 1);
13241367

13251368
/* Reset endpoint before we start reading from it (mandatory) */

0 commit comments

Comments
 (0)