[Solved] How to write an AVAudioEngine TapOnBus callback? lazarus / 33-macOS - Mac OS X ---------------------------------------------------------------------- freq · 2023-07-12 11:26 Hi, have an issue with a simple task to attach a tapBlock with the following function from an audio node (comment out the call and below sample code works fine) inputNode.installTapOnBus_bufferSize_format_block(0,1024,nil,@tapBlock ) I don't know how to write a propper tapBlock callback function so that the following audio engine sequence doesn't crashes (I assume the tapBlock callback function is wrong defined or needs a Objective-C wrapper? Can't find any sample codes even for other OBjective-C callback cases). Any hint very welcome! Thanks in advance! Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i unit Unit1; {$mode delphi}{$modeswitch objectivec1}{$modeswitch cvar}{$packrecords c}  {$linkframework AVFoundation} interface uses  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs,  StdCtrls,   AVFoundation,  CoreAudio, MacTypes, SecBase,DefinedClassesFoundation; type   { TForm1 }   TForm1 = class(TForm)    Button1: TButton;    Label1: TLabel;    procedure Button1Click(Sender: TObject);  private     audioEngine: AVAudioEngine;    unitReverb:AVAudioUnitReverb;    inputNode:AVAudioInputNode;     procedure SetupAudioEngine;   public   end; var  Form1: TForm1; implementation {$R *.lfm}  // issue probably in that definition, it is even never called procedure tapBlock(buffer: AVAudioPCMBufferPtr; when:AVAudioTimePtr);var i:integer;begini:=0;end; { TForm1 } procedure TForm1.Button1Click(Sender: TObject);begin      SetupAudioEngine;end; procedure TForm1.SetupAudioEngine;var inputFormat: AVAudioFormat;    outError:NSError; begin  audioEngine:=  AVAudioEngine.alloc.init; unitReverb:= AVAudioUnitReverb.alloc.init;   audioEngine.attachNode(unitReverb);  inputNode:= audioEngine.inputNode; inputFormat:=  unitReverb.inputFormatForBus(0);   inputNode.installTapOnBus_bufferSize_format_block(0,1024,nil,@tapBlo ck);       // this creates the crash!  audioEngine.connect_to_format(inputNode,unitReverb, inputForma t); audioEngine.connect_to_format(unitReverb,audioEngine.outputNode, inputFormat); if audioEngine.startAndReturnError(@outError) then   begin   Label1.Caption:='ok';   end;end; end. ---------------------------------------------------------------------- Zvoni · 2023-07-12 11:45 Are you sure it's the Callback? I'd rather look at the 3rd Parameter "Format" --> You pass nil https://developer.apple.com/documentation/avfaudio/avaudionode/1387122 -installtaponbus?language=objc ---------------------------------------------------------------------- freq · 2023-07-12 11:50 Quote from: Zvoni on July 12, 2023, 01:45:02 pmAre you sure it's the Callback? I'd rather look at the 3rd Parameter "Format" --> You pass nil https://developer.apple.com/documentation/avfaudio/avaudionode/1387122 -installtaponbus?language=objc Yes, checked, same crash with: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i inputFormat:=  unitReverb.inputFormatForBus(0);  tab:= @tapBlock; unit Reverb.installTapOnBus_bufferSize_format_block(0,1024,inputFormat,tab) ;       // this creates the crash!   Assume that there must be something special in the way Objective-C handles a callback, is that "block" really a callback function or more an NSOBject something? ---------------------------------------------------------------------- Zvoni · 2023-07-12 12:06 No idea, since it's not my corner of expertise. But your Procedure declaration looks good, IMO. Both arguments appear to be Pointers, and it expects a procedure (not a function) The only thing i'm not sure about is "visibility" of your callback- procedure and/or calling-convention @visibility: You have no "Forward"-Declaration under "Interface" --> But not sure about that. @Calling-convention --> What's the standard calling-convention in Apple's objective C? cdecl? ---------------------------------------------------------------------- freq · 2023-07-12 12:20 Quote from: Zvoni on July 12, 2023, 02:06:42 pm@visibility: You have no "Forward"-Declaration under "Interface" --> But not sure about that. @Calling-convention --> What's the standard calling-convention in Apple's objective C? cdecl? Intresting ideas! Thanks, but unfortunately no success with both, I tried to add callback to interface and also tried with removed cdecl (though cdecl is the standard for objective C afaik). I'm still pretty sure that its a callback definition issue as it only crashes when activating the audio engine and when executed the installTapOnBus_bufferSize_format_block before. Interesting that the block definition contains a ^ operator ... how to translate that? typedef void (^AVAudioNodeTapBlock)(AVAudioPCMBuffer *buffer, AVAudioTime *when); ---------------------------------------------------------------------- Zvoni · 2023-07-12 12:34 Quote from: freq on July 12, 2023, 02:20:11 pmIntresting ideas! Thanks, but unfortunately no success with both, I tried to add callback to interface and also tried with removed cdecl (though cdecl is the standard for objective C afaik). I'm still pretty sure that its a callback definition issue as it only crashes when activating the audio engine and when executed the installTapOnBus_bufferSize_format_block before. You might be onto something here, because the standard calling convention in FPC is "Register", not cdecl. I'd really research this. QuoteInteresting that the block definition contains a ^ operator ... how to translate that? typedef void (^AVAudioNodeTapBlock)(AVAudioPCMBuffer *buffer, AVAudioTime *when); Usually this indicates a Function-Pointer, and you provide (correctly!) the "@"-Symbol in your call to it, but you're in mode Delphi! IIRC, in mode delphi you shouldn't provide/need the "@"-Symbol, but i'm not sure about it, since i've never used mode delphi try Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i unitReverb. installTapOnBus_bufferSize_format_block(0,1024,inputFormat,tapBlock); //No @-Symbol! EDIT: Maybe a BINGO? https://www.freepascal.org/docs-html/prog/progse74.html QuoteD.3 Delphi mode This mode is selected by the $MODE DELPHI switch. It tries to emulate, as closely as possible, the behavior of Delphi 4 till recent versions of Delphi . On the command line, this mode is selected by the -Mdelphi switch. 1. You cannot use the address operator to assign procedural variables. "@"-Symbol is the Address-Operator ---------------------------------------------------------------------- freq · 2023-07-12 14:12 Quote from: Zvoni on July 12, 2023, 02:34:31 pm You might be onto something here, because the standard calling convention in FPC is "Register", not cdecl. I'd really research this. No, unfortunately no success with "Register" QuoteUsually this indicates a Function-Pointer, and you provide (correctly!) the "@"-Symbol in your call to it, but you're in mode Delphi! No, unfortunately also no luck to switch to {$mode objfpc}{$H+}  mode ---------------------------------------------------------------------- Zvoni · 2023-07-12 15:02 Quote from: freq on July 12, 2023, 04:12:47 pm No, unfortunately no success with "Register" I think you misunderstood: if you don’t declare the calling convention, FreePascal uses „register“ by default. Research if calling convention in Objective C is cdecl or something else ---------------------------------------------------------------------- freq · 2023-07-13 09:40 Quote from: Zvoni on July 12, 2023, 05:02:07 pmI think you misunderstood: if you don’t declare the calling convention, FreePascal uses „register“ by default. Research if calling convention in Objective C is cdecl or something else Thanks for the clarification, but unfortunately with our without cdecl it doesn't work. ---------------------------------------------------------------------- freq · 2023-07-15 18:48 Thanks for all but I found the solution with the input from here that deals with a similar callback topic: https://forum.lazarus.freepascal.org/index.php?topic=42001.45 As suspected it was a declaration topic and we have to use cblock: This code works now without crashing  : Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i unit Unit1; {$mode objfpc}{$H+}{$modeswitch objectivec1}{$packrecords c}{$modeswitch cblocks} {$linkframework AVFoundation} interface uses  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs,  StdCtrls,   AVFoundation,  CoreAudio, MacTypes, SecBase,DefinedClassesFoundation; type   { TForm1 }   TForm1 = class(TForm)    Button1: TButton;    Label1: TLabel;    procedure Button1Click(Sender: TObject);  private     inputNode: AVAudioInputNode;    audioEngine: AVAudioEngine;    format: AVAudioFormat;     mixerNode:AVAudioMixerNode;     procedure SetupAudioEngine;   public     procedure tapBlock(buffer: AVAudioPCMBufferPtr; when:AVAudioTimePtr);   end;  var  Form1: TForm1; implementation {$R *.lfm}  type  tblock = reference to procedure(buffer: AVAudioPCMBufferPtr; when:AVAudioTimePtr); cdecl; cblock;  procedure TForm1.tapBlock(buffer: AVAudioPCMBufferPtr; when:AVAudioTimePtr);var i:integer;begini:=0;end; procedure TForm1.Button1Click(Sender: TObject);begin      SetupAudioEngine;end; procedure TForm1.SetupAudioEngine;var    outError:NSError;    s:double;    tab: tblock; begin  tab:= @tapBlock;  audioEngine:=  AVAudioEngine.alloc.init;  inputNode:=audio Engine.inputNode;  mixerNode:= AVAudioMixerNode.alloc.init; audioEngine.attachNode(mixerNode); format := inputNode.inputFormatForBus(0);   mixerNode.installTapOnBus_bufferS ize_format_block(0,2048,mixerNode.outputFormatForBus(0), OpaqueCBlock(tab)); // add tap  audioEngine.connect_to_format(inputNod e,mixerNode,format); audioEngine.connect_to_format(mixerNode, audioEngine.outputNode, format);  audioEngine.prepare; if audioEngine.startAndReturnError(@outError) then   begin    Label1.Caption:='ok';   end;end; end.