示例#1
0
static void gviHardwareStopDevice(GVDevice device, GVDeviceType type)
{
	GVIHardwareData * data = (GVIHardwareData *)device->m_data;
	GVICapturedFrame * frame;

	// lock the device
	gviLockDevice(data);

	// capture
	if((type & GV_CAPTURE) && data->m_capturing)
	{
		// unlock so the ioproc can stop
		gviUnlockDevice(data);

		// stop io
		AudioDeviceStop((AudioDeviceID)device->m_deviceID, gviHardwareCaptureIOProc);

		// relock
		gviLockDevice(data);

		// remove the IO proc
		AudioDeviceRemoveIOProc((AudioDeviceID)device->m_deviceID, gviHardwareCaptureIOProc);

		// move captured frames back to the available frames list
		while((frame = gviPopFirstFrame(&data->m_capturedFrames)) != NULL)
			gviPushFirstFrame(&data->m_captureAvailableFrames, frame);

		// no longer capturing
		data->m_capturing = GVFalse;
	}

	// playback
	if((type & GV_PLAYBACK) && data->m_playing)
	{
		// unlock so the ioproc can stop
		gviUnlockDevice(data);

		// stop io
		AudioDeviceStop((AudioDeviceID)device->m_deviceID, gviHardwarePlaybackIOProc);

		// relock
		gviLockDevice(data);

		// remove the IO proc
		AudioDeviceRemoveIOProc((AudioDeviceID)device->m_deviceID, gviHardwarePlaybackIOProc);

		// clear any pending sources & buffers
		gviClearSourceList(data->m_playbackSources);

		// no longer playing
		data->m_playing = GVFalse;
	}

	// unlock the device
	gviUnlockDevice(data);
}
示例#2
0
static void clean_input_device(phastream_t *as) {

	ca_dev *cadev = (ca_dev *) as->drvinfo;

	verify_noerr(AudioDeviceRemoveIOProc(get_audiodeviceid(cadev->inputID), input_proc));

}
示例#3
0
static void coreaudio_fini_out (HWVoiceOut *hw)
{
    OSStatus status;
    int err;
    coreaudioVoiceOut *core = (coreaudioVoiceOut *) hw;

    if (!isAtexit) {
        /* stop playback */
        if (isPlaying(core->outputDeviceID)) {
            status = AudioDeviceStop(core->outputDeviceID, audioDeviceIOProc);
            if (status != kAudioHardwareNoError) {
                coreaudio_logerr (status, "Could not stop playback\n");
            }
        }

        /* remove callback */
        status = AudioDeviceRemoveIOProc(core->outputDeviceID,
                                         audioDeviceIOProc);
        if (status != kAudioHardwareNoError) {
            coreaudio_logerr (status, "Could not remove IOProc\n");
        }
    }
    core->outputDeviceID = kAudioDeviceUnknown;

    /* destroy mutex */
    err = pthread_mutex_destroy(&core->mutex);
    if (err) {
        dolog("Could not destroy mutex\nReason: %s\n", strerror (err));
    }
}
示例#4
0
static void
macosx_close (AUDIO_OUT *audio_out)
{	MACOSX_AUDIO_OUT	*macosx_out ;
	OSStatus	err ;

	if ((macosx_out = (MACOSX_AUDIO_OUT*) audio_out) == NULL)
	{	printf ("macosx_close : AUDIO_OUT is NULL.\n") ;
		return ;
		} ;

	if (macosx_out->magic != MACOSX_MAGIC)
	{	printf ("macosx_close : Bad magic number.\n") ;
		return ;
		} ;


	if ((err = AudioDeviceStop (macosx_out->device, macosx_audio_out_callback)) != noErr)
	{	printf ("AudioDeviceStop failed.\n") ;
		return ;
		} ;

	err = AudioDeviceRemoveIOProc (macosx_out->device, macosx_audio_out_callback) ;
	if (err != noErr)
	{	printf ("AudioDeviceRemoveIOProc failed.\n") ;
		return ;
		} ;

} /* macosx_close */
示例#5
0
static int
ca_free (void) {
    if (device_id) {
        AudioDeviceStop(device_id, ca_buffer_callback);
        AudioDeviceRemovePropertyListener(device_id, 0, 0, kAudioDevicePropertyStreamFormat, ca_fmtchanged);
        AudioDeviceRemoveIOProc(device_id, ca_buffer_callback);
    }
    return 0;
}
示例#6
0
// stop the device attached to a stream.
// 
static int Stream_stop(Stream *s)
{
  AudioDeviceIOProc ioProc= s->direction ? ioProcInput : ioProcOutput;
  checkError(AudioDeviceStop(s->id, ioProc),
	     "DeviceStop", s->direction ? "ioProcIn" : "ioProcOut");
  checkError(AudioDeviceRemoveIOProc(s->id, ioProc),
	     "Remove", s->direction ? "ioProcIn" : "ioProcOut");
  debugf("stream %p[%d] stopped\n", s, s->direction);
  return 1;
}
示例#7
0
enum audio::orchestra::error audio::orchestra::api::Core::closeStream() {
	if (m_state == audio::orchestra::state::closed) {
		ATA_ERROR("no open stream to close!");
		return audio::orchestra::error_warning;
	}
	if (    m_mode == audio::orchestra::mode_output
	     || m_mode == audio::orchestra::mode_duplex) {
		if (m_state == audio::orchestra::state::running) {
			AudioDeviceStop(m_private->id[0], &audio::orchestra::api::Core::callbackEvent);
		}
#if defined(MAC_OS_X_VERSION_10_5) && (MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_5)
		AudioDeviceDestroyIOProcID(m_private->id[0], m_private->procId[0]);
#else
		// deprecated in favor of AudioDeviceDestroyIOProcID()
		AudioDeviceRemoveIOProc(m_private->id[0], &audio::orchestra::api::Core::callbackEvent);
#endif
	}
	if (    m_mode == audio::orchestra::mode_input
	     || (    m_mode == audio::orchestra::mode_duplex
	          && m_device[0] != m_device[1])) {
		if (m_state == audio::orchestra::state::running) {
			AudioDeviceStop(m_private->id[1], &audio::orchestra::api::Core::callbackEvent);
		}
#if defined(MAC_OS_X_VERSION_10_5) && (MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_5)
		AudioDeviceDestroyIOProcID(m_private->id[1], m_private->procId[1]);
#else
		// deprecated in favor of AudioDeviceDestroyIOProcID()
		AudioDeviceRemoveIOProc(m_private->id[1], &audio::orchestra::api::Core::callbackEvent);
#endif
	}
	m_userBuffer[0].clear();
	m_userBuffer[1].clear();
	if (m_deviceBuffer) {
		free(m_deviceBuffer);
		m_deviceBuffer = nullptr;
	}
	m_mode = audio::orchestra::mode_unknow;
	m_state = audio::orchestra::state::closed;
	ATA_VERBOSE("Set state as closed");
	return audio::orchestra::error_none;
}
// return whether we were running
bool	AudioThruEngine::Stop()
{
	if (!mRunning) return false;
	mRunning = false;
	
	mInputProcState = kStopRequested;
	mOutputProcState = kStopRequested;
	
	while (mInputProcState != kOff || mOutputProcState != kOff)
		usleep(5000);

	AudioDeviceRemoveIOProc(mInputDevice.mID, InputIOProc);
	AudioDeviceRemoveIOProc(mOutputDevice.mID, mOutputIOProc);
	
	if (mWorkBuf) {
		delete[] mWorkBuf;
		mWorkBuf = NULL;
	}
	
	return true;
}
示例#9
0
void
audio_output_close(void)
{
	AudioDeviceStop(adid, aprocid);
#ifdef MAC_OS_X_VERSION_10_5
	AudioDeviceDestroyIOProcID(adid, aprocid);
#else /* !MAC_OS_X_VERSION_10_5 */
	AudioDeviceRemoveIOProc(adid, aprocid);
#endif /* MAC_OS_X_VERSION_10_5 */

	g_free(abufnew);
	g_free(abufcur);
}
示例#10
0
// Note: When CloseStream() is called, the multi-api layer ensures that the stream has already been stopped or aborted.
static PaError CloseStream( PaStream* s )
{
    PaError err = paNoError;
    PaMacCoreStream *stream = (PaMacCoreStream*)s;

    PaUtil_ResetCpuLoadMeasurer( &stream->cpuLoadMeasurer );

    if (stream->inputDevice != kAudioDeviceUnknown) {
        if (stream->outputDevice == kAudioDeviceUnknown || stream->outputDevice == stream->inputDevice) {
            err = conv_err(AudioDeviceRemoveIOProc(stream->inputDevice, AudioIOProc));
        }
        else {
            err = conv_err(AudioDeviceRemoveIOProc(stream->inputDevice, AudioInputProc));
            err = conv_err(AudioDeviceRemoveIOProc(stream->outputDevice, AudioOutputProc));
        }
    }
    else {
        err = conv_err(AudioDeviceRemoveIOProc(stream->outputDevice, AudioIOProc));
    }
    
    return err;
}
示例#11
0
void CCoreAudioDevice::RemoveIOProc()
{
  if (!m_DeviceId || !m_IoProc)
    return;
  
  Stop();
  OSStatus ret = AudioDeviceRemoveIOProc(m_DeviceId, m_IoProc);  
  if (ret)
    CLog::Log(LOGERROR, "CCoreAudioDevice::RemoveIOProc: Unable to remove IOProc. Error = 0x%08x (%4.4s).", ret, CONVERT_OSSTATUS(ret));
  else
    CLog::Log(LOGDEBUG, "CCoreAudioDevice::AddIOProc: IOProc removed for device 0x%04x", m_DeviceId);
  m_IoProc = NULL; // Clear the reference no matter what
}
    void stop (bool leaveInterruptRunning)
    {
        {
            const ScopedLock sl (callbackLock);
            callback = nullptr;
        }

        if (started
             && (deviceID != 0)
             && ! leaveInterruptRunning)
        {
            OK (AudioDeviceStop (deviceID, audioIOProc));

           #if MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_5
            OK (AudioDeviceRemoveIOProc (deviceID, audioIOProc));
           #else
            OK (AudioDeviceDestroyIOProcID (deviceID, audioProcID));
            audioProcID = 0;
           #endif

            started = false;

            { const ScopedLock sl (callbackLock); }

            // wait until it's definately stopped calling back..
            for (int i = 40; --i >= 0;)
            {
                Thread::sleep (50);

                UInt32 running = 0;
                UInt32 size = sizeof (running);

                AudioObjectPropertyAddress pa;
                pa.mSelector = kAudioDevicePropertyDeviceIsRunning;
                pa.mScope = kAudioObjectPropertyScopeWildcard;
                pa.mElement = kAudioObjectPropertyElementMaster;

                OK (AudioObjectGetPropertyData (deviceID, &pa, 0, 0, &size, &running));

                if (running == 0)
                    break;
            }

            const ScopedLock sl (callbackLock);
        }

        if (inputDevice != nullptr)
            inputDevice->stop (leaveInterruptRunning);
    }
