PB 6.xx and MIDI

Just starting out? Need help? Post your questions and find answers here.
infratec
Always Here
Always Here
Posts: 7613
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

PB 6.xx and MIDI

Post by infratec »

Isn't it possible to play midi files with native PB commands???

I tried all: Movie, Sound and Music

I thought ModPlug can play MIDI.

Any hint how to play a midi file with native PB commands?

My MIDI file plays in windows without problems:
1. TotalCommander F3
2. VLC (you need a SF2 bank file)
3. OpenMPT

Just looked inside libmodplug.lib and there I can find the word MIDI several times.

https://github.com/Konstanty/libmodplug
- plays all types of MIDI files (*.mid)

Code: Select all

InitSound()

Filename$ = OpenFileRequester("Choose a MIDI file", "", "MIDI|*.mid", 0)
If Filename$
  If LoadMusic(0, Filename$)
    PlayMusic(0)
    Delay(5000)
    StopMusic(0)
    FreeMusic(0)
  Else
    Debug "Failed to load MIDI"
  EndIf
EndIf
Strange ...

If I use #PB_Any, the program does not terminate correct in the IDE with PB 6.21 b10 x86 on win10 x64 with asm backend.

Code: Select all

InitSound()

Filename$ = OpenFileRequester("Choose a MIDI file", "", "MIDI|*.mid", 0)
If Filename$
  Music = LoadMusic(#PB_Any, Filename$)
  If Music
    PlayMusic(Music)
    Delay(5000)
    StopMusic(Music)
    FreeMusic(Music)
  Else
    Debug "Failed to load MIDI"
  EndIf
EndIf
User avatar
idle
Always Here
Always Here
Posts: 5888
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: PB 6.xx and MIDI

Post by idle »

have to use api for now it seems

Code: Select all

Procedure playMIDIFile(hWndNotify,MIDIFileName.s)

    Protected wDeviceID.l;
    Protected dwReturn.l;
    Protected mciOpenParms.MCI_OPEN_PARMS
    Protected mciPlayParms.MCI_PLAY_PARMS
    Protected mciStatusParms.MCI_STATUS_PARMS
    Protected mciSeqSetParms.MCI_SEQ_SET_PARMS
   
    mciOpenParms\lpstrDeviceType = @"sequencer";
    mciOpenParms\lpstrElementName = @MIDIFileName;
    dwReturn = mciSendCommand_(#Null, #MCI_OPEN,#MCI_OPEN_TYPE | #MCI_OPEN_ELEMENT,@mciOpenParms)
    If dwReturn    
       ProcedureReturn dwReturn;
    EndIf 
   
    wDeviceID = mciOpenParms\wDeviceID;

     mciStatusParms\dwItem = #MCI_SEQ_STATUS_PORT;
    dwReturn = mciSendCommand_(wDeviceID, #MCI_STATUS,#MCI_STATUS_ITEM,@mciStatusParms)
    If dwReturn
        mciSendCommand_(wDeviceID, #MCI_CLOSE, 0, #Null);
        ProcedureReturn dwReturn;
    EndIf 
    
    mciPlayParms\dwCallback = hWndNotify;
    dwReturn = mciSendCommand_(wDeviceID, #MCI_PLAY, #MCI_NOTIFY,@mciPlayParms)
    If dwReturn
        mciSendCommand_(wDeviceID, #MCI_CLOSE, 0, NULL);
        ProcedureReturn dwReturn
    EndIf 

    ProcedureReturn 1
 EndProcedure 
 
 
  hwin = OpenWindow(-1,0,0,200,60,"midi",#PB_Window_Invisible)
    playMIDIFile(WindowID(hwin),midifile$) 
    Repeat  
      ev = WaitWindowEvent() 
    Until ev = #PB_Event_CloseWindow Or ev = #MM_MCINOTIFY   

RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4953
Joined: Sun Apr 12, 2009 6:27 am

Re: PB 6.xx and MIDI

Post by RASHAD »

Hi infratec
Do I miss something ?
viewtopic.php?t=86900
Egypt my love
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4953
Joined: Sun Apr 12, 2009 6:27 am

Re: PB 6.xx and MIDI

Post by RASHAD »

Hi idle

Code: Select all

playMIDIFile(WindowID(hwin),midifile.s) 
Egypt my love
User avatar
idle
Always Here
Always Here
Posts: 5888
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: PB 6.xx and MIDI

Post by idle »

RASHAD wrote: Sun Jun 08, 2025 12:06 am Hi idle

Code: Select all

playMIDIFile(WindowID(hwin),midifile.s) 
that's a bit more clear, I changed it to midifile$
infratec
Always Here
Always Here
Posts: 7613
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: PB 6.xx and MIDI

Post by infratec »

RASHAD wrote: Sun Jun 08, 2025 12:00 am Hi infratec
Do I miss something ?
viewtopic.php?t=86900
Hi RASHAD,

I tried all possibilities.
And I tested now your program. It results in:
Can't load the sound.
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4953
Joined: Sun Apr 12, 2009 6:27 am

Re: PB 6.xx and MIDI

Post by RASHAD »

Hi Berned
Strange !
What about idle and MCI ?
Can you upload your file somewhere to test it ?
Egypt my love
infratec
Always Here
Always Here
Posts: 7613
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: PB 6.xx and MIDI

Post by infratec »

RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4953
Joined: Sun Apr 12, 2009 6:27 am

Re: PB 6.xx and MIDI

Post by RASHAD »

Hi Berned
Both files worked fine with the PB snippet and with idle snippet
PB 6.20 x86 Windows 11 x64
So I presume that the problem is your machine sound device or something else
Egypt my love
infratec
Always Here
Always Here
Posts: 7613
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: PB 6.xx and MIDI

Post by infratec »

With Windows API it works always. (idle way)

With you code it works with PB 6.20 x86, but not with PB 6.21 b10 x86.
Maybe it's a bug in PB 6.21.
Post Reply