Read Microphone

From Wiz Wiki
Jump to: navigation, search

The WIZ has a built in microphone, which can be accessed by reading from "/dev/dsp".

I'll make a small example and upload this ASAP. (apr,27 2009)

Here's what is supposed to be working, but it's not. I'll try to find the bug, since all I can read are 0s. If anyone has an idea, please tell me.

int OpenDSPLocks(key_t LockIPCKey) {
	int ipc;
	int s;
	int e;
	int safety = 3;
	union semun u;
	static ushort init_sems[2] = { 1, 1 };
 
	// If semaphore already exists, just use it:
	while ( (ipc = semget(LockIPCKey,2,0666)) < 0 && --safety >= 0 ) {
 
		// Failed to find it, try creating it :
		if ( (ipc = semget(LockIPCKey,2,IPC_CREAT|IPC_EXCL|0666)) < 0 && errno != EEXIST ) {
			printf("%s Unable to create a semaphore set for key 0x%lX",
				sys_errlist[errno],
				LockIPCKey);
			return -1;	// No system IPC resources?
		}
 
		// Set already exists- timing error? Try again.
		if ( ipc < 0 ) {
			sleep(1);
			continue;
		}
 
		/*
		 * We created the semaphore set - initialize so that
		 * each semaphore has the value 1 :
		 */
		u.array = &init_sems[0];
 
		if ( (s = semctl(ipc,0,SETALL,u)) < 0 && errno == EIDRM ) {
			/*
			 * Another process removed our ipc resource :
			 */
			continue;	/* Try again */
		}
 
		if ( s < 0 ) {
			/*
			 * We failed to initialize!
			 */
			e = errno;			/* Save error */
 
			semctl(ipc,0,IPC_RMID,NULL);	/* Destroy bad sems */
 
			printf("%s: Unable to initialize semaphore set values",sys_errlist[errno=e]);
			return -1;			/* Return err ind. */
		}
	}
 
	printf("OpenDSPLocks: %d\n", ipc);
 
	return ipc;	/* Return existing, or newly initialized sems */
}
 
 
 
 
 
 
 
 
//Lock the /dev/dsp device :
int LockDSP(int ipc)
{
	int s;
	static struct sembuf sops[1] = { { 0, -1, SEM_UNDO } };
	sops[0].sem_num = 1;
	sops[0].sem_flg = 0;
 
	while ( (s = semop(ipc,sops,1)) < 0 && errno == EINTR )
	{}
	if ( s < 0 )
		printf("%s: Locking the record semaphore",sys_errlist[errno]);
 
	printf("DSPLocks: %d\n", s);
	return s;
}
 
// Unlock the /dev/dsp device :
int UnlockDSP(int ipc) {
	int s;
	static struct sembuf sops[1] = { { 0, +1, SEM_UNDO } };
	sops[0].sem_num = 1;
	sops[0].sem_flg = 0;
	while ( (s = semop(ipc,sops,1)) < 0 && errno == EINTR ) {}
 
	if ( s < 0 )
		printf("%s: Unlocking the record semaphore",
			sys_errlist[errno]);
	return s;
}
 
 
// EXPORT
int WizOpenMic(int rate=RATE)
{
unsigned long ul;
	int t;
	gWizMicIPC = OpenDSPLocks(AUDIOLCK);
	LockDSP(gWizMicIPC);
 
	int dsp;
	dsp = open("/dev/dsp",O_RDONLY); // O_RDWR);
	if(dsp<0)
	{
		printf("Can't open device\n");
		return -1;
	}
 
		/*
		 * Set the data bit size:
		 */
		t = 16;
	        if ( ioctl(dsp,SNDCTL_DSP_SAMPLESIZE,&t) < 0 ) {
			printf("%s: Setting DSP to %u bits",sys_errlist[errno],(unsigned)t);
			goto errxit;
		}
 
		/*
		 * Set the mode to be Stereo or Mono:
		 */
		t = 0;
		if ( ioctl(dsp,SNDCTL_DSP_STEREO,&t) < 0 ) {
			printf("%s: Unable to set DSP to %s mode",
				sys_errlist[errno],
				t?"Stereo":"Mono");
			goto errxit;
		}
 
		/*
		 * Set the sampling rate:
		 */
		ul = 22050;
		if ( ioctl(dsp,SNDCTL_DSP_SPEED,&ul) < 0 ) {
			printf("Unable to set audio sampling rate",sys_errlist[errno]);
			goto errxit;
		}
 
		printf("dps device: %d\n", dsp);
 
		gWizMicFD = dsp;
		return dsp;
	errxit:
		printf("error, exit.\n");
	    close(dsp);
	    return -1;
}
 
 
 
// return amplitude
int WizReadMic()
{
	unsigned short sig=0;
	if(gWizMicFD>=0)
	{
		unsigned short buffer[1024];
		// int rv = read(gWizMicFD,&sig,2);
		int rv = read(gWizMicFD,&buffer,2 * 1024);
		for(int i=0; i<1024; ++i)
		{
			if(buffer[i]>sig) sig=buffer[i];
		}
 
		static int couno = 0;
		if(++couno<5)
			printf("read %d = %d\n", rv, sig);
	}
	return (int)sig;
}
 
 
void WizCloseMic()
{
	close(gWizMicFD);
	UnlockDSP(gWizMicIPC);
	gWizMicFD=-1;
	gWizMicIPC=-1;
}
Personal tools
View and edit namespaces data
Variants
Actions
Navigation
community
Toolbox