osx - Setting sample rate on AUHAL -
i'm using audio unit framework develop voip app on mac os x. in program, set input auhal , use default stream format (44.1khz,32bit/channel) capture audio mic. in case, program works fine.
here code:
//the default setting in program checkerror(audiounitgetproperty(m_audcapunit, kaudiounitproperty_streamformat, kaudiounitscope_output, //the value 0 inputbus, //the value 1 &m_audcapunitoutputstreamformat, &propertysize), "couldn't outputsample asbd input unit") ; //the inoutputsamplerate 44100.0 m_audcapunitoutputstreamformat.msamplerate = inoutputsamplerate ; checkerror(audiounitsetproperty(m_audcapunit, kaudiounitproperty_streamformat, kaudiounitscope_output, inputbus, &m_audcapunitoutputstreamformat, propertysize), "couldn't set outputsample asbd on input unit"); // since i'm developing voip app, default format (44.1khz, 32bits/channel) isn't appropriate program, want change sample rate 8khz. , had written code change format in program:
//...... inoutputformat.msamplerate = 8000. ; inoutputformat.mformatid = kaudioformatlinearpcm ; inoutputformat.mchannelsperframe = 2 ; inoutputformat.mbitsperchannel = 16 ; inoutputformat.mbytesperframe = 2 ; inoutputformat.mbytesperpacket = 2 ; inoutputformat.mframesperpacket = 1 ; inoutputformat.mformatflags = kaudioformatflagsaudiounitcanonical ; inoutputformat.mreserved = 0 ; checkerror(audiounitsetproperty(m_audcapunit, kaudiounitproperty_streamformat, kaudiounitscope_output, inputbus, &inoutputformat, ui32propsize), "couldn't set auhal unit output format") ; //....... in case, program works fine until program calls audiounitrender in callback function; fails call audiounitrender error code -10876 means kaudiouniterr_noconnection in documentation. error code puzzled me much, googled couldn't find useful information. can tell me error means?
this not end, changed format again code:
//..... inoutputformat.msamplerate = 8000. ; inoutputformat.mformatid = kaudioformatlinearpcm ; inoutputformat.mchannelsperframe = 2 ; inoutputformat.mbitsperchannel = 32 ; inoutputformat.mbytesperframe = 4 ; inoutputformat.mbytesperpacket = 4 ; inoutputformat.mframesperpacket = 1 ; inoutputformat.mformatflags = kaudioformatflagsaudiounitcanonical ; inoutputformat.mreserved = 0 ; checkerror(audiounitsetproperty(m_audcapunit, kaudiounitproperty_streamformat, kaudiounitscope_output, inputbus, &inoutputformat, ui32propsize), "couldn't set auhal unit output format") ; //........ in case, program failed call audiounitrender again , returned error code -10863(kaudiouniterr_cannotdoincurrentcontext). googled it, found something useful. after reading information there, guess sample rate or format set on auhal may not supported hardware.
so wrote code check available sample rates on default input device:
//.......... uint32 propertysize = sizeof(audiodeviceid) ; boolean iswritable = false ; checkerror(audiodevicegetpropertyinfo(indeviceid, //the indeviceid default input device 0, true, kaudiodevicepropertyavailablenominalsamplerates, &propertysize, &iswritable), "get available sample rate count failed") ; m_valuecount = propertysize / sizeof(audiovaluerange) ; printf("available %d sample rate\n",m_valuecount) ; checkerror(audiodevicegetproperty(indeviceid, 0, false, kaudiodevicepropertyavailablenominalsamplerates, &propertysize, m_valuetabe), "get available sample rate count failed") ; for(uint32 = 0 ; < m_valuecount ; ++i) { printf("available sample rate value : %ld\n",(long)m_valuetabe[i].mminimum) ; } //.............. and found available sample rates 8000, 16000, 32000, 44100, 48000, 88200, , 96000.
the 8000 sample rate set before, error code calling audiounitrender, want say, why ?
i had google , read many documentations, can't answer, can solve problem encounter?
in other words; how change sample rate or format on input-only auhal?
finally fixed problem yesterday myself.
here solution:
- firstly , use
audiodevicegetpropertyavailable format list on defaut input device, found available format list contain :8khz, 16khz, 32khz, 44.1khz, 48khz, 88.2khz,96khz(i list sample rate field here ,but there other field in list). - and select 1 of available format obtained in first step. take program example , i select format
(8khz,32bits/channel), useaudiodevicesetpropertyset on default device not auhal , key program work fine after setinng format on auhal (outputscope , inputbus). - the last step , use
audiounitsetpropertyset format wanted , program work fine.
through problem , solution , guess if want set format on input-only auhal ,the format must match or can shift available format input device using. need setting format on input device firstly , set format on input-only auhal next.
Comments
Post a Comment