minix/commands/recwave/recwave.c
Gianluca Guida cc17b27a2b Build NetBSD libc library in world in ELF mode.
3 sets of libraries are built now:
  . ack: all libraries that ack can compile (/usr/lib/i386/)
  . clang+elf: all libraries with minix headers (/usr/lib/)
  . clang+elf: all libraries with netbsd headers (/usr/netbsd/)

Once everything can be compiled with netbsd libraries and headers, the
/usr/netbsd hierarchy will be obsolete and its libraries compiled with
netbsd headers will be installed in /usr/lib, and its headers
in /usr/include. (i.e. minix libc and current minix headers set
will be gone.)

To use the NetBSD libc system (libraries + headers) before
it is the default libc, see:
   http://wiki.minix3.org/en/DevelopersGuide/UsingNetBSDCode
This wiki page also documents the maintenance of the patch
files of minix-specific changes to imported NetBSD code.

Changes in this commit:
  . libsys: Add NBSD compilation and create a safe NBSD-based libc.
  . Port rest of libraries (except libddekit) to new header system.
  . Enable compilation of libddekit with new headers.
  . Enable kernel compilation with new headers.
  . Enable drivers compilation with new headers.
  . Port legacy commands to new headers and libc.
  . Port servers to new headers.
  . Add <sys/sigcontext.h> in compat library.
  . Remove dependency file in tree.
  . Enable compilation of common/lib/libc/atomic in libsys
  . Do not generate RCSID strings in libc.
  . Temporarily disable zoneinfo as they are incompatible with NetBSD format
  . obj-nbsd for .gitignore
  . Procfs: use only integer arithmetic. (Antoine Leca)
  . Increase ramdisk size to create NBSD-based images.
  . Remove INCSYMLINKS handling hack.
  . Add nbsd_include/sys/exec_elf.h
  . Enable ELF compilation with NBSD libc.
  . Add 'make nbsdsrc' in tools to download reference NetBSD sources.
  . Automate minix-port.patch creation.
  . Avoid using fstavfs() as it is *extremely* slow and unneeded.
  . Set err() as PRIVATE to avoid name clash with libc.
  . [NBSD] servers/vm: remove compilation warnings.
  . u32 is not a long in NBSD headers.
  . UPDATING info on netbsd hierarchy
  . commands fixes for netbsd libc
2011-06-24 11:46:30 +02:00

237 lines
5.3 KiB
C

