diff --git a/tinycap.c b/tinycap.c
index a50c4178256e62adcc1a2f890f975a42463331c5..74297507a4eb97e35b05de60d9262ee4bbf0a329 100644 (file)
--- a/tinycap.c
+++ b/tinycap.c
#include <stdlib.h>
#include <stdint.h>
#include <signal.h>
+#include <string.h>
#define ID_RIFF 0x46464952
#define ID_WAVE 0x45564157
unsigned int capture_sample(FILE *file, unsigned int card, unsigned int device,
unsigned int channels, unsigned int rate,
- unsigned int bits, unsigned int period_size,
+ enum pcm_format format, unsigned int period_size,
unsigned int period_count);
void sigint_handler(int sig)
unsigned int frames;
unsigned int period_size = 1024;
unsigned int period_count = 4;
+ enum pcm_format format;
if (argc < 2) {
fprintf(stderr, "Usage: %s file.wav [-D card] [-d device] [-c channels] "
- "[-r rate] [-b bits]\n", argv[0]);
+ "[-r rate] [-b bits] [-p period_size] [-n n_periods]\n", argv[0]);
return 1;
}
header.audio_format = FORMAT_PCM;
header.num_channels = channels;
header.sample_rate = rate;
+
+ switch (bits) {
+ case 32:
+ format = PCM_FORMAT_S32_LE;
+ break;
+ case 24:
+ format = PCM_FORMAT_S24_LE;
+ break;
+ case 16:
+ format = PCM_FORMAT_S16_LE;
+ break;
+ default:
+ fprintf(stderr, "%d bits is not supported.\n", bits);
+ return 1;
+ }
+
+ header.bits_per_sample = pcm_format_to_bits(format);
header.byte_rate = (header.bits_per_sample / 8) * channels * rate;
header.block_align = channels * (header.bits_per_sample / 8);
- header.bits_per_sample = bits;
header.data_id = ID_DATA;
/* leave enough room for header */
/* install signal handler and begin capturing */
signal(SIGINT, sigint_handler);
frames = capture_sample(file, card, device, header.num_channels,
- header.sample_rate, header.bits_per_sample,
+ header.sample_rate, format,
period_size, period_count);
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);
unsigned int capture_sample(FILE *file, unsigned int card, unsigned int device,
unsigned int channels, unsigned int rate,
- unsigned int bits, unsigned int period_size,
+ enum pcm_format format, unsigned int period_size,
unsigned int period_count)
{
struct pcm_config config;
@@ -176,10 +195,7 @@ unsigned int capture_sample(FILE *file, unsigned int card, unsigned int device,
config.rate = rate;
config.period_size = period_size;
config.period_count = period_count;
- if (bits == 32)
- config.format = PCM_FORMAT_S32_LE;
- else if (bits == 16)
- config.format = PCM_FORMAT_S16_LE;
+ config.format = format;
config.start_threshold = 0;
config.stop_threshold = 0;
config.silence_threshold = 0;
return 0;
}
- size = pcm_get_buffer_size(pcm);
+ size = pcm_frames_to_bytes(pcm, pcm_get_buffer_size(pcm));
buffer = malloc(size);
if (!buffer) {
fprintf(stderr, "Unable to allocate %d bytes\n", size);
return 0;
}
- printf("Capturing sample: %u ch, %u hz, %u bit\n", channels, rate, bits);
+ 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) {
free(buffer);
pcm_close(pcm);
- return bytes_read / ((bits / 8) * channels);
+ return pcm_bytes_to_frames(pcm, bytes_read);
}