示例#13
0
/*
==========
idAudioHardwareOSX::Reset
==========
*/
void idAudioHardwareOSX::Reset() {
	OSStatus status;
	if( activeIOProc ) {
		status = AudioDeviceStop( selectedDevice, DeviceIOProc );
		if( status != kAudioHardwareNoError ) {
			common->Warning( "idAudioHardwareOSX::Reset: AudioDeviceStop failed. status: %s", ExtractStatus( status ) );
		}
		status = AudioDeviceRemoveIOProc( selectedDevice, DeviceIOProc );
		if( status != kAudioHardwareNoError ) {
			common->Warning( "idAudioHardwareOSX::Reset: AudioDeviceRemoveIOProc failed. status %s\n", ExtractStatus( status ) );
		}
		activeIOProc = false;
	}
	selectedDevice = kAudioDeviceUnknown;
	AudioHardwareUnload();
}
示例#14
0
static void close_output(void)
{
    OSStatus 	err = 0;
    
    err = AudioDeviceStop(globals.device, appIOProc);
                            // stop playing sound through the device
    if (err != 0) goto Bail;

    err = AudioDeviceRemoveIOProc(globals.device, appIOProc);
                       // remove the IO proc from the device
    if (err != 0) goto Bail;
    
    globals.soundPlaying = 0;
                    // set the playing status global to false
Bail:;
}
CDeviceCoreAudio::~CDeviceCoreAudio()
{
	AudioDeviceStop(mAudioDeviceID, HALIOProc);
	AudioDeviceRemoveIOProc(mAudioDeviceID, HALIOProc);

	if (mppfInputs) {
		delete[] mppfInputs;
	}
	if (mppfOutputs) {
		delete[] mppfOutputs;
	}

	for (int i = 0; i < COREAUDIO_MAX_INPUT_CHANNELS; i++) {
		if (mppfInputTmp[i]) {
			delete[] mppfInputTmp[i];
		}
	}
} // destructor
示例#16
0
// start the device attached to the stream.
// 
static int Stream_startSema(Stream *s, int semaIndex)
{
  AudioDeviceIOProc ioProc= s->direction ? ioProcInput : ioProcOutput;

  debugf("stream %p[%d] startSema: %d\n", s, s->direction, semaIndex);
  
  s->semaphore= semaIndex;	// can be zero
  if (checkError(AudioDeviceAddIOProc(s->id, ioProc, (void *)s),
		 "Add", "ioProcOut"))
    return 0;
  if (checkError(AudioDeviceStart(s->id, ioProc),
		 "DeviceStart", "ioProcOut"))
    {
      AudioDeviceRemoveIOProc(s->id, ioProc);
      return 0;
    }
  debugf("stream %p[%d] running\n", s, s->direction);
  return 1;
}
示例#17
0
文件: au.c 项目: brezniczky/audio
static int audiounits_close(void *usr) {
	au_instance_t *p = (au_instance_t*) usr;
	p->done = YES;
	/* Rprintf(" closing audiounit %p\n", usr); */
	if (p->outUnit) {
		AudioOutputUnitStop(p->outUnit);
		AudioUnitUninitialize(p->outUnit);
		CloseComponent(p->outUnit);
		p->outUnit = 0;
	}
	if (p->inDev) {
#if defined(MAC_OS_X_VERSION_10_5) && (MAC_OS_X_VERSION_MIN_REQUIRED>=MAC_OS_X_VERSION_10_5)
		AudioDeviceDestroyIOProcID(p->inDev, p->inIOProcID);
#else
		AudioDeviceRemoveIOProc(p->inDev, inputRenderProc);
#endif
		p->inDev = 0;
	}
	return 1;
}
void CoreAudioOutDriver::close()
{
  if (!this->is_open())
    return;

  m_impl->m_logger(2, "Closing audio...");

  if (!this->is_open())
    return;

  m_impl->stop_audio();
	
  while (m_impl->m_outputProcState != Impl::kOff)
    usleep(5000);
	
  AudioDeviceRemoveIOProc(m_impl->m_ID, Impl::OutputIOProc);

  m_impl->m_ID = kAudioDeviceUnknown;
  delete m_impl->m_converter;
  m_impl->m_converter = 0;
}
示例#19
0
void macosx_audio_close(audio_desc_t ad)
{
	OSStatus err = noErr;
	// Stop the audio devices.
	err = AudioDeviceStop(devices[ad].inputDeviceID_, audioIOProc);
	if (err != noErr) fprintf(stderr, "Input device error: AudioDeviceStop\n");
	err = AudioOutputUnitStop(devices[ad].outputUnit_);
	if (err != noErr) fprintf(stderr, "Output device error: AudioOutputUnitStop\n");
	// Unregister the AudioDeviceIOProc.
#if defined(MAC_OS_X_VERSION_10_5) && (MAC_OS_X_VERSION_MIN_REQUIRED>=MAC_OS_X_VERSION_10_5)
	err = AudioDeviceDestroyIOProcID(devices[ad].inputDeviceID_, devices[ad].inputDeviceProcID_);
	if (err != noErr) fprintf(stderr, "Input device error: AudioDeviceDestroyIOProcID\n");
#else
	// deprecated in favor of AudioDeviceDestroyIOProcID();
	err = AudioDeviceRemoveIOProc(devices[ad].inputDeviceID_, audioIOProc);
	if (err != noErr) fprintf(stderr, "Input device error: AudioDeviceRemoveIOProc\n");
#endif
	CloseComponent(devices[ad].outputUnit_);
	if (readBuffer_ != NULL) free(readBuffer_); readBuffer_ = NULL;
	if (writeBuffer_ != NULL) free(writeBuffer_); writeBuffer_ = NULL;
	if (zLine_ != NULL) free(zLine_); zLine_ = NULL;
};
示例#20
0
int audio_close(error_t *error) {
  int ret;
  if (audio_started) {
    ret = AudioDeviceStop(audio.device, audio_play_proc);
    if (ret) {
      error_set(error, "Could not stop audio playback");
      return 0;
    }
    audio_started = 0;
  }
  if (audio_initialized) {
    ret = AudioDeviceRemoveIOProc(audio.device, audio_play_proc);
    if (ret) {
      error_set(error, "Could not remove the IOProc");
      return 0;
    }
    audio_initialized = 0;
  }
  rb_destroy(&audio.rb);

  return 1;
}
    bool start (AudioIODeviceCallback* cb)
    {
        if (! started)
        {
            callback = nullptr;

            if (deviceID != 0)
            {
               #if MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_5
                if (OK (AudioDeviceAddIOProc (deviceID, audioIOProc, this)))
               #else
                if (OK (AudioDeviceCreateIOProcID (deviceID, audioIOProc, this, &audioProcID)))
               #endif
                {
                    if (OK (AudioDeviceStart (deviceID, audioIOProc)))
                    {
                        started = true;
                    }
                    else
                    {
                       #if MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_5
                        OK (AudioDeviceRemoveIOProc (deviceID, audioIOProc));
                       #else
                        OK (AudioDeviceDestroyIOProcID (deviceID, audioProcID));
                        audioProcID = 0;
                       #endif
                    }
                }
            }
        }

        if (started)
        {
            const ScopedLock sl (callbackLock);
            callback = cb;
        }

        return started && (inputDevice == nullptr || inputDevice->start (cb));
    }
