Load and Play audio file (either sound or music) from my computer using DirectSound in C++

#include "stdafx.h"
#include <dsound.h>
class AudioPlayer
private:
unsigned long long start_point,
playback_point,
break_point;
bool now_playing, loop, ignore_start_point;
int speed;
//and any data that stores an audio (can be either sound or music)
public:
void Play_or_Resume(); //Either starts playing or resumes the audio stored in private (Runs a thread that either increments or decrements the playback_point by the speed member and sets the now_playing member to true).
void Pause(); //Stops playing the audio stored in private until Play_or_Resume method is called (Just suspends the thread that was mentioned above and sets the now_playing member to false).
void Play(unsigned long long from = 0, unsigned long long length = -1, bool move_start_point_too = true);
//Plays audio from the value of the 'from' parameter along the value of the 'length' parameter and sets the now_playing member to true.
//Playback point is pushed to the value of the 'from' parameter, and break point is pushed to the result of from + length.
//If move_start_point_too parameter is true, then also start point is pushed where playback point is pushed too (to the value of the 'from' parameter).
//Also the value of the 'from' parameter is an absolute position, but otherwise (if move_start_point_too parameter is false) it ('from' parameter) is relative to start point's position until it (start point) is ignored.
//The value of the 'from' parameter never can be negative!
void MovePlaybackPoint(unsigned long long new_position); //Modifies playback point position that is stored in private to a new position that indicates to sample no. new_position.
unsigned long long GetPlaybackPointPosition(); //Returns playback point's position that has been alreadyS mentiond above.
unsigned long long GetLength(); //Returns total number of samples in the audio stored in private.
void MoveBreakPointTo(unsigned long long new_position); //Moves the break point to a new position that indicates to sample no. new position.
//If the loop member is false, then audio should always stops playing and also set the now_playing member to false, when playback point reaches the break point, even the Pause method was not called.
//Then calling again the Play_or_Resume method will bring the playback point back to start point (the default value for this member is zero 0).
//But if the loop member is true, then playback point just returns to start point immediately and continue playing from there (now_playing remains true and it's value is unchanged).
//There is a possibility to move the break point to a negative integer, so audio either stops and sets now_playing to false or replays from start point and left now_playing true according to the loop member, at the end of the audio. The default value of break point member is minus one -1 .
unsigned long long GetBreakpointPosition(); //Returns the position of the break point that has been already mentioned above.
void MoveStartPointTo(unsigned long long new_position); //Moves the start point to a new position that indicates to sample no. new position.
unsigned long long GetStartPointPosition(); //Returns the position of the start point that has been already mentioned above.
void EnableRepeatMode(); //Sets the private loop member to true.
void DisableRepeatMode(); //Sets the private loop member to false.
void ToggleRepeatMode(); //If loop was true before calling this function, so after it is false. Otherwrise it is true.
bool IsRepeating(); //Returns whether audio repeats itself (from start point to either breakpoint or end of buffer). This function just returns the value of the loop member.
void SetSpeed(int num_sam_per_sec); //Modifies the number of samples that are played in every second.
//If number (integer) is negative, then audio is played in backward.
//When playback point reaches start point, then it is transferred to break point if it's position is not negative. Otherwise it is transferred to the end of the audio buffer.
void IgnoreStartPoint(); //Sets the ignore_start_point member to true. Then all methods always relate to start point as zero (even if it's value is not zero).
void ReferStartPoint(); //Sets the ignore_start_point member to false.
bool IsStartPointIgnored(); //Returns whether start point is ignored or not. This method just returns the value of ignore_start_point member.
int GetSpeed(); //Returns the number of samples that are played in every second.
void SetNumberOfChannels(byte new_value); //Modifies number of channels.
byte GetNumberOfChannels(); //Returns number of channels.
void SetBlockAlign(byte new_value); //Sets block align.
byte GetBlockAlign(); //Returns block align.
const char* LoadAudioFromComputer(const char* path_and_name_of_file, AudioPlayer* lpAp, bool auto_start_playing = true);
//This function fills the instance pointed by 'lpAp' parameter with the data of the audio file redirected by 'path_and_name_of_file' parameter, and calls the Play_or_Resume method of this instance if auto_start_playing is true.
//If the function succeeds, then it returns "succeed". Otherwise (if the function fails) then it returns an error string that describes the problem (the reason it failed). It can be sent to output, so it can be read, by either call to MessageBox, printf, std::cout, TextOut, DrawText functions and etc.
//The function can fails, because the file was not exist, or could not be opened for reading, because it was opened by another process, or the file was either encrypted or protected, or corrupted, or was not audio file, but text, image and etc.
I want to implement all methods of the AudioPlayer class and the LoadAudioFromComputer function by myself using DirectSound (the <dsound.h>) and all functions, interfaces, methods, structures, etc... that this header provides, but I don't know how! I
need your help! This is difficult for me to find in the internet the information I need to do this. MSDN is teaching all this stuff, but this is too difficult! I need you to implement all these methods of the AudioPlayer class and the LoadAudioFromComputer
function for me using DirectSound and the <dsound.h> and the explanations I wrote in comments, and then post the code. I will copy it to mine and read it all to learn. Please use comments so I can understand. I will thank and appreciate anyone who will
donate his time to help me and do this for me! :D

