From: Simon Armstrong Date: Wed, 14 Oct 2015 05:39:43 +0000 (+1300) Subject: Fixed freeaudios coreaudio device removing use of deprecated interface X-Git-Url: https://jcornell.net/gitweb/gitweb.cgi?a=commitdiff_plain;h=03ea37f3d789b4969346ccfba0ad0e6adb2b1d24;p=blitzmax.git Fixed freeaudios coreaudio device removing use of deprecated interface --- diff --git a/mod/pub.mod/freeaudio.mod/coreaudiodevice.cpp b/mod/pub.mod/freeaudio.mod/coreaudiodevice.cpp index 5d67f53..d1930fb 100644 --- a/mod/pub.mod/freeaudio.mod/coreaudiodevice.cpp +++ b/mod/pub.mod/freeaudio.mod/coreaudiodevice.cpp @@ -1,137 +1,138 @@ -// apple core audio device - -#include "freeaudio.h" - -#ifdef __APPLE__ - -#include -#include -#include -#include - -extern "C" audiodevice *OpenCoreAudioDevice(); - -OSStatus FeedSound(void *ref,AudioUnitRenderActionFlags *flags,const AudioTimeStamp *time,UInt32 bus,UInt32 frames,AudioBufferList *data); - -struct coreaudio:audiodevice{ - AudioUnit out; - AudioConverterRef conv; - AURenderCallbackStruct callback; - short *buffer; - int tcount; - - int reset(){ - int res; - - mix=new mixer(8192); - mix->freq=44100; - mix->channels=2; - - out=0; - res=initoutput(); - if (res) return res; - - callback.inputProc=FeedSound; - callback.inputProcRefCon=this; - - buffer=new short[8192]; - - res=AudioUnitSetProperty(out,kAudioUnitProperty_SetRenderCallback,kAudioUnitScope_Input,0,&callback,sizeof(callback)); - if (res) return res; - - res=AudioOutputUnitStart(out); - if (res) return res; - - return 0; - } - - int close(){ - int res; - - if (out){ - res=AudioOutputUnitStop(out); - if (res) return res; - out=0; - } - return 0; - } - - int initoutput(){ - ComponentDescription desc; - Component comp; - OSStatus err; - UInt32 size; - Boolean canwrite; - - AudioStreamBasicDescription inputdesc,outputdesc; - - desc.componentType=kAudioUnitType_Output; - desc.componentSubType=kAudioUnitSubType_DefaultOutput; - desc.componentManufacturer=kAudioUnitManufacturer_Apple; - desc.componentFlags=0; - desc.componentFlagsMask=0; - - comp=FindNextComponent(NULL,&desc);if (comp==NULL) return -1; - err=OpenAComponent(comp,&out);if (err) return err; - err=AudioUnitInitialize(out);if (err) return err; - - err=AudioUnitGetPropertyInfo(out,kAudioUnitProperty_StreamFormat,kAudioUnitScope_Output,0,&size,&canwrite); - if (err) return err; - - err=AudioUnitGetProperty(out,kAudioUnitProperty_StreamFormat,kAudioUnitScope_Input,0,&outputdesc,&size); - if (err) return err; - -// dumpdesc(&outputdesc); - - inputdesc.mSampleRate=44100.0; - inputdesc.mFormatID='lpcm'; -#if __BIG_ENDIAN__ - inputdesc.mFormatFlags=0x0e; -#else - inputdesc.mFormatFlags=0x0c; -#endif - inputdesc.mBytesPerPacket=4; - inputdesc.mFramesPerPacket=1; - inputdesc.mBytesPerFrame=4; - inputdesc.mChannelsPerFrame=2; - inputdesc.mBitsPerChannel=16; - inputdesc.mReserved=0; - -// dumpdesc(&inputdesc); - - err=AudioConverterNew(&inputdesc,&outputdesc,&conv); - if (err) { -// printf("AudioConvertNew failed %.*s\n",4,(char*)&err); - return err; - } - - return err; - } - - int read(AudioConverterRef conv,UInt32 *count,AudioBufferList *blist,AudioStreamPacketDescription **outdesc){ - if (*count>4096) *count=4096; -// printf("ac read count=%d\n",*count);fflush(stdout); - mix->mix16(buffer,*count*2); - blist->mBuffers[0].mData=buffer; - blist->mBuffers[0].mDataByteSize=*count*4; - return 0; - } -}; - -OSStatus Feed(AudioConverterRef conv,UInt32 *count,AudioBufferList *blist,AudioStreamPacketDescription **outdesc,void *ref){ - coreaudio *audio; - audio=(coreaudio*)ref; - return audio->read(conv,count,blist,outdesc); -} - -OSStatus FeedSound(void *ref,AudioUnitRenderActionFlags *flags,const AudioTimeStamp *time,UInt32 bus,UInt32 count,AudioBufferList *blist){ - coreaudio *audio; - audio=(coreaudio*)ref; - return AudioConverterFillComplexBuffer(audio->conv,Feed,ref,&count,blist,0); -} - -audiodevice *OpenCoreAudioDevice(){ - return new coreaudio(); -} - -#endif +// apple core audio device + +#include "freeaudio.h" + +#ifdef __APPLE__ + +#include + +extern "C" audiodevice *OpenCoreAudioDevice(); + +OSStatus FeedSound(void *ref,AudioUnitRenderActionFlags *flags,const AudioTimeStamp *time,UInt32 bus,UInt32 frames,AudioBufferList *data); + +struct coreaudio:audiodevice{ + AudioUnit out; + AudioConverterRef conv; + AURenderCallbackStruct callback; + short *buffer; + int tcount; + + int reset(){ + int res; + + mix=new mixer(8192); + mix->freq=44100; + mix->channels=2; + + out=0; + res=initoutput(); + if (res) return res; + + callback.inputProc=FeedSound; + callback.inputProcRefCon=this; + + buffer=new short[8192]; + + res=AudioUnitSetProperty(out,kAudioUnitProperty_SetRenderCallback,kAudioUnitScope_Input,0,&callback,sizeof(callback)); + if (res) return res; + + res=AudioOutputUnitStart(out); + if (res) return res; + + return 0; + } + + int close(){ + int res; + + if (out){ + res=AudioOutputUnitStop(out); + if (res) return res; + out=0; + } + return 0; + } + + int initoutput(){ + AudioComponentDescription desc; + AudioComponent comp; + OSStatus err; + UInt32 size; + Boolean canwrite; + + AudioStreamBasicDescription inputdesc,outputdesc; + + desc.componentType=kAudioUnitType_Output; + desc.componentSubType=kAudioUnitSubType_DefaultOutput; + desc.componentManufacturer=kAudioUnitManufacturer_Apple; + desc.componentFlags=0; + desc.componentFlagsMask=0; + + comp=AudioComponentFindNext(NULL,&desc); + if (comp==NULL) return -1; + + err= AudioComponentInstanceNew(comp,&out); + if (err) return err; + + err=AudioUnitInitialize(out);if (err) return err; + + err=AudioUnitGetPropertyInfo(out,kAudioUnitProperty_StreamFormat,kAudioUnitScope_Output,0,&size,&canwrite); + if (err) return err; + + err=AudioUnitGetProperty(out,kAudioUnitProperty_StreamFormat,kAudioUnitScope_Input,0,&outputdesc,&size); + if (err) return err; + +// dumpdesc(&outputdesc); + + inputdesc.mSampleRate=44100.0; + inputdesc.mFormatID='lpcm'; +#if __BIG_ENDIAN__ + inputdesc.mFormatFlags=0x0e; +#else + inputdesc.mFormatFlags=0x0c; +#endif + inputdesc.mBytesPerPacket=4; + inputdesc.mFramesPerPacket=1; + inputdesc.mBytesPerFrame=4; + inputdesc.mChannelsPerFrame=2; + inputdesc.mBitsPerChannel=16; + inputdesc.mReserved=0; + +// dumpdesc(&inputdesc); + + err=AudioConverterNew(&inputdesc,&outputdesc,&conv); + if (err) { +// printf("AudioConvertNew failed %.*s\n",4,(char*)&err); + return err; + } + + return err; + } + + int read(AudioConverterRef conv,UInt32 *count,AudioBufferList *blist,AudioStreamPacketDescription **outdesc){ + if (*count>4096) *count=4096; +// printf("ac read count=%d\n",*count);fflush(stdout); + mix->mix16(buffer,*count*2); + blist->mBuffers[0].mData=buffer; + blist->mBuffers[0].mDataByteSize=*count*4; + return 0; + } +}; + +OSStatus Feed(AudioConverterRef conv,UInt32 *count,AudioBufferList *blist,AudioStreamPacketDescription **outdesc,void *ref){ + coreaudio *audio; + audio=(coreaudio*)ref; + return audio->read(conv,count,blist,outdesc); +} + +OSStatus FeedSound(void *ref,AudioUnitRenderActionFlags *flags,const AudioTimeStamp *time,UInt32 bus,UInt32 count,AudioBufferList *blist){ + coreaudio *audio; + audio=(coreaudio*)ref; + return AudioConverterFillComplexBuffer(audio->conv,Feed,ref,&count,blist,0); +} + +audiodevice *OpenCoreAudioDevice(){ + return new coreaudio(); +} + +#endif diff --git a/mod/pub.mod/freeaudio.mod/freeaudio.bmx b/mod/pub.mod/freeaudio.mod/freeaudio.bmx index 5dd5e9a..c0f8ab2 100644 --- a/mod/pub.mod/freeaudio.mod/freeaudio.bmx +++ b/mod/pub.mod/freeaudio.mod/freeaudio.bmx @@ -1,125 +1,128 @@ - -Module Pub.FreeAudio - -ModuleInfo "Version: 1.22" -ModuleInfo "Author: Simon Armstrong" -ModuleInfo "License: zlib/libpng" -ModuleInfo "Copyright: Blitz Research Ltd" -ModuleInfo "Modserver: BRL" - -ModuleInfo "History: 1.22 Release" -ModuleInfo "History: Fixed leak with sound recycling" -ModuleInfo "History: 1.21 Release" -ModuleInfo "History: Fixed reference counting for brl.freeaudioaudio" -ModuleInfo "History: 1.20 Release" -ModuleInfo "History: Removed duplication of sample memory" -ModuleInfo "History: 1.19 Release" -ModuleInfo "History: Added DirectSound mode" -ModuleInfo "History: 1.18 Release" -ModuleInfo "History: added fa_ChannelPosition for live sample generation" -ModuleInfo "History: 1.17 Release" -ModuleInfo "History: added check for windows playback position overflow" -ModuleInfo "History: 1.15 Release" -ModuleInfo "History: added low latency windows98 fix" -ModuleInfo "History: 1.14 Release" -ModuleInfo "History: fixed 1.13 recycling of stopped channels fix" -ModuleInfo "History: 1.13 Release" -ModuleInfo "History: fixed recycling of stopped channels" -ModuleInfo "History: 1.12 Release" -ModuleInfo "History: Uses linear interpolation for improved fidelity at low rates" -ModuleInfo "History: 1.11 Release" -ModuleInfo "History: Fixed freepool sounds Not resetting parameters - thanks To Fetze" -ModuleInfo "History: 1.10 Release" -ModuleInfo "History: Added ALSA support for Linux courtesy Craig Kiesau" -ModuleInfo "History: 1.09 Release" -ModuleInfo "History: Improved channel playback timing" -ModuleInfo "History: 1.08 Release" -ModuleInfo "History: Fixed memory leak in fa_FreeSound()" -ModuleInfo "History: 1.07 Release" -ModuleInfo "History: Removed output transitions for queued/paused sounds" -ModuleInfo "History: 1.06 Release" -ModuleInfo "History: Windows device error now silently fails" -ModuleInfo "History: 1.05 Release" -ModuleInfo "History: Linux version now opens audio device on second thread" -ModuleInfo "History: 1.04 Release" -ModuleInfo "History: Removed Linux debug output" - -Import "freeaudio.cpp" -Import "freeaudioglue.cpp" - -?Win32 -Import "dsounddevice.cpp" -Import "mmdevice.cpp" -Extern "C" -Function OpenMultiMediaDevice() -Function OpenDirectSoundDevice() -End Extern -?MacOS -Import "-framework AudioUnit" -Import "-framework AudioToolbox" -Import "coreaudiodevice.cpp" -Extern -Function OpenCoreAudioDevice() -End Extern -?Linux -'Import "-lasound" -'Import "alsadevice.cpp" -Import "ossdevice.cpp" -Extern "C" -Function OpenOSSDevice() -'Function OpenALSADevice() -End Extern -? - -Extern - -Const FA_CHANNELSTATUS_FREE=0 -Const FA_CHANNELSTATUS_STOPPED=1 -Const FA_CHANNELSTATUS_SINGLESHOT=2 -Const FA_CHANNELSTATUS_LOOPING=4 -Const FA_CHANNELSTATUS_STREAMING=8 -Const FA_CHANNELSTATUS_PAUSED=16 - -Function fa_Reset( audiodevice ) -Function fa_Close() -Function fa_CreateSound( length,bits,channels,hertz,samples:Byte Ptr=Null,looping=False ) -Function fa_WriteSound( sound,samples:Byte Ptr,length ) 'length really neceesary? -Function fa_FreeSound( sound ) -Function fa_AllocChannel() -Function fa_FreeChannel( channel ) -Function fa_PlaySound( sound,paused_flag,channel ) - -Function fa_StopChannel( channel ) -Function fa_ChannelStatus( channel ) -Function fa_ChannelPosition( channel ) - -Function fa_SetChannelPaused( channel,paused ) -Function fa_SetChannelVolume( channel,volume# ) -Function fa_SetChannelRate( channel,pitch# ) -Function fa_SetChannelPan( channel,pan# ) -Function fa_SetChannelDepth( channel,depth# ) - -End Extern - -Function fa_Init( deviceid ) - Local device -?Win32 - If deviceid - device=OpenDirectSoundDevice() - Else - device=OpenMultiMediaDevice() - EndIf -?Linux - Select deviceid - Case 0 - device=OpenOSSDevice() -' Case 1 -' device=OpenALSADevice() - EndSelect -?MacOS - device=OpenCoreAudioDevice() -? - Local res=-1 - If device res=fa_Reset(device) - Return res -End Function + +Module Pub.FreeAudio + +ModuleInfo "Version: 1.23" +ModuleInfo "Author: Simon Armstrong" +ModuleInfo "License: zlib/libpng" +ModuleInfo "Copyright: Blitz Research Ltd" +ModuleInfo "Modserver: BRL" + +ModuleInfo "History: 1.23 Release" +ModuleInfo "History: Updated OS X coreaudio to not use deprecated API" +ModuleInfo "History: 1.22 Release" +ModuleInfo "History: Fixed leak with sound recycling" +ModuleInfo "History: 1.21 Release" +ModuleInfo "History: Fixed reference counting for brl.freeaudioaudio" +ModuleInfo "History: 1.20 Release" +ModuleInfo "History: Removed duplication of sample memory" +ModuleInfo "History: 1.19 Release" +ModuleInfo "History: Added DirectSound mode" +ModuleInfo "History: 1.18 Release" +ModuleInfo "History: added fa_ChannelPosition for live sample generation" +ModuleInfo "History: 1.17 Release" +ModuleInfo "History: added check for windows playback position overflow" +ModuleInfo "History: 1.15 Release" +ModuleInfo "History: added low latency windows98 fix" +ModuleInfo "History: 1.14 Release" +ModuleInfo "History: fixed 1.13 recycling of stopped channels fix" +ModuleInfo "History: 1.13 Release" +ModuleInfo "History: fixed recycling of stopped channels" +ModuleInfo "History: 1.12 Release" +ModuleInfo "History: Uses linear interpolation for improved fidelity at low rates" +ModuleInfo "History: 1.11 Release" +ModuleInfo "History: Fixed freepool sounds Not resetting parameters - thanks To Fetze" +ModuleInfo "History: 1.10 Release" +ModuleInfo "History: Added ALSA support for Linux courtesy Craig Kiesau" +ModuleInfo "History: 1.09 Release" +ModuleInfo "History: Improved channel playback timing" +ModuleInfo "History: 1.08 Release" +ModuleInfo "History: Fixed memory leak in fa_FreeSound()" +ModuleInfo "History: 1.07 Release" +ModuleInfo "History: Removed output transitions for queued/paused sounds" +ModuleInfo "History: 1.06 Release" +ModuleInfo "History: Windows device error now silently fails" +ModuleInfo "History: 1.05 Release" +ModuleInfo "History: Linux version now opens audio device on second thread" +ModuleInfo "History: 1.04 Release" +ModuleInfo "History: Removed Linux debug output" + +Import "freeaudio.cpp" +Import "freeaudioglue.cpp" + +?Win32 +Import "dsounddevice.cpp" +Import "mmdevice.cpp" +Extern "C" +Function OpenMultiMediaDevice() +Function OpenDirectSoundDevice() +End Extern +?MacOS +Import "-framework CoreAudio" +Import "-framework AudioUnit" +Import "-framework AudioToolbox" +Import "coreaudiodevice.cpp" +Extern +Function OpenCoreAudioDevice() +End Extern +?Linux +'Import "-lasound" +'Import "alsadevice.cpp" +Import "ossdevice.cpp" +Extern "C" +Function OpenOSSDevice() +'Function OpenALSADevice() +End Extern +? + +Extern + +Const FA_CHANNELSTATUS_FREE=0 +Const FA_CHANNELSTATUS_STOPPED=1 +Const FA_CHANNELSTATUS_SINGLESHOT=2 +Const FA_CHANNELSTATUS_LOOPING=4 +Const FA_CHANNELSTATUS_STREAMING=8 +Const FA_CHANNELSTATUS_PAUSED=16 + +Function fa_Reset( audiodevice ) +Function fa_Close() +Function fa_CreateSound( length,bits,channels,hertz,samples:Byte Ptr=Null,looping=False ) +Function fa_WriteSound( sound,samples:Byte Ptr,length ) 'length really neceesary? +Function fa_FreeSound( sound ) +Function fa_AllocChannel() +Function fa_FreeChannel( channel ) +Function fa_PlaySound( sound,paused_flag,channel ) + +Function fa_StopChannel( channel ) +Function fa_ChannelStatus( channel ) +Function fa_ChannelPosition( channel ) + +Function fa_SetChannelPaused( channel,paused ) +Function fa_SetChannelVolume( channel,volume# ) +Function fa_SetChannelRate( channel,pitch# ) +Function fa_SetChannelPan( channel,pan# ) +Function fa_SetChannelDepth( channel,depth# ) + +End Extern + +Function fa_Init( deviceid ) + Local device +?Win32 + If deviceid + device=OpenDirectSoundDevice() + Else + device=OpenMultiMediaDevice() + EndIf +?Linux + Select deviceid + Case 0 + device=OpenOSSDevice() +' Case 1 +' device=OpenALSADevice() + EndSelect +?MacOS + device=OpenCoreAudioDevice() +? + Local res=-1 + If device res=fa_Reset(device) + Return res +End Function