| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
#include <stdio.h> |
| 8 |
#include <unistd.h> |
| 9 |
#include <stdlib.h> |
| 10 |
#ifndef __STDC__ |
| 11 |
|
| 12 |
#endif |
| 13 |
#include <fcntl.h> |
| 14 |
#ifdef __STDC__ |
| 15 |
#include <string.h> |
| 16 |
#else |
| 17 |
#include <strings.h> |
| 18 |
#endif |
| 19 |
#include <sys/soundcard.h> |
| 20 |
|
| 21 |
#define NUM_BYTES (64*1024) |
| 22 |
#define BLOCK_SIZE (4*1024) |
| 23 |
|
| 24 |
#define AUDIO "/dev/dsp" |
| 25 |
|
| 26 |
char buffer[NUM_BYTES]; |
| 27 |
|
| 28 |
int audioDev = 0; |
| 29 |
|
| 30 |
main (int argc, char *argv[]) |
| 31 |
{ |
| 32 |
int numLeft; |
| 33 |
char *ptr; |
| 34 |
int num; |
| 35 |
int samplesize; |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
audioDev = open (AUDIO, O_RDONLY, 0); |
| 40 |
if (audioDev == -1) |
| 41 |
{ |
| 42 |
perror (AUDIO); |
| 43 |
exit (-1); |
| 44 |
} |
| 45 |
|
| 46 |
|
| 47 |
samplesize = 16; |
| 48 |
ioctl(audioDev, SNDCTL_DSP_SAMPLESIZE, &samplesize); |
| 49 |
if (samplesize != 16) |
| 50 |
{ |
| 51 |
perror("Unable to set the sample size."); |
| 52 |
exit(-1); |
| 53 |
} |
| 54 |
|
| 55 |
|
| 56 |
printf("Begin recording.\n"); |
| 57 |
numLeft = NUM_BYTES; |
| 58 |
ptr = buffer; |
| 59 |
while( numLeft >= BLOCK_SIZE ) |
| 60 |
{ |
| 61 |
if ( (num = read (audioDev, ptr, BLOCK_SIZE)) < 0 ) |
| 62 |
{ |
| 63 |
perror (AUDIO); |
| 64 |
exit (-1); |
| 65 |
} |
| 66 |
else |
| 67 |
{ |
| 68 |
printf("Read %d bytes\n", num); |
| 69 |
ptr += num; |
| 70 |
numLeft -= num; |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
close( audioDev ); |
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
audioDev = open (AUDIO, O_WRONLY, 0); |
| 79 |
if (audioDev == -1) |
| 80 |
{ |
| 81 |
perror (AUDIO); |
| 82 |
exit (-1); |
| 83 |
} |
| 84 |
|
| 85 |
|
| 86 |
samplesize = 16; |
| 87 |
ioctl(audioDev, SNDCTL_DSP_SAMPLESIZE, &samplesize); |
| 88 |
if (samplesize != 16) |
| 89 |
{ |
| 90 |
perror("Unable to set the sample size."); |
| 91 |
exit(-1); |
| 92 |
} |
| 93 |
|
| 94 |
|
| 95 |
printf("Begin playing.\n"); |
| 96 |
numLeft = NUM_BYTES; |
| 97 |
ptr = buffer; |
| 98 |
while( numLeft >= BLOCK_SIZE ) |
| 99 |
{ |
| 100 |
if ( (num = write (audioDev, ptr, BLOCK_SIZE)) < 0 ) |
| 101 |
{ |
| 102 |
perror (AUDIO); |
| 103 |
exit (-1); |
| 104 |
} |
| 105 |
else |
| 106 |
{ |
| 107 |
printf("Wrote %d bytes\n", num); |
| 108 |
ptr += num; |
| 109 |
numLeft -= num; |
| 110 |
} |
| 111 |
} |
| 112 |
|
| 113 |
close( audioDev ); |
| 114 |
} |