コード例 #1
0
// Aquire and enabled audio card
// return 0 if ok, -1 if failed
int HAE_AquireAudioCapture(void *context, UINT32 encoding, UINT32 sampleRate, UINT32 channels, UINT32 bits,
			   UINT32 audioFramesPerBuffer, UINT_PTR *pCaptureHandle)
{
    MMRESULT		theErr;
    WAVEINCAPS		caps;
    WAVEFORMATEX	format;
    INT32			deviceID;
    ULONG			minFramesPerBuffer;

    if (encoding != PCM) {
	// fprintf(stderr, "HAE_AquireAudioCapture: unsupported encoding: %d\n", g_encoding);
	return -1;
    }

    g_encoding = encoding;
    g_bitSize = bits;
    g_channels = channels;
    g_sampleRate = sampleRate;

    minFramesPerBuffer = sampleRate * HAE_WAVEIN_MIN_BUFFERSIZE_IN_MS / 1000;

    if( audioFramesPerBuffer == 0 ) {
	audioFramesPerBuffer = sampleRate * HAE_WAVEIN_DEFAULT_BUFFERSIZE_IN_MS / 1000;
    }

    g_audioFramesPerBuffer = audioFramesPerBuffer;

    if (pCaptureHandle) {
	/* what about multi-thread ? */
	*pCaptureHandle = 0L;
    }

    /* $$fb 2002-04-10: fix for 4514334: JavaSoundDemo Capture not works on Windows2000 with USB Port */
    if (g_soundDeviceIndex == 0) {
	deviceID = WAVE_MAPPER;
    } else {
	deviceID = g_soundDeviceIndex - 1;
    }

    theErr = waveInGetDevCaps(deviceID, &caps, sizeof(WAVEINCAPS));

    if (theErr == MMSYSERR_NOERROR) {
	    format.wFormatTag = WAVE_FORMAT_PCM;
	    format.nSamplesPerSec = sampleRate;
	    format.wBitsPerSample = (WORD)bits;
	    format.nChannels = (WORD)channels;

	    format.nBlockAlign = (WORD)((format.wBitsPerSample * format.nChannels) / 8);
	    format.nAvgBytesPerSec = format.nSamplesPerSec * format.nBlockAlign;
	    format.cbSize = 0;

	    // $$fb 2002-02-01: itanium port: use UINT_PTR for casting a pointer to UINT
	    theErr = waveInOpen(&g_captureSound, deviceID, &format,
				0L, (UINT_PTR)context, CALLBACK_NULL);

	    if (theErr == MMSYSERR_NOERROR) {
		    g_captureShutdown = FALSE;
		    g_waveInStarted = FALSE;

		    if (pCaptureHandle) {
			    *pCaptureHandle = (UINT_PTR)g_captureSound;
			}
		} else {
		    HAE_ReleaseAudioCapture(context);
		}
	}

    return (theErr == MMSYSERR_NOERROR) ? 0 : -1;
}
コード例 #2
0
// Aquire and enabled audio card
// return 0 if ok, -1 if failed
int HAE_AquireAudioCapture(void *context, UINT32 encoding, UINT32 sampleRate, UINT32 channels, UINT32 bits,
			   UINT32 audioFramesPerBuffer, UINT_PTR *pCaptureHandle) {
    audio_info_t sunAudioHeader;
    INT32 error = -1;
	
    char* pAudioDev = HAE_GetAudioDevRec(g_soundDeviceIndex, 0);
    INT32 minFramesPerBuffer;

    //fprintf(stderr, "Entering HAE_AquireAudioCapture(encoding=%d, samplerate=%d, channels=%d, bits=%d, framesPerBuffer=%d)\n", 
    //	encoding, sampleRate, channels, bits, audioFramesPerBuffer);

    g_encoding = encoding;
    g_bitSize = bits;
    g_channels = channels;
    g_sampleRate = sampleRate;	

    if( audioFramesPerBuffer == 0 ) {
	audioFramesPerBuffer = sampleRate * HAE_SOLARIS_DEFAULT_BUFFERSIZE_IN_MS / 1000;
    }

    g_audioFramesToRead = audioFramesPerBuffer / g_audioCaptureBufferSizeDivisor;

    if (pCaptureHandle) {
	*pCaptureHandle = 0L;
    }

    // try to open wave device for recording
    // $$kk: 12.17.97: need O_NONBLOCK flag to be compatible with windows

    // $$kk: 10.13.98: we want O_NONBLOCK so that we return failure immediately if the
    // device is busy (or absent or whatever) rather than blocking.  however, i think that
    // this same O_NONBLOCK flag dictates whether the read() calls should block.  even 
    // without the O_NONBLOCK flag set, read() does *not* block for me, so i'm keeping 
    // the flag for now....
	
    g_captureSound = open(pAudioDev,O_RDONLY|O_NONBLOCK);

    if (g_captureSound > 0) {

	/* set to multiple open */
	if (ioctl(g_captureSound, AUDIO_MIXER_MULTIPLE_OPEN, NULL) >= 0) {
	    TRACE1("HAE_AquireAudioCapture: %s set to multiple open\n", pAudioDev);
	} else {
	    ERROR1("HAE_AquireAudioCapture: ioctl AUDIO_MIXER_MULTIPLE_OPEN failed on %s!\n", pAudioDev);
	}

	AUDIO_INITINFO(&sunAudioHeader);
		
	// Set capture format of the sun device.
	sunAudioHeader.record.sample_rate = sampleRate;
	sunAudioHeader.record.precision = bits;
	sunAudioHeader.record.channels = channels;
	sunAudioHeader.record.buffer_size = g_audioFramesToRead * channels * bits / 8;
		
	sunAudioHeader.record.encoding = AUDIO_ENCODING_LINEAR;
	if (g_encoding == ULAW) {
	    sunAudioHeader.record.encoding = AUDIO_ENCODING_ULAW;			
	} 
	else if (g_encoding == ALAW) {
	    sunAudioHeader.record.encoding = AUDIO_ENCODING_ALAW;			
	} 



	// start out paused so we don't overflow the device driver buffers
	sunAudioHeader.record.pause = 1;
	error = ioctl(g_captureSound, AUDIO_SETINFO, &sunAudioHeader);

	if (error != -1) {
	    // flush anything we might have accumulated in the capture queue before pausing
	    error = ioctl(g_captureSound, I_FLUSH, FLUSHR);

	    error = ioctl(g_captureSound, AUDIO_GETINFO, &sunAudioHeader);
	    g_audioFramesToRead = sunAudioHeader.record.buffer_size / (channels * bits / 8);


	    if (error != -1) {
		if (pCaptureHandle) {
		    *pCaptureHandle = (UINT_PTR)g_captureSound;
		}
	    } 
	}
    }

    if (error == -1) {	// something failed
	HAE_ReleaseAudioCapture(context);
    }

    //fprintf(stderr, "<< HAE_API_SolarisOS_Capture: HAE_AquireAudioCapture() returning %d\n", error);
    return error;
}
コード例 #3
0
// Aquire and enabled audio card
// return 0 if ok, -1 if failed
// $$fb 2002-02-01: itanium port
int HAE_AquireAudioCapture(void *context, UINT32 encoding, UINT32 sampleRate,
			   UINT32 channels, UINT32 bits,
			   UINT32 audioFramesPerBuffer, UINT_PTR *pCaptureHandle)
{
    long			error = -1;
    //long			minFramesPerBuffer;

    g_encoding = encoding;
    g_bitSize = bits;
    g_channels = channels;
    g_sampleRate = sampleRate;

    // $$jb: 05.19.99: Setting the buffer size
    // $$kk: 08.06.99: i got rid of the fixed minimum value for buffersize
    // minFramesPerBuffer = sampleRate * HAE_LINUX_MIN_BUFFERSIZE_IN_MS / 1000;
    if ( audioFramesPerBuffer == 0 ) {
	audioFramesPerBuffer = sampleRate * HAE_LINUX_DEFAULT_BUFFERSIZE_IN_MS / 1000;
    }


    // $$kk: 08.06.99: got rid of fixed minimum buffer size
    /*
      if ( audioFramesPerBuffer >= minFramesPerBuffer ) {
      g_audioFramesToRead = audioFramesPerBuffer;
      } else {
      g_audioFramesToRead = minFramesPerBuffer;
      }
    */
    g_audioFramesToRead = audioFramesPerBuffer / g_audioCaptureBufferSizeDivisor;
    if (pCaptureHandle) {
	*pCaptureHandle = 0L;
    }

    // try to open wave device for recording
    // $$kk: 12.17.97: need O_NONBLOCK flag to be compatible with windows

    // $$kk: 10.13.98: we want O_NONBLOCK so that we return failure immediately if the
    // device is busy (or absent or whatever) rather than blocking.  however, i think that
    // this same O_NONBLOCK flag dictates whether the read() calls should block.  even
    // without the O_NONBLOCK flag set, read() does *not* block for me, so i'm keeping
    // the flag for now....
    HAE_OpenSoundCard(1); // Open for capture

    if (g_waveDevice > 0) {

	int format = AFMT_MU_LAW;
	int stereo = (channels == 2);
	int speed = sampleRate;

	switch (bits) {
	case 8:
	    format = AFMT_MU_LAW;	/* [sbb fix] don't know if this is right or not -- maybe should be s8? */
	    break;

	case 16:
	    format = AFMT_S16_LE;	/* [sbb fix] needs to be conditional? */
	    break;

	//default:
	//    fprintf(stderr, "Warning: unhandled number of data bits %d\n", (int) bits);
	}

	error = ioctl(g_waveDevice, SNDCTL_DSP_SETFMT, &format);

	if (error < 0) {
	    //perror("SNDCTL_DSP_SETFMT");
	    //exit(1);
	}

	error = ioctl(g_waveDevice, SNDCTL_DSP_STEREO, &stereo);

	if (error < 0) {
	    /* [sbb fix] issue some kind of error message */

	    //perror("SNDCTL_DSP_STEREO");
	    //exit(1);
	}

	if (ioctl(g_waveDevice, SNDCTL_DSP_SPEED, &speed) < 0) { /* Fatal error */
	    /* [sbb fix] handle this better */
	    //perror("SNDCTL_DSP_SPEED");
	    // $$ay: dont exit !
	    // exit(1);
	}

	if (speed != (INT32) sampleRate) {
	    /* [sbb fix] need to issue a message */
	}

	if (error == 0) {
	    // flush anything we might have accumulated in the capture queue before pausing
	    HAE_FlushAudioCapture();

	    error = ioctl(g_waveDevice, SNDCTL_DSP_GETBLKSIZE, &g_audioFramesToRead);
	    g_audioFramesToRead = g_audioFramesToRead / (channels * bits / 8);


	    if (error == 0) {
		if (pCaptureHandle) {
		    *pCaptureHandle = (UINT_PTR)g_waveDevice;
		}
	    }
	}
    }

    if (error != 0) {	// something failed
	HAE_ReleaseAudioCapture(context);
    }

    return (error == 0 ? 0 : -1);
}