示例#22
0
文件: coreaudio.c 项目: 0-14N/NDroid
static void
coreaudio_voice_fini (coreaudioVoice*  core)
{
    OSStatus status;
    int err;

    if (!conf.isAtexit) {
        /* stop playback */
        coreaudio_voice_ctl(core, VOICE_DISABLE);

        /* remove callback */
        status = AudioDeviceRemoveIOProc(core->deviceID, core->ioproc);
        if (status != kAudioHardwareNoError) {
            coreaudio_logerr (status, "Could not remove IOProc\n");
        }
    }
    core->deviceID = kAudioDeviceUnknown;

    /* destroy mutex */
    err = pthread_mutex_destroy(&core->mutex);
    if (err) {
        dolog("Could not destroy mutex\nReason: %s\n", strerror (err));
    }
}
示例#23
0
// Cleanup the BlockSound class
BlockSound::~BlockSound() {
#ifdef __APPLE__
	if (sample_size) {
#  if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5
		AudioDeviceStop(device, audio_proc_id);
		AudioDeviceDestroyIOProcID(device, audio_proc_id);
#  else
		AudioDeviceStop(device, audio_cb);
		AudioDeviceRemoveIOProc(device, audio_cb);
#  endif
	}

#elif defined(WIN32)
	if (sample_size) {
		waveOutClose(device);

		GlobalUnlock(header_handle);
		GlobalFree(header_handle);

		GlobalUnlock(data_handle);
		GlobalFree(data_handle);
	}

#else
#  ifdef HAVE_ALSA_ASOUNDLIB_H
	if (handle) {
		snd_pcm_drain(handle);
		snd_pcm_close(handle);
	}
#  endif // HAVE_ALSA_ASOUNDLIB_H
#endif // __APPLE__

	if (sample_size) {
		delete[] sample_data;
	}
}
示例#24
0
文件: coreaudio.c 项目: 0-14N/NDroid
static int
coreaudio_voice_init (coreaudioVoice*    core,
                      struct audsettings*  as,
                      int                frameSize,
                      AudioDeviceIOProc  ioproc,
                      void*              hw,
                      int                input)
{
    OSStatus  status;
    UInt32    propertySize;
    int       err;
    int       bits = 8;
    AudioValueRange frameRange;
    const char*  typ = input ? "input" : "playback";

    core->isInput = input ? true : false;

    /* create mutex */
    err = pthread_mutex_init(&core->mutex, NULL);
    if (err) {
        dolog("Could not create mutex\nReason: %s\n", strerror (err));
        return -1;
    }

    if (as->fmt == AUD_FMT_S16 || as->fmt == AUD_FMT_U16) {
        bits = 16;
    }

    // TODO: audio_pcm_init_info (&hw->info, as);
    /* open default output device */
   /* note: we use DefaultSystemOutputDevice because DefaultOutputDevice seems to
    * always link to the internal speakers, and not the ones selected through system properties
    * go figure...
    */
    propertySize = sizeof(core->deviceID);
    status = AudioHardwareGetProperty(
        input ? kAudioHardwarePropertyDefaultInputDevice :
                kAudioHardwarePropertyDefaultSystemOutputDevice,
        &propertySize,
        &core->deviceID);
    if (status != kAudioHardwareNoError) {
        coreaudio_logerr2 (status, typ,
                           "Could not get default %s device\n", typ);
        return -1;
    }
    if (core->deviceID == kAudioDeviceUnknown) {
        dolog ("Could not initialize %s - Unknown Audiodevice\n", typ);
        return -1;
    }

    /* get minimum and maximum buffer frame sizes */
    propertySize = sizeof(frameRange);
    status = AudioDeviceGetProperty(
        core->deviceID,
        0,
        core->isInput,
        kAudioDevicePropertyBufferFrameSizeRange,
        &propertySize,
        &frameRange);
    if (status != kAudioHardwareNoError) {
        coreaudio_logerr2 (status, typ,
                           "Could not get device buffer frame range\n");
        return -1;
    }

    if (frameRange.mMinimum > frameSize) {
        core->bufferFrameSize = (UInt32) frameRange.mMinimum;
        dolog ("warning: Upsizing Output Buffer Frames to %f\n", frameRange.mMinimum);
    }
    else if (frameRange.mMaximum < frameSize) {
        core->bufferFrameSize = (UInt32) frameRange.mMaximum;
        dolog ("warning: Downsizing Output Buffer Frames to %f\n", frameRange.mMaximum);
    }
    else {
        core->bufferFrameSize = frameSize;
    }

    /* set Buffer Frame Size */
    propertySize = sizeof(core->bufferFrameSize);
    status = AudioDeviceSetProperty(
        core->deviceID,
        NULL,
        0,
        core->isInput,
        kAudioDevicePropertyBufferFrameSize,
        propertySize,
        &core->bufferFrameSize);
    if (status != kAudioHardwareNoError) {
        coreaudio_logerr2 (status, typ,
                           "Could not set device buffer frame size %ld\n",
                           core->bufferFrameSize);
        return -1;
    }

    /* get Buffer Frame Size */
    propertySize = sizeof(core->bufferFrameSize);
    status = AudioDeviceGetProperty(
        core->deviceID,
        0,
        core->isInput,
        kAudioDevicePropertyBufferFrameSize,
        &propertySize,
        &core->bufferFrameSize);
    if (status != kAudioHardwareNoError) {
        coreaudio_logerr2 (status, typ,
                           "Could not get device buffer frame size\n");
        return -1;
    }
    // TODO: hw->samples = *pNBuffers * core->bufferFrameSize;

    /* get StreamFormat */
    propertySize = sizeof(core->streamBasicDescription);
    status = AudioDeviceGetProperty(
        core->deviceID,
        0,
        core->isInput,
        kAudioDevicePropertyStreamFormat,
        &propertySize,
        &core->streamBasicDescription);
    if (status != kAudioHardwareNoError) {
        coreaudio_logerr2 (status, typ,
                           "Could not get Device Stream properties\n");
        core->deviceID = kAudioDeviceUnknown;
        return -1;
    }

    /* set Samplerate */
    core->streamBasicDescription.mSampleRate = (Float64) as->freq;
    propertySize = sizeof(core->streamBasicDescription);
    status = AudioDeviceSetProperty(
        core->deviceID,
        0,
        0,
        core->isInput,
        kAudioDevicePropertyStreamFormat,
        propertySize,
        &core->streamBasicDescription);
    if (status != kAudioHardwareNoError) {
        coreaudio_logerr2 (status, typ, "Could not set samplerate %d\n",
                           as->freq);
        core->deviceID = kAudioDeviceUnknown;
        return -1;
    }

    /* set Callback */
    core->ioproc = ioproc;
    status = AudioDeviceAddIOProc(core->deviceID, ioproc, hw);
    if (status != kAudioHardwareNoError) {
        coreaudio_logerr2 (status, typ, "Could not set IOProc\n");
        core->deviceID = kAudioDeviceUnknown;
        return -1;
    }

    /* start Playback */
    if (!input && !coreaudio_voice_isPlaying(core)) {
        status = AudioDeviceStart(core->deviceID, core->ioproc);
        if (status != kAudioHardwareNoError) {
            coreaudio_logerr2 (status, typ, "Could not start playback\n");
            AudioDeviceRemoveIOProc(core->deviceID, core->ioproc);
            core->deviceID = kAudioDeviceUnknown;
            return -1;
        }
    }

    return 0;
}
示例#25
0
static int coreaudio_init_out(HWVoiceOut *hw, struct audsettings *as,
                              void *drv_opaque)
{
    OSStatus status;
    coreaudioVoiceOut *core = (coreaudioVoiceOut *) hw;
    UInt32 propertySize;
    int err;
    const char *typ = "playback";
    AudioValueRange frameRange;
    CoreaudioConf *conf = drv_opaque;

    /* create mutex */
    err = pthread_mutex_init(&core->mutex, NULL);
    if (err) {
        dolog("Could not create mutex\nReason: %s\n", strerror (err));
        return -1;
    }

    audio_pcm_init_info (&hw->info, as);

    /* open default output device */
    propertySize = sizeof(core->outputDeviceID);
    status = AudioHardwareGetProperty(
        kAudioHardwarePropertyDefaultOutputDevice,
        &propertySize,
        &core->outputDeviceID);
    if (status != kAudioHardwareNoError) {
        coreaudio_logerr2 (status, typ,
                           "Could not get default output Device\n");
        return -1;
    }
    if (core->outputDeviceID == kAudioDeviceUnknown) {
        dolog ("Could not initialize %s - Unknown Audiodevice\n", typ);
        return -1;
    }

    /* get minimum and maximum buffer frame sizes */
    propertySize = sizeof(frameRange);
    status = AudioDeviceGetProperty(
        core->outputDeviceID,
        0,
        0,
        kAudioDevicePropertyBufferFrameSizeRange,
        &propertySize,
        &frameRange);
    if (status != kAudioHardwareNoError) {
        coreaudio_logerr2 (status, typ,
                           "Could not get device buffer frame range\n");
        return -1;
    }

    if (frameRange.mMinimum > conf->buffer_frames) {
        core->audioDevicePropertyBufferFrameSize = (UInt32) frameRange.mMinimum;
        dolog ("warning: Upsizing Buffer Frames to %f\n", frameRange.mMinimum);
    }
    else if (frameRange.mMaximum < conf->buffer_frames) {
        core->audioDevicePropertyBufferFrameSize = (UInt32) frameRange.mMaximum;
        dolog ("warning: Downsizing Buffer Frames to %f\n", frameRange.mMaximum);
    }
    else {
        core->audioDevicePropertyBufferFrameSize = conf->buffer_frames;
    }

    /* set Buffer Frame Size */
    propertySize = sizeof(core->audioDevicePropertyBufferFrameSize);
    status = AudioDeviceSetProperty(
        core->outputDeviceID,
        NULL,
        0,
        false,
        kAudioDevicePropertyBufferFrameSize,
        propertySize,
        &core->audioDevicePropertyBufferFrameSize);
    if (status != kAudioHardwareNoError) {
        coreaudio_logerr2 (status, typ,
                           "Could not set device buffer frame size %" PRIu32 "\n",
                           (uint32_t)core->audioDevicePropertyBufferFrameSize);
        return -1;
    }

    /* get Buffer Frame Size */
    propertySize = sizeof(core->audioDevicePropertyBufferFrameSize);
    status = AudioDeviceGetProperty(
        core->outputDeviceID,
        0,
        false,
        kAudioDevicePropertyBufferFrameSize,
        &propertySize,
        &core->audioDevicePropertyBufferFrameSize);
    if (status != kAudioHardwareNoError) {
        coreaudio_logerr2 (status, typ,
                           "Could not get device buffer frame size\n");
        return -1;
    }
    hw->samples = conf->nbuffers * core->audioDevicePropertyBufferFrameSize;

    /* get StreamFormat */
    propertySize = sizeof(core->outputStreamBasicDescription);
    status = AudioDeviceGetProperty(
        core->outputDeviceID,
        0,
        false,
        kAudioDevicePropertyStreamFormat,
        &propertySize,
        &core->outputStreamBasicDescription);
    if (status != kAudioHardwareNoError) {
        coreaudio_logerr2 (status, typ,
                           "Could not get Device Stream properties\n");
        core->outputDeviceID = kAudioDeviceUnknown;
        return -1;
    }

    /* set Samplerate */
    core->outputStreamBasicDescription.mSampleRate = (Float64) as->freq;
    propertySize = sizeof(core->outputStreamBasicDescription);
    status = AudioDeviceSetProperty(
        core->outputDeviceID,
        0,
        0,
        0,
        kAudioDevicePropertyStreamFormat,
        propertySize,
        &core->outputStreamBasicDescription);
    if (status != kAudioHardwareNoError) {
        coreaudio_logerr2 (status, typ, "Could not set samplerate %d\n",
                           as->freq);
        core->outputDeviceID = kAudioDeviceUnknown;
        return -1;
    }

    /* set Callback */
    status = AudioDeviceAddIOProc(core->outputDeviceID, audioDeviceIOProc, hw);
    if (status != kAudioHardwareNoError) {
        coreaudio_logerr2 (status, typ, "Could not set IOProc\n");
        core->outputDeviceID = kAudioDeviceUnknown;
        return -1;
    }

    /* start Playback */
    if (!isPlaying(core->outputDeviceID)) {
        status = AudioDeviceStart(core->outputDeviceID, audioDeviceIOProc);
        if (status != kAudioHardwareNoError) {
            coreaudio_logerr2 (status, typ, "Could not start playback\n");
            AudioDeviceRemoveIOProc(core->outputDeviceID, audioDeviceIOProc);
            core->outputDeviceID = kAudioDeviceUnknown;
            return -1;
        }
    }

    return 0;
}
示例#26
0
static void
macosx_play (int argc, char *argv [])
{	MacOSXAudioData 	audio_data ;
	OSStatus	err ;
	UInt32		count, buffer_size ;
	int 		k ;

	audio_data.fake_stereo = 0 ;
	audio_data.device = kAudioDeviceUnknown ;

	/*  get the default output device for the HAL */
	count = sizeof (AudioDeviceID) ;
	if ((err = AudioHardwareGetProperty (kAudioHardwarePropertyDefaultOutputDevice,
				&count, (void *) &(audio_data.device))) != noErr)
	{	printf ("AudioHardwareGetProperty (kAudioDevicePropertyDefaultOutputDevice) failed.\n") ;
		return ;
		} ;

	/*  get the buffersize that the default device uses for IO */
	count = sizeof (UInt32) ;
	if ((err = AudioDeviceGetProperty (audio_data.device, 0, false, kAudioDevicePropertyBufferSize,
				&count, &buffer_size)) != noErr)
	{	printf ("AudioDeviceGetProperty (kAudioDevicePropertyBufferSize) failed.\n") ;
		return ;
		} ;

	/*  get a description of the data format used by the default device */
	count = sizeof (AudioStreamBasicDescription) ;
	if ((err = AudioDeviceGetProperty (audio_data.device, 0, false, kAudioDevicePropertyStreamFormat,
				&count, &(audio_data.format))) != noErr)
	{	printf ("AudioDeviceGetProperty (kAudioDevicePropertyStreamFormat) failed.\n") ;
		return ;
		} ;

	/* Base setup completed. Now play files. */
	for (k = 1 ; k < argc ; k++)
	{	printf ("Playing %s\n", argv [k]) ;
		if (! (audio_data.sndfile = sf_open (argv [k], SFM_READ, &(audio_data.sfinfo))))
		{	puts (sf_strerror (NULL)) ;
			continue ;
			} ;

		if (audio_data.sfinfo.channels < 1 || audio_data.sfinfo.channels > 2)
		{	printf ("Error : channels = %d.\n", audio_data.sfinfo.channels) ;
			continue ;
			} ;

		audio_data.format.mSampleRate = audio_data.sfinfo.samplerate ;

		if (audio_data.sfinfo.channels == 1)
		{	audio_data.format.mChannelsPerFrame = 2 ;
			audio_data.fake_stereo = 1 ;
			}
		else
		audio_data.format.mChannelsPerFrame = audio_data.sfinfo.channels ;

		if ((err = AudioDeviceSetProperty (audio_data.device, NULL, 0, false, kAudioDevicePropertyStreamFormat,
					sizeof (AudioStreamBasicDescription), &(audio_data.format))) != noErr)
		{	printf ("AudioDeviceSetProperty (kAudioDevicePropertyStreamFormat) failed.\n") ;
			return ;
			} ;

		/*  we want linear pcm */
		if (audio_data.format.mFormatID != kAudioFormatLinearPCM)
			return ;

		/* Fire off the device. */
		if ((err = AudioDeviceAddIOProc (audio_data.device, macosx_audio_out_callback,
				(void *) &audio_data)) != noErr)
		{	printf ("AudioDeviceAddIOProc failed.\n") ;
			return ;
			} ;

		err = AudioDeviceStart (audio_data.device, macosx_audio_out_callback) ;
		if	(err != noErr)
			return ;

		audio_data.done_playing = SF_FALSE ;

		while (audio_data.done_playing == SF_FALSE)
			usleep (10 * 1000) ; /* 10 000 milliseconds. */

		if ((err = AudioDeviceStop (audio_data.device, macosx_audio_out_callback)) != noErr)
		{	printf ("AudioDeviceStop failed.\n") ;
			return ;
			} ;

		err = AudioDeviceRemoveIOProc (audio_data.device, macosx_audio_out_callback) ;
		if (err != noErr)
		{	printf ("AudioDeviceRemoveIOProc failed.\n") ;
			return ;
			} ;

		sf_close (audio_data.sndfile) ;
		} ;

	return ;
} /* macosx_play */