84
98
#define TO_LE16(x) bswap_16(x)
101
static int snd_pcm_file_append_value(char **string_p, char **index_ch_p,
102
int *len_p, const char *value)
104
char *string, *index_ch;
105
int index, len, value_len;
106
/* input pointer values */
108
string = *(string_p);
109
index_ch = *(index_ch_p);
111
value_len = strlen(value);
112
/* reallocation to accommodate the value */
113
index = index_ch - string;
115
string = realloc(string, len + 1);
118
index_ch = string + index;
119
/* concatenating the new value */
120
strcpy(index_ch, value);
121
index_ch += value_len;
124
*(string_p) = string;
125
*(index_ch_p) = index_ch;
129
static int snd_pcm_file_replace_fname(snd_pcm_file_t *file, char **new_fname_p)
131
char value[VALUE_MAXLEN];
132
char *fname = file->fname;
133
char *new_fname = NULL;
134
char *old_last_ch, *old_index_ch, *new_index_ch;
135
int old_len, new_len, err;
137
snd_pcm_t *pcm = file->gen.slave;
139
/* we want to keep fname, const */
140
old_len = new_len = strlen(fname);
141
old_last_ch = fname + old_len - 1;
142
new_fname = malloc(new_len + 1);
146
old_index_ch = fname; /* first character of the old name */
147
new_index_ch = new_fname; /* first char of the new name */
149
while (old_index_ch <= old_last_ch) {
150
if (*(old_index_ch) == LEADING_KEY &&
151
old_index_ch != old_last_ch) {
152
/* is %, not last char, skipping and checking
154
switch (*(++old_index_ch)) {
156
snprintf(value, sizeof(value), "%d",
158
err = snd_pcm_file_append_value(&new_fname,
159
&new_index_ch, &new_len, value);
165
snprintf(value, sizeof(value), "%d",
167
err = snd_pcm_file_append_value(&new_fname,
168
&new_index_ch, &new_len, value);
174
snprintf(value, sizeof(value), "%d",
175
pcm->frame_bits/pcm->channels);
176
err = snd_pcm_file_append_value(&new_fname,
177
&new_index_ch, &new_len, value);
183
err = snd_pcm_file_append_value(&new_fname,
184
&new_index_ch, &new_len,
185
snd_pcm_format_name(pcm->format));
191
/* non-key char, just copying */
192
*(new_index_ch++) = *(old_index_ch);
197
/* plain copying, shifting both strings to next chars */
198
*(new_index_ch++) = *(old_index_ch++);
201
/* closing the new string */
202
*(new_index_ch) = '\0';
203
*(new_fname_p) = new_fname;
208
static int snd_pcm_file_open_output_file(snd_pcm_file_t *file)
212
/* fname can contain keys, generating final_fname */
213
err = snd_pcm_file_replace_fname(file, &(file->final_fname));
216
/*printf("DEBUG - original fname: %s, final fname: %s\n",
217
file->fname, file->final_fname);*/
219
if (file->final_fname[0] == '|') {
223
pipe = popen(file->final_fname + 1, "w");
225
SYSERR("running %s for writing failed",
232
fd = open(file->final_fname, O_WRONLY|O_CREAT|O_TRUNC,
235
fd = open(file->final_fname, O_WRONLY|O_CREAT|O_EXCL,
238
char *tmpfname = NULL;
240
len = strlen(file->final_fname) + 6;
241
tmpfname = malloc(len);
244
for (idx = 1; idx < 10000; idx++) {
245
snprintf(tmpfname, len,
246
"%s.%04d", file->final_fname,
249
O_WRONLY|O_CREAT|O_EXCL,
252
free(file->final_fname);
253
file->final_fname = tmpfname;
258
SYSERR("open %s for writing failed",
87
270
static void setup_wav_header(snd_pcm_t *pcm, struct wav_fmt *fmt)
89
272
fmt->fmt = TO_LE16(0x01);
546
741
SNDERR("file format %s is unknown", fmt);
551
fd = open(fname, O_WRONLY|O_CREAT|O_TRUNC, perm);
553
fd = open(fname, O_WRONLY|O_CREAT|O_EXCL, perm);
556
len = strlen(fname) + 6;
557
tmpname = malloc(len);
560
for (idx = 1; idx < 10000; idx++) {
561
snprintf(tmpname, len,
562
"%s.%04d", fname, idx);
563
fd = open(tmpname, O_WRONLY|O_CREAT|O_EXCL, perm);
572
SYSERR("open %s for writing failed", fname);
577
744
file = calloc(1, sizeof(snd_pcm_file_t));
749
/* opening output fname is delayed until writing,
750
when PCM params are known */
752
file->fname = strdup(fname);
586
757
ifd = open(ifname, O_RDONLY); /* TODO: mind blocking mode */
588
759
SYSERR("open %s for reading failed", ifname);
598
file->fname = strdup(fname);
600
763
file->ifname = strdup(ifname);
603
767
file->format = format;