Solved

Looping a midi

Posted on 2000-03-15
16
417 Views
Last Modified: 2008-02-01
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
0
Comment
Question by:hongjun
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 10
  • 5
16 Comments
 
LVL 5

Expert Comment

by:Wyn
ID: 2620230
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
 
LVL 4

Expert Comment

by:captainkirk
ID: 2620300
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
 
LVL 5

Expert Comment

by:Wyn
ID: 2620354
Yep,check the ReplayMidi function again and you will also get this hint.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 33

Author Comment

by:hongjun
ID: 2621986
Adjusted points from 50 to 55
0
 
LVL 33

Author Comment

by:hongjun
ID: 2621987
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
 
LVL 5

Expert Comment

by:Wyn
ID: 2622177
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
 
LVL 33

Author Comment

by:hongjun
ID: 2622385
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
 
LVL 5

Expert Comment

by:Wyn
ID: 2622499
Oh ,my fault .
I misundertand it.

I will comment later because now I'v got to backed school.
0
 
LVL 5

Expert Comment

by:Wyn
ID: 2622501
I have the code to run background game midi repeatedly,I will try to find them and post here later:)
0
 
LVL 5

Accepted Solution

by:
Wyn earned 55 total points
ID: 2622554
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
 
LVL 5

Expert Comment

by:Wyn
ID: 2622562
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
 
LVL 5

Expert Comment

by:Wyn
ID: 2622565
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
 
LVL 5

Expert Comment

by:Wyn
ID: 2622576
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
 
LVL 33

Author Comment

by:hongjun
ID: 2626318
Thanks Wyn for your valuable comments. I can loop the midi using time function.

hongjun
0
 
LVL 33

Author Comment

by:hongjun
ID: 2626321
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
 
LVL 5

Expert Comment

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

Featured Post

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your companys IT infrastructure and keeping it secure in todays hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question