SDL++
C++wrapperfortheSDLlibrary.
|
00001 00021 #ifndef SDL_AUDIO_AUDIO_H 00022 #define SDL_AUDIO_AUDIO_H 00023 00024 #include <string> 00025 #include <stdexcept> 00026 00027 #include <SDL.h> 00028 00029 #include "sdlpp/subsystem/Subsystem.h" 00030 #include "sdlpp/audio/Wav.h" 00031 00032 namespace sdl { 00033 namespace audio { 00034 /* 00035 * @class Audio 00036 * @brief Represents the audio system. 00037 */ 00038 class Audio { 00039 public: 00052 Audio (int freq, unsigned short format, unsigned char channels, unsigned char silence, unsigned short samples, unsigned int size) 00053 : obtained_ () { 00054 SDL_AudioSpec desired; 00055 desired.freq = freq; 00056 desired.format = format; 00057 desired.channels = channels; 00058 //desired.silence = silence; 00059 desired.samples = samples; 00060 //desired.size = size; 00061 //desired.callback = mix; 00062 desired.userdata = 0; 00063 00064 if (SDL_OpenAudio (&desired, &obtained_) == -1) 00065 throw runtime_error ("Failed to open audio."); 00066 SDL_PauseAudio (0); 00067 }; 00068 00072 ~Audio () { SDL_CloseAudio (); }; 00073 00079 bool isPlaying () { return SDL_GetAudioStatus () == SDL_AUDIO_PLAYING; }; 00080 00086 bool isPaused () { return SDL_GetAudioStatus () == SDL_AUDIO_PAUSED; }; 00087 00093 bool isStopped () { return SDL_GetAudioStatus () == SDL_AUDIO_STOPPED; }; 00094 00100 bool play () { 00101 SDL_PauseAudio (0); 00102 return isPlaying (); 00103 }; 00104 00112 Audio& play (Wav& wav) { 00113 /*SDL_LoadWAV (wav.name_.c_str (), obtained_, &wav.audioBuf_, &wav.audioLen_); 00114 SDL_AudioCVT cvt; 00115 SDL_BuildAudioCVT (&cvt, obtained_->format, obtained_->channels, obtained_->freq, AUDIO_S8, 1, 11000); 00116 cvt.buf = (Uint8*)malloc (wav.audioLen_ * cvt.len_mult); 00117 memcpy (cvt.buf, wav.audioBuf_, wav.audioLen_); 00118 cvt.len = wav.audioLen_; 00119 SDL_ConvertAudio (&cvt); 00120 SDL_FreeWAV (wav.audioBuf_); 00121 00122 wav.audioBuf_ = cvt.buf; 00123 wav.audioLen_ = cvt.len_cvt; 00124 00125 SDL_PauseAudio (0);*/ 00126 return *this; 00127 }; 00128 00134 bool pause () { 00135 SDL_PauseAudio (1); 00136 return isPaused (); 00137 }; 00138 00144 Audio& lock () { 00145 SDL_LockAudio (); 00146 return *this; 00147 }; 00148 00154 Audio& unlock () { 00155 SDL_UnlockAudio (); 00156 return *this; 00157 }; 00158 00159 private: 00165 Audio (const Audio& rhs); 00166 00174 Audio& operator= (const Audio& rhs); 00175 00176 private: 00177 /* 00178 * The SDL_AudioSpec structure. 00179 */ 00180 SDL_AudioSpec obtained_; 00181 }; //Audio 00182 }; //audio 00183 }; //sdl 00184 00185 #endif //SDL_AUDIO_AUDIO_H 00186