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