Menu

mciSendString playing sound overlapping?

Anonymous
2003-10-15
2012-09-26
  • Anonymous

    Anonymous - 2003-10-15

    I know how to use mciSendString to play a wav through.  However in games a wav file starts and the next one cuts it off short.  How do I overlap wave files?

    my code here below for refference
    -----8<--------cut here----------->8---------
    #include <mmsystem.h>

    VAR(soundvol, 0, 255, 255);
    VAR(musicvol, 0, 128, 255);

    #define MAXVOL 255

    void cleansound() {};

    void initsound() { };

    void midi(char *name)
    {
       int mcierr;
       if(soundvol && musicvol)
        {
            string sn;
            strcpy_s(sn, "open ");
            strcat_s(sn, name);
            strcat_s(sn, " type sequencer alias midi");
            mcierr = mciSendString("close all", "", 0, 0);
            midiOutSetVolume ( 0, 34592);
            if (mcierr)
               {
                conoutf("failed to close midi sequencer!");
                return;
                };
            mcierr = mciSendString(sn, 0, 0, 0);
            if (mcierr)
               {
                conoutf("failed to load midi music!");
                return;
                };
            mcierr = mciSendString("play midi", 0, 0, 0);
            if (mcierr)
               {
                conoutf("failed to play midi!");
                return;
                };
        };
    };

    COMMAND(midi, ARG_1STR);

    cvector snames;

    int registersound(char *name)
    {
        loopv(snames) if(strcmp(snames[i], name)==0) return i;
        snames.add(newstring(name));
    };

    COMMAND(registersound, ARG_1EST);

    void playsoundc(int n) { addmsg(0, 2, SV_SOUND, n); playsound(n); };

    void playsound(int n, vec *loc)
    {
      int mcierr;
        if(soundvol && musicvol)
        {
            string sn;
            strcpy_s(sn, "open sounds/");
            strcat_s(sn, snames[n]);
            strcat_s(sn, ".wav type waveaudio alias wav");
            waveOutSetVolume ( 0, 34952);
            mciSendString("close all", "", 0, 0);
            if (mcierr)
               {
                conoutf("failed to close sound!");
                return;
                };
            mcierr = mciSendString(sn, 0, 0, 0);
            if (mcierr)
               {
                conoutf(sn);
                return;
                };
            mcierr = mciSendString("play wav", 0, 0, 0);
            if (mcierr)
               {
                conoutf("failed to play sound!");
                return;
                };
        };
    };

    void sound(int n) { playsound(n, NULL); };
    COMMAND(sound, ARG_1INT);

     
    • David Bierbaum

      David Bierbaum - 2003-10-16

      I have no idea.  Maybe someone else here has an answer?

       
    • Nobody/Anonymous

      Wow, I haven't seen ---8<---cut here--->8--- in a long time!  You must be older dirt.  I tried to sign up here but they make it a regular pain in the @ss to do it.... so I post as nobody.   I think this should solve your prob.
      set volume as a DWORD and left and right int
      ---8<---cut here--->8---
      void playsound(int n, vec *loc)
      {
          if(!soundvol) return;
          if(lastmillis==lastsoundmillis) soundsatonce++; else soundsatonce = 1;
          lastsoundmillis = lastmillis;
          if(soundsatonce>5) return;
          string wid;
          if(soundvol && musicvol)
          {  
            switch (soundsatonce) //take a number have a seat
            {
            case 1: strcpy_s(wid, "dev01"); break;
            case 2: strcpy_s(wid, "dev02"); break;
            case 3: strcpy_s(wid, "dev03"); break;
            case 4: strcpy_s(wid, "dev04"); break;
            case 5: strcpy_s(wid, "dev05"); break;
             };
       
              //shut down free device        
              string sn;
              strcpy_s(sn, "close ");
              strcat_s(sn, wid);
              mciSendString(sn, 0, 0, 0);
              //open for sound   
              strcpy_s(sn, "open sounds/");
              strcat_s(sn, snames[n]);
              strcat_s(sn, ".wav type waveaudio alias ");
              strcat_s(sn, wid);
          //stereo volume controlls
              VolumeL = soundvol;
              VolumeR = soundvol;
              if(loc)
                  {
                  vdist(d, v, *loc, player1->o);
      //this will fix your volume    
                  VolumeL -= (int)(d*3*soundvol/100);   
                  VolumeR -= (int)(d*3*soundvol/100);
                   };
              Volume = int(VolumeL*655.35)*65536+int(VolumeR*655.35);
              waveOutSetVolume ( 0, Volume);
              if (mciSendString(sn, 0, 0, 0))
                 {
                  conoutf(sn);
                  return;
                  };
              //play the sound   
              strcpy_s(sn, "play ");
              strcat_s(sn, wid);
              if (mciSendString(sn, 0, 0, 0))
                 {
                  conoutf("failed to play sound!");
                  return;
                  };
          };
      };

       
    • Nobody/Anonymous

      Dang!! they don't make it easy to post here...
      ---8<---cut here--->8---
      Whoa I haven't seen that in years!  You must be older than dirt cause so am I!!  Here is my answer to your prob...
      ---8<---cut here--->8---
      int soundsatonce = 1, lastsoundmillis = 0;
      int VolumeL = 50, VolumeR = 50;
      DWORD Volume;

      void playsound(int n, vec *loc)
      {
          if(!soundvol) return;
      //poll for more than 5 sounds running at once
          if(lastmillis==lastsoundmillis) soundsatonce++; else soundsatonce = 1;
          lastsoundmillis = lastmillis;
          if(soundsatonce>5) return;
          string wid;
          if(soundvol && musicvol)
          {  
            switch (soundsatonce) //take a number have a seat
            {
            case 1: strcpy_s(wid, "dev01"); break;
            case 2: strcpy_s(wid, "dev02"); break;
            case 3: strcpy_s(wid, "dev03"); break;
            case 4: strcpy_s(wid, "dev04"); break;
            case 5: strcpy_s(wid, "dev05"); break;
             };
       
              //shut down free device        
              string sn;
              strcpy_s(sn, "close ");
              strcat_s(sn, wid);
              mciSendString(sn, 0, 0, 0);
              //open for sound   
              strcpy_s(sn, "open sounds/");
              strcat_s(sn, snames[n]);
              strcat_s(sn, ".wav type waveaudio alias ");
              strcat_s(sn, wid);
          //stereo volume controlls
              VolumeL = soundvol;
              VolumeR = soundvol;
              if(loc)
                  {
                  vdist(d, v, *loc, player1->o);
             
                  VolumeL -= (int)(d*3*soundvol/100);   
                  VolumeR -= (int)(d*3*soundvol/100);
                   };
              Volume = (VolumeL*655.35)*65536+(VolumeR*655.35);
              waveOutSetVolume ( 0, Volume);
              if (mciSendString(sn, 0, 0, 0))
                 {
                  conoutf(sn);
                  return;
                  };
              //play the sound   
              strcpy_s(sn, "play ");
              strcat_s(sn, wid);
              if (mciSendString(sn, 0, 0, 0))
                 {
                  conoutf("failed to play sound!");
                  return;
                  };
          };
      };

       
    • Nobody/Anonymous

      Try PlaySound() with SND_ASYNC as its flag =)

      Kip

       

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.