Audio Synthesis

Mac OSX specific forum
Wolfram
Enthusiast
Enthusiast
Posts: 568
Joined: Thu May 30, 2013 4:39 pm

Re: Audio Synthesis

Post by Wolfram »

Does some know how to choose different audio outputs on AudioComponentInstanceNew_(comp, @Audio_Unit)?
I want to be able to assign a audio device inside my program independent from the system settings.

A way to list the devices is here:
viewtopic.php?p=540312#p540312
macOS Catalina 10.15.7
Wolfram
Enthusiast
Enthusiast
Posts: 568
Joined: Thu May 30, 2013 4:39 pm

Re: Audio Synthesis

Post by Wolfram »

Got it ...again

Here is how I init a AU Audio:
The audioOutDevices()\deviceID is from my other code: viewtopic.php?p=540312#p540312

Code: Select all


ImportC ""
  AudioComponentGetDescription(comp.i, *AudioComponentDescription)
  AudioComponentCopyName(comp.i, *outName)
  AudioComponentCount(*outName)
  AudioComponentInstanceGetComponent(inInstance)
  AudioUnitGetProperty(inUnit.i, inID.l, inScope.l, inElement.i, *outData, ioDataSize.l)
  AudioUnitSetProperty(inUnit.i, inID.l, inScope.l, inElement.i, *inData, inDataSize.l)
EndImport



 Structure AudioComponentDescription
  componentType.l
  componentSubType.l
  componentManufacturer.l
  componentFlags.l
  componentFlagsMask.l
EndStructure
 
Structure AudioStreamBasicDescription
  mSampleRate.d
  mFormatID.l
  mFormatFlags.l
  mBytesPerPacket.l
  mFramesPerPacket.l
  mBytesPerFrame.l
  mChannelsPerFrame.l
  mBitsPerChannel.l
  mReserved.l
EndStructure

#kAudioDevicePropertyBufferFrameSize = $6673697a; 'fsiz'

#kAudioOutputUnitProperty_HasIO = 2006
#kAudioOutputUnitProperty_CurrentDevice =2000
#kAudioOutputUnitProperty_EnableIO = 2003

#kAudioUnitType_Output = $61756F75; 'auou'
#kAudioUnitSubType_HALOutput = $6168616C ;'ahal'
#kAudioUnitManufacturer_Apple = $6170706C; 'appl'

#kAudioUnitProperty_StreamFormat = 8

#kAudioUnitScope_Global   = 0
#kAudioUnitScope_Input    = 1
#kAudioUnitScope_Output   = 2
#kAudioUnitScope_Group    = 3
#kAudioUnitScope_Part     = 4

#kAudioFormatLinearPCM = $6C70636D ;'lpcm'
#kAudioFormatFlagIsPacked = 8

#kAudioFormatFlagIsSignedInteger = 4
#kLinearPCMFormatFlagIsSignedInteger = #kAudioFormatFlagIsSignedInteger


Procedure setAudioDevice()
  Protected ACD.AudioComponentDescription
  ACD\componentType = #kAudioUnitType_Output
  ACD\componentSubType = #kAudioUnitSubType_HALOutput
  ACD\componentManufacturer = #kAudioUnitManufacturer_Apple
  ACD\componentFlagsMask =0
  ACD\componentFlags =0
  
  comp = AudioComponentFindNext_(#Null, @ACD)
  If comp
    Protected audioUnit
    AudioComponentInstanceNew_(comp, @audioUnit)
    
    hasIO.l = 0
    size_hasIO.l = SizeOf(hasIO)
    AudioUnitGetProperty(audioUnit, #kAudioOutputUnitProperty_HasIO, #kAudioUnitScope_Output, 0, @hasIO, @size_hasIO)
    
    
    If Not hasIO
      enableIO.l = #True
      outputBus.i =1
      AudioUnitSetProperty(audioUnit, #kAudioOutputUnitProperty_EnableIO, #kAudioUnitScope_Global, @outputBus, @enableIO, SizeOf(enableIO))
      
      AudioUnitGetProperty(audioUnit, #kAudioOutputUnitProperty_HasIO, #kAudioUnitScope_Output, 0, @hasIO, @size_hasIO)
      Debug "hasIO_" +hasIO
      
    EndIf
    
    mDeviceID.l =audioOutDevices()\deviceID
    AudioUnitSetProperty(audioUnit, #kAudioOutputUnitProperty_CurrentDevice, #kAudioUnitScope_Global, 0, @mDeviceID, SizeOf(mDeviceID))
    
  EndIf
  
  ProcedureReturn audioUnit
EndProcedure


Procedure Audio_Init(*CallbackFunction, BufferSize.l = 512)
  Protected Dim CallbackStruct.i(1) : CallbackStruct(0) = *CallbackFunction
  
  Global Audio_Unit = setAudioDevice()
  
  If Audio_Unit
    AudioUnitSetProperty_(Audio_Unit, 23, 1, 0, @CallbackStruct(), SizeOf(Integer) << 1)
    
    
    Protected ASBD.AudioStreamBasicDescription
    ASBD\mSampleRate = 44100.0
    ASBD\mFormatID = #kAudioFormatLinearPCM ;$6C70636D
    ASBD\mFormatFlags = #kAudioFormatFlagIsPacked | #kLinearPCMFormatFlagIsSignedInteger
    ASBD\mBytesPerPacket = 2
    ASBD\mFramesPerPacket = 1
    ASBD\mBytesPerFrame = 2
    ASBD\mChannelsPerFrame = 1
    ASBD\mBitsPerChannel = 16
    
    
    AudioUnitSetProperty_(Audio_Unit, #kAudioUnitProperty_StreamFormat, 1, 0, @ASBD, SizeOf(AudioStreamBasicDescription))
    AudioUnitSetProperty_(Audio_Unit, #kAudioDevicePropertyBufferFrameSize, 1, 0, @BufferSize, 4)
    AudioUnitInitialize_(Audio_Unit)
    Audio_SetVolume(50)
  EndIf
EndProcedure

macOS Catalina 10.15.7
Post Reply