Shachar Shemesh wrote:
> Shachar Shemesh wrote:
>> My little experiment failed to record anything at all from /dev/dsp.
>> It will take me a little while to get the data.
> Does anyone care to look at the attached program and tell me why it
> fails to record from the mic?
>
> I've set the mixer independently.
>
> I get a bunch of numbers ranging as high as "0" and as low as "-4", even
> if I speak into the mic while recording. The microphone is functioning
> with other programs, as well as when I set the mic to output to the
> sound's output device.
>
> Ideas?
Works for me.
I've modified the code and run it through ent, the values before
modification were between -20 and 20 so I probably have my mic gain very
low. There is a mic attached but only normal office background noise.
The result from ent for the attached code is:
$ make random && ./random && ent ./random.out
cc random.c -o random
Actual sampling rate is 11025
Entropy = 7.987278 bits per byte.
Optimum compression would reduce the size
of this 12800 byte file by 0 percent.
Chi square distribution for 12800 samples is 224.00, and randomly
would exceed this value 90.00 percent of the times.
Arithmetic mean value of data bytes is 126.8852 (127.5 = random).
Monte Carlo value for Pi is 3.167369902 (error 0.82 percent).
Serial correlation coefficient is -0.005150 (totally uncorrelated = 0.0).
Baruch
/* Extract random data from the sound card
* Copyright (C) 2006 by Shachar Shemesh
* Free to use under the terms of the GNU General Public License version 2.
*/
#include <sys/soundcard.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
// We read a set of samples from the sound device, and output the lowest bit to a stdout
int main( int argc, char * argv[] )
{
int soundfd;
// Extract 12KB of random data
int numbytes=100*1024;
int numbits=0;
unsigned char registr;
signed short buffer[1024];
int byte = 0;
int bits = 0;
FILE *out;
// Open the sound device
soundfd=open("/dev/dsp", O_RDONLY);
if( soundfd<0 )
{
perror("Failed to open sound device");
return 1;
}
{
int format=AFMT_S16_NE;
if( ioctl(soundfd, SNDCTL_DSP_SETFMT, &format)==-1 )
{
perror("Failed to set sound format");
return 1;
}
if( format!=AFMT_S16_NE ) {
fprintf(stderr, "Device doesn't support requested format (returned %d)\n", format);
return 1;
}
}
{
int stereo=0;
if( ioctl(soundfd, SNDCTL_DSP_STEREO, &stereo)==-1 )
{
perror("SNDCTL_DSP_STEREO");
return 1;
}
if( stereo!=0 )
{
fprintf( stderr, "Couldn't set mode to mono\n");
return 1;
}
}
{
int speed=11025;
if( ioctl( soundfd, SNDCTL_DSP_SPEED, &speed )==-1 ) {
perror("SNDCTL_DSP_SPEED");
}
fprintf(stderr, "Actual sampling rate is %d\n", speed);
}
out = fopen("random.out", "wb");
while( numbytes ) {
int numread;
if( (numread=read( soundfd, &buffer, sizeof(buffer) ))<0 ) {
perror("read failed");
return 1;
}
numread/=sizeof(buffer[0]);
numbytes-=numread;
int i;
for( i=0; i<numread; ++i ) {
byte = byte<<1 | buffer[i] & 1;
bits++;
if (bits==8) {
fputc(byte, out);
byte = 0;
bits = 0;
}
}
// fprintf(stderr, "Read returned %d\n", buffer);
}
fclose(out);
return 0;
}