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
LVL 33
hongjunAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
WynConnect With a Mentor Commented:
Sorry,hongjun.
It seems the code cannot repeat play a midi.
Below is my suggestion to achieve this:
================

As I already mentioned,YOu'v got to repeatly call mciSendstring() to play a midi.There is no a parameter you can choose as in playsound().
YOU'V GOT TO TEST IT BYYOURSELF IF IT ENDS!

Okay,under this premise.What shall we do?
In your game,I'm sure you have a Game Main loop which constantly render new scene controled by timer(no matter how you impliment them,mutiltimer or timegettime or whatever).
So ,You can call mciSendString in each loop but you'd set a flag to control it
i.e,look this function:

bool time_enabled=TRUE;

void Start_Or_Restart_Midi_Automatical(void)
{
    (time_enabled)
    mciSendString("play midi from 0", 0, 0, 0);
    time_enabled=false;    
}  



Put it in your game main loop.

You'd use peekmessage to check message.If there aint any message,you'd call game's main loop and keep a variable in your main loop to calculate the time passed.When it exceeds the length of the midi,set time_enable to TRUE,this will do the trick and I think it's the only way.

Hope helpful.
Regards
W.Yinan

 
0
 
WynCommented:
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

0
 
captainkirkCommented:
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.
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
WynCommented:
Yep,check the ReplayMidi function again and you will also get this hint.
0
 
hongjunAuthor Commented:
Adjusted points from 50 to 55
0
 
hongjunAuthor Commented:
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
0
 
WynCommented:
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.
0
 
hongjunAuthor Commented:
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
0
 
WynCommented:
Oh ,my fault .
I misundertand it.

I will comment later because now I'v got to backed school.
0
 
WynCommented:
I have the code to run background game midi repeatedly,I will try to find them and post here later:)
0
 
WynCommented:
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.
0
 
WynCommented:
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.
0
 
WynCommented:
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.
希望有帮助

 
0
 
hongjunAuthor Commented:
Thanks Wyn for your valuable comments. I can loop the midi using time function.

hongjun
0
 
hongjunAuthor Commented:
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
0
 
WynCommented:
hongjun ,happy to answer your question.I will go there next year with live with my relatives:)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.