/*
* recwave.c
*
* Record sound files in wave format. Only MicroSoft PCM is supported.
*
* Michel R. Prevenier.
*/
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <termios.h>
#include <stdlib.h>
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <signal.h>
#include <sys/ioctl.h>
#include <minix/sound.h>
_PROTOTYPE (int main, (int argc, char **argv));
_PROTOTYPE (void usage, (void));
_PROTOTYPE ( void write_wave_header, (void));
_PROTOTYPE ( void terminate, (int s));
/******* Wave format definitions *********/
#define RIFF_ID 0x46464952
#define WAVE_ID1 0x45564157
#define WAVE_ID2 0x20746D66
#define DATA_ID 0x61746164
#define MS_PCM_FORMAT 0x0001
#define WORD short
#define DWORD unsigned long
struct RIFF_fields
{
DWORD RIFF_id;
DWORD RIFF_len;
DWORD WAVE_id1;
DWORD WAVE_id2;
DWORD data_ptr;
} r_fields;
struct common_fields
{
WORD FormatTag;
WORD Channels;
DWORD SamplesPerSec;
DWORD AvgBytesPerSec;
WORD BlockAlign;
} c_fields;
struct specific_fields
{
WORD BitsPerSample;
} s_fields;
DWORD data_id;
DWORD data_len;
/******** End of wave format definitions *********/
/* Default recording values */
unsigned int sign = 0;
unsigned int bits = 8;
unsigned int stereo = 0;
unsigned int rate = 22050;
int old_stdin;
struct termios old_tty, new_tty;
int audio, file;
void usage()
{
fprintf(stderr, "Usage: recwav [-b -s -r] file_name\n");
exit(-1);
}
void terminate(s)
int s;
{
/* Restore terminal parameters */
tcsetattr(0, TCSANOW, &old_tty);
(void) fcntl(0,F_SETFL,old_stdin);
close(audio);
close(file);
exit(0);
}
void write_wave_header()
{
/* RIFF fields */
r_fields.RIFF_id = RIFF_ID;
r_fields.WAVE_id1 = WAVE_ID1;
r_fields.WAVE_id2 = WAVE_ID2;
r_fields.data_ptr = 16;
r_fields.RIFF_len = 20 + r_fields.data_ptr + data_len;
/* MicroSoft PCM specific fields */
s_fields.BitsPerSample = bits;
/* Common fields */
c_fields.FormatTag = MS_PCM_FORMAT;
c_fields.Channels = stereo + 1;
c_fields.SamplesPerSec = rate;
c_fields.AvgBytesPerSec = c_fields.Channels * rate * (bits / 8);
c_fields.BlockAlign = c_fields.Channels * (bits / 8);
/* Data chunk */
data_id = DATA_ID;
/* Write wave-file header */
lseek(file, 0L, SEEK_SET);
write(file, &r_fields, 20);
write(file, &c_fields, 14);
write(file, &s_fields, 2);
write(file, &data_id, sizeof(data_id));
write(file, &data_len, sizeof(data_len));
}
int main(argc, argv)
int argc;
char **argv;
{
unsigned int fragment_size;
char *buffer, *file_name;
char c;
int i;
/* Read parameters */
if (argc < 2) usage();
i = 1;
while (argv[i][0] == '-' && i < argc)
{
if (strncmp(argv[i], "-b", 2) == 0)
bits = atoi(argv[i] + 2);
else if (strncmp(argv[i], "-s", 2) == 0)
stereo = atoi(argv[i] + 2);
else if (strncmp(argv[i], "-r", 2) == 0)
rate = (unsigned int) atol(argv[i] + 2);
else usage();
i++;
}
if (i == argc) usage();
file_name = argv[i];
/* Some sanity checks */
if ((bits != 8 && bits != 16) ||
(rate < 4000 || rate > 44100) ||
(stereo != 0 && stereo != 1))
{
fprintf(stderr, "Invalid parameters\n");
exit(-1);
}
/* Open DSP */
if ((audio = open("/dev/rec", O_RDWR)) < 0)
{
fprintf(stderr, "Cannot open /dev/rec\n");
exit(-1);
}
/* Get maximum fragment size and try to allocate a buffer */
ioctl(audio, DSPIOMAX, &fragment_size);
if ((buffer = malloc(fragment_size)) == (char *) 0)
{
fprintf(stderr, "Cannot allocate buffer\n");
exit(-1);
}
/* Set sample parameters */
ioctl(audio, DSPIOSIZE, &fragment_size);
ioctl(audio, DSPIOSTEREO, &stereo);
ioctl(audio, DSPIORATE, &rate);
ioctl(audio, DSPIOBITS, &bits);
sign = (bits == 16 ? 1 : 0);
ioctl(audio, DSPIOSIGN, &sign);
/* Create sample file */
if ((file = creat(file_name, 511)) < 0)
{
fprintf(stderr, "Cannot create %s\n", argv[1]);
exit(-1);
}
/* Skip wave header */
lseek(file, (long)(sizeof(r_fields) +
sizeof(c_fields) +
sizeof(s_fields) +
sizeof(data_id) +
sizeof(data_len)), SEEK_SET);
printf("\nBits per sample : %u\n", bits);
printf("Stereo : %s\n", (stereo == 1 ? "yes" : "no"));
printf("Samples per second: %u\n", rate);
/* Set terminal parameters and remember the old ones */
tcgetattr(0, &old_tty);
new_tty = old_tty;
new_tty.c_lflag &= ~(ICANON|ECHO);
old_stdin = fcntl(0, F_GETFL);
/* Catch break signal to be able to restore terminal parameters in case
* of a user interrupt
*/
signal(SIGINT, terminate);
/* Go to non-blocking mode */
tcsetattr(0, TCSANOW, &new_tty);
(void) fcntl(0, F_SETFL, old_stdin | O_NONBLOCK);
printf("\nPress spacebar to start sampling...\n");
while(!(read(0, &c, 1) == 1 && c == ' '));
printf("Sampling, press spacebar to stop...\n");
while(!(read(0, &c, 1) == 1 && c == ' '))
{
/* Read sample fragment and write to sample file */
read(audio, buffer, fragment_size);
write(file, buffer, fragment_size);
data_len+= fragment_size;
}
printf("%ld bytes sampled. \n\n", data_len);
/* Construct the wave header in front of the raw sample data */
write_wave_header();
/* Restore terminal parameters and exit */
terminate(1);
}