void Init() { PriorityMap const& modules = _sorted_modules(); for (auto const& value : modules) { struct module_info *m = value.second; try { m->init(); } catch (...) { Priority p = value.first; char buf[1024]; snprintf(buf, sizeof buf, "fail to init module[%s seq(%d) order(%d)]", m->name, p.Seq, p.Order); std::throw_with_nested(InitFailed(buf)); } } }
/* ===================== idAudioHardwareALSA::Initialize ===================== */ bool idAudioHardwareALSA::Initialize( void ) { int err; common->Printf( "------ Alsa Sound Initialization -----\n" ); if ( !DLOpen() ) { InitFailed(); return false; } if ( ( err = id_snd_pcm_open( &m_pcm_handle, s_alsa_pcm.GetString(), SND_PCM_STREAM_PLAYBACK, SND_PCM_NONBLOCK ) ) < 0 ) { common->Printf( "snd_pcm_open SND_PCM_STREAM_PLAYBACK '%s' failed: %s\n", s_alsa_pcm.GetString(), id_snd_strerror( err ) ); InitFailed(); return false; } common->Printf( "opened Alsa PCM device %s for playback\n", s_alsa_pcm.GetString() ); // set hardware parameters ---------------------------------------------------------------------- // init hwparams with the full configuration space snd_pcm_hw_params_t *hwparams; // this one is a define id_snd_pcm_hw_params_alloca( &hwparams ); if ( ( err = id_snd_pcm_hw_params_any( m_pcm_handle, hwparams ) ) < 0 ) { common->Printf( "cannot configure the PCM device: %s\n", id_snd_strerror( err ) ); InitFailed(); return false; } if ( ( err = id_snd_pcm_hw_params_set_access( m_pcm_handle, hwparams, SND_PCM_ACCESS_RW_INTERLEAVED ) ) < 0 ) { common->Printf( "SND_PCM_ACCESS_RW_INTERLEAVED failed: %s\n", id_snd_strerror( err ) ); InitFailed(); return false; } if ( ( err = id_snd_pcm_hw_params_set_format( m_pcm_handle, hwparams, SND_PCM_FORMAT_S16_LE ) ) < 0 ) { common->Printf( "SND_PCM_FORMAT_S16_LE failed: %s\n", id_snd_strerror( err ) ); InitFailed(); return false; } // channels // sanity over number of speakers if ( idSoundSystemLocal::s_numberOfSpeakers.GetInteger() != 6 && idSoundSystemLocal::s_numberOfSpeakers.GetInteger() != 2 ) { common->Warning( "invalid value for s_numberOfSpeakers. Use either 2 or 6" ); idSoundSystemLocal::s_numberOfSpeakers.SetInteger( 2 ); } m_channels = idSoundSystemLocal::s_numberOfSpeakers.GetInteger(); if ( ( err = id_snd_pcm_hw_params_set_channels( m_pcm_handle, hwparams, m_channels ) ) < 0 ) { common->Printf( "error setting %d channels: %s\n", m_channels, id_snd_strerror( err ) ); if ( idSoundSystemLocal::s_numberOfSpeakers.GetInteger() != 2 ) { // fallback to stereo if that works m_channels = 2; if ( ( err = id_snd_pcm_hw_params_set_channels( m_pcm_handle, hwparams, m_channels ) ) < 0 ) { common->Printf( "fallback to stereo failed: %s\n", id_snd_strerror( err ) ); InitFailed(); return false; } else { common->Printf( "fallback to stereo\n" ); idSoundSystemLocal::s_numberOfSpeakers.SetInteger( 2 ); } } else { InitFailed(); return false; } } // set sample rate (frequency) if ( ( err = id_snd_pcm_hw_params_set_rate( m_pcm_handle, hwparams, PRIMARYFREQ, 0 ) ) < 0 ) { common->Printf( "failed to set 44.1KHz rate: %s - try ( +set s_alsa_pcm plughw:0 ? )\n", id_snd_strerror( err ) ); InitFailed(); return false; } // have enough space in the input buffer for our MIXBUFFER_SAMPLE feedings and async ticks snd_pcm_uframes_t frames; frames = MIXBUFFER_SAMPLES + MIXBUFFER_SAMPLES / 3; if ( ( err = id_snd_pcm_hw_params_set_buffer_size_min( m_pcm_handle, hwparams, &frames ) ) < 0 ) { common->Printf( "buffer size select failed: %s\n", id_snd_strerror( err ) ); InitFailed(); return false; } // apply parameters if ( ( err = id_snd_pcm_hw_params( m_pcm_handle, hwparams ) ) < 0 ) { common->Printf( "snd_pcm_hw_params failed: %s\n", id_snd_strerror( err ) ); InitFailed(); return false; } // check the buffer size if ( ( err = id_snd_pcm_hw_params_get_buffer_size( hwparams, &frames ) ) < 0 ) { common->Printf( "snd_pcm_hw_params_get_buffer_size failed: %s\n", id_snd_strerror( err ) ); } else { common->Printf( "device buffer size: %lu frames ( %lu bytes )\n", ( long unsigned int )frames, frames * m_channels * 2 ); } // TODO: can use swparams to setup the device so it doesn't underrun but rather loops over // snd_pcm_sw_params_set_stop_threshold // To get alsa to just loop on underruns. set the swparam stop_threshold to equal buffer size. The sound buffer will just loop and never throw an xrun. // allocate the final mix buffer m_buffer_size = MIXBUFFER_SAMPLES * m_channels * 2; m_buffer = malloc( m_buffer_size ); common->Printf( "allocated a mix buffer of %d bytes\n", m_buffer_size ); #ifdef _DEBUG // verbose the state snd_pcm_state_t curstate = id_snd_pcm_state( m_pcm_handle ); assert( curstate == SND_PCM_STATE_PREPARED ); #endif common->Printf( "--------------------------------------\n" ); return true; }
BOOL BitmapExportDocument::Init(KernelBitmap* pBitmap, const DocRect& RectToExport) { // Don't attach any CCamDoc. if (!Document::Init(0)) return(FALSE); Node *pSpread = FindFirstSpread(); ERROR3IF(pSpread == NULL, "No Spread in document!"); Node *pLayer = pSpread->FindFirstChild(); ERROR3IF(pLayer == NULL, "No Spread-child in document!"); // Store away the rectangle ExportRect = RectToExport; // Now scan the children of the first spread until we find a layer, or run out of nodes while (pLayer != NULL && !pLayer->IsLayer()) pLayer = pLayer->FindNext(); if (pLayer == NULL) // No Layer, so we'd better add one for ourselves { String_256 LayerID = String_256(_R(IDS_K_CLIPINT_LAYERNAME)); pLayer = new Layer(pSpread, LASTCHILD, LayerID); if (pLayer == NULL) return(InitFailed()); } // Create a new NodeRect NodeRect* pRectNode = new NodeRect(pLayer, FIRSTCHILD); // Failed so cleanup and exit if (pRectNode == NULL) return(InitFailed()); // Initilaise the node if (!pRectNode->SetUpPath(6,6)) return(InitFailed()); // Create the rectangle pRectNode->CreateShape(ExportRect); // Give the rectangle a line colour #if 0 // This memory leaks a StrokeColourAttribute StrokeColourAttribute* pAttrValue = new StrokeColourAttribute(DocColour(COLOUR_TRANS)); if (pAttrValue == NULL) return(InitFailed()); NodeAttribute* pAttr = pAttrValue->MakeNode(); if (pAttr == NULL) return(InitFailed()); // Attach the attribute to the rectangle pAttr->AttachNode(pRectNode, FIRSTCHILD); #else // Do what ApplyDefaultBitmapAttrs does Node* pLineColAttr = new AttrStrokeColour(); if (pLineColAttr == NULL) return(InitFailed()); DocColour none(COLOUR_NONE); ((AttrFillGeometry*)pLineColAttr)->SetStartColour(&none); pLineColAttr->AttachNode(pRectNode, FIRSTCHILD); #endif // Create a NodeBitmap (don't attach it to the tree straight away 'cos we // have to put the attributes on it first) pBitmapNode = new NodeBitmap(); if (pBitmapNode == NULL) return(InitFailed()); if (!pBitmapNode->SetUpPath(6,6)) return(InitFailed()); pBitmapNode->CreateShape(ExportRect); if (!SetBitmap(pBitmap)) return(InitFailed()); // Set the bitmap's attributes // This must be done before the NodeBitmap is inserted into the tree if (!pBitmapNode->ApplyDefaultBitmapAttrs(NULL)) return(InitFailed()); // Attach it to the tree as the next sibling of the rectangle pBitmapNode->AttachNode(pRectNode, NEXT); // Success... return(TRUE); }
/* ========== idAudioHardwareOSX::Initialize ========== */ bool idAudioHardwareOSX::Initialize( ) { UInt32 size; OSStatus status; int i, deviceCount; AudioDeviceID *deviceList; char buf[ 1024 ]; status = AudioHardwareGetPropertyInfo( kAudioHardwarePropertyDevices, &size, NULL ); if ( status != kAudioHardwareNoError ) { common->Warning( "AudioHardwareGetPropertyInfo kAudioHardwarePropertyDevices failed. status: %s", ExtractStatus( status ) ); InitFailed(); return false; } deviceCount = size / sizeof( AudioDeviceID ); if ( !deviceCount ) { common->Printf( "No sound device found\n" ); InitFailed(); return false; } deviceList = (AudioDeviceID*)malloc( size ); status = AudioHardwareGetProperty( kAudioHardwarePropertyDevices, &size, deviceList ); if ( status != kAudioHardwareNoError ) { common->Warning( "AudioHardwareGetProperty kAudioHardwarePropertyDevices failed. status: %s", ExtractStatus( status ) ); free( deviceList ); InitFailed(); return false; } common->Printf( "%d sound device(s)\n", deviceCount ); for( i = 0; i < deviceCount; i++ ) { size = 1024; status = AudioDeviceGetProperty( deviceList[ i ], 0, false, kAudioDevicePropertyDeviceName, &size, buf ); if ( status != kAudioHardwareNoError ) { common->Warning( "AudioDeviceGetProperty kAudioDevicePropertyDeviceName %d failed. status: %s", i, ExtractStatus( status ) ); free( deviceList ); InitFailed(); return false; } common->Printf( " %d: ID %d, %s - ", i, deviceList[ i ], buf ); size = 1024; status = AudioDeviceGetProperty( deviceList[ i ], 0, false, kAudioDevicePropertyDeviceManufacturer, &size, buf ); if ( status != kAudioHardwareNoError ) { common->Warning( "AudioDeviceGetProperty kAudioDevicePropertyDeviceManufacturer %d failed. status: %s", i, ExtractStatus( status ) ); free( deviceList ); InitFailed(); return false; } common->Printf( "%s\n", buf ); } if ( s_device.GetInteger() != -1 && s_device.GetInteger() < deviceCount ) { selectedDevice = deviceList[ s_device.GetInteger() ]; common->Printf( "s_device: device ID %d\n", selectedDevice ); } else { size = sizeof( selectedDevice ); status = AudioHardwareGetProperty( kAudioHardwarePropertyDefaultOutputDevice, &size, &selectedDevice ); if ( status != kAudioHardwareNoError ) { common->Warning( "AudioHardwareGetProperty kAudioHardwarePropertyDefaultOutputDevice failed. status: %s", ExtractStatus( status ) ); free( deviceList ); InitFailed(); return false; } common->Printf( "select default device, ID %d\n", selectedDevice ); } free( deviceList ); deviceList = NULL; /* // setup a listener to watch for changes to properties status = AudioDeviceAddPropertyListener( selectedDevice, 0, false, kAudioDeviceProcessorOverload, DeviceListener, this ); if ( status != kAudioHardwareNoError ) { common->Warning( "AudioDeviceAddPropertyListener kAudioDeviceProcessorOverload failed. status: %s", ExtractStatus( status ) ); InitFailed(); return; } */ Float64 sampleRate; size = sizeof( sampleRate ); status = AudioDeviceGetProperty( selectedDevice, 0, false, kAudioDevicePropertyNominalSampleRate, &size, &sampleRate ); if ( status != kAudioHardwareNoError ) { common->Warning( "AudioDeviceGetProperty %d kAudioDevicePropertyNominalSampleRate failed. status: %s", selectedDevice, ExtractStatus( status ) ); InitFailed(); return false; } common->Printf( "current nominal rate: %g\n", sampleRate ); if ( sampleRate != PRIMARYFREQ ) { GetAvailableNominalSampleRates(); sampleRate = PRIMARYFREQ; common->Printf( "setting rate to: %g\n", sampleRate ); status = AudioDeviceSetProperty( selectedDevice, NULL, 0, false, kAudioDevicePropertyNominalSampleRate, size, &sampleRate ); if ( status != kAudioHardwareNoError ) { common->Warning( "AudioDeviceSetProperty %d kAudioDevicePropertyNominalSampleRate %g failed. status: %s", selectedDevice, sampleRate, ExtractStatus( status ) ); InitFailed(); return false; } } UInt32 frameSize; size = sizeof( UInt32 ); status = AudioDeviceGetProperty( selectedDevice, 0, false, kAudioDevicePropertyBufferFrameSize, &size, &frameSize ); if ( status != kAudioHardwareNoError ) { common->Warning( "AudioDeviceGetProperty %d kAudioDevicePropertyBufferFrameSize failed.status: %s", selectedDevice, ExtractStatus( status ) ); InitFailed(); return false; } common->Printf( "current frame size: %d\n", frameSize ); // get the allowed frame size range AudioValueRange frameSizeRange; size = sizeof( AudioValueRange ); status = AudioDeviceGetProperty( selectedDevice, 0, false, kAudioDevicePropertyBufferFrameSizeRange, &size, &frameSizeRange ); if ( status != kAudioHardwareNoError ) { common->Warning( "AudioDeviceGetProperty %d kAudioDevicePropertyBufferFrameSizeRange failed. status: %s", selectedDevice, ExtractStatus( status ) ); InitFailed(); return false; } common->Printf( "frame size allowed range: %g %g\n", frameSizeRange.mMinimum, frameSizeRange.mMaximum ); if ( frameSizeRange.mMaximum < MIXBUFFER_SAMPLES ) { common->Warning( "can't obtain the required frame size of %d bits", MIXBUFFER_SAMPLES ); InitFailed(); return false; } if ( frameSize != (unsigned int)MIXBUFFER_SAMPLES ) { frameSize = MIXBUFFER_SAMPLES; common->Printf( "setting frame size to: %d\n", frameSize ); size = sizeof( frameSize ); status = AudioDeviceSetProperty( selectedDevice, NULL, 0, false, kAudioDevicePropertyBufferFrameSize, size, &frameSize ); if ( status != kAudioHardwareNoError ) { common->Warning( "AudioDeviceSetProperty %d kAudioDevicePropertyBufferFrameSize failed. status: %s", selectedDevice, ExtractStatus( status ) ); InitFailed(); return false; } } if ( idSoundSystemLocal::s_numberOfSpeakers.GetInteger() != 2 ) { common->Warning( "only stereo sound currently supported" ); idSoundSystemLocal::s_numberOfSpeakers.SetInteger( 2 ); } UInt32 channels[ 2 ]; size = 2 * sizeof( UInt32 ); status = AudioDeviceGetProperty( selectedDevice, 0, false, kAudioDevicePropertyPreferredChannelsForStereo, &size, &channels ); if ( status != kAudioHardwareNoError ) { common->Warning( "AudioDeviceGetProperty %d kAudioDevicePropertyPreferredChannelsForStereo failed. status: %s", selectedDevice, ExtractStatus( status ) ); InitFailed(); return false; } common->Printf( "using stereo channel IDs %d %d\n", channels[ 0 ], channels[ 1 ] ); status = AudioDeviceAddIOProc( selectedDevice, DeviceIOProc, NULL ); if ( status != kAudioHardwareNoError ) { common->Warning( "AudioDeviceAddIOProc failed. status: %s", ExtractStatus( status ) ); InitFailed(); return false; } activeIOProc = true; status = AudioDeviceStart( selectedDevice, DeviceIOProc ); if ( status != kAudioHardwareNoError ) { common->Warning( "AudioDeviceStart failed. status: %s", ExtractStatus( status ) ); InitFailed(); return false; } /* // allocate the mix buffer // it has the space for ROOM_SLICES_IN_BUFFER DeviceIOProc loops mixBufferSize = dwSpeakers * dwSampleSize * dwPrimaryBitRate * ROOM_SLICES_IN_BUFFER / 8; mixBuffer = malloc( mixBufferSize ); memset( mixBuffer, 0, mixBufferSize ); */ return true; }
void test_base::InitCancelled() { InitFailed(); }