Playing only part of a .wav file with directSound

Posted on 2005-05-12
Last Modified: 2013-12-08
Hi there,

I need to be be able to take a .wav file and play only part of it eg from 200to 800 ms through the file. I have code that will do it using DirectShow, but I'd like to do it in directSound.  Also, I can play the whole file using DirectSound, based on the examples in the SDK, but I don't know how to just play part of it.  Is it possible?  If the answer is "yes", and I get an explanation of how to make it work, I'll up the points to 250.

I'm using Visual C++ .NET 2003 IDE to write native C++ code, if it makes a difference.

Thank you.
Question by:wilsonian
    LVL 17

    Accepted Solution

    I don't offhand know of a way to do it without copying the data that would get it EXACT -- i.e., you could use a position notification callback to try to stop it really close to where you want it to end, and set the starting position to the desired offset... but no guarantee you'll stop it 'on a dime' where desired...

    Copying the data is pretty trivial -- it's JUST basic PCM audio data we're talking about here.  So clipping out a particular 600ms 'chunk' is simply calculating the byte offset for the start, and the byte length of that many ms, based on the format of the audio (mono vs stereo, samples per second/frequency, etc.).  Just create a DirectSoundBuffer object with the buffer size equal to the 'chunk', Lock the buffer, do a memcpy from the source data ptr + byte offset, to the locked buffer ptr, of the determined byte length.  Unlock, and Play.


    Author Comment

    My boss has decided there are other things it's more important for me to be doing at the moment so I haven't had a chance to try this out but your solution seems sensible so I'll give you the points to save this dragging out.

    Featured Post

    Threat Intelligence Starter Resources

    Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

    Join & Write a Comment

    As game developers, we quickly learn that Artificial Intelligence (AI) doesn’t need to be so tough.  To reference Space Ghost: “Moltar, I have a giant brain that is able to reduce any complex machine into a simple yes or no answer. (http://www.youtu…
    Recently, in one of the tech-blogs I usually read, I saw a post about the best-selling video games through history. The first place in the list is for the classic, extremely addictive Tetris. Well, a long time ago, in a galaxy far far away, I was…
    In this sixth video of the Xpdf series, we discuss and demonstrate the PDFtoPNG utility, which converts a multi-page PDF file to separate color, grayscale, or monochrome PNG files, creating one PNG file for each page in the PDF. It does this via a c…
    Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

    728 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

    20 Experts available now in Live!

    Get 1:1 Help Now