It is currently Mon Nov 11, 2019 10:22 pm

All times are UTC + 1 hour




Post new topic Reply to topic  [ 32 posts ]  Go to page Previous  1, 2, 3
Author Message
 Post subject: Re: Audio Synthesis
PostPosted: Sat Aug 10, 2019 1:56 pm 
Offline
Enthusiast
Enthusiast

Joined: Thu May 30, 2013 4:39 pm
Posts: 468
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:
https://www.purebasic.fr/english/viewtopic.php?p=540312#p540312


Top
 Profile  
Reply with quote  
 Post subject: Re: Audio Synthesis
PostPosted: Sat Aug 10, 2019 8:09 pm 
Offline
Enthusiast
Enthusiast

Joined: Thu May 30, 2013 4:39 pm
Posts: 468
Got it ...again

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

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



Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 32 posts ]  Go to page Previous  1, 2, 3

All times are UTC + 1 hour


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  

 


Powered by phpBB © 2008 phpBB Group
subSilver+ theme by Canver Software, sponsor Sanal Modifiye