Recent

Author Topic: AVMidiPlayer not working - Might be one for you, Trev ;-)  (Read 2015 times)

carl_caulkett

  • Sr. Member
  • ****
  • Posts: 298
AVMidiPlayer not working - Might be one for you, Trev ;-)
« on: November 07, 2021, 10:48:58 pm »
As an experiment, I'm trying to get the example at https://wiki.freepascal.org/macOS_MIDI_Player to run.

I've managed to build a runnable piece of code, but something is amis.

The code is failing at the point just after the AVMIDIPlayer external class is "created" and is then tested to see if it is assigned.

Code: Pascal  [Select][+][-]
  1.       myMidiPlayer := AVMidiPlayer.alloc.initWithContentsOfURL_error(url, Nil, @err);
  2.  
  3.       // Save file duration
  4.       fileDuration := myMidiPlayer.duration;
  5.  
  6.       if Assigned(myMidiPlayer) then  // *** FAILS THIS TEST ***
  7.         begin
  8.           myMidiPlayer.setRate(myRate);
  9.           myMidiPlayer.prepareToPlay;
  10.           Form1.SecondsLabel.Caption := '0 of ' + FormatFloat('#', fileDuration)
  11.              + ' seconds';
  12.           Form1.ProgressBar.Max:= Trunc(fileDuration);
  13.           myMidiPlayer.play(@myCompletionHandler);
  14.         end
  15.       else
  16.         // Use the Applications > Utilities > Console application to find error messages
  17.         NSLog(NSStr('Error in procedure PlayMidi(): %@'), err);
  18.  

I've attached a zip file with the entire source.

Any ideas what the problem is?

[Edited to add code tags.]
« Last Edit: November 07, 2021, 11:42:40 pm by trev »
"It builds... ship it!"

macOS 12.3 Monterey
Lazarus 2.3.0 (from src)
FPC 3.3.1 (from src)

carl_caulkett

  • Sr. Member
  • ****
  • Posts: 298
Re: AVMidiPlayer not working - Might be one for you, Trev ;-)
« Reply #1 on: November 07, 2021, 11:42:19 pm »
I'm wondering if there is a problem with the filename of the MIDI file in question.

This is the start of the PlayMidi function...

Code: Pascal  [Select][+][-]
  1. procedure PlayMidi(midiFileName : NSString);
  2. var
  3.   path: NSString;
  4.   url : NSURL;
  5.   err : NSError;
  6. begin
  7.   // Do nothing if already playing a midi file
  8.   if(myMidiPlayer.IsPlaying) then
  9.     exit;
  10.  
  11.   // If player has not been paused
  12.   if(filePOS = 0) then
  13.     begin
  14.       // Path to your application bundle's resource directory
  15.       // with the midi filename appended
  16.       path := NSBundle.mainBundle.resourcePath.stringByAppendingPathComponent(midiFileName);
  17.       url  := NSURL.fileURLWithPath(path);

I thought I would try to monitor the state of path and url, because url is used in the creation of the AVMidiPlayer object.

Code: Pascal  [Select][+][-]
  1.       if Form1.PathLabel.Caption='PathLabel' then;
  2.          Form1.PathLabel.Caption:=path;
  3.       if Form1.URLLabel.Caption='URLLabel' then;
  4.          Form1.URLLabel.Caption:=url;

This, of course, doesn't work because path is an NSString and url is an NSUrl. How do I convert them to TLabel.Caption-friendly strings?

[Edited to add code tags.]
« Last Edit: November 07, 2021, 11:44:30 pm by trev »
"It builds... ship it!"

macOS 12.3 Monterey
Lazarus 2.3.0 (from src)
FPC 3.3.1 (from src)

trev

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 1978
  • Former Delphi 1-7, 10.2 user
