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