Solved

Looping a midi

Posted on 2000-03-15
16
414 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
  • 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
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 
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

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.

Question has a verified solution.

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

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is
In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

808 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