1 /* SoundPlayer.h */ 2 /* Copyright 1998 Be Incorporated. All rights reserved. */ 3 4 #if !defined(_SOUND_PLAYER_H) 5 #define _SOUND_PLAYER_H 6 7 8 #include <MediaDefs.h> 9 #include <BufferProducer.h> 10 #include <Locker.h> 11 #include <exception> 12 13 14 class BSound; 15 class sound_error : public exception { 16 const char * m_str_const; 17 public: 18 sound_error(const char * str); 19 const char * what() const; 20 }; 21 22 class BSoundPlayer { 23 friend class _SoundPlayNode; 24 public: 25 enum sound_player_notification { 26 B_STARTED = 1, 27 B_STOPPED, 28 B_SOUND_DONE 29 }; 30 31 BSoundPlayer( 32 const char * name = NULL, 33 void (*PlayBuffer)(void *, void * buffer, size_t size, const media_raw_audio_format & format) = NULL, 34 void (*Notifier)(void *, sound_player_notification what, ...) = NULL, 35 void * cookie = NULL); 36 BSoundPlayer( 37 const media_raw_audio_format * format, 38 const char * name = NULL, 39 void (*PlayBuffer)(void *, void * buffer, size_t size, const media_raw_audio_format & format) = NULL, 40 void (*Notifier)(void *, sound_player_notification what, ...) = NULL, 41 void * cookie = NULL); 42 BSoundPlayer( 43 const media_node & toNode, 44 const media_multi_audio_format * format = NULL, 45 const char * name = NULL, 46 const media_input * input = NULL, 47 void (*PlayBuffer)(void *, void * buffer, size_t size, const media_raw_audio_format & format) = NULL, 48 void (*Notifier)(void *, sound_player_notification what, ...) = NULL, 49 void * cookie = NULL); 50 virtual ~BSoundPlayer(); 51 52 status_t InitCheck(); // new in R4.1 53 media_raw_audio_format Format() const; // new in R4.1 54 55 status_t Start(); 56 void Stop( 57 bool block = true, 58 bool flush = true); 59 60 typedef void (*BufferPlayerFunc)(void *, void *, size_t, const media_raw_audio_format &); 61 BufferPlayerFunc BufferPlayer() const; 62 void SetBufferPlayer( 63 void (*PlayBuffer)(void *, void * buffer, size_t size, const media_raw_audio_format & format)); 64 typedef void (*EventNotifierFunc)(void *, sound_player_notification what, ...); 65 EventNotifierFunc EventNotifier() const; 66 void SetNotifier( 67 void (*Notifier)(void *, sound_player_notification what, ...)); 68 void * Cookie() const; 69 void SetCookie( 70 void * cookie); 71 void SetCallbacks( // atomic change 72 void (*PlayBuffer)(void *, void * buffer, size_t size, const media_raw_audio_format & format) = NULL, 73 void (*Notifier)(void *, sound_player_notification what, ...) = NULL, 74 void * cookie = NULL); 75 76 typedef int32 play_id; 77 78 bigtime_t CurrentTime(); 79 bigtime_t PerformanceTime(); 80 status_t Preroll(); 81 play_id StartPlaying( 82 BSound * sound, 83 bigtime_t at_time = 0); 84 play_id StartPlaying( 85 BSound * sound, 86 bigtime_t at_time, 87 float with_volume); 88 status_t SetSoundVolume( 89 play_id sound, // update only non-NULL values 90 float new_volume); 91 bool IsPlaying( 92 play_id id); 93 status_t StopPlaying( 94 play_id id); 95 status_t WaitForSound( 96 play_id id); 97 98 float Volume(); /* 0 - 1.0 */ 99 void SetVolume( /* 0 - 1.0 */ 100 float new_volume); 101 float VolumeDB( /* -xx - +xx (see GetVolumeInfo()) */ 102 bool forcePoll = false); /* if false, cached value will be used if new enough */ 103 void SetVolumeDB( 104 float volume_dB); 105 status_t GetVolumeInfo( 106 media_node * out_node, 107 int32 * out_parameter_id, 108 float * out_min_dB, 109 float * out_max_dB); 110 bigtime_t Latency(); 111 112 virtual bool HasData(); 113 void SetHasData( // this does more than just set a flag 114 bool has_data); 115 116 protected: 117 118 void SetInitError( // new in R4.1 119 status_t in_error); 120 121 private: 122 123 virtual status_t _Reserved_SoundPlayer_0(void *, ...); 124 virtual status_t _Reserved_SoundPlayer_1(void *, ...); 125 virtual status_t _Reserved_SoundPlayer_2(void *, ...); 126 virtual status_t _Reserved_SoundPlayer_3(void *, ...); 127 virtual status_t _Reserved_SoundPlayer_4(void *, ...); 128 virtual status_t _Reserved_SoundPlayer_5(void *, ...); 129 virtual status_t _Reserved_SoundPlayer_6(void *, ...); 130 virtual status_t _Reserved_SoundPlayer_7(void *, ...); 131 132 _SoundPlayNode * fPlayerNode; 133 struct _playing_sound { 134 _playing_sound * next; 135 off_t cur_offset; 136 BSound * sound; 137 play_id id; 138 int32 delta; 139 int32 rate; 140 float volume; 141 }; 142 _playing_sound * _m_sounds; 143 struct _waiting_sound { 144 _waiting_sound * next; 145 bigtime_t start_time; 146 BSound * sound; 147 play_id id; 148 int32 rate; 149 float volume; 150 }; 151 _waiting_sound * _m_waiting; 152 void (*fPlayBufferFunc)(void * cookie, void * buffer, size_t size, const media_raw_audio_format & format); 153 void (*fNotifierFunc)(void * cookie, sound_player_notification what, ...); 154 BLocker fLocker; 155 float fVolumeDB; 156 media_input fMediaInput; // the system mixer 157 media_output fMediaOutput; // the player node 158 void * fCookie; 159 int32 fFlags; 160 161 status_t fInitStatus; // new in R4.1 162 BContinuousParameter * fVolumeSlider; 163 bigtime_t fLastVolumeUpdate; 164 BParameterWeb *fParameterWeb; 165 uint32 _m_reserved[15]; 166 167 void NotifySoundDone( 168 play_id sound, 169 bool got_to_play); 170 171 void get_volume_slider(); 172 void Init( 173 const media_node * node, 174 const media_multi_audio_format * format, 175 const char * name, 176 const media_input * input, 177 void (*PlayBuffer)(void *, void * buffer, size_t size, const media_raw_audio_format & format), 178 void (*Notifier)(void *, sound_player_notification what, ...), 179 void * cookie); 180 // for B_STARTED and B_STOPPED, the argument is BSoundPlayer* (this) 181 // for B_SOUND_DONE, the arguments are play_id and true/false for whether it got to play data at all 182 virtual void Notify( 183 sound_player_notification what, 184 ...); 185 // get data into the buffer to play -- this version will use the queued BSound system 186 virtual void PlayBuffer( 187 void * buffer, 188 size_t size, 189 const media_raw_audio_format & format); 190 }; 191 192 193 #endif /* _SOUND_PLAYER_H */ 194