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?

      Shachar

--
Shachar Shemesh
Lingnu Open Source Consulting ltd.
Have you backed up today's work? http://www.lingnu.com/backup.html

/* 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=12*1024;
    int numbits=0;
    unsigned char registr;
    signed short buffer[1024];

    // 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);
    }
    
    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 ) {
	    printf("%d\n", buffer[i]);
	    //printf("%c", (int)((((long)buffer[i])+(1<<15))>>8) );
	}
//    fprintf(stderr, "Read returned %d\n", buffer);
    }
    
    return 0;
}

Reply via email to