Link to home
Start Free TrialLog in
Avatar of hongjun
hongjunFlag for Singapore

asked on

Looping a midi

The below program compiles successfully and will run and play the midi. However, may I know how to make the midi loops?

#include <windows.h>
#include <mmsystem.h>

void mid_ending(void);

void main()
{
      mid_ending();
}

void mid_ending(void)
{
      char* ReturnString = (char*)malloc(30 *sizeof(char));
      LPTSTR lpszReturnString = ReturnString;

      mciSendString(      "open ending.mid type sequencer alias finch",
                              lpszReturnString, lstrlen(lpszReturnString), NULL);
      mciSendString(      "set finch time format samples", lpszReturnString,
                              lstrlen(lpszReturnString), NULL);
      mciSendString(      "play finch", lpszReturnString,
                              lstrlen(lpszReturnString), NULL);
}

hongjun
Avatar of Wyn
Wyn

Hellow ,hongjun

Check below:
=============
/  // Includes....
 #include "windows.h"
 #include "midi.h"
 #include "stdio.h"  

// Externals.... extern HWNDg_hWnd;  
//------------------------------------------------------------------
//  
// Function: PlayMidi()
//
// Purpose: Plays a midi file
//
//------------------------------------------------------------------  
BOOL PlayMidi(char *sFileName)
{      
char buf[256];      
sprintf(buf, "open %s type sequencer alias MUSIC", sFileName);

if (mciSendString("close all", NULL, 0, NULL) != 0)      
{
return(FALSE);      
}
       
if (mciSendString(buf, NULL, 0, NULL) != 0)
{
return(FALSE);
}

if (mciSendString("play MUSIC from 0", NULL, 0, g_hWnd) != 0)
{
return(FALSE);      
}

// Yahoo!
return TRUE;
}  

//------------------------------------------------------------------
//  
// Function: PauseMidi()
//
// Purpose: Pauses midi file
//
//------------------------------------------------------------------  
BOOL PauseMidi()
{      
// Pause if we're not already paused...      
if (mciSendString("stop MUSIC", NULL, 0, NULL) != 0)      
{
return(FALSE);      
}

// Yahoo!      
return TRUE;
}  

//------------------------------------------------------------------
//  
// Function: ResumeMidi()
//
// Purpose: Resumes playing of a midi file
//
//------------------------------------------------------------------
BOOL ResumeMidi()
{      
// Resume midi      
if (mciSendString("play MUSIC notify", NULL, 0, g_hWnd) != 0)
{
return(FALSE);
}

// Yahoo!

return TRUE;
}

//------------------------------------------------------------------
//  
// Function: StopMidi
//
// Purpose: Stops a midi file playing
//
//------------------------------------------------------------------  
BOOL StopMidi()
{      
if (mciSendString("close all", NULL, 0, NULL) != 0)
{
return(FALSE);
}

// Yahoo!
return TRUE;
}

//------------------------------------------------------------------
//  
// Function: ReplayMidi()
//
// Purpose: Replays a midi file
//
//------------------------------------------------------------------  
BOOL ReplayMidi()
{      
// Replay midi
if (mciSendString("play MUSIC from 0 notify", NULL, 0, g_hWnd) != 0)
{
return(FALSE);
}

return TRUE;
}  

====================

And check:

Playing a MIDI File
http://msdn.microsoft.com/isapi/msdnlib.idc?theURL=/library/psdk/multimed/mci_3wpx.htm

Does above solve all your troubles?
Regards

If you just want to repeat a MIDI segment, just put the mciSendString() call in a loop (for, while, etc.) and use the starting and ending position parameters in the mciSendString() call if you want to just play a part of the midi file.
Yep,check the ReplayMidi function again and you will also get this hint.
Avatar of hongjun

ASKER

Adjusted points from 50 to 55
Avatar of hongjun

ASKER

By the way what I need is a code that is like PlaySound(...,SND_LOOP); that need me to include this statement once and then need not to worry about it anymore. If there's a similar way of looping a midi as looping a wave file, can I have the full proper code with void main() included.

Thanks Wyn for your help but I just cannot gather the pieces and form them into a proper program. I am only a beginer in C++.

Thanks
hongjun
No , you'd use a for loop to repeat it.
Like ReplayMid() function above , if you'd replay it repeatedly,you'd put it into a loop ...no matter while ,for ,do-while or so...

I have given you a link for documents.
Avatar of hongjun

ASKER

But I want the midi to run and act as a background sound. Example in a plane game, the midi is the background and the user is still able to fire bullet and move to kill enemy planes. Using a loop wouldn't it affects the flow of the game?

hongjun
Oh ,my fault .
I misundertand it.

I will comment later because now I'v got to backed school.
I have the code to run background game midi repeatedly,I will try to find them and post here later:)
ASKER CERTIFIED SOLUTION
Avatar of Wyn
Wyn

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Let me know if it works(It should work,right? :)
Btw:above have a typo ,I miss a if statement before (time_enalbe).I think you have detected it out.
Let me know if it works(It should work,right? :)
Btw:above have a typo ,I miss a if statement before (time_enable).I think you have detected it out.

Regards
WANG.
In case your misundertanding ,in my first comment after you reject me,I said you'd use loops.But notice the way to use loop.Now in above suggestion ,you'd put it into your game rendering loop function.
~{O#M{SP0oVz~}

 
Avatar of hongjun

ASKER

Thanks Wyn for your valuable comments. I can loop the midi using time function.

hongjun
Avatar of hongjun

ASKER

The below code will loop midi 5 times.

#include <stdio.h>
#include <time.h>
#include <windows.h>
#include <mmsystem.h>

void main()
{
      time_t time_now, time_begin;
      int time_diff;
      char* ReturnString = (char*)malloc(30 *sizeof(char));
      int i=1;

      LPTSTR lpszReturnString = ReturnString;
      time_now = time(NULL);
      time_begin = time(NULL);
      time_diff = 0;

      while ( i <= 5 )
      {
            if ( time_diff == 0 )
            {
                  mciSendString("open a.mid type sequencer alias finch",
                                       lpszReturnString, lstrlen(lpszReturnString), NULL);
                  mciSendString("set finch time format samples", lpszReturnString,
                                       lstrlen(lpszReturnString), NULL);
                  mciSendString("play finch", lpszReturnString,
                                       lstrlen(lpszReturnString), NULL);
            }
            time_now = time(NULL);
            time_diff = time_now - time_begin;
            if ( time_diff > 5 )
            {
                  i++;
                  time_diff = 0;
                  time_begin = time(NULL);
                  mciSendString("close finch", lpszReturnString,
                                      lstrlen(lpszReturnString), NULL);
            }
      }
                  mciSendString("close finch", lpszReturnString,
                                      lstrlen(lpszReturnString), NULL);
}

hongjun
hongjun ,happy to answer your question.I will go there next year with live with my relatives:)