Microsoft pulled the plug on DirectSound HAL in Vista due to lack of hardware support. The SAL exists for
software compatibility, good intention but bad performance as there is no longer a direct path from
DirectSound to audio drivers.
People choose DirectSound for its Direct-ness
but there is no reason to choose it now.
DirectX SDK was integrated
with WIndows SDK which ships with Visual Studio 2012 so you don't need additional downloads for
WASAPI and XAudio 2.
Just look up the documentation for headers/librarieslike every other Windows API you use. If you have a hard time to find the documentation then you need to go to search engines and find search engine tutorials. 
There are plenty of samples for both WASAPI and XAudio 2, both in the Windows SDK and online. The DirectX team has some suggestions on which to use on their team blog.
You can find experts for those APIs at the Windows Desktop Pro-Audio Application Development forum and the
Audio/XACT forum on MSDN (link left out for you to practice your search skills). Again, it is fine to ask for hints but don't ask the whole
solution. You are competing with others who just need a hint to finish work here. The time spent on working on your assignment would be better spent on helping on giving hints to others. 
Visual C++ MVP

Similar Messages

  • Can anybody explain me how to sample and play audio files with logic's EXS2

    can anybody explain me how to sample and play audio files with logic's EXS24 Sampler???
    i cant find a way to upload and manage my own audio content on this sampler...

    i uderstand , thanx...
    i managed to open an audio file and placed it in the sampler,i can play t sample in the little keyboard in the zones section, howver i dont know how to play it with my controller... the sample shows in C1 on logic's keyboard but if i play C1 on my controller nothing happens... how can i fix this?
    Also, i noticed the sample plays from beginning to end once i click on it, how do i do to just make it last until i release the key? like a logic sound??? (in case i want to play a small portion of the sample only)
    Thanx

  • Help! This probably sounds dumb, but I got my Ipod touch fourth generation yesterday and they said I could sync the music from my computer to the Ipod. Could you guys help me do that? All the websites I have looked on don't help. Thanks!

    Help! This probably sounds dumb, but I got my Ipod touch fourth generation yesterday and they said I could sync the music from my computer to the Ipod. Could you guys help me do that? All the websites I checked didn't help. Thanks!

    You need to read the manual
    Click Support at the top of this page, then click manuals
    Open itunes, connect ipod, select what you want, sync

  • How to import and play audio files in java

    Hi, I'm pretty new to java and im trying to make an application that can import and play simple audio clips (mp3, wav, etc formats) when a button is clicked. If any one can help me it would be greatly appreciated.

    hi,
    i'm not a professional with java sound but it may be possible that you will find something good at this link:
    http://www.jsresources.org/examples/
    peace

  • Recording and playing audio files

    hi, is there a way by which one can record and play an audio file in java ?? I am doing a project in Voip but I can't find any help in this matter(actually i am a newbie in java ) .. can anyone please tell me how to proceed?
    Edited by: streetfi8er on Jun 28, 2009 2:47 AM

    >
    hi, is there a way by which one can record and play an audio file in java ?? I am doing a project in Voip but I can't find any help in this matter(actually i am a newbie in java ) .. can anyone please tell me how to proceed?>You could not find anything on ['java audio'|http://lmgtfy.com/?q=java+audio]? Where did you look, underneath the bed and in the back yard?
    Note that you might help dispel my impression that you are lazy, by applying the shift key once at the start of each sentence.

  • I have a mac laptop OS 10.6.5, and recently upgraded to Firefox 4 beta, but when I try to use my hotmail account, it either never stops loading and/or many files either won't open at all or keep crashing.

    When I open email in my inbox, they continually load and flash. I often have to use the "back" arrow several times to make it stop. Sometimes the message just won't open at all. Also, I can't open "sent" mail at all.

    There's an HTML5 compatibility issue with Windows Live / Hotmail that affects the Firefox 4.0 betas. A Hotmail rep has said that will be fixed before the Firefox 4.0 Release Candidate comes out.
    Try hitting the '''Esc''' key to stop that "cycling".

  • When I use Home sharing, I can see the library I want to copy, but when I highlight the library, the import button does not show up. Also, it will not let me drop and drag. How do I get music from one computer to the other computer?

    When I use Home Sharing, I can see the library I want to copy under Shared, but I can't get it save on the computer under the regular library. I go to edit and "select all" but there is no import button that is in the right hand corner. Also, it will not let me click and drag the music. How do I get it from one computer to the other?

    Since both computers are connected via the network, you could simply copy the ENTIRE iTunes folder from one computer to another via the network.
    If both computers are running Windows, use the Easy File Transfer Utility built into Windows to move iTunes and all other user media/data.

  • TS1717 I can play audio files in iTunes but I can't play downloaded tv shows or movies.  Everytime I attempt to play them, iTunes stops working and closes.

    I can play audio files in iTunes but I can't play downloaded tv shows or movies.  Everytime I try to play them, iTunes stops working and closes.  Any information will help.

    Is QuickTime installed? If not it might help to install it.
    If so is Direct3D Video Acceleration enabled? If so it might help to disable.
    tt2

  • The audo files do not play from websites instead ask for download. This has occured from the time I have updated Firefox 4 to Firefox 5. Why? And how can i play audio files?

    The audo files do not play from websites instead ask for download. This has occured from the time I have updated Firefox 4 to Firefox 5. Why? And how can i play audio files from websites?

    You have a <b>general.useragent.override</b> pref that identifies you as Firefox/3.5.7
    *Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.1.7) Gecko/20091221 Firefox/3.5.7 (.NET CLR 3.5.30729)
    See:
    *https://support.mozilla.com/kb/Websites+or+add-ons+incorrectly+report+incompatible+browser
    *http://kb.mozillazine.org/Resetting_your_useragent_string_to_its_compiled-in_default

  • Quicktime video (.mov file) will not load and play in Firefox 3.6.16

    I have a computer with Firefox 3.6.16 installed that will not play Quicktime videos (.mov files) in the browser. This is a desktop computer.
    Another computer (laptop) with exactly the same version of Firefox installed will load and play them perfectly in the Quicktime player.
    Both computers have Quicktime addin 7.6.9.0 installed and designated as the default application for Quicktime movies.
    Both computers play the videos correctly in IE 8.
    When attempting to play the video in Firefox, the player window opens and attempts to load the video, but soon seems to freeze leaving a black window open where the player should be.
    From what I have observed, this seems to be a problem related specifically to this computer, and since the video plays in IE I have ruled out the firewall blocking loading from the site.

    Hi, I've exactly the same problem, 'cept I've XP Pro. When I say exactly, I mean the format used is HDV 1080i60, along with everything else there.
    And I'm right there with you. Why Quicktime can't play a natively encoded HDV Quicktime file (using Final Cut Pro), is beyond me, but basically, I've used K-Lite Codec Pack, and Quicktime. Neither work, although Quicktime does ask for internet connectivity to download resources or something similar (I don't have Internet Access at home). I've used VLC on a friend's PC, and, while it works, there is a very strange, and irritating flicker where there's heavy movement
    Where can I get the resource needed, as another party needs to view the clip, and they already have it. So if they can update their codec library or similar, then that would be much better (as I wouldn't need to upload a FullHD Video again)
    thanks

  • 1120 error when attempting to load and play sounds

    I'm attempting to load and play a sound (which seems quite
    basic) but I am getting an 1120 error. Could anyone please help me
    figure out what I'm doing wrong?
    var hitSound:Sound = new Sound();
    var reqHitSound:URLRequest = new URLRequest("myAudio.mp3");
    hitSound.load(reqHitSound);
    hitSound.play();
    1120: Access of undefined property hitSound
    1120: Access of undefined property reqHitSound

    Yes, the mp3 is good and the path is correct. I'll put my
    complete code below.
    The error I get is:
    1120: Access of undefined property hitSound. (line 14 -
    hitSound.load(reqHitSound);)
    1120: Access of undefined property reqHitSound. (line 14 -
    hitSound.load(reqHitSound);)
    1120: Access of undefined property hitSound. (line 15 -
    hitSound.play();)
    Its almost like the code doesn't feel I've really defined
    hitSound as a Sound.
    Help please? Am I missing a component?

  • Loading and Playing Multiple Sounds

    I have written a loadSound function which handles loading and playing multiple sounds. This function works fine when I call this function in another keyframe i.e. the frame in which the function is not written. But when I call the function on the same frame in which the function is written the sounds gets mixed up. For example the function is written in 1st keyframe. If I call this function in 2nd, 3rd and 4th keyframe (with different sounds) it works fine. But when i call the same function in 1st, 2nd and 3rd keyframe it all gets messed up.
    In 1st keyframe the sound plays properly. When I move to 2nd Keyframe the 2nd sound plays. But when I move back to the 1st keyframe the 2nd and 1st keyframe sounds all get mixed up. This doesn't happen when I call the function in 2nd, 3rd and 4th keyframes.
    Below is my function:
    import flash.events.Event;
    import flash.media.Sound;
    import flash.net.URLRequest;
    import flash.events.ProgressEvent;
    import flash.events.IOErrorEvent;
    import flash.errors.IOError;
    import flash.media.SoundChannel;
    import flash.media.SoundMixer;
    import flash.events.TimerEvent;
    import flash.utils.Timer;
    import flash.display.MovieClip;
    import flash.media.SoundTransform;
    stop();
    var s:Sound;
    var localSound:SoundChannel = new SoundChannel();
    var req:URLRequest;
    var loadedPct:uint;
    var currentFrameArr:Array = ["One","Two","Three"];
    var urlArr:Array = ["sound/m01_t01_s01.mp3","sound/m01_t01_s02.mp3","sound/page10_11.mp3"];
    function loadSnd(currentFrm:String)
    s = new Sound();
    s.addEventListener(ProgressEvent.PROGRESS, onSoundProgress);
    s.addEventListener(Event.COMPLETE, onSoundLoaded);
    s.addEventListener(IOErrorEvent.IO_ERROR, onIOError);
    localSound.stop();
    for (var i:uint=0; i<currentFrameArr.length; i++)
      if (currentFrm == currentFrameArr[i])
       req = new URLRequest(urlArr[i]);
    s.load(req);
    function onSoundProgress(evt:ProgressEvent):void
      loadedPct = Math.round(100 * (evt.bytesLoaded / evt.bytesTotal));
      trace("The sound is " + loadedPct + " % loaded");
    function onSoundLoaded(evt:Event):void
      localSound = s.play();
    function onIOError(evt:IOErrorEvent):void
      trace("The sound could not be loaded: " + evt.text);
    Call to this function in different frames:
    loadSnd("One");
    loadSnd("Two");
    etc
    If I pass the parameter 'One' in loadSnd it plays the 1st element in urlArray, 'Two' 2nd element and so on.
    I have been scratching my head since long over this.
    Can anybody please pinpoint the error.

    Got the solution. The SoundMixer.stopAll(); statement did the trick. The complete workable function is pasted below:
    import flash.events.Event;
    import flash.media.Sound;
    import flash.net.URLRequest;
    import flash.events.ProgressEvent;
    import flash.events.IOErrorEvent;
    import flash.errors.IOError;
    import flash.media.SoundChannel;
    import flash.media.SoundMixer;
    import flash.events.TimerEvent;
    import flash.utils.Timer;
    import flash.display.MovieClip;
    import flash.media.SoundTransform;
    stop();
    var s:Sound;
    var localSound:SoundChannel = new SoundChannel();
    var req:URLRequest;
    var loadedPct:uint;
    var currentFrameArr:Array = ["One","Two","Three"];
    var urlArr:Array = ["sound/m01_t01_s01.mp3","sound/m01_t01_s02.mp3","sound/page10_11.mp3"];
    function loadSnd(currentFrm:String)
      s = new Sound();
      s.addEventListener(ProgressEvent.PROGRESS, onSoundProgress);
      s.addEventListener(Event.COMPLETE, onSoundLoaded);
      s.addEventListener(IOErrorEvent.IO_ERROR, onIOError);
      SoundMixer.stopAll();
      localSound.stop();
      for (var i:uint=0; i<currentFrameArr.length; i++)
      if (currentFrm == currentFrameArr[i])
      req = new URLRequest(urlArr[i]);
      break;
      s.load(req);
    function onSoundProgress(evt:ProgressEvent):void
      loadedPct = Math.round(100 * (evt.bytesLoaded / evt.bytesTotal));
      trace("The sound is " + loadedPct + " % loaded");
    function onSoundLoaded(evt:Event):void
      localSound = s.play();
    function onIOError(evt:IOErrorEvent):void
      trace("The sound could not be loaded: " + evt.text);
    Calling the function
    loadSnd("One");
    loadSnd("Two"); etc

  • Play Audio Files in Pro7

    I loaded QT Pro 7 in Win2K and I can't play audio files. The progress pointer slides across but no audio and no indication on the little bar graphic thingy.
    Is it the file association? The sound card seems to be set up properly.
    Thanks,
    Bill

    Safe Mode,Wave out only should release so it could be a indication of the problem.
    If you go to C:\Documents and Settings\All users\Application Data\Apple Computer\Quicktime, Quicktime qtp is the file for QT preferences. If you delete Quicktime qtp a new one will be created when you run QT again.
    It will also be removing your Pro Key registration, so you'll want to have that before deleting qtp so you'll be able to re-enter it to get your Pro features back.
    Give it a try, if it don't solve the audio, I can suggest a few more things to try within the XP OS that relate to audio.

  • I am having trouble with my speakers not working while online in particular on windows.My audio works when playing audio files

    I am having trouble with my speakers not working while online particular on facebook. My audio works when playing audio files.

    Hi,
    Did it happen all the time or sometime?
    Please check online browser status:
    Click Volume icon in the taskbar, click Mixer link button as below:
    If it's fine, follow this guide to run troubleshooter to detect and fix the issue:
    Tips for fixing common sound problems
    http://windows.microsoft.com/en-in/windows/tips-fixing-common-sound-problems#tips-fixing-common-sound-problems=windows-7
    Meanwhile, this similar thread also could be referred:
    https://social.technet.microsoft.com/forums/ie/en-US/a4a1cfe5-93a5-4c0b-9bf6-f7db0304f2ba/no-sound-on-youtube-or-any-other-webpage
    Please remember to mark the replies as answers if they help, and unmark the answers if they provide no help. If you have feedback for TechNet Support, contact [email protected]

  • IOS 6.0.1 Safari won't play audio files. Error: "Cannot play audio file."

    I recently updated my iPad to iOS 6.0.1, now Safari won't play audio files in any format. Error: "Cannot play audio file." Previously, it worked fine. I tried downloading and using GoogleChrome and Opera Mini, and these also won't play audio. What's up??

    Solved the problem. It seems that once iOS 6.0.1 installed, it caused all of my browsers to mishandle audio files. When I reset the iPad, that cleared up the problem.
    Before upgrading to iOS 6, my browsers would "autoplay" the sound files. After iOS 6, "autoplay" is disabled so you have to manually click play to play them.
    Nonetheless, everything is working fine again except for "autoplay," which I can live with.

Maybe you are looking for

  • Where are my applications stored in Lion (Parallels VM etc)?

    i have a home folder that has a "Applications" folder and i have a sidebar folder that says "Applications". in the "sidebar folder" i have tons of apps including the apple applications but in the Home Folder > Applications folder i have two applicati

  • How to have mac's on classroom network autoupdate Adobe Flash?

    I have 30 older macs running Mavericks.  Every time Adobe comes up with a flash update, I have to walk around to every computer and either log in as myself, or put in my user name and password when flash updates.  Is there any way I can have the comp

  • Lever icon in jtree

    i want to remove blue lever shown with each branch node in jtree please help i searched but couldn't find a solution thanx

  • Value is not updating to infoobject

    Hi experts, I am facing a strange issue. Issue is , Value is not updating to the infoobject in master data attribute. I am able to see the value at PSA level. what may be the reason, the object is consistent. regards, rajesh.

  • Processing of a deployed Tabular model inside SSDT

    Hi, operating inside SSDT, does it exist a manner to process a tabular model (or a table/partition) already deployed on the SSAS instance? I know that it is possible to process only the workspace data. Thanks