Solved

Looping a midi

Posted on 2000-03-15
16
397 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
 
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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
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.
~{O#M{SP0oVz~}

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
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.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

705 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now