?
Solved

getting length of  a  wav file

Posted on 1998-05-31
2
Medium Priority
?
295 Views
Last Modified: 2010-08-05
I know it is possible to get the length of a wav file (in milleseconds or seconds) but have not been able to do it.  Below is a function that is supposed to return the length of a wave file (IT DOESN'T WORK... IT RETURNS 0.0 EVERY TIME).  The code uses MCI_WAVE_OPEN_PARMS and mciSendCommand() to open a file (which it does with out error) but the dwBufferSeconds member of MCI_WAVE_OPEN_PARMS is always 0.0 after opening the wav.  Am I wrong in assuming that dwBufferSeconds should hold the length of the wav file opened or am I missing a needed flag or something?  Does somebody know what I am doing wrong or know of another way to get the length of a wav file?  Thanks.

// Function takes LPSTR pointer to string containing the
// path to a wave file, returns length of wave file
long CPlaywaveDlg::GetWavLengthSeconds(LPSTR wavPath)
{       // wavPath points to string containing wave file path
      MCI_WAVE_OPEN_PARMS mciOpenParms;
      
      DWORD mciError, mciFlags;
      int wDeviceID;
#define BUF_LEN 256
      char mciErrorString[BUF_LEN];

      //Initialize data structures...
      mciOpenParms.dwCallback = 0;  // no callback
      mciOpenParms.wDeviceID = 0;  
      mciOpenParms.lpstrDeviceType = "waveaudio";
      mciOpenParms.lpstrElementName = wavPath;
      mciOpenParms.lpstrAlias= NULL;       // no alias
      mciOpenParms.dwBufferSeconds = 0L;  
      mciFlags = MCI_OPEN_TYPE | MCI_OPEN_ELEMENT

      // Send the open command
      mciError = mciSendCommand(0, MCI_OPEN, mciFlags, (DWORD)&mciOpenParms);
      if (!mciError)
      {

            // if open was successful, retrieve the length
            double secs = mciOpenParms.dwBufferSeconds;
               
            mciError = mciSendCommand(wDeviceID, MCI_CLOSE, 0, 0L); // close the file
            }
      

            return secs;  // return the length

      }
      else
      {
            int dummy = mciGetErrorString(mciError, mciErrorString, BUF_LEN);
            
            CString errorMsg = "Media Control Interface Error ";
            errorMsg += mciErrorString;
            MessageBox(errorMsg, "Error", MB_ICONSTOP);
      }
      return -999;  // error

}

0
Comment
Question by:cww1
[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
2 Comments
 

Author Comment

by:cww1
ID: 1164999
Edited text of question
0
 
LVL 3

Accepted Solution

by:
tma050898 earned 200 total points
ID: 1165000
cww1,

I only have a minute to post this so I'm tryig to copy & paste this out of my code very quickly. Try this and if it doesn't work I'll take a little more time when I get back home...

// Get Minutes...
MCI_STATUS_PARMS StatusParms;
StatusParms.dwItem = MCI_STATUS_POSITION;
mciSendCommand( m_wDeviceID, MCI_STATUS, MCI_WAIT | MCI_STATUS_ITEM, (DWORD)(LPVOID) &StatusParms);

iMinutes = ((int)((StatusParms.dwReturn / 1000) / 60 ));


// Get seconds...
MCI_STATUS_PARMS StatusParms;
StatusParms.dwItem = MCI_STATUS_POSITION;
mciSendCommand( m_wDeviceID, MCI_STATUS, MCI_WAIT | MCI_STATUS_ITEM, (DWORD)(LPVOID) &StatusParms);

iSeconds = (int)((StatusParms.dwReturn / 1000 ) % 60 ));

Tom
0

Featured Post

Want to be a Web Developer? Get Certified Today!

Enroll in the Certified Web Development Professional course package to learn HTML, Javascript, and PHP. Build a solid foundation to work toward your dream job!

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…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
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…

765 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