使用C ++中的DirectSound从我的计算机加载和播放音频文件(声音或音乐) [英] Load and Play audio file (either sound or music) from my computer using DirectSound in C++

查看:164
本文介绍了使用C ++中的DirectSound从我的计算机加载和播放音频文件(声音或音乐)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#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.


我想自己使用DirectSound(< dsound.h>)以及此标头提供的所有函数,接口,方法,结构等来实现AudioPlayer类和LoadAudioFromComputer函数的所有方法,但是我不知道怎么样!我需要你的帮助!我很难在互联网上找到我需要这样做的信息。 MSDN正在教授所有这些东西,但这太难了!我需要你使用DirectSound和< dsound.h>为我实现AudioPlayer类的所有这些方法和LoadAudioFromComputer
函数。和我在评论中写的解释,然后发布代码。我会将它复制到我的并阅读所有内容以便学习。请使用评论,以便我能理解。我将感谢并感谢任何愿意花b $ b b的人来帮助我并为我做这件事! :D

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

推荐答案

DirectSound已被弃用了很长时间。我怀疑,如果互联网上的所有样本都无法教你任何东西,那么任何人都会花费这么多时间在一项死技术上来满足你的需求。对于新的Windows音频开发,如果仍需要XP支持,则应使用  WASAPI
或XAudio2。

DirectSound is deprecated for a long, long time. I doubt anyone would spend this much time on a dead technology to meet your demand if all the samples on the internet cannot teach you anything. For new Windows audio development you should use WASAPI or XAudio2 if you still need XP support.


这篇关于使用C ++中的DirectSound从我的计算机加载和播放音频文件(声音或音乐)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