Re: AVMidiPlayer not working - Might be one for you, Trev ;-)
« Reply #2 on: November 07, 2021, 11:54:04 pm »
I just checked my example still compiles and runs in Monterey. I'll take a look at your project later today when I get a moment.
Lazarus 2.3, FPC 3.3.1 macOS 12.3.1 x86_64 Xcode 13.4
Lazarus 2.3, FPC 3.3.1 macOS 12.3.1 aarch64 Xcode 13.4

trev

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 1978
  • Former Delphi 1-7, 10.2 user
Re: AVMidiPlayer not working - Might be one for you, Trev ;-)
« Reply #3 on: November 08, 2021, 12:00:34 am »
This, of course, doesn't work because path is an NSString and url is an NSUrl. How do I convert them to TLabel.Caption-friendly strings?

Add CocoaUtils to the Uses clause, then:

Code: Pascal  [Select][+][-]
  1. ShowMessage(NSStringToString(url.absoluteString));
Lazarus 2.3, FPC 3.3.1 macOS 12.3.1 x86_64 Xcode 13.4
Lazarus 2.3, FPC 3.3.1 macOS 12.3.1 aarch64 Xcode 13.4

carl_caulkett

  • Sr. Member
  • ****
  • Posts: 298
Re: AVMidiPlayer not working - Might be one for you, Trev ;-)
« Reply #4 on: November 08, 2021, 12:02:59 am »
Thanks Trev! I've made some progress. I changed the code to...

      if Form1.PathLabel.Caption='PathLabel' then;
         Form1.PathLabel.Caption:=StrPas(path.cString);
      if Form1.URLLabel.Caption='URLLabel' then;
         Form1.URLLabel.Caption:=StrPas(url.path.cString);

This revealed that the program was looking for the code in the depths of the macOS app...

Should be able to mangle the MIDI path accordingly.
"It builds... ship it!"

macOS 12.3 Monterey
Lazarus 2.3.0 (from src)
FPC 3.3.1 (from src)

carl_caulkett

  • Sr. Member
  • ****
  • Posts: 298
Re: AVMidiPlayer not working - Might be one for you, Trev ;-)
« Reply #5 on: November 08, 2021, 12:18:38 am »
Cool! We have MIDI!!
Revised code attached...

Of course the next problem is that the MIDI contains drums, but the app is automatically playing piano. What I would like ideally is for the app to detect whether the MIDI contain melodic or drum content and play accordingly. The purpose of all of this is to find a way around the inability of macOS to support auditioning MIDI samples. It's been this way since Yosemite days, I understand.

Now to read I=up about channel 10... 😉
« Last Edit: November 08, 2021, 12:35:53 am by carl_caulkett »
"It builds... ship it!"

macOS 12.3 Monterey
Lazarus 2.3.0 (from src)
FPC 3.3.1 (from src)

trev

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 1978
  • Former Delphi 1-7, 10.2 user
Re: AVMidiPlayer not working - Might be one for you, Trev ;-)
« Reply #6 on: November 08, 2021, 12:28:50 am »
I think you will find that you have all of your tracks assigned to midi instrument #1 which just happens to be a grand piano.
Lazarus 2.3, FPC 3.3.1 macOS 12.3.1 x86_64 Xcode 13.4
Lazarus 2.3, FPC 3.3.1 macOS 12.3.1 aarch64 Xcode 13.4

carl_caulkett

  • Sr. Member
  • ****
  • Posts: 298
Re: AVMidiPlayer not working - Might be one for you, Trev ;-)
« Reply #7 on: November 08, 2021, 12:30:15 am »
Trev, it seems I was using some deprecated code with the use of the cString. I've now converted the code to use your suggestion. Thanks! That CocoaUtils unit looks really handy.
« Last Edit: November 08, 2021, 12:31:49 am by carl_caulkett »
"It builds... ship it!"

macOS 12.3 Monterey
Lazarus 2.3.0 (from src)
FPC 3.3.1 (from src)

 

TinyPortal © 2005-2018